Fix buckets for query timing
[akkoma] / lib / pleroma / migration_helper / notification_backfill.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.MigrationHelper.NotificationBackfill do
6 alias Pleroma.Repo
7 alias Pleroma.User
8
9 import Ecto.Query
10
11 def fill_in_notification_types do
12 query =
13 from(n in Pleroma.Notification,
14 where: is_nil(n.type),
15 preload: :activity
16 )
17
18 query
19 |> Repo.chunk_stream(100)
20 |> Enum.each(fn notification ->
21 if notification.activity do
22 type = type_from_activity(notification.activity)
23
24 notification
25 |> Ecto.Changeset.change(%{type: type})
26 |> Repo.update()
27 end
28 end)
29 end
30
31 defp get_by_ap_id(ap_id) do
32 q =
33 from(u in User,
34 select: u.id
35 )
36
37 Repo.get_by(q, ap_id: ap_id)
38 end
39
40 # This is copied over from Notifications to keep this stable.
41 defp type_from_activity(%{data: %{"type" => type}} = activity) do
42 case type do
43 "Follow" ->
44 accepted_function = fn activity ->
45 with %User{} = follower <- get_by_ap_id(activity.data["actor"]),
46 %User{} = followed <- get_by_ap_id(activity.data["object"]) do
47 Pleroma.FollowingRelationship.following?(follower, followed)
48 end
49 end
50
51 if accepted_function.(activity) do
52 "follow"
53 else
54 "follow_request"
55 end
56
57 "Announce" ->
58 "reblog"
59
60 "Like" ->
61 "favourite"
62
63 "Move" ->
64 "move"
65
66 "EmojiReact" ->
67 "pleroma:emoji_reaction"
68
69 # Compatibility with old reactions
70 "EmojiReaction" ->
71 "pleroma:emoji_reaction"
72
73 "Create" ->
74 type_from_activity_object(activity)
75
76 t ->
77 raise "No notification type for activity type #{t}"
78 end
79 end
80
81 defp type_from_activity_object(%{data: %{"type" => "Create"}}), do: "mention"
82 end