where: q.user_id == ^user.id,
select: %{
timeline: "notifications",
- user_id: ^user.id,
- unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END ) as unread_count")
+ 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)
}
)
select: %{
timeline: "notifications",
user_id: q.user_id,
- unread_count: fragment("COUNT(*) FILTER (WHERE seen = false) as unread_count"),
- last_read_id: fragment("(MAX(id) FILTER (WHERE seen = true)::text) as last_read_id ")
+ 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)
},
group_by: [q.user_id]
)
|> Repo.all()
- |> Enum.reduce(Ecto.Multi.new(), fn attrs, multi ->
- marker =
- Pleroma.Marker
- |> struct(attrs)
- |> Ecto.Changeset.change()
-
- multi
- |> Ecto.Multi.insert(attrs[:user_id], marker,
+ |> Enum.each(fn attrs ->
+ Pleroma.Marker
+ |> struct(attrs)
+ |> Ecto.Changeset.change()
+ |> Pleroma.Repo.insert(
returning: true,
on_conflict: {:replace, [:last_read_id, :unread_count]},
conflict_target: [:user_id, :timeline]
)
end)
- |> Pleroma.Repo.transaction()
end
end