3 link::{LinkExt, Mention},
6 use itertools::Itertools;
10 use lemmy_api_common::blocking;
11 use lemmy_apub_lib::{object_id::ObjectId, traits::ActorType};
12 use lemmy_db_schema::{
13 newtypes::LocalUserId,
14 source::{comment::Comment, person::Person, post::Post},
19 request::{retry, RecvError},
20 utils::{scrape_text_for_mentions, MentionData},
23 use lemmy_websocket::{send::send_local_notifs, LemmyContext};
26 fetcher::webfinger::WebfingerResponse,
27 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
30 pub mod create_or_update;
32 async fn get_notif_recipients(
33 actor: &ObjectId<ApubPerson>,
35 context: &LemmyContext,
36 request_counter: &mut i32,
37 ) -> Result<Vec<LocalUserId>, LemmyError> {
38 let post_id = comment.post_id;
39 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
40 let actor = actor.dereference(context, request_counter).await?;
43 // Although mentions could be gotten from the post tags (they are included there), or the ccs,
44 // Its much easier to scrape them from the comment body, since the API has to do that
46 // TODO: for compatibility with other projects, it would be much better to read this from cc or tags
47 let mentions = scrape_text_for_mentions(&comment.content);
48 send_local_notifs(mentions, comment, &*actor, &post, true, context).await
51 pub struct MentionsAndAddresses {
53 pub inboxes: Vec<Url>,
54 pub tags: Vec<Mention>,
57 /// This takes a comment, and builds a list of to_addresses, inboxes,
58 /// and mention tags, so they know where to be sent to.
59 /// Addresses are the persons / addresses that go in the cc field.
60 pub async fn collect_non_local_mentions(
61 comment: &ApubComment,
62 community: &ApubCommunity,
63 context: &LemmyContext,
64 ) -> Result<MentionsAndAddresses, LemmyError> {
65 let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
66 let mut addressed_ccs: Vec<Url> = vec![community.actor_id(), parent_creator.actor_id()];
67 // Note: dont include community inbox here, as we send to it separately with `send_to_community()`
68 let mut inboxes = vec![parent_creator.shared_inbox_or_inbox_url()];
70 // Add the mention tag
71 let mut tags = Vec::new();
73 // Get the person IDs for any mentions
74 let mentions = scrape_text_for_mentions(&comment.content)
76 // Filter only the non-local ones
77 .filter(|m| !m.is_local(&context.settings().hostname))
78 .collect::<Vec<MentionData>>();
80 for mention in &mentions {
81 // TODO should it be fetching it every time?
82 if let Ok(actor_id) = fetch_webfinger_url(mention, context).await {
83 let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
84 debug!("mention actor_id: {}", actor_id);
85 addressed_ccs.push(actor_id.to_string().parse()?);
87 let mention_person = actor_id.dereference(context, &mut 0).await?;
88 inboxes.push(mention_person.shared_inbox_or_inbox_url());
90 let mut mention_tag = Mention::new();
92 .set_href(actor_id.into())
93 .set_name(mention.full_name());
94 tags.push(mention_tag);
98 let inboxes = inboxes.into_iter().unique().collect();
100 Ok(MentionsAndAddresses {
107 /// Returns the apub ID of the person this comment is responding to. Meaning, in case this is a
108 /// top-level comment, the creator of the post, otherwise the creator of the parent comment.
109 async fn get_comment_parent_creator(
112 ) -> Result<ApubPerson, LemmyError> {
113 let parent_creator_id = if let Some(parent_comment_id) = comment.parent_id {
115 blocking(pool, move |conn| Comment::read(conn, parent_comment_id)).await??;
116 parent_comment.creator_id
118 let parent_post_id = comment.post_id;
119 let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
120 parent_post.creator_id
123 blocking(pool, move |conn| Person::read(conn, parent_creator_id))
129 /// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
131 async fn fetch_webfinger_url(
132 mention: &MentionData,
133 context: &LemmyContext,
134 ) -> Result<Url, LemmyError> {
135 let fetch_url = format!(
136 "{}://{}/.well-known/webfinger?resource=acct:{}@{}",
137 context.settings().get_protocol_string(),
142 debug!("Fetching webfinger url: {}", &fetch_url);
144 let response = retry(|| context.client().get(&fetch_url).send()).await?;
146 let res: WebfingerResponse = response
149 .map_err(|e| RecvError(e.to_string()))?;
154 .find(|l| l.type_.eq(&Some("application/activity+json".to_string())))
155 .ok_or_else(|| anyhow!("No application/activity+json link found."))?;
159 .ok_or_else(|| anyhow!("No href found.").into())