Don't return activities from blocked users.
authorRoger Braun <roger@rogerbraun.net>
Thu, 2 Nov 2017 21:37:26 +0000 (22:37 +0100)
committerRoger Braun <roger@rogerbraun.net>
Thu, 2 Nov 2017 21:37:26 +0000 (22:37 +0100)
lib/pleroma/web/activity_pub/activity_pub.ex
test/web/activity_pub/activity_pub_test.exs

index 71e52cb4628fb40e8bf1e1e33abc2b42801d673c..75b59f375489b07425670aa42db421d86c16fc75 100644 (file)
@@ -163,6 +163,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
       where: activity.id > ^since
   end
 
+  defp restrict_blocked(query, %{"blocking_user" => user}) do
+    blocks = user.info["blocks"] || []
+    from activity in query,
+      where: fragment("not (?->>'actor' = ANY(?))", activity.data, ^blocks)
+  end
+  defp restrict_blocked(query, _), do: query
+
   def fetch_activities(recipients, opts \\ %{}) do
     base_query = from activity in Activity,
       limit: 20,
@@ -178,6 +185,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     |> restrict_type(opts)
     |> restrict_favorited_by(opts)
     |> restrict_recent(opts)
+    |> restrict_blocked(opts)
     |> Repo.all
     |> Enum.reverse
   end
index a088e97be36318c3bd30fccd5f0e6f88645bfe3c..4c73c0b8af4f09bcadd6349ba787082f34e54dd0 100644 (file)
@@ -80,6 +80,25 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
   end
 
+  test "doesn't return blocked activities" do
+    activity_one = insert(:note_activity)
+    activity_two = insert(:note_activity)
+    user = insert(:user)
+    {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
+
+    activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
+
+    assert Enum.member?(activities, activity_two)
+    refute Enum.member?(activities, activity_one)
+
+    {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
+
+    activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
+
+    assert Enum.member?(activities, activity_two)
+    assert Enum.member?(activities, activity_one)
+  end
+
   describe "public fetch activities" do
     test "retrieves public activities" do
       %{public: public} = ActivityBuilder.public_and_non_public