2020-02-18 08:57:00 +01:00
|
|
|
defmodule Mobilizon.Web.Email.Group do
|
|
|
|
@moduledoc """
|
2021-11-10 16:36:32 +01:00
|
|
|
Handles emails sent about group changes.
|
2020-02-18 08:57:00 +01:00
|
|
|
"""
|
2022-04-05 12:16:22 +02:00
|
|
|
use Phoenix.Swoosh, view: Mobilizon.Web.EmailView
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
import Mobilizon.Web.Gettext
|
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
alias Mobilizon.{Actors, Config, Users}
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Actors.{Actor, Member}
|
2021-11-10 16:36:32 +01:00
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Users.{Setting, User}
|
2021-07-27 19:47:54 +02:00
|
|
|
alias Mobilizon.Web.Email
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2021-11-10 16:36:32 +01:00
|
|
|
@spec notify_of_new_event(Event.t()) :: :ok
|
|
|
|
def notify_of_new_event(%Event{attributed_to: %Actor{} = group} = event) do
|
|
|
|
# TODO: When we have events restricted to members, don't send emails to followers
|
|
|
|
group
|
|
|
|
|> Actors.list_actors_to_notify_from_group_event()
|
2022-05-10 13:14:45 +02:00
|
|
|
|> Enum.reduce([], fn actor, users ->
|
|
|
|
# No emails for remote actors
|
|
|
|
if is_nil(actor.user_id) do
|
|
|
|
users
|
|
|
|
else
|
|
|
|
users ++ [Users.get_user_with_activity_settings!(actor.user_id)]
|
|
|
|
end
|
|
|
|
end)
|
2021-11-10 16:36:32 +01:00
|
|
|
|> Enum.each(¬ify_follower(event, group, &1))
|
|
|
|
end
|
2020-08-14 11:32:23 +02:00
|
|
|
|
2021-11-10 16:36:32 +01:00
|
|
|
def notify_of_new_event(%Event{}), do: :ok
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2022-05-10 13:14:45 +02:00
|
|
|
defp notify_follower(%Event{} = event, %Actor{type: :Group} = group, %User{
|
|
|
|
email: email,
|
|
|
|
locale: locale,
|
|
|
|
settings: %Setting{timezone: timezone},
|
|
|
|
activity_settings: activity_settings,
|
|
|
|
default_actor_id: default_actor_id
|
2021-11-10 16:36:32 +01:00
|
|
|
}) do
|
2022-05-10 13:14:45 +02:00
|
|
|
if default_actor_id != event.organizer_actor_id &&
|
2021-11-10 16:36:32 +01:00
|
|
|
accepts_new_events_notifications(activity_settings) do
|
2020-08-14 11:32:23 +02:00
|
|
|
Gettext.put_locale(locale)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
2021-11-10 16:36:32 +01:00
|
|
|
"📅 Just scheduled by %{group}: %{event}",
|
|
|
|
group: Actor.display_name(group),
|
|
|
|
event: event.title
|
2020-08-14 11:32:23 +02:00
|
|
|
)
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:event_group_follower_notification, %{
|
|
|
|
locale: locale,
|
|
|
|
group: group,
|
|
|
|
event: event,
|
|
|
|
timezone: timezone,
|
|
|
|
subject: subject
|
|
|
|
})
|
|
|
|
|> Email.Mailer.send_email()
|
2020-08-14 11:32:23 +02:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-10 16:36:32 +01:00
|
|
|
@spec accepts_new_events_notifications(list()) :: boolean()
|
|
|
|
defp accepts_new_events_notifications(activity_settings) do
|
|
|
|
case Enum.find(activity_settings, &(&1.key == "event_created" && &1.method == "email")) do
|
|
|
|
nil -> false
|
|
|
|
%{enabled: enabled} -> enabled
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
# TODO : def send_confirmation_to_inviter()
|
2020-08-27 11:53:24 +02:00
|
|
|
|
|
|
|
@member_roles [:administrator, :moderator, :member]
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec send_group_suspension_notification(Member.t()) :: :ok
|
2020-08-27 11:53:24 +02:00
|
|
|
def send_group_suspension_notification(%Member{actor: %Actor{user_id: nil}}), do: :ok
|
|
|
|
|
|
|
|
def send_group_suspension_notification(%Member{role: role}) when role not in @member_roles,
|
|
|
|
do: :ok
|
|
|
|
|
|
|
|
def send_group_suspension_notification(%Member{
|
|
|
|
actor: %Actor{user_id: user_id},
|
|
|
|
parent: %Actor{domain: nil} = group,
|
|
|
|
role: member_role
|
|
|
|
}) do
|
|
|
|
with %User{email: email, locale: locale} <- Users.get_user!(user_id) do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"The group %{group} has been suspended on %{instance}",
|
|
|
|
group: group.name,
|
2021-09-30 09:34:39 +02:00
|
|
|
instance: Config.instance_name()
|
2020-08-27 11:53:24 +02:00
|
|
|
)
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:group_suspension, %{
|
|
|
|
locale: locale,
|
|
|
|
group: group,
|
|
|
|
role: member_role,
|
|
|
|
subject: subject
|
|
|
|
})
|
|
|
|
|> Email.Mailer.send_email()
|
2020-08-27 11:53:24 +02:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|