2020-02-18 07:57:00 +00:00
|
|
|
defmodule Mobilizon.Web.Email.Group do
|
|
|
|
@moduledoc """
|
|
|
|
Handles emails sent about participation.
|
|
|
|
"""
|
|
|
|
use Bamboo.Phoenix, view: Mobilizon.Web.EmailView
|
|
|
|
|
|
|
|
import Bamboo.Phoenix
|
|
|
|
import Mobilizon.Web.Gettext
|
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
alias Mobilizon.{Actors, Config, Users}
|
2020-02-18 07:57:00 +00:00
|
|
|
alias Mobilizon.Actors.{Actor, Member}
|
|
|
|
alias Mobilizon.Users.User
|
2021-07-27 17:47:54 +00:00
|
|
|
alias Mobilizon.Web.Email
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Send emails to local user
|
|
|
|
"""
|
2020-08-14 09:32:23 +00:00
|
|
|
@spec send_invite_to_user(Member.t(), String.t()) :: :ok
|
|
|
|
def send_invite_to_user(member, locale \\ "en")
|
|
|
|
def send_invite_to_user(%Member{actor: %Actor{user_id: nil}}, _locale), do: :ok
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
def send_invite_to_user(
|
|
|
|
%Member{actor: %Actor{user_id: user_id}, parent: %Actor{} = group, role: :invited} =
|
|
|
|
member,
|
2020-08-14 09:32:23 +00:00
|
|
|
locale
|
2020-02-18 07:57:00 +00:00
|
|
|
) do
|
2020-12-01 09:21:07 +00:00
|
|
|
with %User{email: email} = user <- Users.get_user!(user_id) do
|
|
|
|
locale = Map.get(user, :locale, locale)
|
2020-02-18 07:57:00 +00:00
|
|
|
Gettext.put_locale(locale)
|
|
|
|
%Actor{name: invited_by_name} = inviter = Actors.get_actor(member.invited_by_id)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"You have been invited by %{inviter} to join group %{group}",
|
|
|
|
inviter: invited_by_name,
|
|
|
|
group: group.name
|
|
|
|
)
|
|
|
|
|
|
|
|
Email.base_email(to: email, subject: subject)
|
|
|
|
|> assign(:locale, locale)
|
|
|
|
|> assign(:inviter, inviter)
|
|
|
|
|> assign(:group, group)
|
|
|
|
|> assign(:subject, subject)
|
|
|
|
|> render(:group_invite)
|
2021-04-20 13:02:24 +00:00
|
|
|
|> Email.Mailer.send_email_later()
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-14 09:32:23 +00:00
|
|
|
# Only send notification to local members
|
|
|
|
def send_notification_to_removed_member(%Member{actor: %Actor{user_id: nil}}), do: :ok
|
|
|
|
|
|
|
|
def send_notification_to_removed_member(%Member{
|
|
|
|
actor: %Actor{user_id: user_id},
|
|
|
|
parent: %Actor{} = group,
|
|
|
|
role: :rejected
|
|
|
|
}) do
|
|
|
|
with %User{email: email, locale: locale} <- Users.get_user!(user_id) do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"You have been removed from group %{group}",
|
|
|
|
group: group.name
|
|
|
|
)
|
|
|
|
|
|
|
|
Email.base_email(to: email, subject: subject)
|
|
|
|
|> assign(:locale, locale)
|
|
|
|
|> assign(:group, group)
|
|
|
|
|> assign(:subject, subject)
|
2020-08-27 09:53:24 +00:00
|
|
|
|> render(:group_member_removal)
|
2021-04-20 13:02:24 +00:00
|
|
|
|> Email.Mailer.send_email_later()
|
2020-08-14 09:32:23 +00:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
# TODO : def send_confirmation_to_inviter()
|
2020-08-27 09:53:24 +00:00
|
|
|
|
|
|
|
@member_roles [:administrator, :moderator, :member]
|
|
|
|
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)
|
|
|
|
instance = Config.instance_name()
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"The group %{group} has been suspended on %{instance}",
|
|
|
|
group: group.name,
|
|
|
|
instance: instance
|
|
|
|
)
|
|
|
|
|
|
|
|
Email.base_email(to: email, subject: subject)
|
|
|
|
|> assign(:locale, locale)
|
|
|
|
|> assign(:group, group)
|
|
|
|
|> assign(:role, member_role)
|
|
|
|
|> assign(:subject, subject)
|
|
|
|
|> assign(:instance, instance)
|
|
|
|
|> render(:group_suspension)
|
2021-04-20 13:02:24 +00:00
|
|
|
|> Email.Mailer.send_email_later()
|
2020-08-27 09:53:24 +00:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_group_deletion_notification(%Member{actor: %Actor{user_id: nil}}, _author), do: :ok
|
|
|
|
|
|
|
|
def send_group_deletion_notification(%Member{role: role}, _author)
|
|
|
|
when role not in @member_roles,
|
|
|
|
do: :ok
|
|
|
|
|
2021-09-10 09:36:05 +00:00
|
|
|
@spec send_group_deletion_notification(Member.t(), Actor.t()) :: :ok
|
2020-08-27 09:53:24 +00:00
|
|
|
def send_group_deletion_notification(
|
|
|
|
%Member{
|
2021-09-10 09:36:05 +00:00
|
|
|
actor: %Actor{user_id: user_id, id: actor_id} = member
|
2020-08-27 09:53:24 +00:00
|
|
|
},
|
|
|
|
%Actor{id: author_id} = author
|
|
|
|
) do
|
|
|
|
with %User{email: email, locale: locale} <- Users.get_user!(user_id),
|
|
|
|
{:member_not_author, true} <- {:member_not_author, author_id !== actor_id} do
|
2021-09-10 09:36:05 +00:00
|
|
|
do_send_group_deletion_notification(member, author: author, email: email, locale: locale)
|
2020-08-27 09:53:24 +00:00
|
|
|
else
|
|
|
|
# Skip if it's the author itself
|
|
|
|
{:member_not_author, _} ->
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
2021-09-10 09:36:05 +00:00
|
|
|
|
|
|
|
@spec send_group_deletion_notification(Member.t()) :: :ok
|
|
|
|
def send_group_deletion_notification(%Member{actor: %Actor{user_id: user_id}} = member) do
|
|
|
|
case Users.get_user!(user_id) do
|
|
|
|
%User{email: email, locale: locale} ->
|
|
|
|
do_send_group_deletion_notification(member, email: email, locale: locale)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_send_group_deletion_notification(
|
|
|
|
%Member{role: member_role, parent: %Actor{domain: nil} = group},
|
|
|
|
options
|
|
|
|
) do
|
|
|
|
locale = Keyword.get(options, :locale)
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
instance = Config.instance_name()
|
|
|
|
author = Keyword.get(options, :author)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"The group %{group} has been deleted on %{instance}",
|
|
|
|
group: group.name,
|
|
|
|
instance: instance
|
|
|
|
)
|
|
|
|
|
|
|
|
Email.base_email(to: Keyword.get(options, :email), subject: subject)
|
|
|
|
|> assign(:locale, locale)
|
|
|
|
|> assign(:group, group)
|
|
|
|
|> assign(:role, member_role)
|
|
|
|
|> assign(:subject, subject)
|
|
|
|
|> assign(:instance, instance)
|
|
|
|
|> assign(:author, author)
|
|
|
|
|> render(:group_deletion)
|
|
|
|
|> Email.Mailer.send_email_later()
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|