From a5424c3681a537e08fa1333ee9749ab22fd3ece0 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sat, 8 Jun 2024 13:08:06 -0400 Subject: [PATCH 1/3] Test that end of poll notifications are streamed over websockets --- test/pleroma/notification_test.exs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/test/pleroma/notification_test.exs b/test/pleroma/notification_test.exs index 2c582c708..02ae06c63 100644 --- a/test/pleroma/notification_test.exs +++ b/test/pleroma/notification_test.exs @@ -5,6 +5,7 @@ defmodule Pleroma.NotificationTest do use Pleroma.DataCase, async: false + import Mock import Pleroma.Factory alias Pleroma.FollowingRelationship @@ -183,9 +184,31 @@ defmodule Pleroma.NotificationTest do {:ok, _, _} = CommonAPI.vote(user2, question, [0]) {:ok, _, _} = CommonAPI.vote(user3, question, [1]) - {:ok, notifications} = Notification.create_poll_notifications(activity) + with_mocks([ + { + Pleroma.Web.Streamer, + [], + [ + stream: fn _, _ -> nil end + ] + }, + { + Pleroma.Web.Push, + [], + [ + send: fn _ -> nil end + ] + } + ]) do + {:ok, notifications} = Notification.create_poll_notifications(activity) - assert [user2.id, user3.id, user1.id] == Enum.map(notifications, & &1.user_id) + Enum.each(notifications, fn notification -> + assert called(Pleroma.Web.Streamer.stream(["user", "user:notification"], notification)) + assert called(Pleroma.Web.Push.send(notification)) + end) + + assert [user2.id, user3.id, user1.id] == Enum.map(notifications, & &1.user_id) + end end describe "create_notification" do From b1c52c30628212f881825a702ba75e6170f82407 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sat, 8 Jun 2024 12:40:32 -0400 Subject: [PATCH 2/3] Rename Notification.send/1 to Notification.stream/1 Also update other places where we use the term "send" instead of "stream". This should make it clearer that we are streaming these over websockets / web push and not sending an activity. --- lib/pleroma/notification.ex | 5 +++-- lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- lib/pleroma/web/activity_pub/side_effects.ex | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index f521a2998..de2508b93 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -757,8 +757,9 @@ defmodule Pleroma.Notification do |> Repo.update_all(set: [seen: true]) end - @spec send(list(Notification.t())) :: :ok - def send(notifications) do + @doc "Streams a list of notifications over websockets and web push" + @spec stream(list(Notification.t())) :: :ok + def stream(notifications) do Enum.each(notifications, fn notification -> Streamer.stream(["user", "user:notification"], notification) Push.send(notification) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 1247ae7ce..6c1d6ded9 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -201,7 +201,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do def notify_and_stream(activity) do {:ok, notifications} = Notification.create_notifications(activity) - Notification.send(notifications) + Notification.stream(notifications) original_activity = case activity do diff --git a/lib/pleroma/web/activity_pub/side_effects.ex b/lib/pleroma/web/activity_pub/side_effects.ex index 60b4d5f1b..2a141b0f5 100644 --- a/lib/pleroma/web/activity_pub/side_effects.ex +++ b/lib/pleroma/web/activity_pub/side_effects.ex @@ -592,9 +592,9 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do with {:ok, _} <- Repo.delete(object), do: :ok end - defp send_notifications(meta) do + defp stream_notifications(meta) do Keyword.get(meta, :notifications, []) - |> Notification.send() + |> Notification.stream() meta end @@ -625,7 +625,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do @impl true def handle_after_transaction(meta) do meta - |> send_notifications() + |> stream_notifications() |> send_streamables() end end From 471412ad34e87106641101c9e9e44fb1df2ca905 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 4 Jun 2024 14:39:03 -0400 Subject: [PATCH 3/3] Stream end of poll notification over websockets and web push --- changelog.d/stream-end-poll.fix | 1 + lib/pleroma/notification.ex | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 changelog.d/stream-end-poll.fix diff --git a/changelog.d/stream-end-poll.fix b/changelog.d/stream-end-poll.fix new file mode 100644 index 000000000..db513efdc --- /dev/null +++ b/changelog.d/stream-end-poll.fix @@ -0,0 +1 @@ +End of poll notifications were not streamed over websockets or web push diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index de2508b93..b9694a353 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -479,6 +479,8 @@ defmodule Pleroma.Notification do end end) + stream(notifications) + {:ok, notifications} end end