From fa97eddf8a7e5c3a0ed51eff562d6592bd478b95 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Thu, 28 Nov 2019 10:38:38 +0000 Subject: [PATCH 1/7] make follows take precedence over domain blocks --- lib/pleroma/user.ex | 2 +- test/user_test.exs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index b18a4c6a5..6a97e1928 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1020,7 +1020,7 @@ def muted_notifications?(user, %{ap_id: ap_id}), do: Enum.member?(user.muted_notifications, ap_id) def blocks?(%User{} = user, %User{} = target) do - blocks_ap_id?(user, target) || blocks_domain?(user, target) + blocks_ap_id?(user, target) || (!User.following?(user, target) && blocks_domain?(user, target)) end def blocks?(nil, _), do: false diff --git a/test/user_test.exs b/test/user_test.exs index 82e338e75..6a3820455 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -879,6 +879,16 @@ test "unblocks domains" do refute User.blocks?(user, collateral_user) end + + test "follows take precedence over domain blocks" do + user = insert(:user) + good_eggo = insert(:user, %{ap_id: "https://meanies.social/user/cuteposter"}) + + {:ok, user} = User.block_domain(user, "meanies.social") + {:ok, user} = User.follow(user, good_eggo) + + refute User.blocks?(user, good_eggo) + end end describe "blocks_import" do From c7cc80a9ee00f7bf9e307a09c5f2cc85fedd67d5 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Thu, 28 Nov 2019 10:40:50 +0000 Subject: [PATCH 2/7] obligatory format commit --- lib/pleroma/user.ex | 3 ++- test/user_test.exs | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 6a97e1928..63c5b4102 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1020,7 +1020,8 @@ def muted_notifications?(user, %{ap_id: ap_id}), do: Enum.member?(user.muted_notifications, ap_id) def blocks?(%User{} = user, %User{} = target) do - blocks_ap_id?(user, target) || (!User.following?(user, target) && blocks_domain?(user, target)) + blocks_ap_id?(user, target) || + (!User.following?(user, target) && blocks_domain?(user, target)) end def blocks?(nil, _), do: false diff --git a/test/user_test.exs b/test/user_test.exs index 6a3820455..6ae563258 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -881,13 +881,13 @@ test "unblocks domains" do end test "follows take precedence over domain blocks" do - user = insert(:user) - good_eggo = insert(:user, %{ap_id: "https://meanies.social/user/cuteposter"}) - - {:ok, user} = User.block_domain(user, "meanies.social") - {:ok, user} = User.follow(user, good_eggo) + user = insert(:user) + good_eggo = insert(:user, %{ap_id: "https://meanies.social/user/cuteposter"}) - refute User.blocks?(user, good_eggo) + {:ok, user} = User.block_domain(user, "meanies.social") + {:ok, user} = User.follow(user, good_eggo) + + refute User.blocks?(user, good_eggo) end end From 19d228cc586a1304ef6e982a447a77f8c3a48772 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Tue, 10 Dec 2019 16:40:12 +0000 Subject: [PATCH 3/7] modify SQL to include followed-but-domain-blocked activities --- lib/pleroma/user.ex | 7 +++++ lib/pleroma/web/activity_pub/activity_pub.ex | 7 +++-- test/web/activity_pub/activity_pub_test.exs | 32 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 63c5b4102..601aa9cf0 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -762,6 +762,13 @@ def get_friends(user, page \\ nil) do |> Repo.all() end + def get_friends_ap_ids(user) do + user + |> get_friends_query(nil) + |> select([u], u.ap_id) + |> Repo.all() + end + def get_friends_ids(user, page \\ nil) do user |> get_friends_query(page) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index f25314ff6..3c4aed241 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -915,6 +915,9 @@ defp restrict_muted(query, _), do: query defp restrict_blocked(query, %{"blocking_user" => %User{} = user}) do blocks = user.blocks || [] domain_blocks = user.domain_blocks || [] + following_ap_ids = + user + |> User.get_friends_ap_ids() query = if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query) @@ -930,8 +933,8 @@ defp restrict_blocked(query, %{"blocking_user" => %User{} = user}) do activity.data, ^blocks ), - where: fragment("not (split_part(?, '/', 3) = ANY(?))", activity.actor, ^domain_blocks), - where: fragment("not (split_part(?->>'actor', '/', 3) = ANY(?))", o.data, ^domain_blocks) + where: fragment("(not (split_part(?, '/', 3) = ANY(?))) or ? = ANY(?)", activity.actor, ^domain_blocks, activity.actor, ^following_ap_ids), + where: fragment("(not (split_part(?->>'actor', '/', 3) = ANY(?))) or (?->>'actor') = ANY(?)", o.data, ^domain_blocks, o.data, ^following_ap_ids) ) end diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index d437ad456..503bbf0db 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -605,6 +605,38 @@ test "doesn't return activities from blocked domains" do refute repeat_activity in activities end + test "does return activities from followed users on blocked domains" do + domain = "meanies.social" + domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"}) + blocker = insert(:user) + + {:ok, blocker} = User.follow(blocker, domain_user) + {:ok, blocker} = User.block_domain(blocker, domain) + + assert User.following?(blocker, domain_user) + assert User.blocks_domain?(blocker, domain_user) + refute User.blocks?(blocker, domain_user) + + note = insert(:note, %{data: %{"actor" => domain_user.ap_id}}) + activity = insert(:note_activity, %{note: note}) + + activities = + ActivityPub.fetch_activities([], %{"blocking_user" => blocker, "skip_preload" => true}) + + assert activity in activities + + # And check that if the guy we DO follow boosts someone else from their domain, that should be hidden + another_user = insert(:user, %{ap_id: "https://#{domain}/@meanie2"}) + bad_note = insert(:note, %{data: %{"actor" => another_user.ap_id}}) + bad_activity = insert(:note_activity, %{note: bad_note}) + {:ok, repeat_activity, _} = CommonAPI.repeat(bad_activity.id, domain_user) + + activities = + ActivityPub.fetch_activities([], %{"blocking_user" => blocker, "skip_preload" => true}) + + refute repeat_activity in activities + end + test "doesn't return muted activities" do activity_one = insert(:note_activity) activity_two = insert(:note_activity) From 25f774f7c19a955398976b3f4462cbd51ec700d6 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Tue, 10 Dec 2019 16:41:43 +0000 Subject: [PATCH 4/7] format activity_pub.ex --- lib/pleroma/web/activity_pub/activity_pub.ex | 23 ++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 3c4aed241..29cc1a17b 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -915,9 +915,10 @@ defp restrict_muted(query, _), do: query defp restrict_blocked(query, %{"blocking_user" => %User{} = user}) do blocks = user.blocks || [] domain_blocks = user.domain_blocks || [] + following_ap_ids = - user - |> User.get_friends_ap_ids() + user + |> User.get_friends_ap_ids() query = if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query) @@ -933,8 +934,22 @@ defp restrict_blocked(query, %{"blocking_user" => %User{} = user}) do activity.data, ^blocks ), - where: fragment("(not (split_part(?, '/', 3) = ANY(?))) or ? = ANY(?)", activity.actor, ^domain_blocks, activity.actor, ^following_ap_ids), - where: fragment("(not (split_part(?->>'actor', '/', 3) = ANY(?))) or (?->>'actor') = ANY(?)", o.data, ^domain_blocks, o.data, ^following_ap_ids) + where: + fragment( + "(not (split_part(?, '/', 3) = ANY(?))) or ? = ANY(?)", + activity.actor, + ^domain_blocks, + activity.actor, + ^following_ap_ids + ), + where: + fragment( + "(not (split_part(?->>'actor', '/', 3) = ANY(?))) or (?->>'actor') = ANY(?)", + o.data, + ^domain_blocks, + o.data, + ^following_ap_ids + ) ) end From 5abee19b63aa1577f8895b48ae69f0a51edb4dc3 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Tue, 10 Dec 2019 16:50:58 +0000 Subject: [PATCH 5/7] make analysis happy --- test/web/activity_pub/activity_pub_test.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 503bbf0db..8b45a186b 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -625,7 +625,8 @@ test "does return activities from followed users on blocked domains" do assert activity in activities - # And check that if the guy we DO follow boosts someone else from their domain, that should be hidden + # And check that if the guy we DO follow boosts someone else from their domain, + # that should be hidden another_user = insert(:user, %{ap_id: "https://#{domain}/@meanie2"}) bad_note = insert(:note, %{data: %{"actor" => another_user.ap_id}}) bad_activity = insert(:note_activity, %{note: bad_note}) From 457821adb07d899fc5094de46027b482e07daf9f Mon Sep 17 00:00:00 2001 From: Sadposter Date: Tue, 10 Dec 2019 18:49:41 +0000 Subject: [PATCH 6/7] Apply suggestion to lib/pleroma/user.ex --- lib/pleroma/user.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index cebadc8ab..706aee2ff 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1143,7 +1143,7 @@ def muted_notifications?(%User{} = user, %User{} = target), def blocks?(nil, _), do: false def blocks?(%User{} = user, %User{} = target) do - blocks_ap_id?(user, target) || + blocks_user?(user, target) || (!User.following?(user, target) && blocks_domain?(user, target)) end From 1ad96d667ae2f8a14a0b7c701a99c7fa72985a00 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Wed, 11 Dec 2019 09:08:20 +0000 Subject: [PATCH 7/7] remove single-step pipe --- lib/pleroma/web/activity_pub/activity_pub.ex | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index c6744e5f2..2bb3ad635 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -950,9 +950,7 @@ defp restrict_blocked(query, %{"blocking_user" => %User{} = user} = opts) do blocked_ap_ids = opts["blocked_users_ap_ids"] || User.blocked_users_ap_ids(user) domain_blocks = user.domain_blocks || [] - following_ap_ids = - user - |> User.get_friends_ap_ids() + following_ap_ids = User.get_friends_ap_ids(user) query = if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query)