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