ActivityPub: Fix non-federating blocks.
authorlain <lain@soykaf.club>
Tue, 12 May 2020 08:52:46 +0000 (10:52 +0200)
committerlain <lain@soykaf.club>
Tue, 12 May 2020 08:52:46 +0000 (10:52 +0200)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/activity_pub/utils.ex
test/web/activity_pub/activity_pub_test.exs

index 4955243ab3ac884606b8e0e17f9c5a572e2066f0..d752f4f04a800abe57893038ccc209a681d52ca6 100644 (file)
@@ -439,7 +439,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   end
 
   defp do_block(blocker, blocked, activity_id, local) do
-    outgoing_blocks = Config.get([:activitypub, :outgoing_blocks])
     unfollow_blocked = Config.get([:activitypub, :unfollow_blocked])
 
     if unfollow_blocked do
@@ -447,8 +446,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
       if follow_activity, do: unfollow(blocker, blocked, nil, local)
     end
 
-    with true <- outgoing_blocks,
-         block_data <- make_block_data(blocker, blocked, activity_id),
+    with block_data <- make_block_data(blocker, blocked, activity_id),
          {:ok, activity} <- insert(block_data, local),
          _ <- notify_and_stream(activity),
          :ok <- maybe_federate(activity) do
index 09b80fa576520823ed75cd84618675622a2cced0..f2375bcc4f7d1119cf923361b58fd920bfdab4f7 100644 (file)
@@ -6,6 +6,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
   alias Ecto.Changeset
   alias Ecto.UUID
   alias Pleroma.Activity
+  alias Pleroma.Config
   alias Pleroma.Notification
   alias Pleroma.Object
   alias Pleroma.Repo
@@ -169,8 +170,11 @@ defmodule Pleroma.Web.ActivityPub.Utils do
   Enqueues an activity for federation if it's local
   """
   @spec maybe_federate(any()) :: :ok
-  def maybe_federate(%Activity{local: true} = activity) do
-    if Pleroma.Config.get!([:instance, :federating]) do
+  def maybe_federate(%Activity{local: true, data: %{"type" => type}} = activity) do
+    outgoing_blocks = Config.get([:activitypub, :outgoing_blocks])
+
+    with true <- Config.get!([:instance, :federating]),
+         true <- type != "Block" || outgoing_blocks do
       Pleroma.Web.Federator.publish(activity)
     end
 
index 0739cbfef025421e902fbbd8fcf429036ecab33d..59bdd53cd3a5e67a52012fe1940e0efbf661c725 100644 (file)
@@ -1061,14 +1061,38 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
 
     test "creates a block activity" do
+      clear_config([:instance, :federating], true)
       blocker = insert(:user)
       blocked = insert(:user)
 
-      {:ok, activity} = ActivityPub.block(blocker, blocked)
+      with_mock Pleroma.Web.Federator,
+        publish: fn _ -> nil end do
+        {: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
+        assert activity.data["type"] == "Block"
+        assert activity.data["actor"] == blocker.ap_id
+        assert activity.data["object"] == blocked.ap_id
+
+        assert called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "works with outgoing blocks disabled, but doesn't federate" do
+      clear_config([:instance, :federating], true)
+      clear_config([:activitypub, :outgoing_blocks], false)
+      blocker = insert(:user)
+      blocked = insert(:user)
+
+      with_mock Pleroma.Web.Federator,
+        publish: fn _ -> nil end do
+        {: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
+
+        refute called(Pleroma.Web.Federator.publish(:_))
+      end
     end
   end