Skip to content

Commit

Permalink
Handle Like, Undo/Like activities from Mastodon, add tests (fixes #2378
Browse files Browse the repository at this point in the history
…) (#2380)
  • Loading branch information
Nutomic committed Aug 5, 2022
1 parent b561a17 commit c84c295
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 11 deletions.
7 changes: 7 additions & 0 deletions crates/apub/assets/mastodon/activities/like_page.json
Original file line number Diff line number Diff line change
@@ -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"
}
12 changes: 12 additions & 0 deletions crates/apub/assets/mastodon/activities/undo_like_page.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
3 changes: 1 addition & 2 deletions crates/apub/src/activities/voting/undo_vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,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,
Expand Down
4 changes: 1 addition & 3 deletions crates/apub/src/activities/voting/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,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(),
Expand All @@ -61,7 +59,7 @@ impl Vote {
})
.await??
.into();
let vote = Vote::new(object, actor, &community, kind, context)?;
let vote = Vote::new(object, actor, kind, context)?;
let vote_id = vote.id.clone();

let activity = AnnouncableActivities::Vote(vote);
Expand Down
40 changes: 40 additions & 0 deletions crates/apub/src/activity_lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,43 @@ impl Id for AnnouncableActivities {
}
}
}

#[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();
}
}
2 changes: 2 additions & 0 deletions crates/apub/src/protocol/activities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 2 additions & 3 deletions crates/apub/src/protocol/activities/voting/undo_vote.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
deserialize_one_or_many,
objects::person::ApubPerson,
protocol::{activities::voting::vote::Vote, Unparsed},
};
Expand All @@ -11,10 +12,8 @@ use url::Url;
#[serde(rename_all = "camelCase")]
pub struct UndoVote {
pub(crate) actor: ObjectId<ApubPerson>,
#[serde(deserialize_with = "crate::deserialize_one_or_many")]
pub(crate) to: Vec<Url>,
pub(crate) object: Vote,
#[serde(deserialize_with = "crate::deserialize_one_or_many")]
#[serde(deserialize_with = "deserialize_one_or_many", default)]
pub(crate) cc: Vec<Url>,
#[serde(rename = "type")]
pub(crate) kind: UndoType,
Expand Down
5 changes: 2 additions & 3 deletions crates/apub/src/protocol/activities/voting/vote.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
deserialize_one_or_many,
fetcher::post_or_comment::PostOrComment,
objects::person::ApubPerson,
protocol::Unparsed,
Expand All @@ -14,10 +15,8 @@ use url::Url;
#[serde(rename_all = "camelCase")]
pub struct Vote {
pub(crate) actor: ObjectId<ApubPerson>,
#[serde(deserialize_with = "crate::deserialize_one_or_many")]
pub(crate) to: Vec<Url>,
pub(crate) object: ObjectId<PostOrComment>,
#[serde(deserialize_with = "crate::deserialize_one_or_many")]
#[serde(deserialize_with = "deserialize_one_or_many", default)]
pub(crate) cc: Vec<Url>,
#[serde(rename = "type")]
pub(crate) kind: VoteType,
Expand Down

0 comments on commit c84c295

Please sign in to comment.