Merge branch 'issue/1577' into 'develop'
[akkoma] / lib / pleroma / notification.ex
index da05ff2e45e84571108ba75c890d72db6cc2e9d9..9a109dfabf66f6ebe55d56e6cb7731aa37daff70 100644 (file)
@@ -88,19 +88,7 @@ defmodule Pleroma.Notification do
 
     query
     |> where([n, a], a.actor not in ^blocked_ap_ids)
-    |> where(
-      [n, a],
-      fragment(
-        # "NOT (actor's domain in domain_blocks) OR (actor is in followed AP IDs)"
-        "NOT (substring(? from '.*://([^/]*)') = ANY(?)) OR \
-          ? = ANY(SELECT ap_id FROM users AS u INNER JOIN following_relationships AS fr \
-            ON u.id = fr.following_id WHERE fr.follower_id = ? AND fr.state = 'accept')",
-        a.actor,
-        ^user.domain_blocks,
-        a.actor,
-        ^User.binary_id(user.id)
-      )
-    )
+    |> FollowingRelationship.keep_following_or_not_domain_blocked(user)
   end
 
   defp exclude_notification_muted(query, _, %{@include_muted_option => true}) do
@@ -273,6 +261,16 @@ defmodule Pleroma.Notification do
     |> Repo.delete_all()
   end
 
+  def dismiss(%Pleroma.Activity{} = activity) do
+    Notification
+    |> where([n], n.activity_id == ^activity.id)
+    |> Repo.delete_all()
+    |> case do
+      {_, notifications} -> {:ok, notifications}
+      _ -> {:error, "Cannot dismiss notification"}
+    end
+  end
+
   def dismiss(%{id: user_id} = _user, id) do
     notification = Repo.get(Notification, id)
 
@@ -295,8 +293,17 @@ defmodule Pleroma.Notification do
     end
   end
 
+  def create_notifications(%Activity{data: %{"type" => "Follow"}} = activity) do
+    if Pleroma.Config.get([:notifications, :enable_follow_request_notifications]) ||
+         Activity.follow_accepted?(activity) do
+      do_create_notifications(activity)
+    else
+      {:ok, []}
+    end
+  end
+
   def create_notifications(%Activity{data: %{"type" => type}} = activity)
-      when type in ["Like", "Announce", "Follow", "Move", "EmojiReact"] do
+      when type in ["Like", "Announce", "Move", "EmojiReact"] do
     do_create_notifications(activity)
   end
 
@@ -365,7 +372,7 @@ defmodule Pleroma.Notification do
 
   def get_notified_from_activity(_, _local_only), do: {[], []}
 
-  @doc "Filters out AP IDs of users who domain-block and not follow activity actor"
+  @doc "Filters out AP IDs domain-blocking and not following the activity's actor"
   def exclude_domain_blocker_ap_ids(ap_ids, activity, preloaded_users \\ [])
 
   def exclude_domain_blocker_ap_ids([], _activity, _preloaded_users), do: []