Merge branch 'develop' into issue/1276
[akkoma] / lib / pleroma / notification.ex
index 158903c4b2f1d41693f9d8589353c4de37c7bb21..d6149cd0da2bf8500bab549e7b3fd0efc063fa6e 100644 (file)
@@ -36,15 +36,22 @@ defmodule Pleroma.Notification do
     |> cast(attrs, [:seen])
   end
 
-  @spec notifications_info_query(User.t()) :: Ecto.Queryable.t()
-  def notifications_info_query(user) do
+  @spec unread_count_query(User.t()) :: Ecto.Queryable.t()
+  def unread_count_query(user) do
     from(q in Pleroma.Notification,
       where: q.user_id == ^user.id,
-      select: %{
-        unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END )"),
-        last_read_id:
-          type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string)
-      }
+      where: q.seen == false
+    )
+  end
+
+  @spec last_read_query(User.t()) :: Ecto.Queryable.t()
+  def last_read_query(user) do
+    from(q in Pleroma.Notification,
+      where: q.user_id == ^user.id,
+      where: q.seen == true,
+      select: type(q.id, :string),
+      limit: 1,
+      order_by: [desc: :id]
     )
   end
 
@@ -273,11 +280,11 @@ defmodule Pleroma.Notification do
     end
   end
 
-  def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
-      when type in ["Like", "Announce", "Follow"] do
+  def create_notifications(%Activity{data: %{"type" => type}} = activity)
+      when type in ["Like", "Announce", "Follow", "Move"] do
     notifications =
       activity
-      |> get_notified_from_activity
+      |> get_notified_from_activity()
       |> Enum.map(&create_notification(activity, &1))
 
     {:ok, notifications}
@@ -304,19 +311,15 @@ defmodule Pleroma.Notification do
 
   def get_notified_from_activity(activity, local_only \\ true)
 
-  def get_notified_from_activity(
-        %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
-        local_only
-      )
-      when type in ["Create", "Like", "Announce", "Follow"] do
-    recipients =
-      []
-      |> Utils.maybe_notify_to_recipients(activity)
-      |> Utils.maybe_notify_mentioned_recipients(activity)
-      |> Utils.maybe_notify_subscribers(activity)
-      |> Enum.uniq()
-
-    User.get_users_from_set(recipients, local_only)
+  def get_notified_from_activity(%Activity{data: %{"type" => type}} = activity, local_only)
+      when type in ["Create", "Like", "Announce", "Follow", "Move"] do
+    []
+    |> Utils.maybe_notify_to_recipients(activity)
+    |> Utils.maybe_notify_mentioned_recipients(activity)
+    |> Utils.maybe_notify_subscribers(activity)
+    |> Utils.maybe_notify_followers(activity)
+    |> Enum.uniq()
+    |> User.get_users_from_set(local_only)
   end
 
   def get_notified_from_activity(_, _local_only), do: []