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 ->
22 if notification.activity do
23 type = type_from_activity(notification.activity)
26 |> 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"
75 type_from_activity_object(activity)
78 raise "No notification type for activity type #{t}"
82 defp type_from_activity_object(%{data: %{"type" => "Create", "object" => %{}}}), do: "mention"
84 defp type_from_activity_object(%{data: %{"type" => "Create"}} = activity) do
85 object = Object.get_by_ap_id(activity.data["object"])
87 case object && object.data["type"] do
88 "ChatMessage" -> "pleroma:chat_mention"