Merge branch 'develop' into issue/1276
[akkoma] / lib / mix / tasks / pleroma / marker.ex
1 defmodule Mix.Tasks.Pleroma.Marker do
2 use Mix.Task
3 import Mix.Pleroma
4 import Ecto.Query
5
6 alias Pleroma.Notification
7 alias Pleroma.Repo
8
9 def run(["update_markers"]) do
10 start_pleroma()
11
12 from(q in Notification,
13 select: %{
14 timeline: "notifications",
15 user_id: q.user_id,
16 unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END )"),
17 last_read_id:
18 type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string)
19 },
20 group_by: [q.user_id]
21 )
22 |> Repo.all()
23 |> Enum.each(fn attrs ->
24 Pleroma.Marker
25 |> struct(attrs)
26 |> Ecto.Changeset.change()
27 |> Pleroma.Repo.insert(
28 returning: true,
29 on_conflict: {:replace, [:last_read_id, :unread_count]},
30 conflict_target: [:user_id, :timeline]
31 )
32 end)
33
34 shell_info("Done")
35 end
36 end