This commit is contained in:
Maksim Pechnikov 2019-10-24 09:50:41 +03:00
parent 99cf1ef9be
commit 922e3d082c
3 changed files with 21 additions and 13 deletions

View File

@ -51,19 +51,7 @@ defmodule Pleroma.Marker 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 ->
query = {:ok, Repo.one(Pleroma.Notification.notifications_info_query(user))}
from(q in Pleroma.Notification,
where: q.user_id == ^user.id,
select: %{
timeline: "notifications",
user_id: type(^user.id, :string),
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)
}
)
{:ok, Repo.one(query)}
end) end)
|> Multi.insert( |> Multi.insert(
:marker, :marker,

View File

@ -36,6 +36,20 @@ defmodule Pleroma.Notification do
|> cast(attrs, [:seen]) |> cast(attrs, [:seen])
end end
@spec notifications_info_query(User.t()) :: Ecto.Queryable.t()
def notifications_info_query(user) do
from(q in Pleroma.Notification,
where: q.user_id == ^user.id,
select: %{
timeline: "notifications",
user_id: type(^user.id, :string),
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)
}
)
end
def for_user_query(user, opts \\ []) do def for_user_query(user, opts \\ []) do
Notification Notification
|> where(user_id: ^user.id) |> where(user_id: ^user.id)

View File

@ -21,6 +21,12 @@ defmodule Pleroma.MarkerTest do
"notifications" "notifications"
) )
end end
test "return empty multi" do
user = insert(:user)
multi = Ecto.Multi.new()
assert Marker.multi_set_unread_count(multi, user, "home") == multi
end
end end
describe "get_markers/2" do describe "get_markers/2" do