aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaksim Pechnikov <parallel588@gmail.com>2019-11-12 15:59:34 +0300
committerMaksim Pechnikov <parallel588@gmail.com>2019-11-12 15:59:34 +0300
commitb5b62f42b2864dc8b95c8ba7d650321ebcc332ad (patch)
treedd27600fd44b54da4e1cd9fd114c38a1116634db
parentddbfc995ac40db9bd1da137b03e5acf6d050ddc5 (diff)
downloadpleroma-b5b62f42b2864dc8b95c8ba7d650321ebcc332ad.tar.gz
update Marker.multi_set_unread_count
-rw-r--r--lib/pleroma/marker.ex7
-rw-r--r--lib/pleroma/notification.ex21
2 files changed, 20 insertions, 8 deletions
diff --git a/lib/pleroma/marker.ex b/lib/pleroma/marker.ex
index d5ca27bf2..a32546094 100644
--- a/lib/pleroma/marker.ex
+++ b/lib/pleroma/marker.ex
@@ -9,6 +9,7 @@ defmodule Pleroma.Marker do
import Ecto.Query
alias Ecto.Multi
+ alias Pleroma.Notification
alias Pleroma.Repo
alias Pleroma.User
alias __MODULE__
@@ -51,7 +52,11 @@ defmodule Pleroma.Marker do
def multi_set_unread_count(multi, %User{} = user, "notifications") do
multi
|> 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)
|> Multi.insert(
:marker,
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
index 158903c4b..1cc6a4735 100644
--- a/lib/pleroma/notification.ex
+++ b/lib/pleroma/notification.ex
@@ -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