refactor: use dedicated email for event announcements
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -104,6 +104,7 @@ defmodule Mobilizon.Conversations do
|
||||
|> join(:inner, [_cp, _c, _e, _a, _lc, _oc, p], ap in Actor, on: p.actor_id == ap.id)
|
||||
|> where([_cp, c], c.event_id == ^event_id)
|
||||
|> where([cp], cp.actor_id == ^actor_id)
|
||||
|> order_by([cp], desc: cp.unread, desc: cp.updated_at)
|
||||
|> preload([_cp, c, e, a, lc, oc, p, ap],
|
||||
actor: a,
|
||||
conversation:
|
||||
|
||||
@@ -38,7 +38,7 @@ defmodule Mobilizon.Service.Activity.Conversation do
|
||||
%Conversation{
|
||||
id: conversation_id
|
||||
} = conversation,
|
||||
%Comment{actor_id: actor_id},
|
||||
%Comment{actor_id: actor_id, text: last_comment_text},
|
||||
_options
|
||||
)
|
||||
when subject in [
|
||||
@@ -50,27 +50,33 @@ defmodule Mobilizon.Service.Activity.Conversation do
|
||||
|
||||
conversation_id
|
||||
|> Conversations.list_conversation_participants_for_conversation()
|
||||
|> Enum.each(fn %ConversationParticipant{id: conversation_participant_id} =
|
||||
|> Enum.each(fn %ConversationParticipant{
|
||||
id: conversation_participant_id,
|
||||
actor_id: conversation_participant_actor_id
|
||||
} =
|
||||
conversation_participant ->
|
||||
LegacyNotifierBuilder.enqueue(
|
||||
:legacy_notify,
|
||||
%{
|
||||
"subject" => subject,
|
||||
"subject_params" =>
|
||||
Map.merge(
|
||||
%{
|
||||
conversation_id: conversation_id,
|
||||
conversation_participant_id: conversation_participant_id
|
||||
},
|
||||
event_subject_params(conversation)
|
||||
),
|
||||
"type" => :conversation,
|
||||
"object_type" => :conversation,
|
||||
"author_id" => actor_id,
|
||||
"object_id" => to_string(conversation_id),
|
||||
"participant" => Map.take(conversation_participant, [:id, :actor_id])
|
||||
}
|
||||
)
|
||||
if actor_id != conversation_participant_actor_id do
|
||||
LegacyNotifierBuilder.enqueue(
|
||||
:legacy_notify,
|
||||
%{
|
||||
"subject" => subject,
|
||||
"subject_params" =>
|
||||
Map.merge(
|
||||
%{
|
||||
conversation_id: conversation_id,
|
||||
conversation_participant_id: conversation_participant_id,
|
||||
conversation_text: last_comment_text
|
||||
},
|
||||
event_subject_params(conversation)
|
||||
),
|
||||
"type" => :conversation,
|
||||
"object_type" => :conversation,
|
||||
"author_id" => actor_id,
|
||||
"object_id" => to_string(conversation_id),
|
||||
"participant" => Map.take(conversation_participant, [:id, :actor_id])
|
||||
}
|
||||
)
|
||||
end
|
||||
end)
|
||||
|
||||
{:ok, :enqueued}
|
||||
|
||||
@@ -13,25 +13,35 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
|
||||
use Mobilizon.Service.Workers.Helper, queue: "activity"
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(%Job{args: args}) do
|
||||
def perform(%Job{args: %{"op" => "legacy_notify"} = args}) do
|
||||
{"legacy_notify", args} = Map.pop(args, "op")
|
||||
activity = build_activity(args)
|
||||
Logger.debug("Handling activity #{activity.subject} to notify in LegacyNotifierBuilder")
|
||||
|
||||
if args["subject"] == "participation_event_comment" do
|
||||
notify_anonymous_participants(get_in(args, ["subject_params", "event_id"]), activity)
|
||||
if args["subject"] in ["participation_event_comment", "conversation_created"] do
|
||||
special_handling(args["subject"], args, activity)
|
||||
:ok
|
||||
else
|
||||
args
|
||||
|> users_to_notify(author_id: args["author_id"], group_id: Map.get(args, "group_id"))
|
||||
|> Enum.each(¬ify_user(&1, activity))
|
||||
end
|
||||
end
|
||||
|
||||
if args["subject"] == "conversation_created" do
|
||||
notify_anonymous_participants(
|
||||
get_in(args, ["subject_params", "conversation_event_id"]),
|
||||
activity
|
||||
)
|
||||
end
|
||||
defp special_handling("participation_event_comment", args, activity) do
|
||||
notify_participants(
|
||||
get_in(args, ["subject_params", "event_id"]),
|
||||
activity,
|
||||
args["author_id"]
|
||||
)
|
||||
end
|
||||
|
||||
args
|
||||
|> users_to_notify(author_id: args["author_id"], group_id: Map.get(args, "group_id"))
|
||||
|> Enum.each(¬ify_user(&1, activity))
|
||||
defp special_handling("conversation_created", args, activity) do
|
||||
notify_participants(
|
||||
get_in(args, ["subject_params", "conversation_event_id"]),
|
||||
activity,
|
||||
args["author_id"]
|
||||
)
|
||||
end
|
||||
|
||||
defp build_activity(args) do
|
||||
@@ -117,6 +127,22 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
|
||||
|> Enum.map(&Users.get_user_with_activity_settings!/1)
|
||||
end
|
||||
|
||||
defp notify_participants(nil, _activity, _author_id), do: :ok
|
||||
|
||||
defp notify_participants(event_id, activity, author_id) do
|
||||
event_id
|
||||
|> Events.list_actors_participants_for_event()
|
||||
|> Enum.map(& &1.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)
|
||||
|
||||
notify_anonymous_participants(event_id, activity)
|
||||
end
|
||||
|
||||
defp notify_anonymous_participants(nil, _activity), do: :ok
|
||||
|
||||
defp notify_anonymous_participants(event_id, activity) do
|
||||
|
||||
@@ -46,6 +46,7 @@ defmodule Mobilizon.Web.Email.Activity do
|
||||
options
|
||||
) do
|
||||
locale = Keyword.get(options, :locale, "en")
|
||||
Gettext.put_locale(locale)
|
||||
|
||||
subject =
|
||||
dgettext(
|
||||
@@ -54,8 +55,6 @@ defmodule Mobilizon.Web.Email.Activity do
|
||||
event: subject_params["conversation_event_title"]
|
||||
)
|
||||
|
||||
conversation = Mobilizon.Conversations.get_conversation(activity.object_id)
|
||||
|
||||
Logger.debug("Going to send anonymous activity of type #{activity.type} to #{email}")
|
||||
|
||||
[to: email, subject: subject]
|
||||
@@ -63,15 +62,13 @@ defmodule Mobilizon.Web.Email.Activity do
|
||||
|> render_body(:email_anonymous_activity, %{
|
||||
subject: subject,
|
||||
activity: activity,
|
||||
locale: locale,
|
||||
extra: %{
|
||||
"conversation" => conversation
|
||||
}
|
||||
locale: locale
|
||||
})
|
||||
end
|
||||
|
||||
def anonymous_activity(email, %Activity{subject_params: subject_params} = activity, options) do
|
||||
locale = Keyword.get(options, :locale, "en")
|
||||
Gettext.put_locale(locale)
|
||||
|
||||
subject =
|
||||
dgettext(
|
||||
|
||||
@@ -113,9 +113,7 @@
|
||||
event:
|
||||
"<a href=\"#{Routes.page_url(Mobilizon.Web.Endpoint,
|
||||
:event,
|
||||
@activity.subject_params["conversation_event_uuid"]) |> URI.decode()}\">
|
||||
#{escape_html(@activity.subject_params["conversation_event_title"])}
|
||||
</a>"
|
||||
@activity.subject_params["conversation_event_uuid"]) |> URI.decode()}\">#{escape_html(@activity.subject_params["conversation_event_title"])}</a>"
|
||||
}
|
||||
)
|
||||
|> raw %>
|
||||
@@ -137,7 +135,7 @@
|
||||
<tr>
|
||||
<td align="center">
|
||||
<blockquote style="border-left-width: 0.25rem;border-left-color: #e2e8f0;border-left-style: solid;padding-left: 1em;margin: 0;text-align: start;color: #474467;font-family: 'Roboto', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400;line-height: 25px;">
|
||||
<%= @extra["conversation"].last_comment.text
|
||||
<%= @activity.subject_params["conversation_text"]
|
||||
|> sanitize_to_basic_html()
|
||||
|> raw() %>
|
||||
</blockquote>
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
--
|
||||
|
||||
<%= @extra["conversation"].last_comment.text |> html_to_text() |> mail_quote() %>
|
||||
<%= @activity.subject_params["conversation_text"] |> html_to_text() |> mail_quote() %>
|
||||
|
||||
--
|
||||
|
||||
|
||||
Reference in New Issue
Block a user