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
6 alias Pleroma.Notification
13 def fill_in_notification_types do
15 from(n in Pleroma.Notification,
16 where: is_nil(n.type),
21 |> Repo.chunk_stream(100)
22 |> Enum.each(fn notification ->
25 |> type_from_activity()
28 |> Notification.changeset(%{type: type})
33 # This is copied over from Notifications to keep this stable.
34 defp type_from_activity(%{data: %{"type" => type}} = activity) do
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)
44 if accepted_function.(activity) do
60 "pleroma:emoji_reaction"
62 # Compatibility with old reactions
64 "pleroma:emoji_reaction"
68 |> type_from_activity_object()
71 raise "No notification type for activity type #{t}"
75 defp type_from_activity_object(%{data: %{"type" => "Create", "object" => %{}}}), do: "mention"
77 defp type_from_activity_object(%{data: %{"type" => "Create"}} = activity) do
78 object = Object.get_by_ap_id(activity.data["object"])
80 case object && object.data["type"] do
81 "ChatMessage" -> "pleroma:chat_mention"