forked from YokaiRick/akkoma
activitypub transmogrifier: cleanups and tests for incoming accepts/rejects
This commit is contained in:
parent
f35e6bf75b
commit
7cf3cf77cf
3 changed files with 152 additions and 7 deletions
|
@ -152,10 +152,10 @@ defp get_follow_activity(follow_object) do
|
||||||
{:ok, follow_object}
|
{:ok, follow_object}
|
||||||
|
|
||||||
is_binary(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
|
if object do
|
||||||
{:ok, object}
|
{:ok, object.data}
|
||||||
else
|
else
|
||||||
{:error, nil}
|
{:error, nil}
|
||||||
end
|
end
|
||||||
|
@ -170,12 +170,13 @@ def handle_incoming(
|
||||||
) do
|
) do
|
||||||
with %User{} = followed <- User.get_or_fetch_by_ap_id(actor),
|
with %User{} = followed <- User.get_or_fetch_by_ap_id(actor),
|
||||||
{:ok, follow_activity} <- get_follow_activity(follow_object),
|
{: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
|
if not User.following?(follower, followed) do
|
||||||
User.follow(follower, followed)
|
{:ok, follower} = User.follow(follower, followed)
|
||||||
end
|
end
|
||||||
|
|
||||||
{:ok, data}
|
{:ok, activity}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -184,10 +185,11 @@ def handle_incoming(
|
||||||
) do
|
) do
|
||||||
with %User{} = followed <- User.get_or_fetch_by_ap_id(actor),
|
with %User{} = followed <- User.get_or_fetch_by_ap_id(actor),
|
||||||
{:ok, follow_activity} <- get_follow_activity(follow_object),
|
{: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)
|
User.unfollow(follower, followed)
|
||||||
|
|
||||||
{:ok, data}
|
{:ok, activity}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
34
test/fixtures/mastodon-reject-activity.json
vendored
Normal file
34
test/fixtures/mastodon-reject-activity.json
vendored
Normal file
|
@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase
|
||||||
alias Pleroma.Web.ActivityPub.Transmogrifier
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
||||||
alias Pleroma.Web.ActivityPub.Utils
|
alias Pleroma.Web.ActivityPub.Utils
|
||||||
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
alias Pleroma.Web.OStatus
|
alias Pleroma.Web.OStatus
|
||||||
alias Pleroma.Activity
|
alias Pleroma.Activity
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
|
@ -385,6 +386,114 @@ test "it works for incoming unblocks with an existing block" do
|
||||||
|
|
||||||
refute User.blocks?(blocker, user)
|
refute User.blocks?(blocker, user)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe "prepare outgoing" do
|
describe "prepare outgoing" do
|
||||||
|
|
Loading…
Reference in a new issue