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 pub async fn collect_non_local_mentions(
38 comment: &ApubComment,
39 community_id: ObjectId<ApubCommunity>,
40 context: &LemmyContext,
41 request_counter: &mut i32,
42 ) -> Result<MentionsAndAddresses, LemmyError> {
43 let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
44 let mut addressed_ccs: Vec<Url> = vec![community_id.into(), parent_creator.actor_id()];
46 // Add the mention tag
47 let parent_creator_tag = Mention {
48 href: parent_creator.actor_id.clone().into(),
52 &parent_creator.actor_id().domain().expect("has domain")
54 kind: MentionType::Mention,
56 let mut tags = vec![parent_creator_tag];
58 // Get the person IDs for any mentions
59 let mentions = scrape_text_for_mentions(&comment.content)
61 // Filter only the non-local ones
62 .filter(|m| !m.is_local(&context.settings().hostname))
63 .collect::<Vec<MentionData>>();
65 for mention in &mentions {
66 // TODO should it be fetching it every time?
67 let identifier = format!("{}@{}", mention.name, mention.domain);
69 webfinger_resolve_actor::<ApubPerson>(&identifier, context, request_counter).await;
70 if let Ok(actor_id) = actor_id {
71 let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
72 addressed_ccs.push(actor_id.to_string().parse()?);
74 let mention_tag = Mention {
75 href: actor_id.into(),
76 name: Some(mention.full_name()),
77 kind: MentionType::Mention,
79 tags.push(mention_tag);
83 Ok(MentionsAndAddresses {
89 /// Returns the apub ID of the person this comment is responding to. Meaning, in case this is a
90 /// top-level comment, the creator of the post, otherwise the creator of the parent comment.
91 async fn get_comment_parent_creator(
94 ) -> Result<ApubPerson, LemmyError> {
95 let parent_creator_id = if let Some(parent_comment_id) = comment.parent_id {
97 blocking(pool, move |conn| Comment::read(conn, parent_comment_id)).await??;
98 parent_comment.creator_id
100 let parent_post_id = comment.post_id;
101 let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
102 parent_post.creator_id
105 blocking(pool, move |conn| Person::read(conn, parent_creator_id))