]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/comment/mod.rs
Major refactor, adding newtypes for apub crate
[lemmy.git] / crates / apub / src / activities / comment / mod.rs
1 use crate::{
2   fetcher::object_id::ObjectId,
3   objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
4 };
5 use activitystreams::{
6   base::BaseExt,
7   link::{LinkExt, Mention},
8 };
9 use anyhow::anyhow;
10 use itertools::Itertools;
11 use lemmy_api_common::blocking;
12 use lemmy_apub_lib::{traits::ActorType, webfinger::WebfingerResponse};
13 use lemmy_db_schema::{
14   newtypes::LocalUserId,
15   source::{comment::Comment, person::Person, post::Post},
16   traits::Crud,
17   DbPool,
18 };
19 use lemmy_utils::{
20   request::{retry, RecvError},
21   utils::{scrape_text_for_mentions, MentionData},
22   LemmyError,
23 };
24 use lemmy_websocket::{send::send_local_notifs, LemmyContext};
25 use log::debug;
26 use url::Url;
27
28 pub mod create_or_update;
29
30 async fn get_notif_recipients(
31   actor: &ObjectId<ApubPerson>,
32   comment: &Comment,
33   context: &LemmyContext,
34   request_counter: &mut i32,
35 ) -> Result<Vec<LocalUserId>, LemmyError> {
36   let post_id = comment.post_id;
37   let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
38   let actor = actor.dereference(context, request_counter).await?;
39
40   // Note:
41   // Although mentions could be gotten from the post tags (they are included there), or the ccs,
42   // Its much easier to scrape them from the comment body, since the API has to do that
43   // anyway.
44   // TODO: for compatibility with other projects, it would be much better to read this from cc or tags
45   let mentions = scrape_text_for_mentions(&comment.content);
46   send_local_notifs(mentions, comment, &*actor, &post, true, context).await
47 }
48
49 pub struct MentionsAndAddresses {
50   pub ccs: Vec<Url>,
51   pub inboxes: Vec<Url>,
52   pub tags: Vec<Mention>,
53 }
54
55 /// This takes a comment, and builds a list of to_addresses, inboxes,
56 /// and mention tags, so they know where to be sent to.
57 /// Addresses are the persons / addresses that go in the cc field.
58 pub async fn collect_non_local_mentions(
59   comment: &ApubComment,
60   community: &ApubCommunity,
61   context: &LemmyContext,
62 ) -> Result<MentionsAndAddresses, LemmyError> {
63   let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
64   let mut addressed_ccs: Vec<Url> = vec![community.actor_id(), parent_creator.actor_id()];
65   // Note: dont include community inbox here, as we send to it separately with `send_to_community()`
66   let mut inboxes = vec![parent_creator.shared_inbox_or_inbox_url()];
67
68   // Add the mention tag
69   let mut tags = Vec::new();
70
71   // Get the person IDs for any mentions
72   let mentions = scrape_text_for_mentions(&comment.content)
73     .into_iter()
74     // Filter only the non-local ones
75     .filter(|m| !m.is_local(&context.settings().hostname))
76     .collect::<Vec<MentionData>>();
77
78   for mention in &mentions {
79     // TODO should it be fetching it every time?
80     if let Ok(actor_id) = fetch_webfinger_url(mention, context).await {
81       let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
82       debug!("mention actor_id: {}", actor_id);
83       addressed_ccs.push(actor_id.to_string().parse()?);
84
85       let mention_person = actor_id.dereference(context, &mut 0).await?;
86       inboxes.push(mention_person.shared_inbox_or_inbox_url());
87
88       let mut mention_tag = Mention::new();
89       mention_tag
90         .set_href(actor_id.into())
91         .set_name(mention.full_name());
92       tags.push(mention_tag);
93     }
94   }
95
96   let inboxes = inboxes.into_iter().unique().collect();
97
98   Ok(MentionsAndAddresses {
99     ccs: addressed_ccs,
100     inboxes,
101     tags,
102   })
103 }
104
105 /// Returns the apub ID of the person this comment is responding to. Meaning, in case this is a
106 /// top-level comment, the creator of the post, otherwise the creator of the parent comment.
107 async fn get_comment_parent_creator(
108   pool: &DbPool,
109   comment: &Comment,
110 ) -> Result<ApubPerson, LemmyError> {
111   let parent_creator_id = if let Some(parent_comment_id) = comment.parent_id {
112     let parent_comment =
113       blocking(pool, move |conn| Comment::read(conn, parent_comment_id)).await??;
114     parent_comment.creator_id
115   } else {
116     let parent_post_id = comment.post_id;
117     let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
118     parent_post.creator_id
119   };
120   Ok(
121     blocking(pool, move |conn| Person::read(conn, parent_creator_id))
122       .await??
123       .into(),
124   )
125 }
126
127 /// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
128 /// using webfinger.
129 async fn fetch_webfinger_url(
130   mention: &MentionData,
131   context: &LemmyContext,
132 ) -> Result<Url, LemmyError> {
133   let fetch_url = format!(
134     "{}://{}/.well-known/webfinger?resource=acct:{}@{}",
135     context.settings().get_protocol_string(),
136     mention.domain,
137     mention.name,
138     mention.domain
139   );
140   debug!("Fetching webfinger url: {}", &fetch_url);
141
142   let response = retry(|| context.client().get(&fetch_url).send()).await?;
143
144   let res: WebfingerResponse = response
145     .json()
146     .await
147     .map_err(|e| RecvError(e.to_string()))?;
148
149   let link = res
150     .links
151     .iter()
152     .find(|l| l.type_.eq(&Some("application/activity+json".to_string())))
153     .ok_or_else(|| anyhow!("No application/activity+json link found."))?;
154   link
155     .href
156     .to_owned()
157     .ok_or_else(|| anyhow!("No href found.").into())
158 }