]> Untitled Git - lemmy.git/commitdiff
Handle Like, Undo/Like activities from Mastodon, add tests (fixes #2378) (#2380)
authorNutomic <me@nutomic.com>
Fri, 29 Jul 2022 13:32:12 +0000 (15:32 +0200)
committerGitHub <noreply@github.com>
Fri, 29 Jul 2022 13:32:12 +0000 (15:32 +0200)
crates/apub/assets/mastodon/activities/like_page.json [new file with mode: 0644]
crates/apub/assets/mastodon/activities/undo_like_page.json [new file with mode: 0644]
crates/apub/src/activities/voting/undo_vote.rs
crates/apub/src/activities/voting/vote.rs
crates/apub/src/activity_lists.rs
crates/apub/src/http/mod.rs
crates/apub/src/protocol/activities/mod.rs
crates/apub/src/protocol/activities/voting/undo_vote.rs
crates/apub/src/protocol/activities/voting/vote.rs

diff --git a/crates/apub/assets/mastodon/activities/like_page.json b/crates/apub/assets/mastodon/activities/like_page.json
new file mode 100644 (file)
index 0000000..d8468f7
--- /dev/null
@@ -0,0 +1,7 @@
+{
+  "@context":"https://www.w3.org/ns/activitystreams",
+  "id":"https://mastodon.madrid/users/felix#likes/212340",
+  "type":"Like",
+  "actor":"https://mastodon.madrid/users/felix",
+  "object":"https://ds9.lemmy.ml/post/147"
+}
diff --git a/crates/apub/assets/mastodon/activities/undo_like_page.json b/crates/apub/assets/mastodon/activities/undo_like_page.json
new file mode 100644 (file)
index 0000000..83afb21
--- /dev/null
@@ -0,0 +1,12 @@
+{
+  "@context":"https://www.w3.org/ns/activitystreams",
+  "id":"https://mastodon.madrid/users/felix#likes/212341/undo",
+  "type":"Undo",
+  "actor":"https://mastodon.madrid/users/felix",
+  "object": {
+    "id":"https://mastodon.madrid/users/felix#likes/212341",
+    "type":"Like",
+    "actor":"https://mastodon.madrid/users/felix",
+    "object":"https://ds9.lemmy.ml/post/147"
+  }
+}
\ No newline at end of file
index 35b64251942dbdc28e787b1540c376a41f718423..69abdac51b1e25608c52cab1af0ab9695496b1e0 100644 (file)
@@ -46,14 +46,13 @@ impl UndoVote {
     .await??
     .into();
 
-    let object = Vote::new(object, actor, &community, kind.clone(), context)?;
+    let object = Vote::new(object, actor, kind.clone(), context)?;
     let id = generate_activity_id(
       UndoType::Undo,
       &context.settings().get_protocol_and_hostname(),
     )?;
     let undo_vote = UndoVote {
       actor: ObjectId::new(actor.actor_id()),
-      to: vec![community.actor_id()],
       object,
       cc: vec![public()],
       kind: UndoType::Undo,
index b4f4423880153cbe45fc65f3eb7e1d15a1ecc06e..d83ce3643fb5fdd347f33db36a9dde08fc617e87 100644 (file)
@@ -31,13 +31,11 @@ impl Vote {
   pub(in crate::activities::voting) fn new(
     object: &PostOrComment,
     actor: &ApubPerson,
-    community: &ApubCommunity,
     kind: VoteType,
     context: &LemmyContext,
   ) -> Result<Vote, LemmyError> {
     Ok(Vote {
       actor: ObjectId::new(actor.actor_id()),
-      to: vec![community.actor_id()],
       object: ObjectId::new(object.ap_id()),
       cc: vec![public()],
       kind: kind.clone(),
@@ -59,7 +57,7 @@ impl Vote {
     })
     .await??
     .into();
-    let vote = Vote::new(object, actor, &community, kind, context)?;
+    let vote = Vote::new(object, actor, kind, context)?;
 
     let activity = AnnouncableActivities::Vote(vote);
     send_activity_in_community(activity, actor, &community, vec![], context).await
index 656c8020693b9f8e1eccb7d5e29a54fba3a298ea..dbd185bddebf2bdc3cb913a8aac0aa0ad7d06c60 100644 (file)
@@ -197,3 +197,43 @@ impl ActivityHandler for GroupInboxActivities {
     }
   }
 }
+
+#[cfg(test)]
+mod tests {
+  use crate::{
+    activity_lists::{GroupInboxActivities, PersonInboxActivities, SiteInboxActivities},
+    protocol::tests::test_parse_lemmy_item,
+  };
+
+  #[test]
+  fn test_group_inbox() {
+    test_parse_lemmy_item::<GroupInboxActivities>("assets/lemmy/activities/following/follow.json")
+      .unwrap();
+    test_parse_lemmy_item::<GroupInboxActivities>(
+      "assets/lemmy/activities/create_or_update/create_note.json",
+    )
+    .unwrap();
+  }
+
+  #[test]
+  fn test_person_inbox() {
+    test_parse_lemmy_item::<PersonInboxActivities>("assets/lemmy/activities/following/accept.json")
+      .unwrap();
+    test_parse_lemmy_item::<PersonInboxActivities>(
+      "assets/lemmy/activities/create_or_update/create_note.json",
+    )
+    .unwrap();
+    test_parse_lemmy_item::<PersonInboxActivities>(
+      "assets/lemmy/activities/create_or_update/create_private_message.json",
+    )
+    .unwrap();
+  }
+
+  #[test]
+  fn test_site_inbox() {
+    test_parse_lemmy_item::<SiteInboxActivities>(
+      "assets/lemmy/activities/deletion/delete_user.json",
+    )
+    .unwrap();
+  }
+}
index 87a8a41277297b6fe9c3facb9e90e1caf1c41157..e2fb0b4292acecd6209ee2bc079efff5583f4a2e 100644 (file)
@@ -56,6 +56,7 @@ where
   for<'de2> <ActorT as ApubObject>::ApubType: serde::Deserialize<'de2>,
 {
   let activity_value: Value = serde_json::from_str(&payload)?;
+  debug!("Received activity {:#}", payload.as_str());
   let activity: Activity = serde_json::from_value(activity_value.clone())?;
   // Log the activity, so we avoid receiving and parsing it twice.
   let insert = insert_activity(activity.id(), activity_value, false, true, context.pool()).await?;
index 35d14f8f2f96348dd6d113e0120b1f3a659dff31..82c5215b4410e1a787ff2943fbdba9b1a8a08287 100644 (file)
@@ -45,6 +45,8 @@ mod tests {
     test_json::<Delete>("assets/mastodon/activities/delete.json").unwrap();
     test_json::<FollowCommunity>("assets/mastodon/activities/follow.json").unwrap();
     test_json::<UndoFollowCommunity>("assets/mastodon/activities/undo_follow.json").unwrap();
+    test_json::<Vote>("assets/mastodon/activities/like_page.json").unwrap();
+    test_json::<UndoVote>("assets/mastodon/activities/undo_like_page.json").unwrap();
   }
 
   #[test]
index 77e526884624270084915aa0532ae3e238ef2dce..6c43f48922fc64a3809591cbc5512a298dbf47e8 100644 (file)
@@ -11,10 +11,8 @@ use url::Url;
 #[serde(rename_all = "camelCase")]
 pub struct UndoVote {
   pub(crate) actor: ObjectId<ApubPerson>,
-  #[serde(deserialize_with = "deserialize_one_or_many")]
-  pub(crate) to: Vec<Url>,
   pub(crate) object: Vote,
-  #[serde(deserialize_with = "deserialize_one_or_many")]
+  #[serde(deserialize_with = "deserialize_one_or_many", default)]
   pub(crate) cc: Vec<Url>,
   #[serde(rename = "type")]
   pub(crate) kind: UndoType,
index 80ea2ed5629ece1b551eec6025f2b05b647cc374..2bc674f1d8179617fd543153049203352b4e33fb 100644 (file)
@@ -14,10 +14,8 @@ use url::Url;
 #[serde(rename_all = "camelCase")]
 pub struct Vote {
   pub(crate) actor: ObjectId<ApubPerson>,
-  #[serde(deserialize_with = "deserialize_one_or_many")]
-  pub(crate) to: Vec<Url>,
   pub(crate) object: ObjectId<PostOrComment>,
-  #[serde(deserialize_with = "deserialize_one_or_many")]
+  #[serde(deserialize_with = "deserialize_one_or_many", default)]
   pub(crate) cc: Vec<Url>,
   #[serde(rename = "type")]
   pub(crate) kind: VoteType,