1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.MigrationHelper.NotificationBackfill do
11 def fill_in_notification_types do
13 from(n in Pleroma.Notification,
14 where: is_nil(n.type),
19 |> Repo.chunk_stream(100)
20 |> Enum.each(fn notification ->
21 if notification.activity do
22 type = type_from_activity(notification.activity)
25 |> Ecto.Changeset.change(%{type: type})
31 defp get_by_ap_id(ap_id) do
37 Repo.get_by(q, ap_id: ap_id)
40 # This is copied over from Notifications to keep this stable.
41 defp type_from_activity(%{data: %{"type" => type}} = activity) do
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)
51 if accepted_function.(activity) do
67 "pleroma:emoji_reaction"
69 # Compatibility with old reactions
71 "pleroma:emoji_reaction"
74 type_from_activity_object(activity)
77 raise "No notification type for activity type #{t}"
81 defp type_from_activity_object(%{data: %{"type" => "Create"}}), do: "mention"