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,
|> 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)
"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