Merge branch 'worker-messages' into 'develop'
[akkoma] / lib / pleroma / marker.ex
index 2d217a0b7a7a83e580fe2cecfa837a25fff58fff..4439273922d2916e11886c302e9f7603b237ea07 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Marker do
@@ -9,36 +9,22 @@ defmodule Pleroma.Marker do
   import Ecto.Query
 
   alias Ecto.Multi
-  alias Pleroma.Notification
   alias Pleroma.Repo
   alias Pleroma.User
-  alias __MODULE__
 
   @timelines ["notifications"]
-  @type t :: %__MODULE__{}
 
   schema "markers" do
     field(:last_read_id, :string, default: "")
     field(:timeline, :string, default: "")
     field(:lock_version, :integer, default: 0)
-    field(:unread_count, :integer, default: 0)
 
     belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
     timestamps()
   end
 
-  @doc """
-  Gets markers by user and timeline.
-
-  opts:
-  `recount_unread` - run force recount unread notifications for `true` value
-  """
-  @spec get_markers(User.t(), list(String), map()) :: list(t())
-  def get_markers(user, timelines \\ [], opts \\ %{}) do
-    user
-    |> get_query(timelines)
-    |> recount_unread_notifications(opts[:recount_unread])
-    |> Repo.all()
+  def get_markers(user, timelines \\ []) do
+    Repo.all(get_query(user, timelines))
   end
 
   def upsert(%User{} = user, attrs) do
@@ -52,38 +38,13 @@ defmodule Pleroma.Marker do
 
       Multi.insert(multi, timeline, marker,
         returning: true,
-        on_conflict: {:replace, [:last_read_id, :unread_count]},
+        on_conflict: {:replace, [:last_read_id]},
         conflict_target: [:user_id, :timeline]
       )
     end)
     |> Repo.transaction()
   end
 
-  @spec multi_set_unread_count(Multi.t(), User.t(), String.t()) :: Multi.t()
-  def multi_set_unread_count(multi, %User{} = user, "notifications") do
-    multi
-    |> Multi.run(:counters, fn _repo, _changes ->
-      {:ok,
-       %{
-         unread_count: Repo.aggregate(Notification.unread_count_query(user), :count, :id),
-         last_read_id: Repo.one(Notification.last_read_query(user))
-       }}
-    end)
-    |> Multi.insert(
-      :marker,
-      fn %{counters: attrs} ->
-        %Marker{timeline: "notifications", user_id: user.id}
-        |> struct(attrs)
-        |> Ecto.Changeset.change()
-      end,
-      returning: true,
-      on_conflict: {:replace, [:last_read_id, :unread_count]},
-      conflict_target: [:user_id, :timeline]
-    )
-  end
-
-  def multi_set_unread_count(multi, _, _), do: multi
-
   defp get_marker(user, timeline) do
     case Repo.find_resource(get_query(user, timeline)) do
       {:ok, marker} -> %__MODULE__{marker | user: user}
@@ -94,7 +55,7 @@ defmodule Pleroma.Marker do
   @doc false
   defp changeset(marker, attrs) do
     marker
-    |> cast(attrs, [:last_read_id, :unread_count])
+    |> cast(attrs, [:last_read_id])
     |> validate_required([:user_id, :timeline, :last_read_id])
     |> validate_inclusion(:timeline, @timelines)
   end
@@ -110,18 +71,4 @@ defmodule Pleroma.Marker do
     |> by_user_id(user.id)
     |> by_timeline(timelines)
   end
-
-  defp recount_unread_notifications(query, true) do
-    from(
-      q in query,
-      left_join: n in "notifications",
-      on: n.user_id == q.user_id and n.seen == false,
-      group_by: [:id],
-      select_merge: %{
-        unread_count: fragment("count(?)", n.id)
-      }
-    )
-  end
-
-  defp recount_unread_notifications(query, _), do: query
 end