fix(backend): fix sending N notifications to a single conversation participant

Because of the conversation participant ID being necessary in the email, we launch N jobs to send
notifications, so we need to send the email straight away.

Anonymous participants are always only a single conversation actor
participant, so we're fine

Closes #1384

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2024-01-09 12:23:59 +01:00
parent 681f8dcd04
commit 95379885c8
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
2 changed files with 23 additions and 4 deletions

View file

@ -792,7 +792,7 @@ defmodule Mobilizon.Events do
end end
end end
def get_participant(event_id, actor_id, %{}) do def get_participant(event_id, actor_id, _params) do
case Participant case Participant
|> Repo.get_by(event_id: event_id, actor_id: actor_id) |> Repo.get_by(event_id: event_id, actor_id: actor_id)
|> Repo.preload(@participant_preloads) do |> Repo.preload(@participant_preloads) do

View file

@ -4,7 +4,7 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
""" """
alias Mobilizon.Activities.Activity alias Mobilizon.Activities.Activity
alias Mobilizon.{Actors, Events, Users} alias Mobilizon.{Actors, Config, Events, Users}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Events.{Event, Participant} alias Mobilizon.Events.{Event, Participant}
alias Mobilizon.Service.Notifier alias Mobilizon.Service.Notifier
@ -37,9 +37,10 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
end end
defp special_handling("conversation_created", args, activity) do defp special_handling("conversation_created", args, activity) do
notify_participants( notify_participant(
get_in(args, ["subject_params", "conversation_event_id"]), get_in(args, ["subject_params", "conversation_event_id"]),
activity, activity,
get_in(args, ["participant", "actor_id"]),
args["author_id"] args["author_id"]
) )
end end
@ -143,6 +144,24 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
notify_anonymous_participants(event_id, activity) notify_anonymous_participants(event_id, activity)
end end
defp notify_participant(nil, _activity, _conversation_participant_actor_id, _author_id),
do: :ok
defp notify_participant(event_id, activity, conversation_participant_actor_id, author_id) do
# Anonymous participation
if conversation_participant_actor_id == Config.anonymous_actor_id() do
notify_anonymous_participants(event_id, activity)
else
[conversation_participant_actor_id]
|> users_from_actor_ids(author_id)
|> Enum.each(fn user ->
Notifier.Email.send_anonymous_activity(user.email, activity,
locale: Map.get(user, :locale, "en")
)
end)
end
end
defp notify_anonymous_participants(nil, _activity), do: :ok defp notify_anonymous_participants(nil, _activity), do: :ok
defp notify_anonymous_participants(event_id, activity) do defp notify_anonymous_participants(event_id, activity) do
@ -154,7 +173,7 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
|> Enum.map(fn %Participant{metadata: metadata} -> metadata end) |> Enum.map(fn %Participant{metadata: metadata} -> metadata end)
|> Enum.map(fn %{email: email} = metadata -> |> Enum.map(fn %{email: email} = metadata ->
Notifier.Email.send_anonymous_activity(email, activity, Notifier.Email.send_anonymous_activity(email, activity,
locale: Map.get(metadata, :locale, "en") locale: Map.get(metadata, :locale, "en") || "en"
) )
end) end)
end end