2 fetcher::webfinger::webfinger_resolve_actor,
3 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
5 use activitystreams_kinds::link::MentionType;
6 use lemmy_api_common::blocking;
7 use lemmy_apub_lib::{object_id::ObjectId, traits::ActorType};
9 source::{comment::Comment, person::Person, post::Post},
14 utils::{scrape_text_for_mentions, MentionData},
17 use lemmy_websocket::LemmyContext;
18 use serde::{Deserialize, Serialize};
21 #[derive(Clone, Debug, Deserialize, Serialize)]
25 #[serde(rename = "type")]
26 pub kind: MentionType,
29 pub struct MentionsAndAddresses {
31 pub tags: Vec<Mention>,
34 /// This takes a comment, and builds a list of to_addresses, inboxes,
35 /// and mention tags, so they know where to be sent to.
36 /// Addresses are the persons / addresses that go in the cc field.
37 #[tracing::instrument(skip(comment, community_id, context))]
38 pub async fn collect_non_local_mentions(
39 comment: &ApubComment,
40 community_id: ObjectId<ApubCommunity>,
41 context: &LemmyContext,
42 request_counter: &mut i32,
43 ) -> Result<MentionsAndAddresses, LemmyError> {
44 let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
45 let mut addressed_ccs: Vec<Url> = vec![community_id.into(), parent_creator.actor_id()];
47 // Add the mention tag
48 let parent_creator_tag = Mention {
49 href: parent_creator.actor_id.clone().into(),
53 &parent_creator.actor_id().domain().expect("has domain")
55 kind: MentionType::Mention,
57 let mut tags = vec![parent_creator_tag];
59 // Get the person IDs for any mentions
60 let mentions = scrape_text_for_mentions(&comment.content)
62 // Filter only the non-local ones
63 .filter(|m| !m.is_local(&context.settings().hostname))
64 .collect::<Vec<MentionData>>();
66 for mention in &mentions {
67 // TODO should it be fetching it every time?
68 let identifier = format!("{}@{}", mention.name, mention.domain);
70 webfinger_resolve_actor::<ApubPerson>(&identifier, context, request_counter).await;
71 if let Ok(actor_id) = actor_id {
72 let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
73 addressed_ccs.push(actor_id.to_string().parse()?);
75 let mention_tag = Mention {
76 href: actor_id.into(),
77 name: Some(mention.full_name()),
78 kind: MentionType::Mention,
80 tags.push(mention_tag);
84 Ok(MentionsAndAddresses {
90 /// Returns the apub ID of the person this comment is responding to. Meaning, in case this is a
91 /// top-level comment, the creator of the post, otherwise the creator of the parent comment.
92 #[tracing::instrument(skip(pool, comment))]
93 async fn get_comment_parent_creator(
96 ) -> Result<ApubPerson, LemmyError> {
97 let parent_creator_id = if let Some(parent_comment_id) = comment.parent_id {
99 blocking(pool, move |conn| Comment::read(conn, parent_comment_id)).await??;
100 parent_comment.creator_id
102 let parent_post_id = comment.post_id;
103 let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
104 parent_post.creator_id
107 blocking(pool, move |conn| Person::read(conn, parent_creator_id))