From f4b777c3d652fccd077d815822f8d6ed6501b66d Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 8 Oct 2020 08:53:25 +0200 Subject: [PATCH] Make sure event update notification email gets sent to anonymous participants Signed-off-by: Thomas Citharel --- lib/mobilizon/events/events.ex | 7 ++++-- lib/web/email/event.ex | 32 ++++++++++++++++++++------- test/graphql/resolvers/event_test.exs | 4 ++-- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/mobilizon/events/events.ex b/lib/mobilizon/events/events.ex index 1975f85a..9810acfa 100644 --- a/lib/mobilizon/events/events.ex +++ b/lib/mobilizon/events/events.ex @@ -1572,8 +1572,11 @@ defmodule Mobilizon.Events do Participant |> join(:inner, [p], a in Actor, on: p.actor_id == a.id and is_nil(a.domain)) |> join(:left, [_p, a], u in User, on: a.user_id == u.id) - |> where([p], p.event_id == ^event_id) - |> select([_p, a, u], {a, u}) + |> where( + [p], + p.event_id == ^event_id and p.role not in [^:not_approved, ^:not_confirmed, ^:rejected] + ) + |> select([p, a, u], {p, a, u}) end @spec list_participations_for_user_query(integer()) :: Ecto.Query.t() diff --git a/lib/web/email/event.ex b/lib/web/email/event.ex index 32336bf6..b4cd2e0e 100644 --- a/lib/web/email/event.ex +++ b/lib/web/email/event.ex @@ -11,25 +11,26 @@ defmodule Mobilizon.Web.Email.Event do alias Mobilizon.Actors.Actor alias Mobilizon.Events - alias Mobilizon.Events.Event + alias Mobilizon.Events.{Event, Participant} alias Mobilizon.Storage.Repo alias Mobilizon.Users.User - alias Mobilizon.Web.{Email, Gettext} + alias Mobilizon.Web.Email + alias Mobilizon.Web.Gettext, as: GettextBackend @important_changes [:title, :begins_on, :ends_on, :status, :physical_address] - @spec event_updated(User.t(), Actor.t(), Event.t(), Event.t(), MapSet.t(), String.t()) :: + @spec event_updated(String.t(), Actor.t(), Event.t(), Event.t(), MapSet.t(), String.t()) :: Bamboo.Email.t() def event_updated( - %User{} = user, + email, %Actor{} = actor, %Event{} = old_event, %Event{} = event, changes, locale \\ "en" ) do - Gettext.put_locale(locale) + GettextBackend.put_locale(locale) subject = gettext( @@ -37,7 +38,7 @@ defmodule Mobilizon.Web.Email.Event do title: old_event.title ) - Email.base_email(to: {Actor.display_name(actor), user.email}, subject: subject) + Email.base_email(to: {Actor.display_name(actor), email}, subject: subject) |> assign(:locale, locale) |> assign(:event, event) |> assign(:old_event, old_event) @@ -73,12 +74,27 @@ defmodule Mobilizon.Web.Email.Event do end defp send_notification_for_event_update_to_participant( - {%Actor{} = actor, %User{locale: locale} = user}, + {%Participant{} = _participant, %Actor{} = actor, + %User{locale: locale, email: email} = _user}, %Event{} = old_event, %Event{} = event, diff ) do - user + email + |> Email.Event.event_updated(actor, old_event, event, diff, locale) + |> Email.Mailer.deliver_later() + end + + defp send_notification_for_event_update_to_participant( + {%Participant{metadata: %{email: email}} = _participant, %Actor{} = actor, nil}, + %Event{} = old_event, + %Event{} = event, + diff + ) + when not is_nil(email) do + locale = Gettext.get_locale() + + email |> Email.Event.event_updated(actor, old_event, event, diff, locale) |> Email.Mailer.deliver_later() end diff --git a/test/graphql/resolvers/event_test.exs b/test/graphql/resolvers/event_test.exs index 70e04cc7..d7299de1 100644 --- a/test/graphql/resolvers/event_test.exs +++ b/test/graphql/resolvers/event_test.exs @@ -837,7 +837,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do assert_delivered_email( Email.Event.event_updated( - user, + user.email, actor, event, new_event, @@ -847,7 +847,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do assert_delivered_email( Email.Event.event_updated( - participant_user, + participant_user.email, participant_actor, event, new_event,