2020-10-06 09:54:07 +02:00
|
|
|
defmodule Mobilizon.Web.Email.Follow do
|
|
|
|
@moduledoc """
|
|
|
|
Handles emails sent about (instance) follow.
|
|
|
|
"""
|
2022-04-05 12:16:22 +02:00
|
|
|
use Phoenix.Swoosh, view: Mobilizon.Web.EmailView
|
2020-10-06 09:54:07 +02:00
|
|
|
|
|
|
|
import Mobilizon.Web.Gettext
|
|
|
|
|
|
|
|
alias Mobilizon.Users
|
|
|
|
alias Mobilizon.Actors.{Actor, Follower}
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Relay
|
|
|
|
alias Mobilizon.Users.User
|
2021-07-27 19:47:54 +02:00
|
|
|
alias Mobilizon.Web.Email
|
2020-10-06 09:54:07 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Send follow notification to admins if the followed actor
|
|
|
|
"""
|
|
|
|
@spec send_notification_to_admins(Follower.t()) :: :ok
|
|
|
|
def send_notification_to_admins(
|
|
|
|
%Follower{
|
|
|
|
# approved: false,
|
2022-01-17 17:50:00 +01:00
|
|
|
actor: %Actor{type: :Application} = follower,
|
2020-10-06 09:54:07 +02:00
|
|
|
target_actor: %Actor{id: target_actor_id}
|
|
|
|
} = _follow
|
|
|
|
) do
|
|
|
|
relay_actor = Relay.get_actor()
|
|
|
|
|
|
|
|
if relay_actor.id == target_actor_id do
|
|
|
|
Enum.each(Users.list_admins(), fn admin ->
|
|
|
|
send_notification_to_admin(admin, follower)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_notification_to_admins(_), do: :ok
|
|
|
|
|
|
|
|
defp send_notification_to_admin(
|
|
|
|
%User{email: email, locale: locale},
|
|
|
|
%Actor{type: follower_type} = follower
|
|
|
|
) do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
if follower_type == :Application do
|
|
|
|
gettext(
|
|
|
|
"Instance %{name} (%{domain}) requests to follow your instance",
|
|
|
|
name: follower.name,
|
|
|
|
domain: follower.domain
|
|
|
|
)
|
|
|
|
else
|
|
|
|
gettext(
|
|
|
|
"%{name} requests to follow your instance",
|
|
|
|
name: Actor.display_name_and_username(follower)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:instance_follow, %{locale: locale, follower: follower, subject: subject})
|
|
|
|
|> Email.Mailer.send_email()
|
2020-10-06 09:54:07 +02:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
defp send_notification_to_admin(_, _), do: :ok
|
|
|
|
end
|