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