Notification: Add function to backfill notification types
authorlain <lain@soykaf.club>
Tue, 2 Jun 2020 12:49:56 +0000 (14:49 +0200)
committerlain <lain@soykaf.club>
Tue, 2 Jun 2020 12:50:10 +0000 (14:50 +0200)
lib/pleroma/notification.ex
test/notification_test.exs

index 41ac5350586beda729b1466b0592aae980caa507..c8b964400d1b3f955ddfa7ec935e2538c87d3c0e 100644 (file)
@@ -37,6 +37,26 @@ defmodule Pleroma.Notification do
     timestamps()
   end
 
+  def fill_in_notification_types() do
+    query =
+      from(n in __MODULE__,
+        where: is_nil(n.type),
+        preload: :activity
+      )
+
+    query
+    |> Repo.all()
+    |> Enum.each(fn notification ->
+      type =
+        notification.activity
+        |> type_from_activity()
+
+      notification
+      |> changeset(%{type: type})
+      |> Repo.update()
+    end)
+  end
+
   def update_notification_type(user, activity) do
     with %__MODULE__{} = notification <-
            Repo.get_by(__MODULE__, user_id: user.id, activity_id: activity.id) do
index 421b7fc4076ebec4110665d9391bcb64517df1ef..6bc2b6904b5972025b48c59d5898a1ad9cc9fcd0 100644 (file)
@@ -20,6 +20,34 @@ defmodule Pleroma.NotificationTest do
   alias Pleroma.Web.Push
   alias Pleroma.Web.Streamer
 
+  describe "fill_in_notification_types" do
+    test "it fills in missing notification types" do
+      user = insert(:user)
+      other_user = insert(:user)
+
+      {:ok, post} = CommonAPI.post(user, %{status: "yeah, @#{other_user.nickname}"})
+      {:ok, chat} = CommonAPI.post_chat_message(user, other_user, "yo")
+      {:ok, react} = CommonAPI.react_with_emoji(post.id, other_user, "☕")
+      {:ok, like} = CommonAPI.favorite(other_user, post.id)
+
+      assert {4, nil} = Repo.update_all(Notification, set: [type: nil])
+
+      Notification.fill_in_notification_types()
+
+      assert %{type: "mention"} =
+               Repo.get_by(Notification, user_id: other_user.id, activity_id: post.id)
+
+      assert %{type: "favourite"} =
+               Repo.get_by(Notification, user_id: user.id, activity_id: like.id)
+
+      assert %{type: "pleroma:emoji_reaction"} =
+               Repo.get_by(Notification, user_id: user.id, activity_id: react.id)
+
+      assert %{type: "pleroma:chat_mention"} =
+               Repo.get_by(Notification, user_id: other_user.id, activity_id: chat.id)
+    end
+  end
+
   describe "create_notifications" do
     test "creates a notification for an emoji reaction" do
       user = insert(:user)