1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.MigrationHelper.NotificationBackfill do
12 def fill_in_notification_types do
14 from(n in Pleroma.Notification,
15 where: is_nil(n.type),
20 |> Repo.chunk_stream(100)
21 |> Enum.each(fn notification ->
24 |> type_from_activity()
27 |> Ecto.Changeset.change(%{type: type})
32 defp get_by_ap_id(ap_id) do
38 Repo.get_by(q, ap_id: ap_id)
41 # This is copied over from Notifications to keep this stable.
42 defp type_from_activity(%{data: %{"type" => type}} = activity) do
45 accepted_function = fn activity ->
46 with %User{} = follower <- get_by_ap_id(activity.data["actor"]),
47 %User{} = followed <- get_by_ap_id(activity.data["object"]) do
48 Pleroma.FollowingRelationship.following?(follower, followed)
52 if accepted_function.(activity) do
68 "pleroma:emoji_reaction"
70 # Compatibility with old reactions
72 "pleroma:emoji_reaction"
76 |> type_from_activity_object()
79 raise "No notification type for activity type #{t}"
83 defp type_from_activity_object(%{data: %{"type" => "Create", "object" => %{}}}), do: "mention"
85 defp type_from_activity_object(%{data: %{"type" => "Create"}} = activity) do
86 object = Object.get_by_ap_id(activity.data["object"])
88 case object && object.data["type"] do
89 "ChatMessage" -> "pleroma:chat_mention"