From 7cf3cf77cfffd1e1f6187d7a3485bf2a5d18ca00 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Fri, 25 May 2018 12:51:04 +0000 Subject: [PATCH] activitypub transmogrifier: cleanups and tests for incoming accepts/rejects --- .../web/activity_pub/transmogrifier.ex | 16 +-- test/fixtures/mastodon-reject-activity.json | 34 ++++++ test/web/activity_pub/transmogrifier_test.exs | 109 ++++++++++++++++++ 3 files changed, 152 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/mastodon-reject-activity.json diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 519548788..41198d4e6 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -152,10 +152,10 @@ defp get_follow_activity(follow_object) do {:ok, follow_object} is_binary(follow_object) -> - object = get_obj_helper(follow_object) || ActivityPub.fetch_object_from_id(follow_object) + object = Activity.get_by_ap_id(follow_object) if object do - {:ok, object} + {:ok, object.data} else {:error, nil} end @@ -170,12 +170,13 @@ def handle_incoming( ) do with %User{} = followed <- User.get_or_fetch_by_ap_id(actor), {:ok, follow_activity} <- get_follow_activity(follow_object), - %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity["actor"]) do + %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity["actor"]), + {:ok, activity} <- ActivityPub.insert(data, true) do if not User.following?(follower, followed) do - User.follow(follower, followed) + {:ok, follower} = User.follow(follower, followed) end - {:ok, data} + {:ok, activity} end end @@ -184,10 +185,11 @@ def handle_incoming( ) do with %User{} = followed <- User.get_or_fetch_by_ap_id(actor), {:ok, follow_activity} <- get_follow_activity(follow_object), - %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity["actor"]) do + %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity["actor"]), + {:ok, activity} <- ActivityPub.insert(data, true) do User.unfollow(follower, followed) - {:ok, data} + {:ok, activity} end end diff --git a/test/fixtures/mastodon-reject-activity.json b/test/fixtures/mastodon-reject-activity.json new file mode 100644 index 000000000..9559d6c73 --- /dev/null +++ b/test/fixtures/mastodon-reject-activity.json @@ -0,0 +1,34 @@ +{ + "type": "Reject", + "signature": { + "type": "RsaSignature2017", + "signatureValue": "rBzK4Kqhd4g7HDS8WE5oRbWQb2R+HF/6awbUuMWhgru/xCODT0SJWSri0qWqEO4fPcpoUyz2d25cw6o+iy9wiozQb3hQNnu69AR+H5Mytc06+g10KCHexbGhbAEAw/7IzmeXELHUbaqeduaDIbdt1zw4RkwLXdqgQcGXTJ6ND1wM3WMHXQCK1m0flasIXFoBxpliPAGiElV8s0+Ltuh562GvflG3kB3WO+j+NaR0ZfG5G9N88xMj9UQlCKit5gpAE5p6syUsCU2WGBHywTumv73i3OVTIFfq+P9AdMsRuzw1r7zoKEsthW4aOzLQDi01ZjvdBz8zH6JnjDU7SMN/Ig==", + "creator": "http://mastodon.example.org/users/admin#main-key", + "created": "2018-02-17T14:36:41Z" + }, + "object": { + "type": "Follow", + "object": "http://mastodon.example.org/users/admin", + "id": "http://localtesting.pleroma.lol/users/lain#follows/4", + "actor": "http://localtesting.pleroma.lol/users/lain" + }, + "nickname": "lain", + "id": "http://mastodon.example.org/users/admin#rejects/follows/4", + "actor": "http://mastodon.example.org/users/admin", + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "toot": "http://joinmastodon.org/ns#", + "sensitive": "as:sensitive", + "ostatus": "http://ostatus.org#", + "movedTo": "as:movedTo", + "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", + "inReplyToAtomUri": "ostatus:inReplyToAtomUri", + "conversation": "ostatus:conversation", + "atomUri": "ostatus:atomUri", + "Hashtag": "as:Hashtag", + "Emoji": "toot:Emoji" + } + ] +} diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index cf6b1d0b5..b51e02b08 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -2,6 +2,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do use Pleroma.DataCase alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Web.ActivityPub.Utils + alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.OStatus alias Pleroma.Activity alias Pleroma.User @@ -385,6 +386,114 @@ test "it works for incoming unblocks with an existing block" do refute User.blocks?(blocker, user) end + + test "it works for incoming accepts which were pre-accepted" do + follower = insert(:user) + followed = insert(:user) + + {:ok, follower} = User.follow(follower, followed) + assert User.following?(follower, followed) == true + + accept_data = + File.read!("test/fixtures/mastodon-accept-activity.json") + |> Poison.decode!() + |> Map.put("actor", followed.ap_id) + + accept_data = + Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id)) + + {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(accept_data) + + follower = Repo.get(User, follower.id) + + assert User.following?(follower, followed) == true + end + + test "it works for incoming accepts which were orphaned" do + follower = insert(:user) + followed = insert(:user, %{info: %{"locked" => true}}) + + {:ok, follow_activity} = ActivityPub.follow(follower, followed) + + accept_data = + File.read!("test/fixtures/mastodon-accept-activity.json") + |> Poison.decode!() + |> Map.put("actor", followed.ap_id) + + accept_data = + Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id)) + + {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(accept_data) + + follower = Repo.get(User, follower.id) + + assert User.following?(follower, followed) == true + end + + test "it works for incoming accepts which are referenced by IRI only" do + follower = insert(:user) + followed = insert(:user, %{info: %{"locked" => true}}) + + {:ok, follow_activity} = ActivityPub.follow(follower, followed) + + accept_data = + File.read!("test/fixtures/mastodon-accept-activity.json") + |> Poison.decode!() + |> Map.put("actor", followed.ap_id) + |> Map.put("object", follow_activity.data["id"]) + + {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(accept_data) + + follower = Repo.get(User, follower.id) + + assert User.following?(follower, followed) == true + end + + test "it works for incoming rejects which are orphaned" do + follower = insert(:user) + followed = insert(:user, %{info: %{"locked" => true}}) + + {:ok, follower} = User.follow(follower, followed) + {:ok, follow_activity} = ActivityPub.follow(follower, followed) + + assert User.following?(follower, followed) == true + + reject_data = + File.read!("test/fixtures/mastodon-reject-activity.json") + |> Poison.decode!() + |> Map.put("actor", followed.ap_id) + + reject_data = + Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id)) + + {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data) + + follower = Repo.get(User, follower.id) + + assert User.following?(follower, followed) == false + end + + test "it works for incoming rejects which are referenced by IRI only" do + follower = insert(:user) + followed = insert(:user, %{info: %{"locked" => true}}) + + {:ok, follower} = User.follow(follower, followed) + {:ok, follow_activity} = ActivityPub.follow(follower, followed) + + assert User.following?(follower, followed) == true + + reject_data = + File.read!("test/fixtures/mastodon-reject-activity.json") + |> Poison.decode!() + |> Map.put("actor", followed.ap_id) + |> Map.put("object", follow_activity.data["id"]) + + {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data) + + follower = Repo.get(User, follower.id) + + assert User.following?(follower, followed) == false + end end describe "prepare outgoing" do