Notification: Add function to backfill notification types

This commit is contained in:
lain 2020-06-02 14:49:56 +02:00
parent 904295d53b
commit 38dce485c4
2 changed files with 48 additions and 0 deletions

View File

@ -37,6 +37,26 @@ defmodule Pleroma.Notification do
timestamps() timestamps()
end 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 def update_notification_type(user, activity) do
with %__MODULE__{} = notification <- with %__MODULE__{} = notification <-
Repo.get_by(__MODULE__, user_id: user.id, activity_id: activity.id) do Repo.get_by(__MODULE__, user_id: user.id, activity_id: activity.id) do

View File

@ -20,6 +20,34 @@ defmodule Pleroma.NotificationTest do
alias Pleroma.Web.Push alias Pleroma.Web.Push
alias Pleroma.Web.Streamer 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 describe "create_notifications" do
test "creates a notification for an emoji reaction" do test "creates a notification for an emoji reaction" do
user = insert(:user) user = insert(:user)