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 @@ def upsert(%User{} = user, attrs) do
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: 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)}
{:ok, Repo.one(Pleroma.Notification.notifications_info_query(user))}
end)
|> Multi.insert(
:marker,

View File

@ -36,6 +36,20 @@ def changeset(%Notification{} = notification, attrs) do
|> cast(attrs, [:seen])
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
Notification
|> where(user_id: ^user.id)

View File

@ -21,6 +21,12 @@ test "returns multi" do
"notifications"
)
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
describe "get_markers/2" do