X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fmarker.ex;h=4d82860f5a2aab752aaad56f6e49c7a2e168ae27;hb=58ccd2bbfd9ab589aba21c886b497979a43bc591;hp=a3254609488cfc3136b1d2b9ecad3c335be6a4fa;hpb=b5b62f42b2864dc8b95c8ba7d650321ebcc332ad;p=akkoma
diff --git a/lib/pleroma/marker.ex b/lib/pleroma/marker.ex
index a32546094..4d82860f5 100644
--- a/lib/pleroma/marker.ex
+++ b/lib/pleroma/marker.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Marker do
@@ -15,21 +15,28 @@ defmodule Pleroma.Marker do
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)
+ field(:unread_count, :integer, default: 0, virtual: true)
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
timestamps()
end
+ @doc "Gets markers by user and timeline."
+ @spec get_markers(User.t(), list(String)) :: list(t())
def get_markers(user, timelines \\ []) do
- Repo.all(get_query(user, timelines))
+ user
+ |> get_query(timelines)
+ |> unread_count_query()
+ |> Repo.all()
end
+ @spec upsert(User.t(), map()) :: {:ok | :error, any()}
def upsert(%User{} = user, attrs) do
attrs
|> Map.take(@timelines)
@@ -41,22 +48,18 @@ 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
+ @spec multi_set_last_read_id(Multi.t(), User.t(), String.t()) :: Multi.t()
+ def multi_set_last_read_id(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))
- }}
+ {:ok, %{last_read_id: Repo.one(Notification.last_read_query(user))}}
end)
|> Multi.insert(
:marker,
@@ -66,12 +69,12 @@ defmodule Pleroma.Marker do
|> Ecto.Changeset.change()
end,
returning: true,
- on_conflict: {:replace, [:last_read_id, :unread_count]},
+ on_conflict: {:replace, [:last_read_id]},
conflict_target: [:user_id, :timeline]
)
end
- def multi_set_unread_count(multi, _, _), do: multi
+ def multi_set_last_read_id(multi, _, _), do: multi
defp get_marker(user, timeline) do
case Repo.find_resource(get_query(user, timeline)) do
@@ -83,7 +86,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
@@ -99,4 +102,16 @@ defmodule Pleroma.Marker do
|> by_user_id(user.id)
|> by_timeline(timelines)
end
+
+ defp unread_count_query(query) 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
end