From 48380aecccc74b0a376ed1efb55a269b35516566 Mon Sep 17 00:00:00 2001
From: eal <eal@waifu.club>
Date: Sat, 14 Apr 2018 14:26:20 +0300
Subject: [PATCH] ActivityPub.ex: do not return boosted statuses from blocked
 users.

---
 lib/pleroma/web/activity_pub/activity_pub.ex |  5 ++++-
 test/web/activity_pub/activity_pub_test.exs  | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
index 04b50c1cc..37fbf3d6d 100644
--- a/lib/pleroma/web/activity_pub/activity_pub.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -307,7 +307,10 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
 
   defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
     blocks = info["blocks"] || []
-    from(activity in query, where: fragment("not (? = ANY(?))", activity.actor, ^blocks))
+    from(activity in query,
+      where: fragment("not (? = ANY(?))", activity.actor, ^blocks),
+      where: fragment("not (?->'to' \\?| ?)", activity.data, ^blocks)
+    )
   end
 
   defp restrict_blocked(query, _), do: query
diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs
index 657d75a55..e3f78d002 100644
--- a/test/web/activity_pub/activity_pub_test.exs
+++ b/test/web/activity_pub/activity_pub_test.exs
@@ -2,6 +2,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
   use Pleroma.DataCase
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.ActivityPub.Utils
+  alias Pleroma.Web.CommonAPI
   alias Pleroma.{Activity, Object, User}
   alias Pleroma.Builders.ActivityBuilder
 
@@ -128,12 +129,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
   test "doesn't return blocked activities" do
     activity_one = insert(:note_activity)
     activity_two = insert(:note_activity)
+    activity_three = insert(:note_activity)
     user = insert(:user)
+    booster = 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)
+    assert Enum.member?(activities, activity_three)
     refute Enum.member?(activities, activity_one)
 
     {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
@@ -141,11 +145,26 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
 
     assert Enum.member?(activities, activity_two)
+    assert Enum.member?(activities, activity_three)
+    assert Enum.member?(activities, activity_one)
+
+    {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
+    {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
+    %Activity{} = boost_activity = Activity.get_create_activity_by_object_ap_id(id)
+    activity_three = Repo.get(Activity, activity_three.id)
+
+    activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
+
+    assert Enum.member?(activities, activity_two)
+    refute Enum.member?(activities, activity_three)
+    refute Enum.member?(activities, boost_activity)
     assert Enum.member?(activities, activity_one)
 
     activities = ActivityPub.fetch_activities([], %{"blocking_user" => nil})
 
     assert Enum.member?(activities, activity_two)
+    assert Enum.member?(activities, activity_three)
+    assert Enum.member?(activities, boost_activity)
     assert Enum.member?(activities, activity_one)
   end