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