forked from AkkomaGang/akkoma
add test
This commit is contained in:
parent
99cf1ef9be
commit
922e3d082c
3 changed files with 21 additions and 13 deletions
|
@ -51,19 +51,7 @@ 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 ->
|
||||||
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,
|
||||||
|
|
|
@ -36,6 +36,20 @@ def changeset(%Notification{} = notification, attrs) 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)
|
||||||
|
|
|
@ -21,6 +21,12 @@ test "returns multi" 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
|
||||||
|
|
Loading…
Reference in a new issue