[#1364] Disabled notifications on activities from blocked domains.
authorIvan Tashkinov <ivantashkinov@gmail.com>
Sat, 11 Apr 2020 13:01:09 +0000 (16:01 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Sat, 11 Apr 2020 13:01:09 +0000 (16:01 +0300)
CHANGELOG.md
lib/pleroma/notification.ex
test/notification_test.exs

index 36897503ac1fa3fbfb3a0601c2353defb0a7a443..22d0645faab420ca9235c9f57704aa8fdd7e334e 100644 (file)
@@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 
 ### Fixed
 - Support pagination in conversations API
+- Filtering of push notifications on activities from blocked domains
 
 ## [unreleased-patch]
 
index 04ee510b9f13fa7d346064988dacaea2d95babc1..02363ddb0ea58684d712d49a75a4ba8f12f43b07 100644 (file)
@@ -321,10 +321,11 @@ defmodule Pleroma.Notification do
 
   @doc """
   Returns a tuple with 2 elements:
-    {enabled notification receivers, currently disabled receivers (blocking / [thread] muting)}
+    {notification-enabled receivers, currently disabled receivers (blocking / [thread] muting)}
 
   NOTE: might be called for FAKE Activities, see ActivityPub.Utils.get_notified_from_object/1
   """
+  @spec get_notified_from_activity(Activity.t(), boolean()) :: {list(User.t()), list(User.t())}
   def get_notified_from_activity(activity, local_only \\ true)
 
   def get_notified_from_activity(%Activity{data: %{"type" => type}} = activity, local_only)
@@ -337,17 +338,22 @@ defmodule Pleroma.Notification do
       |> Utils.maybe_notify_followers(activity)
       |> Enum.uniq()
 
-    # Since even subscribers and followers can mute / thread-mute, filtering all above AP IDs
-    notification_enabled_ap_ids =
-      potential_receiver_ap_ids
-      |> exclude_relationship_restricted_ap_ids(activity)
-      |> exclude_thread_muter_ap_ids(activity)
-
     potential_receivers =
       potential_receiver_ap_ids
       |> Enum.uniq()
       |> User.get_users_from_set(local_only)
 
+    activity_actor_domain = activity.actor && URI.parse(activity.actor).host
+
+    notification_enabled_ap_ids =
+      for u <- potential_receivers, activity_actor_domain not in u.domain_blocks, do: u.ap_id
+
+    # Since even subscribers and followers can mute / thread-mute, filtering all above AP IDs
+    notification_enabled_ap_ids =
+      notification_enabled_ap_ids
+      |> exclude_relationship_restricted_ap_ids(activity)
+      |> exclude_thread_muter_ap_ids(activity)
+
     notification_enabled_users =
       Enum.filter(potential_receivers, fn u -> u.ap_id in notification_enabled_ap_ids end)
 
index 837a9dacd17a9ea854878580817ebd4a39515430..caa9419348d2810d40a1fa6fa8df634ee1958033 100644 (file)
@@ -609,6 +609,21 @@ defmodule Pleroma.NotificationTest do
       assert [other_user] == disabled_receivers
       refute other_user in enabled_receivers
     end
+
+    test "it returns domain-blocking recipient in disabled recipients list" do
+      blocked_domain = "blocked.domain"
+      user = insert(:user, %{ap_id: "https://#{blocked_domain}/@actor"})
+      other_user = insert(:user)
+
+      {:ok, other_user} = User.block_domain(other_user, blocked_domain)
+
+      {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}!"})
+
+      {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
+
+      assert [] == enabled_receivers
+      assert [other_user] == disabled_receivers
+    end
   end
 
   describe "notification lifecycle" do