Migrations: Add a migration to backfill notification types.
authorlain <lain@soykaf.club>
Tue, 2 Jun 2020 13:13:19 +0000 (15:13 +0200)
committerlain <lain@soykaf.club>
Tue, 2 Jun 2020 13:13:19 +0000 (15:13 +0200)
lib/pleroma/notification.ex
priv/repo/migrations/20200602125218_backfill_notification_types.exs [new file with mode: 0644]

index c8b964400d1b3f955ddfa7ec935e2538c87d3c0e..d89ee464568fada6f9614abb56c19e7a90782455 100644 (file)
@@ -49,7 +49,7 @@ defmodule Pleroma.Notification do
     |> Enum.each(fn notification ->
       type =
         notification.activity
-        |> type_from_activity()
+        |> type_from_activity(no_cachex: true)
 
       notification
       |> changeset(%{type: type})
@@ -364,10 +364,23 @@ defmodule Pleroma.Notification do
     {:ok, notifications}
   end
 
-  defp type_from_activity(%{data: %{"type" => type}} = activity) do
+  defp type_from_activity(%{data: %{"type" => type}} = activity, opts \\ []) do
     case type do
       "Follow" ->
-        if Activity.follow_accepted?(activity) do
+        accepted_function =
+          if Keyword.get(opts, :no_cachex, false) do
+            # A special function to make this usable in a migration.
+            fn activity ->
+              with %User{} = follower <- User.get_by_ap_id(activity.data["actor"]),
+                   %User{} = followed <- User.get_by_ap_id(activity.data["object"]) do
+                Pleroma.FollowingRelationship.following?(follower, followed)
+              end
+            end
+          else
+            &Activity.follow_accepted?/1
+          end
+
+        if accepted_function.(activity) do
           "follow"
         else
           "follow_request"
@@ -394,8 +407,10 @@ defmodule Pleroma.Notification do
     end
   end
 
+  defp type_from_activity_object(%{data: %{"type" => "Create", "object" => %{}}}), do: "mention"
+
   defp type_from_activity_object(%{data: %{"type" => "Create"}} = activity) do
-    object = Object.normalize(activity, false)
+    object = Object.get_by_ap_id(activity.data["object"])
 
     case object.data["type"] do
       "ChatMessage" -> "pleroma:chat_mention"
diff --git a/priv/repo/migrations/20200602125218_backfill_notification_types.exs b/priv/repo/migrations/20200602125218_backfill_notification_types.exs
new file mode 100644 (file)
index 0000000..493c028
--- /dev/null
@@ -0,0 +1,10 @@
+defmodule Pleroma.Repo.Migrations.BackfillNotificationTypes do
+  use Ecto.Migration
+
+  def up do
+    Pleroma.Notification.fill_in_notification_types()
+  end
+
+  def down do
+  end
+end