purge chat and shout endpoints
[akkoma] / lib / pleroma / notification.ex
index 43a0e8f49aaac708e64d102e495cc6225bbac0e1..d8878338ee5859b90f9a1ca843a4afc4871e4d69 100644 (file)
@@ -68,7 +68,6 @@ defmodule Pleroma.Notification do
     follow_request
     mention
     move
-    pleroma:chat_mention
     pleroma:emoji_reaction
     pleroma:report
     reblog
@@ -128,6 +127,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 +141,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
@@ -329,6 +340,14 @@ defmodule Pleroma.Notification do
     |> Repo.delete_all()
   end
 
+  def destroy_multiple_from_types(%{id: user_id}, types) do
+    from(n in Notification,
+      where: n.user_id == ^user_id,
+      where: n.type in ^types
+    )
+    |> Repo.delete_all()
+  end
+
   def dismiss(%Pleroma.Activity{} = activity) do
     Notification
     |> where([n], n.activity_id == ^activity.id)
@@ -424,16 +443,7 @@ defmodule Pleroma.Notification do
     end
   end
 
-  defp type_from_activity_object(%{data: %{"type" => "Create", "object" => %{}}}), do: "mention"
-
-  defp type_from_activity_object(%{data: %{"type" => "Create"}} = activity) do
-    object = Object.get_by_ap_id(activity.data["object"])
-
-    case object && object.data["type"] do
-      "ChatMessage" -> "pleroma:chat_mention"
-      _ -> "mention"
-    end
-  end
+  defp type_from_activity_object(%{data: %{"type" => "Create"}}), do: "mention"
 
   # TODO move to sql, too.
   def create_notification(%Activity{} = activity, %User{} = user, opts \\ []) do
@@ -471,9 +481,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 +643,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