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