From 13ff312c48c338c93ee9aa85ec2e48c2059d5168 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Mon, 8 Apr 2019 12:46:12 +0100 Subject: [PATCH] Sever subscription in case of block --- lib/pleroma/user.ex | 31 ++++++++++++++++++++++++------- test/user_test.exs | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index f1565ade7..9a80601c1 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -924,14 +924,23 @@ def unmute(muter, %{ap_id: ap_id}) do end def subscribe(subscriber, %{ap_id: ap_id}) do - with %User{} = user <- get_or_fetch_by_ap_id(ap_id) do - info_cng = - user.info - |> User.Info.add_to_subscribers(subscriber.ap_id) + user_config = Application.get_env(:pleroma, :user) + deny_follow_blocked = Keyword.get(user_config, :deny_follow_blocked) - change(user) - |> put_embed(:info, info_cng) - |> update_and_set_cache() + with %User{} = subscribed <- get_or_fetch_by_ap_id(ap_id) do + blocked = blocks?(subscribed, subscriber) and deny_follow_blocked + + if blocked do + {:error, "Could not subscribe: #{subscribed.nickname} is blocking you"} + else + info_cng = + subscribed.info + |> User.Info.add_to_subscribers(subscriber.ap_id) + + change(subscribed) + |> put_embed(:info, info_cng) + |> update_and_set_cache() + end end end @@ -957,6 +966,14 @@ def block(blocker, %User{ap_id: ap_id} = blocked) do blocker end + blocker = + if subscribed_to?(blocked, blocker) do + {:ok, blocker} = unsubscribe(blocked, blocker) + blocker + else + blocker + end + if following?(blocked, blocker) do unfollow(blocked, blocker) end diff --git a/test/user_test.exs b/test/user_test.exs index 38712cebb..2368f95f5 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -146,6 +146,15 @@ test "can't follow a user who blocked us" do {:error, _} = User.follow(blockee, blocker) end + test "can't subscribe to a user who blocked us" do + blocker = insert(:user) + blocked = insert(:user) + + {:ok, blocker} = User.block(blocker, blocked) + + {:error, _} = User.subscribe(blocked, blocker) + end + test "local users do not automatically follow local locked accounts" do follower = insert(:user, info: %{locked: true}) followed = insert(:user, info: %{locked: true}) @@ -729,6 +738,22 @@ test "blocks tear down blocked->blocker follow relationships" do refute User.following?(blocker, blocked) refute User.following?(blocked, blocker) end + + test "blocks tear down blocked->blocker subscription relationships" do + blocker = insert(:user) + blocked = insert(:user) + + {:ok, blocker} = User.subscribe(blocked, blocker) + + assert User.subscribed_to?(blocked, blocker) + refute User.subscribed_to?(blocker, blocked) + + {:ok, blocker} = User.block(blocker, blocked) + + assert User.blocks?(blocker, blocked) + refute User.subscribed_to?(blocker, blocked) + refute User.subscribed_to?(blocked, blocker) + end end describe "domain blocking" do