aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2020-06-02 14:49:56 +0200
committerlain <lain@soykaf.club>2020-06-02 14:50:10 +0200
commit38dce485c47e9315663c5c9cfd67dab4164b1bbe (patch)
treeb8dfaf11e099a12044c49e451c098f9a978d5e0e
parent904295d53b82fcfd7dfefd4c970815b36f06faf0 (diff)
downloadpleroma-38dce485c47e9315663c5c9cfd67dab4164b1bbe.tar.gz
Notification: Add function to backfill notification types
-rw-r--r--lib/pleroma/notification.ex20
-rw-r--r--test/notification_test.exs28
2 files changed, 48 insertions, 0 deletions
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
index 41ac53505..c8b964400 100644
--- a/lib/pleroma/notification.ex
+++ b/lib/pleroma/notification.ex
@@ -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
diff --git a/test/notification_test.exs b/test/notification_test.exs
index 421b7fc40..6bc2b6904 100644
--- a/test/notification_test.exs
+++ b/test/notification_test.exs
@@ -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)