Blocking: Don't federate if the options is set.
authorlain <lain@soykaf.club>
Thu, 25 Jun 2020 10:03:14 +0000 (12:03 +0200)
committerlain <lain@soykaf.club>
Thu, 25 Jun 2020 10:03:14 +0000 (12:03 +0200)
lib/pleroma/web/activity_pub/object_validator.ex
test/web/common_api/common_api_test.exs

index 737c0fd64a8f216924150631b2d14a1b1a7ef4d3..bb6324460dcf08078d701942fdc1b7cbdff0355b 100644 (file)
@@ -31,6 +31,15 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
            |> BlockValidator.cast_and_validate()
            |> Ecto.Changeset.apply_action(:insert) do
       block_activity = stringify_keys(block_activity)
+      outgoing_blocks = Pleroma.Config.get([:activitypub, :outgoing_blocks])
+
+      meta =
+        if !outgoing_blocks do
+          Keyword.put(meta, :do_not_federate, true)
+        else
+          meta
+        end
+
       {:ok, block_activity, meta}
     end
   end
index 6bd26050ef003b04cf763f1f6e599de6b900942e..fc3bb845d8755bbdc9bbdd7a1154442aa0a6aa17 100644 (file)
@@ -25,6 +25,52 @@ defmodule Pleroma.Web.CommonAPITest do
   setup do: clear_config([:instance, :limit])
   setup do: clear_config([:instance, :max_pinned_statuses])
 
+  describe "blocking" do
+    setup do
+      blocker = insert(:user)
+      blocked = insert(:user)
+      User.follow(blocker, blocked)
+      User.follow(blocked, blocker)
+      %{blocker: blocker, blocked: blocked}
+    end
+
+    test "it blocks and federates", %{blocker: blocker, blocked: blocked} do
+      clear_config([:instance, :federating], true)
+
+      with_mock Pleroma.Web.Federator,
+        publish: fn _ -> nil end do
+        assert {:ok, block} = CommonAPI.block(blocker, blocked)
+
+        assert block.local
+        assert User.blocks?(blocker, blocked)
+        refute User.following?(blocker, blocked)
+        refute User.following?(blocked, blocker)
+
+        assert called(Pleroma.Web.Federator.publish(block))
+      end
+    end
+
+    test "it blocks and does not federate if outgoing blocks are disabled", %{
+      blocker: blocker,
+      blocked: blocked
+    } do
+      clear_config([:instance, :federating], true)
+      clear_config([:activitypub, :outgoing_blocks], false)
+
+      with_mock Pleroma.Web.Federator,
+        publish: fn _ -> nil end do
+        assert {:ok, block} = CommonAPI.block(blocker, blocked)
+
+        assert block.local
+        assert User.blocks?(blocker, blocked)
+        refute User.following?(blocker, blocked)
+        refute User.following?(blocked, blocker)
+
+        refute called(Pleroma.Web.Federator.publish(block))
+      end
+    end
+  end
+
   describe "posting chat messages" do
     setup do: clear_config([:instance, :chat_limit])