Send announcement notifications to anonymous participants

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-06-27 18:16:34 +02:00
parent 6007a27345
commit 5cdac95a85
51 changed files with 1172 additions and 595 deletions

View File

@@ -1611,6 +1611,19 @@ defmodule Mobilizon.Events do
|> select([p, a, u, s], {p, a, u, s})
end
@doc """
List anonymous participations to an event
"""
@spec list_anonymous_participants_for_event(String.t()) :: [Participant.t()]
def list_anonymous_participants_for_event(event_id) do
Participant
|> where(
[p],
p.event_id == ^event_id and p.role not in [^:not_approved, ^:not_confirmed, ^:rejected]
)
|> Repo.all()
end
@spec list_participations_for_user_query(integer()) :: Ecto.Query.t()
defp list_participations_for_user_query(user_id) do
from(

View File

@@ -51,6 +51,18 @@ defmodule Mobilizon.Service.Notifier.Email do
end
end
@doc """
Send an anonymous activity directly to an email, for anonymous participants for instance
"""
@spec send_anonymous_activity(String.t(), Activity.t(), Keyword.t()) :: {:ok, :sent}
def send_anonymous_activity(email, %Activity{} = activity, options) do
email
|> EmailActivity.anonymous_activity(activity, options)
|> Mailer.send_email()
{:ok, :sent}
end
# These notifications are using LegacyNotifierBuilder and don't have any history,
# so we always send them directly, as long as the setting isn't none
@always_direct_subjects [

View File

@@ -6,7 +6,7 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
alias Mobilizon.Activities.Activity
alias Mobilizon.{Actors, Events, Users}
alias Mobilizon.Actors.Actor
alias Mobilizon.Events.Event
alias Mobilizon.Events.{Event, Participant}
alias Mobilizon.Service.Notifier
use Mobilizon.Service.Workers.Helper, queue: "activity"
@@ -16,6 +16,10 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
with {"legacy_notify", args} <- Map.pop(args, "op") do
activity = build_activity(args)
if args["subject"] == "participation_event_comment" do
notify_anonymous_participants(get_in(args, ["subject_params", "event_id"]), activity)
end
args
|> users_to_notify(author_id: args["author_id"], group_id: Map.get(args, "group_id"))
|> Enum.each(&Notifier.notify(&1, activity, single_activity: true))
@@ -91,4 +95,20 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
|> Enum.uniq()
|> Enum.map(&Users.get_user_with_settings!/1)
end
defp notify_anonymous_participants(nil, _activity), do: :ok
defp notify_anonymous_participants(event_id, activity) do
event_id
|> Events.list_anonymous_participants_for_event()
|> Enum.filter(fn %Participant{metadata: metadata} ->
is_map(metadata) && is_binary(metadata.email)
end)
|> Enum.map(fn %Participant{metadata: metadata} -> metadata end)
|> Enum.map(fn metadata ->
Notifier.Email.send_anonymous_activity(metadata.email, activity,
locale: Map.get(metadata, :locale, "en")
)
end)
end
end

View File

@@ -38,6 +38,24 @@ defmodule Mobilizon.Web.Email.Activity do
|> render(:email_direct_activity)
end
@spec anonymous_activity(String.t(), Activity.t(), Keyword.t()) :: Bamboo.Email.t()
def anonymous_activity(email, %Activity{subject_params: subject_params} = activity, options) do
locale = Keyword.get(options, :locale, "en")
subject =
dgettext(
"activity",
"Announcement for your event %{event}",
event: subject_params["event_title"]
)
Email.base_email(to: email, subject: subject)
|> assign(:subject, subject)
|> assign(:activity, activity)
|> assign(:locale, locale)
|> render(:email_anonymous_activity)
end
@spec chunk_activities(list()) :: map()
defp chunk_activities(activities) do
activities

View File

@@ -0,0 +1,83 @@
<!-- HERO -->
<tr>
<td bgcolor="#474467" align="center" style="padding: 0px 10px 0px 10px;">
<!--[if (gte mso 9)|(IE)]>
<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
<tr>
<td align="center" valign="top" width="600">
<![endif]-->
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;" >
<tr>
<td bgcolor="#ffffff" align="center" valign="top" style="padding: 40px 20px 20px 20px; border-radius: 4px 4px 0px 0px; color: #3A384C; font-family: 'Roboto', Helvetica, Arial, sans-serif; font-size: 48px; font-weight: 400; line-height: 48px;">
<h1 style="font-size: 48px; font-weight: 400; margin: 0;">
<%= @subject %>
</h1>
</td>
</tr>
</table>
<!--[if (gte mso 9)|(IE)]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
<!-- COPY BLOCK -->
<tr>
<td bgcolor="#E6E4F4" align="center" style="padding: 0px 10px 0px 10px;">
<!--[if (gte mso 9)|(IE)]>
<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
<tr>
<td align="center" valign="top" width="600">
<![endif]-->
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;" >
<!-- COPY -->
<tr>
<td bgcolor="#ffffff" align="left">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" style="border-radius: 3px; text-align: left; padding: 10px 5% 0px 30px; color: #474467; font-family: 'Roboto', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400;line-height: 25px;" >
<%=
dgettext("activity", "%{profile} has posted an announcement under event %{event}.",
%{
profile: "<b>#{Mobilizon.Actors.Actor.display_name_and_username(@activity.author)}</b>",
event: "<a href=\"#{
page_url(
Mobilizon.Web.Endpoint,
:event,
@activity.subject_params["event_uuid"]
) |> URI.decode()}\">
#{@activity.subject_params["event_title"]}
</a>"
}
) |> raw %>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="#ffffff" align="left">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#ffffff" align="center" style="padding: 20px 30px 60px 30px;">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" style="border-radius: 3px;" bgcolor="#3C376E"><a href="<%= page_url(Mobilizon.Web.Endpoint, :event, @activity.subject_params["event_uuid"]) %>" target="_blank" style="font-size: 20px; font-family: Helvetica, Arial, sans-serif; color: #ffffff; text-decoration: none; padding: 15px 25px; border-radius: 2px; border: 1px solid #3C376E; display: inline-block;">
<%= gettext "Visit event page" %>
</a></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!--[if (gte mso 9)|(IE)]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>

View File

@@ -0,0 +1,11 @@
<%= @subject %>
==
<%= dgettext("activity", "%{profile} has posted an announcement under event %{event}.",
%{
profile: Mobilizon.Actors.Actor.display_name_and_username(@activity.author),
event: @activity.subject_params["event_title"]
}
) %>
<%= page_url(Mobilizon.Web.Endpoint, :event, @activity.subject_params["event_uuid"]) |> URI.decode() %>