From d8c842a7713ab2c9dd49bdabfad4f5a1b6120fcf Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Thu, 17 May 2018 22:15:57 -0400 Subject: [PATCH 01/22] Add support for incoming remote unfollows --- lib/pleroma/user.ex | 24 ++++++++------ .../web/activity_pub/transmogrifier.ex | 18 ++++++++++ test/fixtures/mastodon-unfollow-activity.json | 33 +++++++++++++++++++ test/web/activity_pub/transmogrifier_test.exs | 24 ++++++++++++++ 4 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 test/fixtures/mastodon-unfollow-activity.json diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 399a66787..6a8129ac8 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -404,18 +404,22 @@ defmodule Pleroma.User do from( u in User, select_merge: %{ - search_distance: fragment( - "? <-> (? || ?)", - ^query, - u.nickname, - u.name - )} + search_distance: + fragment( + "? <-> (? || ?)", + ^query, + u.nickname, + u.name + ) + } ) - q = from(s in subquery(inner), - order_by: s.search_distance, - limit: 20 - ) + q = + from( + s in subquery(inner), + order_by: s.search_distance, + limit: 20 + ) Repo.all(q) end diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 463d1e59d..0bec8c4dd 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -241,6 +241,24 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do end end + def handle_incoming( + %{ + "type" => "Undo", + "object" => %{"type" => "Follow", "object" => followed}, + "actor" => follower, + "id" => id + } = data + ) do + with %User{local: true} = followed = User.get_cached_by_ap_id(followed), + %User{} = follower = User.get_or_fetch_by_ap_id(follower), + {:ok, activity} <- ActivityPub.unfollow(follower, followed, false) do + User.unfollow(follower, followed) + {:ok, activity} + else + e -> :error + end + end + # TODO # Accept # Undo for non-Announce diff --git a/test/fixtures/mastodon-unfollow-activity.json b/test/fixtures/mastodon-unfollow-activity.json new file mode 100644 index 000000000..022b47acd --- /dev/null +++ b/test/fixtures/mastodon-unfollow-activity.json @@ -0,0 +1,33 @@ +{ + "@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" + } + ], + "signature": { + "type": "RsaSignature2017", + "signatureValue": "Kn1/UkAQGJVaXBfWLAHcnwHg8YMAUqlEaBuYLazAG+pz5hqivsyrBmPV186Xzr+B4ZLExA9+SnOoNx/GOz4hBm0kAmukNSILAsUd84tcJ2yT9zc1RKtembK4WiwOw7li0+maeDN0HaB6t+6eTqsCWmtiZpprhXD8V1GGT8yG7X24fQ9oFGn+ng7lasbcCC0988Y1eGqNe7KryxcPuQz57YkDapvtONzk8gyLTkZMV4De93MyRHq6GVjQVIgtiYabQAxrX6Q8C+4P/jQoqdWJHEe+MY5JKyNaT/hMPt2Md1ok9fZQBGHlErk22/zy8bSN19GdG09HmIysBUHRYpBLig==", + "creator": "http://mastodon.example.org/users/admin#main-key", + "created": "2018-02-17T13:29:31Z" + }, + "type": "Undo", + "object": { + "type": "Follow", + "object": "http://localtesting.pleroma.lol/users/lain", + "nickname": "lain", + "id": "http://mastodon.example.org/users/admin#follows/2", + "actor": "http://mastodon.example.org/users/admin" + }, + "actor": "http://mastodon.example.org/users/admin" +} diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index a3408da9d..8ed7147f4 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -260,6 +260,30 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do assert data["object"]["id"] == "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity" end + + test "it works for incomming unfollows" do + user = insert(:user) + + follow_data = + File.read!("test/fixtures/mastodon-follow-activity.json") + |> Poison.decode!() + |> Map.put("object", user.ap_id) + + {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data) + + data = + File.read!("test/fixtures/mastodon-unfollow-activity.json") + |> Poison.decode!() + |> Map.put("object", follow_data) + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + + assert data["type"] == "Undo" + assert data["object"]["type"] == "Follow" + assert data["actor"] == "http://mastodon.example.org/users/admin" + + refute User.following?(User.get_by_ap_id(data["actor"]), user) + end end describe "prepare outgoing" do From e6dc15b96cb971836211a1e57be1756936456c17 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Thu, 17 May 2018 23:53:34 -0400 Subject: [PATCH 02/22] Undo formatting in user.ex --- .../web/activity_pub/transmogrifier.ex | 18 ---------- test/fixtures/mastodon-unfollow-activity.json | 33 ------------------- test/web/activity_pub/transmogrifier_test.exs | 24 -------------- 3 files changed, 75 deletions(-) delete mode 100644 test/fixtures/mastodon-unfollow-activity.json diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 0bec8c4dd..463d1e59d 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -241,24 +241,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do end end - def handle_incoming( - %{ - "type" => "Undo", - "object" => %{"type" => "Follow", "object" => followed}, - "actor" => follower, - "id" => id - } = data - ) do - with %User{local: true} = followed = User.get_cached_by_ap_id(followed), - %User{} = follower = User.get_or_fetch_by_ap_id(follower), - {:ok, activity} <- ActivityPub.unfollow(follower, followed, false) do - User.unfollow(follower, followed) - {:ok, activity} - else - e -> :error - end - end - # TODO # Accept # Undo for non-Announce diff --git a/test/fixtures/mastodon-unfollow-activity.json b/test/fixtures/mastodon-unfollow-activity.json deleted file mode 100644 index 022b47acd..000000000 --- a/test/fixtures/mastodon-unfollow-activity.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "@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" - } - ], - "signature": { - "type": "RsaSignature2017", - "signatureValue": "Kn1/UkAQGJVaXBfWLAHcnwHg8YMAUqlEaBuYLazAG+pz5hqivsyrBmPV186Xzr+B4ZLExA9+SnOoNx/GOz4hBm0kAmukNSILAsUd84tcJ2yT9zc1RKtembK4WiwOw7li0+maeDN0HaB6t+6eTqsCWmtiZpprhXD8V1GGT8yG7X24fQ9oFGn+ng7lasbcCC0988Y1eGqNe7KryxcPuQz57YkDapvtONzk8gyLTkZMV4De93MyRHq6GVjQVIgtiYabQAxrX6Q8C+4P/jQoqdWJHEe+MY5JKyNaT/hMPt2Md1ok9fZQBGHlErk22/zy8bSN19GdG09HmIysBUHRYpBLig==", - "creator": "http://mastodon.example.org/users/admin#main-key", - "created": "2018-02-17T13:29:31Z" - }, - "type": "Undo", - "object": { - "type": "Follow", - "object": "http://localtesting.pleroma.lol/users/lain", - "nickname": "lain", - "id": "http://mastodon.example.org/users/admin#follows/2", - "actor": "http://mastodon.example.org/users/admin" - }, - "actor": "http://mastodon.example.org/users/admin" -} diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 8ed7147f4..a3408da9d 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -260,30 +260,6 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do assert data["object"]["id"] == "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity" end - - test "it works for incomming unfollows" do - user = insert(:user) - - follow_data = - File.read!("test/fixtures/mastodon-follow-activity.json") - |> Poison.decode!() - |> Map.put("object", user.ap_id) - - {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data) - - data = - File.read!("test/fixtures/mastodon-unfollow-activity.json") - |> Poison.decode!() - |> Map.put("object", follow_data) - - {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) - - assert data["type"] == "Undo" - assert data["object"]["type"] == "Follow" - assert data["actor"] == "http://mastodon.example.org/users/admin" - - refute User.following?(User.get_by_ap_id(data["actor"]), user) - end end describe "prepare outgoing" do From 8d11bae0d5e649f9dc0570b24ee5c4c74b8b463f Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Thu, 17 May 2018 23:55:00 -0400 Subject: [PATCH 03/22] Revert "Undo formatting in user.ex" This reverts commit e6dc15b96cb971836211a1e57be1756936456c17. --- .../web/activity_pub/transmogrifier.ex | 18 ++++++++++ test/fixtures/mastodon-unfollow-activity.json | 33 +++++++++++++++++++ test/web/activity_pub/transmogrifier_test.exs | 24 ++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 test/fixtures/mastodon-unfollow-activity.json diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 463d1e59d..0bec8c4dd 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -241,6 +241,24 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do end end + def handle_incoming( + %{ + "type" => "Undo", + "object" => %{"type" => "Follow", "object" => followed}, + "actor" => follower, + "id" => id + } = data + ) do + with %User{local: true} = followed = User.get_cached_by_ap_id(followed), + %User{} = follower = User.get_or_fetch_by_ap_id(follower), + {:ok, activity} <- ActivityPub.unfollow(follower, followed, false) do + User.unfollow(follower, followed) + {:ok, activity} + else + e -> :error + end + end + # TODO # Accept # Undo for non-Announce diff --git a/test/fixtures/mastodon-unfollow-activity.json b/test/fixtures/mastodon-unfollow-activity.json new file mode 100644 index 000000000..022b47acd --- /dev/null +++ b/test/fixtures/mastodon-unfollow-activity.json @@ -0,0 +1,33 @@ +{ + "@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" + } + ], + "signature": { + "type": "RsaSignature2017", + "signatureValue": "Kn1/UkAQGJVaXBfWLAHcnwHg8YMAUqlEaBuYLazAG+pz5hqivsyrBmPV186Xzr+B4ZLExA9+SnOoNx/GOz4hBm0kAmukNSILAsUd84tcJ2yT9zc1RKtembK4WiwOw7li0+maeDN0HaB6t+6eTqsCWmtiZpprhXD8V1GGT8yG7X24fQ9oFGn+ng7lasbcCC0988Y1eGqNe7KryxcPuQz57YkDapvtONzk8gyLTkZMV4De93MyRHq6GVjQVIgtiYabQAxrX6Q8C+4P/jQoqdWJHEe+MY5JKyNaT/hMPt2Md1ok9fZQBGHlErk22/zy8bSN19GdG09HmIysBUHRYpBLig==", + "creator": "http://mastodon.example.org/users/admin#main-key", + "created": "2018-02-17T13:29:31Z" + }, + "type": "Undo", + "object": { + "type": "Follow", + "object": "http://localtesting.pleroma.lol/users/lain", + "nickname": "lain", + "id": "http://mastodon.example.org/users/admin#follows/2", + "actor": "http://mastodon.example.org/users/admin" + }, + "actor": "http://mastodon.example.org/users/admin" +} diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index a3408da9d..8ed7147f4 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -260,6 +260,30 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do assert data["object"]["id"] == "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity" end + + test "it works for incomming unfollows" do + user = insert(:user) + + follow_data = + File.read!("test/fixtures/mastodon-follow-activity.json") + |> Poison.decode!() + |> Map.put("object", user.ap_id) + + {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data) + + data = + File.read!("test/fixtures/mastodon-unfollow-activity.json") + |> Poison.decode!() + |> Map.put("object", follow_data) + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + + assert data["type"] == "Undo" + assert data["object"]["type"] == "Follow" + assert data["actor"] == "http://mastodon.example.org/users/admin" + + refute User.following?(User.get_by_ap_id(data["actor"]), user) + end end describe "prepare outgoing" do From 6604bea9d03c2a0eb48cd08d95de88a31656e181 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Thu, 17 May 2018 23:56:47 -0400 Subject: [PATCH 04/22] actually rvert those changes --- lib/pleroma/user.ex | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 6a8129ac8..399a66787 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -404,22 +404,18 @@ defmodule Pleroma.User do from( u in User, select_merge: %{ - search_distance: - fragment( - "? <-> (? || ?)", - ^query, - u.nickname, - u.name - ) - } + search_distance: fragment( + "? <-> (? || ?)", + ^query, + u.nickname, + u.name + )} ) - q = - from( - s in subquery(inner), - order_by: s.search_distance, - limit: 20 - ) + q = from(s in subquery(inner), + order_by: s.search_distance, + limit: 20 + ) Repo.all(q) end From 0ec1abb3b65d9885e81a470efcc5b7437c4f8d4c Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Fri, 18 May 2018 18:09:56 -0400 Subject: [PATCH 05/22] Add federated blocks --- lib/pleroma/web/activity_pub/activity_pub.ex | 23 ++++++++++++ .../web/activity_pub/transmogrifier.ex | 18 ++++++++++ lib/pleroma/web/activity_pub/utils.ex | 35 +++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 491ad3705..48eba36fd 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -199,6 +199,29 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end end + def block(blocker, blocked, local \\ true) do + follow_activity = fetch_latest_follow(blocker, blocked) + + if follow_activity do + unfollow(blocker, blocked, local) + end + + with block_data <- make_block_data(blocker, blocked), + {:ok, activity} <- insert(block_data, local), + :ok <- maybe_federate(activity) do + {:ok, activity} + end + end + + def unblock(blocker, blocked, local \\ true) do + with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked), + unblock_data <- make_unblock_data(blocker, blocked, block_activity), + {:ok, activity} <- insert(unblock_data, local), + :ok <- maybe_federate(activity) do + {:ok, activity} + end + end + def fetch_activities_for_context(context, opts \\ %{}) do public = ["https://www.w3.org/ns/activitystreams#Public"] diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 0bec8c4dd..cc0033a5f 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -259,6 +259,24 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do end end + def handle_incoming(%{"type" => "Undo", "object" => object_id} = data) do + object = Object.get_by_ap_id(object_id).data + data = Map.put(data, "object", object) + + handle_incoming(data) + end + + def handle_incoming(%{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = data) do + with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked), + %User{} = blocker = User.get_or_fetch_by_ap_id(blocker), + {:ok, activity} <- ActivityPub.block(blocker, blocked, false) do + User.unfollow(follower, followed) + User.block(blocker, blocked.ap_id) + {:ok, activity} + else + e -> :error + end + end # TODO # Accept # Undo for non-Announce diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index f98545336..c29fc6b07 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -322,6 +322,41 @@ defmodule Pleroma.Web.ActivityPub.Utils do } end + #### Block-related helpers + def fetch_latest_block(%User{ap_id: blocker_id}, %User{ap_id: blocked_id}) do + query = + from( + activity in Activity, + where: + fragment( + "? @> ?", + activity.data, + ^%{type: "Block", actor: blocker_id, object: blocked_id} + ), + order_by: [desc: :id], + limit: 1 + ) + + Repo.one(query) + end + + def make_block_data(blocker, blocked) do + %{ + "type" => "Block", + "actor" => blocker.ap_id, + "to" => [blocked.ap_id], + "object" => blocked.ap_id + } + end + + def make_unblock_data(blocker, blocked, block_activity) do + %{ + "type" => "Undo", + "actor" => blocker.ap_id, + "to" => [blocked.ap_id], + "object" => block_activity.data + } + end #### Create-related helpers def make_create_data(params, additional) do From da9744eb8481b4df527d0e360aa30be9ca6b5a3a Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Sat, 19 May 2018 20:57:37 -0400 Subject: [PATCH 06/22] Use correct variable names in unfollow --- lib/pleroma/web/activity_pub/transmogrifier.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index cc0033a5f..0d539c02c 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -270,7 +270,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked), %User{} = blocker = User.get_or_fetch_by_ap_id(blocker), {:ok, activity} <- ActivityPub.block(blocker, blocked, false) do - User.unfollow(follower, followed) + User.unfollow(blocker, blocked) User.block(blocker, blocked.ap_id) {:ok, activity} else From 1e43de0f86ea632a313f9bf7434183a6db38f0b6 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Sat, 19 May 2018 21:23:52 -0400 Subject: [PATCH 07/22] Formatting and add tests for blocks/unblocks --- lib/pleroma/user.ex | 24 ++++++---- .../web/activity_pub/transmogrifier.ex | 5 +- lib/pleroma/web/activity_pub/utils.ex | 1 + test/fixtures/mastodon-block-activity.json | 29 ++++++++++++ test/fixtures/mastodon-unblock-activity.json | 33 +++++++++++++ test/web/activity_pub/transmogrifier_test.exs | 46 +++++++++++++++++++ 6 files changed, 127 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/mastodon-block-activity.json create mode 100644 test/fixtures/mastodon-unblock-activity.json diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 399a66787..6a8129ac8 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -404,18 +404,22 @@ defmodule Pleroma.User do from( u in User, select_merge: %{ - search_distance: fragment( - "? <-> (? || ?)", - ^query, - u.nickname, - u.name - )} + search_distance: + fragment( + "? <-> (? || ?)", + ^query, + u.nickname, + u.name + ) + } ) - q = from(s in subquery(inner), - order_by: s.search_distance, - limit: 20 - ) + q = + from( + s in subquery(inner), + order_by: s.search_distance, + limit: 20 + ) Repo.all(q) end diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 0d539c02c..fe0294c5c 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -266,7 +266,9 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do handle_incoming(data) end - def handle_incoming(%{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = data) do + def handle_incoming( + %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = data + ) do with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked), %User{} = blocker = User.get_or_fetch_by_ap_id(blocker), {:ok, activity} <- ActivityPub.block(blocker, blocked, false) do @@ -277,6 +279,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do e -> :error end end + # TODO # Accept # Undo for non-Announce diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index c29fc6b07..846dd97c2 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -357,6 +357,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do "object" => block_activity.data } end + #### Create-related helpers def make_create_data(params, additional) do diff --git a/test/fixtures/mastodon-block-activity.json b/test/fixtures/mastodon-block-activity.json new file mode 100644 index 000000000..186719d2c --- /dev/null +++ b/test/fixtures/mastodon-block-activity.json @@ -0,0 +1,29 @@ +{ + "type": "Block", + "signature": { + "type": "RsaSignature2017", + "signatureValue": "Kn1/UkAQGJVaXBfWLAHcnwHg8YMAUqlEaBuYLazAG+pz5hqivsyrBmPV186Xzr+B4ZLExA9+SnOoNx/GOz4hBm0kAmukNSILAsUd84tcJ2yT9zc1RKtembK4WiwOw7li0+maeDN0HaB6t+6eTqsCWmtiZpprhXD8V1GGT8yG7X24fQ9oFGn+ng7lasbcCC0988Y1eGqNe7KryxcPuQz57YkDapvtONzk8gyLTkZMV4De93MyRHq6GVjQVIgtiYabQAxrX6Q8C+4P/jQoqdWJHEe+MY5JKyNaT/hMPt2Md1ok9fZQBGHlErk22/zy8bSN19GdG09HmIysBUHRYpBLig==", + "creator": "http://mastodon.example.org/users/admin#main-key", + "created": "2018-02-17T13:29:31Z" + }, + "object": "http://localtesting.pleroma.lol/users/lain", + "nickname": "lain", + "id": "http://mastodon.example.org/users/admin#follows/2", + "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/fixtures/mastodon-unblock-activity.json b/test/fixtures/mastodon-unblock-activity.json new file mode 100644 index 000000000..53b2f42a8 --- /dev/null +++ b/test/fixtures/mastodon-unblock-activity.json @@ -0,0 +1,33 @@ +{ + "@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" + } + ], + "signature": { + "type": "RsaSignature2017", + "signatureValue": "Kn1/UkAQGJVaXBfWLAHcnwHg8YMAUqlEaBuYLazAG+pz5hqivsyrBmPV186Xzr+B4ZLExA9+SnOoNx/GOz4hBm0kAmukNSILAsUd84tcJ2yT9zc1RKtembK4WiwOw7li0+maeDN0HaB6t+6eTqsCWmtiZpprhXD8V1GGT8yG7X24fQ9oFGn+ng7lasbcCC0988Y1eGqNe7KryxcPuQz57YkDapvtONzk8gyLTkZMV4De93MyRHq6GVjQVIgtiYabQAxrX6Q8C+4P/jQoqdWJHEe+MY5JKyNaT/hMPt2Md1ok9fZQBGHlErk22/zy8bSN19GdG09HmIysBUHRYpBLig==", + "creator": "http://mastodon.example.org/users/admin#main-key", + "created": "2018-02-17T13:29:31Z" + }, + "type": "Undo", + "object": { + "type": "Block", + "object": "http://localtesting.pleroma.lol/users/lain", + "nickname": "lain", + "id": "http://mastodon.example.org/users/admin#follows/2", + "actor": "http://mastodon.example.org/users/admin" + }, + "actor": "http://mastodon.example.org/users/admin" +} diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 8ed7147f4..0ca00a07e 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -280,10 +280,56 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do assert data["type"] == "Undo" assert data["object"]["type"] == "Follow" + assert data["object"]["object"] == user.ap_id assert data["actor"] == "http://mastodon.example.org/users/admin" refute User.following?(User.get_by_ap_id(data["actor"]), user) end + + test "it works for incoming blocks" do + user = insert(:user) + + data = + File.read!("test/fixtures/mastodon-block-activity.json") + |> Poison.decode!() + |> Map.put("object", user.ap_id) + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + + assert data["type"] == "Block" + assert data["object"] == user.ap_id + assert data["actor"] == "https://mastodon.example.org/users/admin" + + blocker = User.get_by_ap_id(data["actor"]) + + assert User.blocks?(blocker, user.ap_id) + end + + test "it works for incoming unblocks" do + user = insert(:user) + + block_data = + File.read!("test/fixtures/mastodon-block-activity.json") + |> Poison.decode!() + |> Map.put("object", user.ap_id) + + {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data) + + data = + File.read!("test/fixtures/mastodon-unblock-activity.json") + |> Poison.decode!() + |> Map.put("object", block_data) + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + assert data["type"] == "Undo" + assert data["object"]["type"] == "Block" + assert data["object"]["object"] == user.ap_id + assert data["actor"] == "https://mastodon.example.org/users/admin" + + blocker = User.get_by_ap_id(data["actor"]) + + refute User.blocks?(blocker, user.ap_id) + end end describe "prepare outgoing" do From 3b706ea493cbb3ee1202fba96d3c2e509c07fb64 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Sat, 19 May 2018 21:34:30 -0400 Subject: [PATCH 08/22] Remove handler for incoming undos with object references --- lib/pleroma/web/activity_pub/transmogrifier.ex | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index fe0294c5c..887ffe2d0 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -259,13 +259,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do end end - def handle_incoming(%{"type" => "Undo", "object" => object_id} = data) do - object = Object.get_by_ap_id(object_id).data - data = Map.put(data, "object", object) - - handle_incoming(data) - end - def handle_incoming( %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = data ) do From a9141d88aad331cfe6573e1d6b3d77f5dab70be7 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Sat, 19 May 2018 22:02:13 -0400 Subject: [PATCH 09/22] Pass user instead of their ap_id to User.block --- lib/pleroma/web/activity_pub/transmogrifier.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 887ffe2d0..47b84a469 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -266,7 +266,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do %User{} = blocker = User.get_or_fetch_by_ap_id(blocker), {:ok, activity} <- ActivityPub.block(blocker, blocked, false) do User.unfollow(blocker, blocked) - User.block(blocker, blocked.ap_id) + User.block(blocker, blocked) {:ok, activity} else e -> :error From d46393f6efd6475bb20b6a12a7c6ebf800f6c5b5 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Sun, 20 May 2018 02:15:56 -0400 Subject: [PATCH 10/22] Fix test for blocks --- test/web/activity_pub/transmogrifier_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 0ca00a07e..1d38661eb 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -298,7 +298,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do assert data["type"] == "Block" assert data["object"] == user.ap_id - assert data["actor"] == "https://mastodon.example.org/users/admin" + assert data["actor"] == "http://mastodon.example.org/users/admin" blocker = User.get_by_ap_id(data["actor"]) From 56de0216ae36394dba1aa9bac6c931af67233658 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Sun, 20 May 2018 19:45:05 -0400 Subject: [PATCH 11/22] pass user to User.blocks?/2 instead of ap_id --- test/web/activity_pub/transmogrifier_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 87fadece2..da55909bc 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -357,7 +357,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do blocker = User.get_by_ap_id(data["actor"]) - assert User.blocks?(blocker, user.ap_id) + assert User.blocks?(blocker, user) end test "it works for incoming unblocks" do From 89c5de90969cbd2b7cd5854c7209795443659062 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Sun, 20 May 2018 21:01:14 -0400 Subject: [PATCH 12/22] Fix unfollows --- lib/pleroma/web/activity_pub/activity_pub.ex | 15 +++-- .../web/activity_pub/transmogrifier.ex | 4 +- lib/pleroma/web/activity_pub/utils.ex | 17 ++--- .../representers/activity_representer.ex | 2 +- test/fixtures/mastodon-unfollow-activity.json | 63 ++++++++++--------- test/web/activity_pub/activity_pub_test.exs | 2 +- 6 files changed, 53 insertions(+), 50 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index a711e6b76..c2d540db9 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -192,12 +192,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end end - def unfollow(follower, followed, local \\ true) do + def unfollow(follower, followed, activity_id \\ nil, local \\ true) do with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed), - unfollow_data <- make_unfollow_data(follower, followed, follow_activity), + unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id), {:ok, activity} <- insert(unfollow_data, local), - :ok, - maybe_federate(activity) do + :ok <- maybe_federate(activity) do {:ok, activity} end end @@ -221,23 +220,23 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end end - def block(blocker, blocked, local \\ true) do + def block(blocker, blocked, activity_id \\ nil, local \\ true) do follow_activity = fetch_latest_follow(blocker, blocked) if follow_activity do unfollow(blocker, blocked, local) end - with block_data <- make_block_data(blocker, blocked), + with block_data <- make_block_data(blocker, blocked, activity_id), {:ok, activity} <- insert(block_data, local), :ok <- maybe_federate(activity) do {:ok, activity} end end - def unblock(blocker, blocked, local \\ true) do + def unblock(blocker, blocked, activity_id \\ nil, local \\ true) do with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked), - unblock_data <- make_unblock_data(blocker, blocked, block_activity), + unblock_data <- make_unblock_data(blocker, blocked, block_activity, activity_id), {:ok, activity} <- insert(unblock_data, local), :ok <- maybe_federate(activity) do {:ok, activity} diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index d92ca9b65..20b1603df 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -251,7 +251,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do ) do with %User{local: true} = followed = User.get_cached_by_ap_id(followed), %User{} = follower = User.get_or_fetch_by_ap_id(follower), - {:ok, activity} <- ActivityPub.unfollow(follower, followed, false) do + {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do User.unfollow(follower, followed) {:ok, activity} else @@ -264,7 +264,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do ) do with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked), %User{} = blocker = User.get_or_fetch_by_ap_id(blocker), - {:ok, activity} <- ActivityPub.block(blocker, blocked, false) do + {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do User.unfollow(blocker, blocked) User.block(blocker, blocked) {:ok, activity} diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 050413d51..acf23c53e 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -346,13 +346,14 @@ defmodule Pleroma.Web.ActivityPub.Utils do #### Unfollow-related helpers - def make_unfollow_data(follower, followed, follow_activity) do - %{ + def make_unfollow_data(follower, followed, follow_activity, activity_id) do + data = %{ "type" => "Undo", "actor" => follower.ap_id, "to" => [followed.ap_id], - "object" => follow_activity.data["id"] + "object" => follow_activity.data } + if activity_id, do: Map.put(data, "id", activity_id), else: data end #### Block-related helpers @@ -373,22 +374,24 @@ defmodule Pleroma.Web.ActivityPub.Utils do Repo.one(query) end - def make_block_data(blocker, blocked) do - %{ + def make_block_data(blocker, blocked, activity_id) do + data = %{ "type" => "Block", "actor" => blocker.ap_id, "to" => [blocked.ap_id], "object" => blocked.ap_id } + if activity_id, do: Map.put(data, "id", activity_id), else: data end - def make_unblock_data(blocker, blocked, block_activity) do - %{ + def make_unblock_data(blocker, blocked, block_activity, activity_id) do + data = %{ "type" => "Undo", "actor" => blocker.ap_id, "to" => [blocked.ap_id], "object" => block_activity.data } + if activity_id, do: Map.put(data, "id", activity_id), else: data end #### Create-related helpers diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index c2e1f07a5..57837205e 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -99,7 +99,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do ) do created_at = created_at |> Utils.date_to_asctime() - text = "#{user.nickname} undid the action at #{undid_activity}" + text = "#{user.nickname} undid the action at #{undid_activity["id"]}" %{ "id" => activity.id, diff --git a/test/fixtures/mastodon-unfollow-activity.json b/test/fixtures/mastodon-unfollow-activity.json index 022b47acd..ae5ab7c66 100644 --- a/test/fixtures/mastodon-unfollow-activity.json +++ b/test/fixtures/mastodon-unfollow-activity.json @@ -1,33 +1,34 @@ { - "@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" - } - ], - "signature": { - "type": "RsaSignature2017", - "signatureValue": "Kn1/UkAQGJVaXBfWLAHcnwHg8YMAUqlEaBuYLazAG+pz5hqivsyrBmPV186Xzr+B4ZLExA9+SnOoNx/GOz4hBm0kAmukNSILAsUd84tcJ2yT9zc1RKtembK4WiwOw7li0+maeDN0HaB6t+6eTqsCWmtiZpprhXD8V1GGT8yG7X24fQ9oFGn+ng7lasbcCC0988Y1eGqNe7KryxcPuQz57YkDapvtONzk8gyLTkZMV4De93MyRHq6GVjQVIgtiYabQAxrX6Q8C+4P/jQoqdWJHEe+MY5JKyNaT/hMPt2Md1ok9fZQBGHlErk22/zy8bSN19GdG09HmIysBUHRYpBLig==", - "creator": "http://mastodon.example.org/users/admin#main-key", - "created": "2018-02-17T13:29:31Z" - }, - "type": "Undo", - "object": { - "type": "Follow", - "object": "http://localtesting.pleroma.lol/users/lain", - "nickname": "lain", - "id": "http://mastodon.example.org/users/admin#follows/2", - "actor": "http://mastodon.example.org/users/admin" - }, - "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" + } + ], + "signature":{ + "type":"RsaSignature2017", + "signatureValue":"Kn1/UkAQGJVaXBfWLAHcnwHg8YMAUqlEaBuYLazAG+pz5hqivsyrBmPV186Xzr+B4ZLExA9+SnOoNx/GOz4hBm0kAmukNSILAsUd84tcJ2yT9zc1RKtembK4WiwOw7li0+maeDN0HaB6t+6eTqsCWmtiZpprhXD8V1GGT8yG7X24fQ9oFGn+ng7lasbcCC0988Y1eGqNe7KryxcPuQz57YkDapvtONzk8gyLTkZMV4De93MyRHq6GVjQVIgtiYabQAxrX6Q8C+4P/jQoqdWJHEe+MY5JKyNaT/hMPt2Md1ok9fZQBGHlErk22/zy8bSN19GdG09HmIysBUHRYpBLig==", + "creator":"http://mastodon.example.org/users/admin#main-key", + "created":"2018-02-17T13:29:31Z" + }, + "type":"Undo", + "object":{ + "type":"Follow", + "object":"http://localtesting.pleroma.lol/users/lain", + "nickname":"lain", + "id":"http://mastodon.example.org/users/admin#follows/2", + "actor":"http://mastodon.example.org/users/admin" + }, + "actor":"http://mastodon.example.org/users/admin", + "id": "http://mastodon.example.org/users/admin#follow/2/undo" } diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 9adce84b5..35f0deffe 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -425,7 +425,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert activity.data["type"] == "Undo" assert activity.data["actor"] == follower.ap_id - assert activity.data["object"] == follow_activity.data["id"] + assert activity.data["object"]["id"] == follow_activity.data["id"] end end From c8a16f334dd6df12f90ee7f45b509642c8098b5b Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Sun, 20 May 2018 21:02:06 -0400 Subject: [PATCH 13/22] Format --- lib/pleroma/web/activity_pub/utils.ex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index acf23c53e..3f88a4672 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -353,6 +353,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do "to" => [followed.ap_id], "object" => follow_activity.data } + if activity_id, do: Map.put(data, "id", activity_id), else: data end @@ -381,6 +382,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do "to" => [blocked.ap_id], "object" => blocked.ap_id } + if activity_id, do: Map.put(data, "id", activity_id), else: data end @@ -391,6 +393,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do "to" => [blocked.ap_id], "object" => block_activity.data } + if activity_id, do: Map.put(data, "id", activity_id), else: data end From 25733322140d13a08e723b9366646cf971a84381 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Mon, 21 May 2018 04:35:43 -0400 Subject: [PATCH 14/22] Add undo block handler --- .../web/activity_pub/transmogrifier.ex | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 20b1603df..24e876efb 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -247,10 +247,10 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do "object" => %{"type" => "Follow", "object" => followed}, "actor" => follower, "id" => id - } = data + } = _data ) do - with %User{local: true} = followed = User.get_cached_by_ap_id(followed), - %User{} = follower = User.get_or_fetch_by_ap_id(follower), + with %User{local: true} = followed <- User.get_cached_by_ap_id(followed), + %User{} = follower <- User.get_or_fetch_by_ap_id(follower), {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do User.unfollow(follower, followed) {:ok, activity} @@ -259,6 +259,23 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do end end + def handle_incoming( + %{ + "type" => "Undo", + "object" => %{"type" => "Block", "object" => blocked}, + "actor" => blocker, + "id" => id + } = _data + ) do + with %User{local: true} = blocked <- User.get_cached_by_ap_id(blocked), + %User{} = blocker <- User.get_or_fetch_by_ap_id(blocker), + {:ok, activity} <- ActivityPub.unblock(blocker, blocked, id, false) do + {:ok, activity} + else + e -> :error + end + end + def handle_incoming( %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = data ) do From f2d6c3a512d86ac632787c7cd98bc5147e74e598 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Mon, 21 May 2018 04:36:20 -0400 Subject: [PATCH 15/22] Handle unfollows from OStatus --- .../web/ostatus/activity_representer.ex | 52 +++++++++---------- .../web/ostatus/handlers/unfollow_handler.ex | 17 ++++++ lib/pleroma/web/ostatus/ostatus.ex | 5 +- 3 files changed, 45 insertions(+), 29 deletions(-) create mode 100644 lib/pleroma/web/ostatus/handlers/unfollow_handler.ex diff --git a/lib/pleroma/web/ostatus/activity_representer.ex b/lib/pleroma/web/ostatus/activity_representer.ex index 64cadba1b..a9c2b89b4 100644 --- a/lib/pleroma/web/ostatus/activity_representer.ex +++ b/lib/pleroma/web/ostatus/activity_representer.ex @@ -232,7 +232,12 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do end # Only undos of follow for now. Will need to get redone once there are more - def to_simple_form(%{data: %{"type" => "Undo"}} = activity, user, with_author) do + def to_simple_form( + %{data: %{"type" => "Undo", "object" => %{"type" => "Follow"} = follow_activity}} = + activity, + user, + with_author + ) do h = fn str -> [to_charlist(str)] end updated_at = activity.data["published"] @@ -240,34 +245,25 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: [] - follow_activity = - if is_map(activity.data["object"]) do - Activity.get_by_ap_id(activity.data["object"]["id"]) - else - Activity.get_by_ap_id(activity.data["object"]) - end - mentions = (activity.recipients || []) |> get_mentions - - if follow_activity do - [ - {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']}, - {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']}, - {:id, h.(activity.data["id"])}, - {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']}, - {:content, [type: 'html'], - ['#{user.nickname} stopped following #{follow_activity.data["object"]}']}, - {:published, h.(inserted_at)}, - {:updated, h.(updated_at)}, - {:"activity:object", - [ - {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']}, - {:id, h.(follow_activity.data["object"])}, - {:uri, h.(follow_activity.data["object"])} - ]}, - {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []} - ] ++ mentions ++ author - end + follow_activity = Activity.get_by_ap_id(follow_activity["id"]) + [ + {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']}, + {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']}, + {:id, h.(activity.data["id"])}, + {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']}, + {:content, [type: 'html'], + ['#{user.nickname} stopped following #{follow_activity.data["object"]}']}, + {:published, h.(inserted_at)}, + {:updated, h.(updated_at)}, + {:"activity:object", + [ + {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']}, + {:id, h.(follow_activity.data["object"])}, + {:uri, h.(follow_activity.data["object"])} + ]}, + {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []} + ] ++ mentions ++ author end def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do diff --git a/lib/pleroma/web/ostatus/handlers/unfollow_handler.ex b/lib/pleroma/web/ostatus/handlers/unfollow_handler.ex new file mode 100644 index 000000000..a115bf4c8 --- /dev/null +++ b/lib/pleroma/web/ostatus/handlers/unfollow_handler.ex @@ -0,0 +1,17 @@ +defmodule Pleroma.Web.OStatus.UnfollowHandler do + alias Pleroma.Web.{XML, OStatus} + alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.User + + def handle(entry, doc) do + with {:ok, actor} <- OStatus.find_make_or_update_user(doc), + id when not is_nil(id) <- XML.string_from_xpath("/entry/id", entry), + followed_uri when not is_nil(followed_uri) <- + XML.string_from_xpath("/entry/activity:object/id", entry), + {:ok, followed} <- OStatus.find_or_make_user(followed_uri), + {:ok, activity} <- ActivityPub.unfollow(actor, followed, id, false) do + User.unfollow(actor, followed) + {:ok, activity} + end + end +end diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index 5c4a1fd69..f0ff0624f 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -8,7 +8,7 @@ defmodule Pleroma.Web.OStatus do alias Pleroma.{Repo, User, Web, Object, Activity} alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.{WebFinger, Websub} - alias Pleroma.Web.OStatus.{FollowHandler, NoteHandler, DeleteHandler} + alias Pleroma.Web.OStatus.{FollowHandler, UnfollowHandler, NoteHandler, DeleteHandler} alias Pleroma.Web.ActivityPub.Transmogrifier def feed_path(user) do @@ -47,6 +47,9 @@ defmodule Pleroma.Web.OStatus do 'http://activitystrea.ms/schema/1.0/follow' -> with {:ok, activity} <- FollowHandler.handle(entry, doc), do: activity + 'http://activitystrea.ms/schema/1.0/unfollow' -> + with {:ok, activity} <- UnfollowHandler.handle(entry, doc), do: activity + 'http://activitystrea.ms/schema/1.0/share' -> with {:ok, activity, retweeted_activity} <- handle_share(entry, doc), do: [activity, retweeted_activity] From cc5373b846d7f816a3e78af342468f2ea8415b8d Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Mon, 21 May 2018 05:00:58 -0400 Subject: [PATCH 16/22] Fix unblock handling --- lib/pleroma/web/activity_pub/transmogrifier.ex | 1 + test/fixtures/mastodon-unblock-activity.json | 5 +++-- test/web/activity_pub/transmogrifier_test.exs | 8 ++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 24e876efb..b941fdf16 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -270,6 +270,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do with %User{local: true} = blocked <- User.get_cached_by_ap_id(blocked), %User{} = blocker <- User.get_or_fetch_by_ap_id(blocker), {:ok, activity} <- ActivityPub.unblock(blocker, blocked, id, false) do + User.unblock(blocker, blocked) {:ok, activity} else e -> :error diff --git a/test/fixtures/mastodon-unblock-activity.json b/test/fixtures/mastodon-unblock-activity.json index 53b2f42a8..f4a2d30dd 100644 --- a/test/fixtures/mastodon-unblock-activity.json +++ b/test/fixtures/mastodon-unblock-activity.json @@ -26,8 +26,9 @@ "type": "Block", "object": "http://localtesting.pleroma.lol/users/lain", "nickname": "lain", - "id": "http://mastodon.example.org/users/admin#follows/2", + "id": "http://mastodon.example.org/users/admin#blocks/2", "actor": "http://mastodon.example.org/users/admin" }, - "actor": "http://mastodon.example.org/users/admin" + "actor": "http://mastodon.example.org/users/admin", + "id": "http://mastodon.example.org/users/admin#blocks/2/undo" } diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index da55909bc..cf6b1d0b5 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -316,7 +316,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity" end - test "it works for incomming unfollows" do + test "it works for incomming unfollows with an existing follow" do user = insert(:user) follow_data = @@ -360,7 +360,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do assert User.blocks?(blocker, user) end - test "it works for incoming unblocks" do + test "it works for incoming unblocks with an existing block" do user = insert(:user) block_data = @@ -379,11 +379,11 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do assert data["type"] == "Undo" assert data["object"]["type"] == "Block" assert data["object"]["object"] == user.ap_id - assert data["actor"] == "https://mastodon.example.org/users/admin" + assert data["actor"] == "http://mastodon.example.org/users/admin" blocker = User.get_by_ap_id(data["actor"]) - refute User.blocks?(blocker, user.ap_id) + refute User.blocks?(blocker, user) end end From d045b96f957e0698ecd0110ca240f0527e3fea43 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Mon, 21 May 2018 05:11:13 -0400 Subject: [PATCH 17/22] Add tests for incoming unfollow over OStatus --- .../web/ostatus/activity_representer.ex | 1 + test/fixtures/unfollow.xml | 68 +++++++++++++++++++ test/web/ostatus/ostatus_test.exs | 24 +++++++ 3 files changed, 93 insertions(+) create mode 100644 test/fixtures/unfollow.xml diff --git a/lib/pleroma/web/ostatus/activity_representer.ex b/lib/pleroma/web/ostatus/activity_representer.ex index a9c2b89b4..4179d86c9 100644 --- a/lib/pleroma/web/ostatus/activity_representer.ex +++ b/lib/pleroma/web/ostatus/activity_representer.ex @@ -247,6 +247,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do mentions = (activity.recipients || []) |> get_mentions follow_activity = Activity.get_by_ap_id(follow_activity["id"]) + [ {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']}, {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']}, diff --git a/test/fixtures/unfollow.xml b/test/fixtures/unfollow.xml new file mode 100644 index 000000000..7a8f8fd2e --- /dev/null +++ b/test/fixtures/unfollow.xml @@ -0,0 +1,68 @@ + + + GNU social + https://social.heldscal.la/api/statuses/user_timeline/23211.atom + lambadalambda timeline + Updates from lambadalambda on social.heldscal.la! + https://social.heldscal.la/avatar/23211-96-20170416114255.jpeg + 2017-05-07T09:54:49+00:00 + + http://activitystrea.ms/schema/1.0/person + https://social.heldscal.la/user/23211 + lambadalambda + Call me Deacon Blues. + + + + + + lambadalambda + Constance Variable + Call me Deacon Blues. + + Berlin + + + homepage + https://heldscal.la + true + + + + + + + + + + + + + undo:tag:social.heldscal.la,2017-05-07:subscription:23211:person:44803:2017-05-07T09:54:48+00:00 + Constance Variable (lambadalambda@social.heldscal.la)'s status on Sunday, 07-May-2017 09:54:49 UTC + <a href="https://social.heldscal.la/lambadalambda">Constance Variable</a> stopped following <a href="https://pawoo.net/@pekorino">mono</a>. + + http://activitystrea.ms/schema/1.0/unfollow + 2017-05-07T09:54:49+00:00 + 2017-05-07T09:54:49+00:00 + + http://activitystrea.ms/schema/1.0/person + https://pawoo.net/users/pekorino + mono + http://shitposter.club/mono 孤独のグルメ + + + + + pekorino + mono + http://shitposter.club/mono 孤独のグルメ + + + tag:social.heldscal.la,2017-05-07:objectType=thread:nonce=6e80caf94e03029f + + + + + + diff --git a/test/web/ostatus/ostatus_test.exs b/test/web/ostatus/ostatus_test.exs index 3e7cf0155..f095e41dd 100644 --- a/test/web/ostatus/ostatus_test.exs +++ b/test/web/ostatus/ostatus_test.exs @@ -278,6 +278,30 @@ defmodule Pleroma.Web.OStatusTest do assert User.following?(follower, followed) end + test "handle incoming unfollows with existing follow" do + incoming_follow = File.read!("test/fixtures/follow.xml") + {:ok, [_activity]} = OStatus.handle_incoming(incoming_follow) + + incoming = File.read!("test/fixtures/unfollow.xml") + {:ok, [activity]} = OStatus.handle_incoming(incoming) + + assert activity.data["type"] == "Undo" + + assert activity.data["id"] == + "undo:tag:social.heldscal.la,2017-05-07:subscription:23211:person:44803:2017-05-07T09:54:48+00:00" + + assert activity.data["actor"] == "https://social.heldscal.la/user/23211" + assert is_map(activity.data["object"]) + assert activity.data["object"]["type"] == "Follow" + assert activity.data["object"]["object"] == "https://pawoo.net/users/pekorino" + refute activity.local + + follower = User.get_by_ap_id(activity.data["actor"]) + followed = User.get_by_ap_id(activity.data["object"]["object"]) + + refute User.following?(follower, followed) + end + describe "new remote user creation" do test "returns local users" do local_user = insert(:user) From 46427cb90f31d72435b10134a85fad674e10f1cd Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Mon, 21 May 2018 11:57:15 -0400 Subject: [PATCH 18/22] Pass correct number of arguments to unfollow --- lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index c2d540db9..8485a8009 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -224,7 +224,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do follow_activity = fetch_latest_follow(blocker, blocked) if follow_activity do - unfollow(blocker, blocked, local) + unfollow(blocker, blocked, nil, local) end with block_data <- make_block_data(blocker, blocked, activity_id), From b2c6ae7d820fb9d6bf81c6912e3e9b10a6fa7dd2 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Tue, 22 May 2018 04:27:40 -0400 Subject: [PATCH 19/22] Hook up unfollow and (un)block to MastoAPI + tests --- .../mastodon_api/mastodon_api_controller.ex | 17 ++++------ test/web/activity_pub/activity_pub_test.exs | 33 +++++++++++++++++++ 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 85f9c5b7b..b21f5de20 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -457,24 +457,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end - # TODO: Clean up and unify def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do with %User{} = followed <- Repo.get(User, id), - {:ok, follower, follow_activity} <- User.unfollow(follower, followed), - {:ok, _activity} <- - ActivityPub.insert(%{ - "type" => "Undo", - "actor" => follower.ap_id, - # get latest Follow for these users - "object" => follow_activity.data["id"] - }) do + {:ok, _activity} <- ActivityPub.unfollow(follower, followed), + {:ok, follower, _} <- User.unfollow(follower, followed) do render(conn, AccountView, "relationship.json", %{user: follower, target: followed}) end end def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do with %User{} = blocked <- Repo.get(User, id), - {:ok, blocker} <- User.block(blocker, blocked) do + {:ok, blocker} <- User.block(blocker, blocked), + {:ok, _activity} <- ActivityPub.block(blocker, blocked) do render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked}) else {:error, message} -> @@ -486,7 +480,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do with %User{} = blocked <- Repo.get(User, id), - {:ok, blocker} <- User.unblock(blocker, blocked) do + {:ok, blocker} <- User.unblock(blocker, blocked), + {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked}) else {:error, message} -> diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 35f0deffe..081c202b1 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -425,10 +425,43 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert activity.data["type"] == "Undo" assert activity.data["actor"] == follower.ap_id + + assert is_map(activity.data["object"]) + assert activity.data["object"]["type"] == "Follow" + assert activity.data["object"]["object"] == followed.ap_id assert activity.data["object"]["id"] == follow_activity.data["id"] end end + describe "blocking / unblocking" do + test "creates a block activity" do + blocker = insert(:user) + blocked = insert(:user) + + {:ok, activity} = ActivityPub.block(blocker, blocked) + + assert activity.data["type"] == "Block" + assert activity.data["actor"] == blocker.ap_id + assert activity.data["object"] == blocked.ap_id + end + + test "creates an undo activity for the last block" do + blocker = insert(:user) + blocked = insert(:user) + + {:ok, block_activity} = ActivityPub.block(blocker, blocked) + {:ok, activity} = ActivityPub.unblock(blocker, blocked) + + assert activity.data["type"] == "Undo" + assert activity.data["actor"] == blocker.ap_id + + assert is_map(activity.data["object"]) + assert activity.data["object"]["type"] == "Block" + assert activity.data["object"]["object"] == blocked.ap_id + assert activity.data["object"]["id"] == block_activity.data["id"] + end + end + describe "deletion" do test "it creates a delete activity and deletes the original object" do note = insert(:note_activity) From 55f9aefd08dd922013fcd55c02552ddbb89bcda3 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Tue, 22 May 2018 04:49:30 -0400 Subject: [PATCH 20/22] Hook unfollow into TwitterAPI --- lib/pleroma/web/twitter_api/twitter_api.ex | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 722e436e2..5eeebe161 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -36,14 +36,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do def unfollow(%User{} = follower, params) do with {:ok, %User{} = unfollowed} <- get_user(params), {:ok, follower, follow_activity} <- User.unfollow(follower, unfollowed), - {:ok, _activity} <- - ActivityPub.insert(%{ - "type" => "Undo", - "actor" => follower.ap_id, - # get latest Follow for these users - "object" => follow_activity.data["id"], - "published" => make_date() - }) do + {:ok, _activity} <- ActivityPub.unfollow(follower, unfollowed) do {:ok, follower, unfollowed} else err -> err From 72b93d13f8208f22f9a771dc8ba18c474fc81849 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Tue, 22 May 2018 05:41:17 -0400 Subject: [PATCH 21/22] Hook up block/unblock to TwitterAPI --- lib/pleroma/web/twitter_api/twitter_api.ex | 6 ++++-- test/web/twitter_api/twitter_api_controller_test.exs | 2 +- test/web/twitter_api/twitter_api_test.exs | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 5eeebe161..3ccdaed6f 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -45,7 +45,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do def block(%User{} = blocker, params) do with {:ok, %User{} = blocked} <- get_user(params), - {:ok, blocker} <- User.block(blocker, blocked) do + {:ok, blocker} <- User.block(blocker, blocked), + {:ok, _activity} <- ActivityPub.block(blocker, blocked) do {:ok, blocker, blocked} else err -> err @@ -54,7 +55,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do def unblock(%User{} = blocker, params) do with {:ok, %User{} = blocked} <- get_user(params), - {:ok, blocker} <- User.unblock(blocker, blocked) do + {:ok, blocker} <- User.unblock(blocker, blocked), + {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do {:ok, blocker, blocked} else err -> err diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 02aba0bc8..625303b84 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -443,7 +443,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do test "with credentials", %{conn: conn, user: current_user} do blocked = insert(:user) - {:ok, current_user} = User.block(current_user, blocked) + {:ok, current_user, blocked} = TwitterAPI.block(current_user, %{"user_id" => blocked.id}) assert User.blocks?(current_user, blocked) conn = diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index af565c0e5..4716abb84 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -166,7 +166,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do test "Unblock another user using user_id" do unblocked = insert(:user) user = insert(:user) - User.block(user, unblocked) + {:ok, user, _unblocked} = TwitterAPI.block(user, %{"user_id" => unblocked.id}) {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"user_id" => unblocked.id}) assert user.info["blocks"] == [] @@ -175,7 +175,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do test "Unblock another user using screen_name" do unblocked = insert(:user) user = insert(:user) - User.block(user, unblocked) + {:ok, user, _unblocked} = TwitterAPI.block(user, %{"screen_name" => unblocked.nickname}) {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"screen_name" => unblocked.nickname}) assert user.info["blocks"] == [] From 1197ec10a83c2e61251abe383bea2bcc690d9c09 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Thu, 24 May 2018 13:26:59 -0400 Subject: [PATCH 22/22] Rewrite block fetch query --- lib/pleroma/web/activity_pub/utils.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 3f88a4672..89d11781d 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -366,8 +366,9 @@ defmodule Pleroma.Web.ActivityPub.Utils do fragment( "? @> ?", activity.data, - ^%{type: "Block", actor: blocker_id, object: blocked_id} + ^%{type: "Block", object: blocked_id} ), + where: activity.actor == ^blocker_id, order_by: [desc: :id], limit: 1 )