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};
19 use serde_json::Value;
22 #[derive(Clone, Debug, Deserialize, Serialize)]
24 pub enum MentionOrValue {
29 #[derive(Clone, Debug, Deserialize, Serialize)]
33 #[serde(rename = "type")]
34 pub kind: MentionType,
37 pub struct MentionsAndAddresses {
39 pub tags: Vec<MentionOrValue>,
42 /// This takes a comment, and builds a list of to_addresses, inboxes,
43 /// and mention tags, so they know where to be sent to.
44 /// Addresses are the persons / addresses that go in the cc field.
45 #[tracing::instrument(skip(comment, community_id, context))]
46 pub async fn collect_non_local_mentions(
47 comment: &ApubComment,
48 community_id: ObjectId<ApubCommunity>,
49 context: &LemmyContext,
50 request_counter: &mut i32,
51 ) -> Result<MentionsAndAddresses, LemmyError> {
52 let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
53 let mut addressed_ccs: Vec<Url> = vec![community_id.into(), parent_creator.actor_id()];
55 // Add the mention tag
56 let parent_creator_tag = Mention {
57 href: parent_creator.actor_id.clone().into(),
61 &parent_creator.actor_id().domain().expect("has domain")
63 kind: MentionType::Mention,
65 let mut tags = vec![parent_creator_tag];
67 // Get the person IDs for any mentions
68 let mentions = scrape_text_for_mentions(&comment.content)
70 // Filter only the non-local ones
71 .filter(|m| !m.is_local(&context.settings().hostname))
72 .collect::<Vec<MentionData>>();
74 for mention in &mentions {
75 // TODO should it be fetching it every time?
76 let identifier = format!("{}@{}", mention.name, mention.domain);
78 webfinger_resolve_actor::<ApubPerson>(&identifier, context, request_counter).await;
79 if let Ok(actor_id) = actor_id {
80 let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
81 addressed_ccs.push(actor_id.to_string().parse()?);
83 let mention_tag = Mention {
84 href: actor_id.into(),
85 name: Some(mention.full_name()),
86 kind: MentionType::Mention,
88 tags.push(mention_tag);
92 let tags = tags.into_iter().map(MentionOrValue::Mention).collect();
93 Ok(MentionsAndAddresses {
99 /// Returns the apub ID of the person this comment is responding to. Meaning, in case this is a
100 /// top-level comment, the creator of the post, otherwise the creator of the parent comment.
101 #[tracing::instrument(skip(pool, comment))]
102 async fn get_comment_parent_creator(
105 ) -> Result<ApubPerson, LemmyError> {
106 let parent_creator_id = if let Some(parent_comment_id) = comment.parent_id {
108 blocking(pool, move |conn| Comment::read(conn, parent_comment_id)).await??;
109 parent_comment.creator_id
111 let parent_post_id = comment.post_id;
112 let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
113 parent_post.creator_id
116 blocking(pool, move |conn| Person::read(conn, parent_creator_id))