Merge remote-tracking branch 'upstream/develop' into email-fix-develop
[akkoma] / lib / pleroma / migration_helper / notification_backfill.ex
index 09647d12a97c2d73712b99dd6b0342eca6839aa1..24f4733fe796c47905c14f014bb5ff36a2cd21a2 100644 (file)
@@ -3,7 +3,6 @@
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.MigrationHelper.NotificationBackfill do
-  alias Pleroma.Notification
   alias Pleroma.Object
   alias Pleroma.Repo
   alias Pleroma.User
@@ -18,25 +17,34 @@ defmodule Pleroma.MigrationHelper.NotificationBackfill do
       )
 
     query
-    |> Repo.all()
+    |> Repo.chunk_stream(100)
     |> Enum.each(fn notification ->
-      type =
-        notification.activity
-        |> type_from_activity()
+      if notification.activity do
+        type = type_from_activity(notification.activity)
 
-      notification
-      |> Notification.changeset(%{type: type})
-      |> Repo.update()
+        notification
+        |> Ecto.Changeset.change(%{type: type})
+        |> Repo.update()
+      end
     end)
   end
 
+  defp get_by_ap_id(ap_id) do
+    q =
+      from(u in User,
+        select: u.id
+      )
+
+    Repo.get_by(q, ap_id: ap_id)
+  end
+
   # This is copied over from Notifications to keep this stable.
   defp type_from_activity(%{data: %{"type" => type}} = activity) do
     case type do
       "Follow" ->
         accepted_function = fn activity ->
-          with %User{} = follower <- User.get_by_ap_id(activity.data["actor"]),
-               %User{} = followed <- User.get_by_ap_id(activity.data["object"]) do
+          with %User{} = follower <- get_by_ap_id(activity.data["actor"]),
+               %User{} = followed <- get_by_ap_id(activity.data["object"]) do
             Pleroma.FollowingRelationship.following?(follower, followed)
           end
         end
@@ -64,8 +72,7 @@ defmodule Pleroma.MigrationHelper.NotificationBackfill do
         "pleroma:emoji_reaction"
 
       "Create" ->
-        activity
-        |> type_from_activity_object()
+        type_from_activity_object(activity)
 
       t ->
         raise "No notification type for activity type #{t}"