Sever subscription in case of block
authorSadposter <hannah+pleroma@coffee-and-dreams.uk>
Mon, 8 Apr 2019 11:46:12 +0000 (12:46 +0100)
committerSadposter <hannah+pleroma@coffee-and-dreams.uk>
Mon, 8 Apr 2019 11:46:12 +0000 (12:46 +0100)
lib/pleroma/user.ex
test/user_test.exs

index f1565ade7b94fe9550620c505fa296c3ec449eee..9a80601c1a5fbdab37257ae3cf7635912662c3ca 100644 (file)
@@ -924,14 +924,23 @@ defmodule Pleroma.User 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 @@ defmodule Pleroma.User 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
index 38712cebb6aafb1edf7f4639806b4a52a5a87d97..2368f95f5490cd54b0edf20e45052a9e81923fe6 100644 (file)
@@ -146,6 +146,15 @@ defmodule Pleroma.UserTest 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 @@ defmodule Pleroma.UserTest 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