diff options
author | Maksim Pechnikov <parallel588@gmail.com> | 2019-10-22 16:13:22 +0300 |
---|---|---|
committer | Maksim Pechnikov <parallel588@gmail.com> | 2019-10-23 11:22:48 +0300 |
commit | 9a4afbd2a0486238bfaf4047d91376d32635514a (patch) | |
tree | d3a5140191d5edc97997be42f42e7feb01c365ad /lib/pleroma/marker.ex | |
parent | d4270397dcb2aebde8ed14fd89998ab57aaae545 (diff) | |
download | pleroma-9a4afbd2a0486238bfaf4047d91376d32635514a.tar.gz |
added update unread_count for notifications
Diffstat (limited to 'lib/pleroma/marker.ex')
-rw-r--r-- | lib/pleroma/marker.ex | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/pleroma/marker.ex b/lib/pleroma/marker.ex index c4d554980..4b8198690 100644 --- a/lib/pleroma/marker.ex +++ b/lib/pleroma/marker.ex @@ -11,6 +11,7 @@ defmodule Pleroma.Marker do alias Ecto.Multi alias Pleroma.Repo alias Pleroma.User + alias __MODULE__ @timelines ["notifications"] @@ -46,6 +47,41 @@ defmodule Pleroma.Marker do |> 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 -> + query = + from(q in Pleroma.Notification, + where: q.user_id == ^user.id, + select: %{ + timeline: "notifications", + user_id: ^user.id, + unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END ) as unread_count") + } + ) + + {:ok, Repo.one(query)} + end) + |> Multi.insert( + :marker, + fn %{counters: attrs} -> + Marker + |> struct(attrs) + |> Ecto.Changeset.change() + end, + returning: true, + on_conflict: {:replace, [:last_read_id, :unread_count]}, + conflict_target: [:user_id, :timeline] + ) + end + + def set_unread_count(%User{} = user, timeline) do + Multi.new() + |> multi_set_unread_count(user, timeline) + |> Repo.transaction() + end + defp get_marker(user, timeline) do case Repo.find_resource(get_query(user, timeline)) do {:ok, marker} -> %__MODULE__{marker | user: user} |