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