Merge branch 'benchmark-fixes' into 'develop'
[akkoma] / lib / pleroma / notification.ex
index 43a0e8f49aaac708e64d102e495cc6225bbac0e1..9e0ce0329e2638652eded2a150d4fc1087c9d013 100644 (file)
@@ -128,6 +128,7 @@ defmodule Pleroma.Notification do
     |> where([user_actor: user_actor], user_actor.is_active)
     |> exclude_notification_muted(user, exclude_notification_muted_opts)
     |> exclude_blocked(user, exclude_blocked_opts)
+    |> exclude_blockers(user)
     |> exclude_filtered(user)
     |> exclude_visibility(opts)
   end
@@ -141,6 +142,17 @@ defmodule Pleroma.Notification do
     |> FollowingRelationship.keep_following_or_not_domain_blocked(user)
   end
 
+  defp exclude_blockers(query, user) do
+    if Pleroma.Config.get([:activitypub, :blockers_visible]) == true do
+      query
+    else
+      blocker_ap_ids = User.incoming_relationships_ungrouped_ap_ids(user, [:block])
+
+      query
+      |> where([n, a], a.actor not in ^blocker_ap_ids)
+    end
+  end
+
   defp exclude_notification_muted(query, _, %{@include_muted_option => true}) do
     query
   end
@@ -471,9 +483,11 @@ defmodule Pleroma.Notification do
         end
 
       notifications =
-        Enum.map([actor | voters], fn ap_id ->
-          with %User{} = user <- User.get_by_ap_id(ap_id) do
-            create_notification(activity, user, type: "poll")
+        Enum.reduce([actor | voters], [], fn ap_id, acc ->
+          with %User{local: true} = user <- User.get_by_ap_id(ap_id) do
+            [create_notification(activity, user, type: "poll") | acc]
+          else
+            _ -> acc
           end
         end)
 
@@ -631,11 +645,17 @@ defmodule Pleroma.Notification do
         :block_from_strangers,
         %Activity{} = activity,
         %User{notification_settings: %{block_from_strangers: true}} = user,
-        _opts
+        opts
       ) do
     actor = activity.data["actor"]
     follower = User.get_cached_by_ap_id(actor)
-    !User.following?(follower, user)
+
+    cond do
+      opts[:type] == "poll" -> false
+      user.ap_id == actor -> false
+      !User.following?(follower, user) -> true
+      true -> false
+    end
   end
 
   # To do: consider defining recency in hours and checking FollowingRelationship with a single SQL