2 fetcher::webfinger::WebfingerResponse,
3 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
7 link::{LinkExt, Mention},
10 use lemmy_api_common::blocking;
11 use lemmy_apub_lib::{object_id::ObjectId, traits::ActorType};
12 use lemmy_db_schema::{
13 source::{comment::Comment, person::Person, post::Post},
18 request::{retry, RecvError},
19 utils::{scrape_text_for_mentions, MentionData},
22 use lemmy_websocket::LemmyContext;
26 pub struct MentionsAndAddresses {
28 pub tags: Vec<Mention>,
31 /// This takes a comment, and builds a list of to_addresses, inboxes,
32 /// and mention tags, so they know where to be sent to.
33 /// Addresses are the persons / addresses that go in the cc field.
34 pub async fn collect_non_local_mentions(
35 comment: &ApubComment,
36 community_id: ObjectId<ApubCommunity>,
37 context: &LemmyContext,
38 ) -> Result<MentionsAndAddresses, LemmyError> {
39 let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
40 let mut addressed_ccs: Vec<Url> = vec![community_id.into(), parent_creator.actor_id()];
42 // Add the mention tag
43 let mut parent_creator_tag = Mention::new();
45 .set_href(parent_creator.actor_id.clone().into())
49 &parent_creator.actor_id().domain().expect("has domain")
51 let mut tags = vec![parent_creator_tag];
53 // Get the person IDs for any mentions
54 let mentions = scrape_text_for_mentions(&comment.content)
56 // Filter only the non-local ones
57 .filter(|m| !m.is_local(&context.settings().hostname))
58 .collect::<Vec<MentionData>>();
60 for mention in &mentions {
61 // TODO should it be fetching it every time?
62 if let Ok(actor_id) = fetch_webfinger_url(mention, context).await {
63 let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
64 debug!("mention actor_id: {}", actor_id);
65 addressed_ccs.push(actor_id.to_string().parse()?);
67 let mut mention_tag = Mention::new();
69 .set_href(actor_id.into())
70 .set_name(mention.full_name());
71 tags.push(mention_tag);
75 Ok(MentionsAndAddresses {
81 /// Returns the apub ID of the person this comment is responding to. Meaning, in case this is a
82 /// top-level comment, the creator of the post, otherwise the creator of the parent comment.
83 async fn get_comment_parent_creator(
86 ) -> Result<ApubPerson, LemmyError> {
87 let parent_creator_id = if let Some(parent_comment_id) = comment.parent_id {
89 blocking(pool, move |conn| Comment::read(conn, parent_comment_id)).await??;
90 parent_comment.creator_id
92 let parent_post_id = comment.post_id;
93 let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
94 parent_post.creator_id
97 blocking(pool, move |conn| Person::read(conn, parent_creator_id))
103 /// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
105 async fn fetch_webfinger_url(
106 mention: &MentionData,
107 context: &LemmyContext,
108 ) -> Result<Url, LemmyError> {
109 let fetch_url = format!(
110 "{}://{}/.well-known/webfinger?resource=acct:{}@{}",
111 context.settings().get_protocol_string(),
116 debug!("Fetching webfinger url: {}", &fetch_url);
118 let response = retry(|| context.client().get(&fetch_url).send()).await?;
120 let res: WebfingerResponse = response
123 .map_err(|e| RecvError(e.to_string()))?;
128 .find(|l| l.type_.eq(&Some("application/activity+json".to_string())))
129 .ok_or_else(|| anyhow!("No application/activity+json link found."))?;
133 .ok_or_else(|| anyhow!("No href found.").into())