Create and Note always need to tag parent creator, for mastodon notifications
authorFelix Ableitner <me@nutomic.com>
Mon, 15 Nov 2021 22:54:25 +0000 (23:54 +0100)
committerFelix Ableitner <me@nutomic.com>
Tue, 16 Nov 2021 17:07:47 +0000 (18:07 +0100)
14 files changed:
crates/api_crud/src/comment/create.rs
crates/api_crud/src/comment/update.rs
crates/apub/assets/lemmy/activities/create_or_update/create_note.json
crates/apub/assets/lemmy/objects/note.json
crates/apub/assets/smithereen/activities/create_note.json
crates/apub/assets/smithereen/objects/note.json
crates/apub/src/activities/comment/create_or_update.rs
crates/apub/src/activities/comment/mod.rs
crates/apub/src/lib.rs
crates/apub/src/mentions.rs [new file with mode: 0644]
crates/apub/src/objects/comment.rs
crates/apub/src/protocol/activities/create_or_update/mod.rs
crates/apub/src/protocol/mod.rs
crates/apub/src/protocol/objects/note.rs

index 1c133be234a9de3901a699c843ddea0b286214e1..09163aef46110d15dea66816fb800d1eb9a2b151 100644 (file)
@@ -152,6 +152,7 @@ impl PerformCrud for CreateComment {
       &local_user_view.person.clone().into(),
       CreateOrUpdateType::Create,
       context,
+      &mut 0,
     )
     .await?;
     let object = PostOrComment::Comment(Box::new(apub_comment));
index 4802b1d50bd997f78d20d2cf5f0061b4d324515b..7b2937922447a39f35b163f60b28f6e00676e08c 100644 (file)
@@ -91,6 +91,7 @@ impl PerformCrud for EditComment {
       &local_user_view.person.into(),
       CreateOrUpdateType::Update,
       context,
+      &mut 0,
     )
     .await?;
 
index 4360ce92af9ac3b1c911c7ce5a6f6dfc3251fb95..33ce1cfd9430238d33d955cb4fe1b9af7e9121a4 100644 (file)
     "http://enterprise.lemmy.ml/c/main",
     "http://ds9.lemmy.ml/u/lemmy_alpha"
   ],
-  "tag": [],
+  "tag": [
+    {
+      "href": "http://ds9.lemmy.ml/u/lemmy_alpha",
+      "type": "Mention",
+      "name": "@lemmy_alpha@ds9.lemmy.ml"
+    }
+  ],
   "type": "Create",
   "id": "http://ds9.lemmy.ml/activities/create/1e77d67c-44ac-45ed-bf2a-460e21f60236"
 }
\ No newline at end of file
index c353d0b2bff545f079ed3704a2c5d38f0313de74..269063a769e7f1ccd9622221cc63121c210772f9 100644 (file)
@@ -3,6 +3,10 @@
   "type": "Note",
   "attributedTo": "https://enterprise.lemmy.ml/u/picard",
   "to": ["https://www.w3.org/ns/activitystreams#Public"],
+  "cc": [
+    "https://enterprise.lemmy.ml/c/tenforward",
+    "https://enterprise.lemmy.ml/u/picard"
+  ],
   "inReplyTo": "https://enterprise.lemmy.ml/post/55143",
   "content": "<p>first comment!</p>\n",
   "mediaType": "text/html",
     "content": "first comment!",
     "mediaType": "text/markdown"
   },
+  "tag": [
+    {
+      "href": "https://enterprise.lemmy.ml/u/picard",
+      "type": "Mention",
+      "name": "@picard@enterprise.lemmy.ml"
+    }
+  ],
   "published": "2021-03-01T13:42:43.966208+00:00",
   "updated": "2021-03-01T13:43:03.955787+00:00"
 }
index a30bc9a53429c99dc9fe03e3ede8975514d24e62..4bd199d4a8c4bdca0957c910d55b7891b20d4908 100644 (file)
     "content": "<p>So does this federate now?</p>",
     "inReplyTo": "https://ds9.lemmy.ml/post/1723",
     "published": "2021-11-09T11:42:35Z",
-    "tag": {
-      "type": "Mention",
-      "href": "https://ds9.lemmy.ml/u/nutomic"
-    },
+    "tag": [
+      {
+        "type": "Mention",
+        "href": "https://ds9.lemmy.ml/u/nutomic"
+      }
+    ],
     "url": "https://friends.grishka.me/posts/66561",
     "to": [
       "https://www.w3.org/ns/activitystreams#Public"
index 24d665b265ab9da00c5ac97832cbe7a10a0f5abd..0a948ee3daa14bdf0f7136ba0f93e9900f455b00 100644 (file)
@@ -5,10 +5,12 @@
   "content": "<p>So does this federate now?</p>",
   "inReplyTo": "https://ds9.lemmy.ml/post/1723",
   "published": "2021-11-09T11:42:35Z",
-  "tag": {
-    "type": "Mention",
-    "href": "https://ds9.lemmy.ml/u/nutomic"
-  },
+  "tag": [
+    {
+      "type": "Mention",
+      "href": "https://ds9.lemmy.ml/u/nutomic"
+    }
+  ],
   "url": "https://friends.grishka.me/posts/66561",
   "to": [
     "https://www.w3.org/ns/activitystreams#Public"
index eab2a5771a4b7a04c0c231d7380faa52f37fcc49..ad686251ba697e0880cc6cffae6ea4208378ae8b 100644 (file)
@@ -1,7 +1,7 @@
 use crate::{
   activities::{
     check_community_deleted_or_removed,
-    comment::{collect_non_local_mentions, get_notif_recipients},
+    comment::get_notif_recipients,
     community::{announce::GetCommunity, send_activity_in_community},
     generate_activity_id,
     verify_activity,
@@ -12,7 +12,7 @@ use crate::{
   objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
   protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
 };
-use activitystreams::public;
+use activitystreams::{link::LinkExt, public};
 use lemmy_api_common::{blocking, check_post_deleted_or_removed};
 use lemmy_apub_lib::{
   data::Data,
@@ -33,6 +33,7 @@ impl CreateOrUpdateComment {
     actor: &ApubPerson,
     kind: CreateOrUpdateType,
     context: &LemmyContext,
+    request_counter: &mut i32,
   ) -> Result<(), LemmyError> {
     // TODO: might be helpful to add a comment method to retrieve community directly
     let post_id = comment.post_id;
@@ -48,21 +49,34 @@ impl CreateOrUpdateComment {
       kind.clone(),
       &context.settings().get_protocol_and_hostname(),
     )?;
-    let maa = collect_non_local_mentions(&comment, &community, context).await?;
+    let note = comment.into_apub(context).await?;
 
     let create_or_update = CreateOrUpdateComment {
       actor: ObjectId::new(actor.actor_id()),
       to: vec![public()],
-      object: comment.into_apub(context).await?,
-      cc: maa.ccs,
-      tag: maa.tags,
+      cc: note.cc.clone(),
+      tag: note.tag.clone(),
+      object: note,
       kind,
       id: id.clone(),
       unparsed: Default::default(),
     };
 
+    let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
+      .tag
+      .iter()
+      .map(|t| t.href())
+      .flatten()
+      .map(|t| ObjectId::new(t.clone()))
+      .collect();
+    let mut inboxes = vec![];
+    for t in tagged_users {
+      let person = t.dereference(context, request_counter).await?;
+      inboxes.push(person.shared_inbox_or_inbox_url());
+    }
+
     let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
-    send_activity_in_community(activity, &id, actor, &community, maa.inboxes, context).await
+    send_activity_in_community(activity, &id, actor, &community, inboxes, context).await
   }
 }
 
index d104db2feac8f25b4a7a77fa0726bf631c640ccd..b425385877fcbe9808fc2623cb87b38b674b946c 100644 (file)
@@ -1,32 +1,14 @@
-use activitystreams::{
-  base::BaseExt,
-  link::{LinkExt, Mention},
-};
-use anyhow::anyhow;
-use itertools::Itertools;
-use log::debug;
-use url::Url;
-
+use crate::objects::person::ApubPerson;
 use lemmy_api_common::blocking;
-use lemmy_apub_lib::{object_id::ObjectId, traits::ActorType};
+use lemmy_apub_lib::object_id::ObjectId;
 use lemmy_db_schema::{
   newtypes::LocalUserId,
-  source::{comment::Comment, person::Person, post::Post},
+  source::{comment::Comment, post::Post},
   traits::Crud,
-  DbPool,
-};
-use lemmy_utils::{
-  request::{retry, RecvError},
-  utils::{scrape_text_for_mentions, MentionData},
-  LemmyError,
 };
+use lemmy_utils::{utils::scrape_text_for_mentions, LemmyError};
 use lemmy_websocket::{send::send_local_notifs, LemmyContext};
 
-use crate::{
-  fetcher::webfinger::WebfingerResponse,
-  objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
-};
-
 pub mod create_or_update;
 
 async fn get_notif_recipients(
@@ -47,114 +29,3 @@ async fn get_notif_recipients(
   let mentions = scrape_text_for_mentions(&comment.content);
   send_local_notifs(mentions, comment, &*actor, &post, true, context).await
 }
-
-pub struct MentionsAndAddresses {
-  pub ccs: Vec<Url>,
-  pub inboxes: Vec<Url>,
-  pub tags: Vec<Mention>,
-}
-
-/// This takes a comment, and builds a list of to_addresses, inboxes,
-/// and mention tags, so they know where to be sent to.
-/// Addresses are the persons / addresses that go in the cc field.
-pub async fn collect_non_local_mentions(
-  comment: &ApubComment,
-  community: &ApubCommunity,
-  context: &LemmyContext,
-) -> Result<MentionsAndAddresses, LemmyError> {
-  let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
-  let mut addressed_ccs: Vec<Url> = vec![community.actor_id(), parent_creator.actor_id()];
-  // Note: dont include community inbox here, as we send to it separately with `send_to_community()`
-  let mut inboxes = vec![parent_creator.shared_inbox_or_inbox_url()];
-
-  // Add the mention tag
-  let mut tags = Vec::new();
-
-  // Get the person IDs for any mentions
-  let mentions = scrape_text_for_mentions(&comment.content)
-    .into_iter()
-    // Filter only the non-local ones
-    .filter(|m| !m.is_local(&context.settings().hostname))
-    .collect::<Vec<MentionData>>();
-
-  for mention in &mentions {
-    // TODO should it be fetching it every time?
-    if let Ok(actor_id) = fetch_webfinger_url(mention, context).await {
-      let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
-      debug!("mention actor_id: {}", actor_id);
-      addressed_ccs.push(actor_id.to_string().parse()?);
-
-      let mention_person = actor_id.dereference(context, &mut 0).await?;
-      inboxes.push(mention_person.shared_inbox_or_inbox_url());
-
-      let mut mention_tag = Mention::new();
-      mention_tag
-        .set_href(actor_id.into())
-        .set_name(mention.full_name());
-      tags.push(mention_tag);
-    }
-  }
-
-  let inboxes = inboxes.into_iter().unique().collect();
-
-  Ok(MentionsAndAddresses {
-    ccs: addressed_ccs,
-    inboxes,
-    tags,
-  })
-}
-
-/// Returns the apub ID of the person this comment is responding to. Meaning, in case this is a
-/// top-level comment, the creator of the post, otherwise the creator of the parent comment.
-async fn get_comment_parent_creator(
-  pool: &DbPool,
-  comment: &Comment,
-) -> Result<ApubPerson, LemmyError> {
-  let parent_creator_id = if let Some(parent_comment_id) = comment.parent_id {
-    let parent_comment =
-      blocking(pool, move |conn| Comment::read(conn, parent_comment_id)).await??;
-    parent_comment.creator_id
-  } else {
-    let parent_post_id = comment.post_id;
-    let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
-    parent_post.creator_id
-  };
-  Ok(
-    blocking(pool, move |conn| Person::read(conn, parent_creator_id))
-      .await??
-      .into(),
-  )
-}
-
-/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
-/// using webfinger.
-async fn fetch_webfinger_url(
-  mention: &MentionData,
-  context: &LemmyContext,
-) -> Result<Url, LemmyError> {
-  let fetch_url = format!(
-    "{}://{}/.well-known/webfinger?resource=acct:{}@{}",
-    context.settings().get_protocol_string(),
-    mention.domain,
-    mention.name,
-    mention.domain
-  );
-  debug!("Fetching webfinger url: {}", &fetch_url);
-
-  let response = retry(|| context.client().get(&fetch_url).send()).await?;
-
-  let res: WebfingerResponse = response
-    .json()
-    .await
-    .map_err(|e| RecvError(e.to_string()))?;
-
-  let link = res
-    .links
-    .iter()
-    .find(|l| l.type_.eq(&Some("application/activity+json".to_string())))
-    .ok_or_else(|| anyhow!("No application/activity+json link found."))?;
-  link
-    .href
-    .to_owned()
-    .ok_or_else(|| anyhow!("No href found.").into())
-}
index ea6bf65be4c38a22ab4d8150a6b6a29c5ed380ef..e400f81e8f014162bed0a396cb4d82dbe91241ce 100644 (file)
@@ -12,6 +12,7 @@ pub(crate) mod collections;
 mod context;
 pub mod fetcher;
 pub mod http;
+pub(crate) mod mentions;
 pub mod migrations;
 pub mod objects;
 pub mod protocol;
diff --git a/crates/apub/src/mentions.rs b/crates/apub/src/mentions.rs
new file mode 100644 (file)
index 0000000..268a169
--- /dev/null
@@ -0,0 +1,134 @@
+use crate::{
+  fetcher::webfinger::WebfingerResponse,
+  objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
+};
+use activitystreams::{
+  base::BaseExt,
+  link::{LinkExt, Mention},
+};
+use anyhow::anyhow;
+use lemmy_api_common::blocking;
+use lemmy_apub_lib::{object_id::ObjectId, traits::ActorType};
+use lemmy_db_schema::{
+  source::{comment::Comment, person::Person, post::Post},
+  traits::Crud,
+  DbPool,
+};
+use lemmy_utils::{
+  request::{retry, RecvError},
+  utils::{scrape_text_for_mentions, MentionData},
+  LemmyError,
+};
+use lemmy_websocket::LemmyContext;
+use log::debug;
+use url::Url;
+
+pub struct MentionsAndAddresses {
+  pub ccs: Vec<Url>,
+  pub tags: Vec<Mention>,
+}
+
+/// This takes a comment, and builds a list of to_addresses, inboxes,
+/// and mention tags, so they know where to be sent to.
+/// Addresses are the persons / addresses that go in the cc field.
+pub async fn collect_non_local_mentions(
+  comment: &ApubComment,
+  community_id: ObjectId<ApubCommunity>,
+  context: &LemmyContext,
+) -> Result<MentionsAndAddresses, LemmyError> {
+  let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
+  let mut addressed_ccs: Vec<Url> = vec![community_id.into(), parent_creator.actor_id()];
+
+  // Add the mention tag
+  let mut parent_creator_tag = Mention::new();
+  parent_creator_tag
+    .set_href(parent_creator.actor_id.clone().into())
+    .set_name(format!(
+      "@{}@{}",
+      &parent_creator.name,
+      &parent_creator.actor_id().domain().expect("has domain")
+    ));
+  let mut tags = vec![parent_creator_tag];
+
+  // Get the person IDs for any mentions
+  let mentions = scrape_text_for_mentions(&comment.content)
+    .into_iter()
+    // Filter only the non-local ones
+    .filter(|m| !m.is_local(&context.settings().hostname))
+    .collect::<Vec<MentionData>>();
+
+  for mention in &mentions {
+    // TODO should it be fetching it every time?
+    if let Ok(actor_id) = fetch_webfinger_url(mention, context).await {
+      let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
+      debug!("mention actor_id: {}", actor_id);
+      addressed_ccs.push(actor_id.to_string().parse()?);
+
+      let mut mention_tag = Mention::new();
+      mention_tag
+        .set_href(actor_id.into())
+        .set_name(mention.full_name());
+      tags.push(mention_tag);
+    }
+  }
+
+  Ok(MentionsAndAddresses {
+    ccs: addressed_ccs,
+    tags,
+  })
+}
+
+/// Returns the apub ID of the person this comment is responding to. Meaning, in case this is a
+/// top-level comment, the creator of the post, otherwise the creator of the parent comment.
+async fn get_comment_parent_creator(
+  pool: &DbPool,
+  comment: &Comment,
+) -> Result<ApubPerson, LemmyError> {
+  let parent_creator_id = if let Some(parent_comment_id) = comment.parent_id {
+    let parent_comment =
+      blocking(pool, move |conn| Comment::read(conn, parent_comment_id)).await??;
+    parent_comment.creator_id
+  } else {
+    let parent_post_id = comment.post_id;
+    let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
+    parent_post.creator_id
+  };
+  Ok(
+    blocking(pool, move |conn| Person::read(conn, parent_creator_id))
+      .await??
+      .into(),
+  )
+}
+
+/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
+/// using webfinger.
+async fn fetch_webfinger_url(
+  mention: &MentionData,
+  context: &LemmyContext,
+) -> Result<Url, LemmyError> {
+  let fetch_url = format!(
+    "{}://{}/.well-known/webfinger?resource=acct:{}@{}",
+    context.settings().get_protocol_string(),
+    mention.domain,
+    mention.name,
+    mention.domain
+  );
+  debug!("Fetching webfinger url: {}", &fetch_url);
+
+  let response = retry(|| context.client().get(&fetch_url).send()).await?;
+
+  let res: WebfingerResponse = response
+    .json()
+    .await
+    .map_err(|e| RecvError(e.to_string()))?;
+
+  let link = res
+    .links
+    .iter()
+    .find(|l| l.type_.eq(&Some("application/activity+json".to_string())))
+    .ok_or_else(|| anyhow!("No application/activity+json link found."))?;
+  link
+    .href
+    .to_owned()
+    .ok_or_else(|| anyhow!("No href found.").into())
+}
index 83895e8d3c32c3bf894f379cbf5eaa72b8b88945..406838181c0cb96967f3a275ece67ec1db505249 100644 (file)
@@ -1,6 +1,7 @@
 use crate::{
   activities::{verify_is_public, verify_person_in_community},
   check_is_apub_id_valid,
+  mentions::collect_non_local_mentions,
   protocol::{
     objects::{
       note::{Note, SourceCompat},
@@ -93,6 +94,11 @@ impl ApubObject for ApubComment {
 
     let post_id = self.post_id;
     let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
+    let community_id = post.community_id;
+    let community = blocking(context.pool(), move |conn| {
+      Community::read(conn, community_id)
+    })
+    .await??;
 
     let in_reply_to = if let Some(comment_id) = self.parent_id {
       let parent_comment =
@@ -101,13 +107,14 @@ impl ApubObject for ApubComment {
     } else {
       ObjectId::<PostOrComment>::new(post.ap_id)
     };
+    let maa = collect_non_local_mentions(&self, ObjectId::new(community.actor_id), context).await?;
 
     let note = Note {
       r#type: NoteType::Note,
       id: ObjectId::new(self.ap_id.clone()),
       attributed_to: ObjectId::new(creator.actor_id),
       to: vec![public()],
-      cc: vec![],
+      cc: maa.ccs,
       content: markdown_to_html(&self.content),
       media_type: Some(MediaTypeHtml::Html),
       source: SourceCompat::Lemmy(Source {
@@ -117,6 +124,7 @@ impl ApubObject for ApubComment {
       in_reply_to,
       published: Some(convert_datetime(self.published)),
       updated: self.updated.map(convert_datetime),
+      tag: maa.tags,
       unparsed: Default::default(),
     };
 
index d391e8286cdb90972cb87d8b5d937c8f8406de48..f5db71c20e6c8e46876f0bdaf9d35d7b7b4f8e4e 100644 (file)
@@ -14,7 +14,7 @@ mod tests {
 
   #[actix_rt::test]
   #[serial]
-  async fn test_parsey_create_or_update() {
+  async fn test_parse_create_or_update() {
     test_parse_lemmy_item::<CreateOrUpdatePost>(
       "assets/lemmy/activities/create_or_update/create_page.json",
     );
index 22df0354d07ee9d3da2c406bc320d5df444b25a2..37a29f8fa0c8e70d68445bb58311d5a9a131b392 100644 (file)
@@ -30,7 +30,9 @@ pub(crate) mod tests {
   use serde::{de::DeserializeOwned, Serialize};
   use std::collections::HashMap;
 
-  pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned>(path: &str) -> T {
+  pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned + std::fmt::Debug>(
+    path: &str,
+  ) -> T {
     let parsed = file_to_json_object::<T>(path);
 
     // ensure that no field is ignored when parsing
index 784e6f9f869cc29c837ff6836f38bcda99a34f3f..fdd6ddd96849e812a567f5b1047ba90c7685cb33 100644 (file)
@@ -3,7 +3,7 @@ use crate::{
   objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
   protocol::Source,
 };
-use activitystreams::{object::kind::NoteType, unparsed::Unparsed};
+use activitystreams::{link::Mention, object::kind::NoteType, unparsed::Unparsed};
 use anyhow::anyhow;
 use chrono::{DateTime, FixedOffset};
 use lemmy_api_common::blocking;
@@ -38,6 +38,8 @@ pub struct Note {
   pub(crate) in_reply_to: ObjectId<PostOrComment>,
   pub(crate) published: Option<DateTime<FixedOffset>>,
   pub(crate) updated: Option<DateTime<FixedOffset>>,
+  #[serde(default)]
+  pub(crate) tag: Vec<Mention>,
   #[serde(flatten)]
   pub(crate) unparsed: Unparsed,
 }