2021-11-10 16:36:32 +01:00
|
|
|
defmodule Mobilizon.Web.Email.Member do
|
|
|
|
@moduledoc """
|
|
|
|
Handles emails sent about group members.
|
|
|
|
"""
|
2022-04-05 12:16:22 +02:00
|
|
|
use Phoenix.Swoosh, view: Mobilizon.Web.EmailView
|
2021-11-10 16:36:32 +01:00
|
|
|
|
|
|
|
import Mobilizon.Web.Gettext
|
|
|
|
|
|
|
|
alias Mobilizon.{Actors, Users}
|
|
|
|
alias Mobilizon.Actors.{Actor, Member}
|
|
|
|
alias Mobilizon.Users.User
|
|
|
|
alias Mobilizon.Web.Email
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Send emails to local user
|
|
|
|
"""
|
|
|
|
@spec send_invite_to_user(Member.t()) :: :ok
|
|
|
|
def send_invite_to_user(%Member{actor: %Actor{user_id: nil}}), do: :ok
|
|
|
|
|
|
|
|
def send_invite_to_user(
|
|
|
|
%Member{actor: %Actor{user_id: user_id}, parent: %Actor{} = group, role: :invited} =
|
|
|
|
member
|
|
|
|
) do
|
|
|
|
with %User{email: email} = user <- Users.get_user!(user_id) do
|
|
|
|
locale = Map.get(user, :locale, "en")
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:group_invite, %{
|
|
|
|
locale: locale,
|
|
|
|
inviter: inviter,
|
|
|
|
group: group,
|
|
|
|
subject: subject
|
|
|
|
})
|
|
|
|
|> Email.Mailer.send_email()
|
2021-11-10 16:36:32 +01:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-12 15:42:52 +01:00
|
|
|
# Only send notification to local members
|
|
|
|
def send_notification_to_approved_member(%Member{actor: %Actor{user_id: nil}}), do: :ok
|
|
|
|
|
|
|
|
def send_notification_to_approved_member(%Member{
|
|
|
|
actor: %Actor{user_id: user_id},
|
|
|
|
parent: %Actor{} = group
|
|
|
|
}) do
|
|
|
|
with %User{email: email, locale: locale} <- Users.get_user!(user_id) do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"Your membership request for group %{group} has been approved",
|
|
|
|
group: Actor.display_name(group)
|
|
|
|
)
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:group_membership_approval, %{locale: locale, group: group, subject: subject})
|
|
|
|
|> Email.Mailer.send_email()
|
2021-11-12 15:42:52 +01:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-10 16:36:32 +01:00
|
|
|
# Only send notification to local members
|
|
|
|
def send_notification_to_removed_member(%Member{actor: %Actor{user_id: nil}}), do: :ok
|
|
|
|
|
2021-11-12 15:42:52 +01:00
|
|
|
# Member rejection
|
2021-11-10 16:36:32 +01:00
|
|
|
def send_notification_to_removed_member(%Member{
|
|
|
|
actor: %Actor{user_id: user_id},
|
|
|
|
parent: %Actor{} = group,
|
2021-11-12 15:42:52 +01:00
|
|
|
role: :not_approved
|
|
|
|
}) do
|
|
|
|
with %User{email: email, locale: locale} <- Users.get_user!(user_id) do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"Your membership request for group %{group} has been rejected",
|
|
|
|
group: Actor.display_name(group)
|
|
|
|
)
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:group_membership_rejection, %{
|
|
|
|
locale: locale,
|
|
|
|
group: group,
|
|
|
|
subject: subject
|
|
|
|
})
|
|
|
|
|> Email.Mailer.send_email()
|
2021-11-12 15:42:52 +01:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_notification_to_removed_member(%Member{
|
|
|
|
actor: %Actor{user_id: user_id},
|
|
|
|
parent: %Actor{} = group
|
2021-11-10 16:36:32 +01:00
|
|
|
}) 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}",
|
2021-11-12 15:42:52 +01:00
|
|
|
group: Actor.display_name(group)
|
2021-11-10 16:36:32 +01:00
|
|
|
)
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:group_member_removal, %{locale: locale, group: group, subject: subject})
|
|
|
|
|> Email.Mailer.send_email()
|
2021-11-10 16:36:32 +01:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO : def send_confirmation_to_inviter()
|
|
|
|
end
|