]> Untitled Git - lemmy.git/blob - crates/apub/src/mentions.rs
f8de438556fa0189279d3761d59cbe2cadadad7a
[lemmy.git] / crates / apub / src / mentions.rs
1 use crate::{
2   fetcher::webfinger::webfinger_resolve_actor,
3   objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
4 };
5 use activitystreams_kinds::link::MentionType;
6 use lemmy_api_common::blocking;
7 use lemmy_apub_lib::{object_id::ObjectId, traits::ActorType};
8 use lemmy_db_schema::{
9   source::{comment::Comment, person::Person, post::Post},
10   traits::Crud,
11   DbPool,
12 };
13 use lemmy_utils::{
14   utils::{scrape_text_for_mentions, MentionData},
15   LemmyError,
16 };
17 use lemmy_websocket::LemmyContext;
18 use serde::{Deserialize, Serialize};
19 use url::Url;
20
21 #[derive(Clone, Debug, Deserialize, Serialize)]
22 pub struct Mention {
23   pub href: Url,
24   name: Option<String>,
25   #[serde(rename = "type")]
26   pub kind: MentionType,
27 }
28
29 pub struct MentionsAndAddresses {
30   pub ccs: Vec<Url>,
31   pub tags: Vec<Mention>,
32 }
33
34 /// This takes a comment, and builds a list of to_addresses, inboxes,
35 /// and mention tags, so they know where to be sent to.
36 /// Addresses are the persons / addresses that go in the cc field.
37 pub async fn collect_non_local_mentions(
38   comment: &ApubComment,
39   community_id: ObjectId<ApubCommunity>,
40   context: &LemmyContext,
41   request_counter: &mut i32,
42 ) -> Result<MentionsAndAddresses, LemmyError> {
43   let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
44   let mut addressed_ccs: Vec<Url> = vec![community_id.into(), parent_creator.actor_id()];
45
46   // Add the mention tag
47   let parent_creator_tag = Mention {
48     href: parent_creator.actor_id.clone().into(),
49     name: Some(format!(
50       "@{}@{}",
51       &parent_creator.name,
52       &parent_creator.actor_id().domain().expect("has domain")
53     )),
54     kind: MentionType::Mention,
55   };
56   let mut tags = vec![parent_creator_tag];
57
58   // Get the person IDs for any mentions
59   let mentions = scrape_text_for_mentions(&comment.content)
60     .into_iter()
61     // Filter only the non-local ones
62     .filter(|m| !m.is_local(&context.settings().hostname))
63     .collect::<Vec<MentionData>>();
64
65   for mention in &mentions {
66     // TODO should it be fetching it every time?
67     let identifier = format!("{}@{}", mention.name, mention.domain);
68     let actor_id =
69       webfinger_resolve_actor::<ApubPerson>(&identifier, context, request_counter).await;
70     if let Ok(actor_id) = actor_id {
71       let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
72       addressed_ccs.push(actor_id.to_string().parse()?);
73
74       let mention_tag = Mention {
75         href: actor_id.into(),
76         name: Some(mention.full_name()),
77         kind: MentionType::Mention,
78       };
79       tags.push(mention_tag);
80     }
81   }
82
83   Ok(MentionsAndAddresses {
84     ccs: addressed_ccs,
85     tags,
86   })
87 }
88
89 /// Returns the apub ID of the person this comment is responding to. Meaning, in case this is a
90 /// top-level comment, the creator of the post, otherwise the creator of the parent comment.
91 async fn get_comment_parent_creator(
92   pool: &DbPool,
93   comment: &Comment,
94 ) -> Result<ApubPerson, LemmyError> {
95   let parent_creator_id = if let Some(parent_comment_id) = comment.parent_id {
96     let parent_comment =
97       blocking(pool, move |conn| Comment::read(conn, parent_comment_id)).await??;
98     parent_comment.creator_id
99   } else {
100     let parent_post_id = comment.post_id;
101     let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
102     parent_post.creator_id
103   };
104   Ok(
105     blocking(pool, move |conn| Person::read(conn, parent_creator_id))
106       .await??
107       .into(),
108   )
109 }