forked from AkkomaGang/akkoma
update Marker.multi_set_unread_count
This commit is contained in:
parent
ddbfc995ac
commit
b5b62f42b2
2 changed files with 20 additions and 8 deletions
|
@ -9,6 +9,7 @@ defmodule Pleroma.Marker do
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
alias Ecto.Multi
|
alias Ecto.Multi
|
||||||
|
alias Pleroma.Notification
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
alias __MODULE__
|
alias __MODULE__
|
||||||
|
@ -51,7 +52,11 @@ def upsert(%User{} = user, attrs) do
|
||||||
def multi_set_unread_count(multi, %User{} = user, "notifications") do
|
def multi_set_unread_count(multi, %User{} = user, "notifications") do
|
||||||
multi
|
multi
|
||||||
|> Multi.run(:counters, fn _repo, _changes ->
|
|> Multi.run(:counters, fn _repo, _changes ->
|
||||||
{:ok, Repo.one(Pleroma.Notification.notifications_info_query(user))}
|
{:ok,
|
||||||
|
%{
|
||||||
|
unread_count: Repo.aggregate(Notification.unread_count_query(user), :count, :id),
|
||||||
|
last_read_id: Repo.one(Notification.last_read_query(user))
|
||||||
|
}}
|
||||||
end)
|
end)
|
||||||
|> Multi.insert(
|
|> Multi.insert(
|
||||||
:marker,
|
:marker,
|
||||||
|
|
|
@ -36,15 +36,22 @@ def changeset(%Notification{} = notification, attrs) do
|
||||||
|> cast(attrs, [:seen])
|
|> cast(attrs, [:seen])
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec notifications_info_query(User.t()) :: Ecto.Queryable.t()
|
@spec unread_count_query(User.t()) :: Ecto.Queryable.t()
|
||||||
def notifications_info_query(user) do
|
def unread_count_query(user) do
|
||||||
from(q in Pleroma.Notification,
|
from(q in Pleroma.Notification,
|
||||||
where: q.user_id == ^user.id,
|
where: q.user_id == ^user.id,
|
||||||
select: %{
|
where: q.seen == false
|
||||||
unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END )"),
|
)
|
||||||
last_read_id:
|
end
|
||||||
type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string)
|
|
||||||
}
|
@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
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue