1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-22 16:03:25 +00:00
mobilizon/lib/web/email/follow.ex
Thomas Citharel a670a7d7a7
Fix and improve language handling
- Refactor plugs to detect and set language
- Translate ecto validation errors
- Use Gettext directly, not Mobilizon.Web.Gettext
- Set the language in the <html> attribute according to the one loaded
  on front-end

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2021-07-27 20:11:56 +02:00

71 lines
1.8 KiB
Elixir

defmodule Mobilizon.Web.Email.Follow do
@moduledoc """
Handles emails sent about (instance) follow.
"""
use Bamboo.Phoenix, view: Mobilizon.Web.EmailView
import Bamboo.Phoenix
import Mobilizon.Web.Gettext
alias Mobilizon.Users
alias Mobilizon.Actors.{Actor, Follower}
alias Mobilizon.Federation.ActivityPub.Relay
alias Mobilizon.Users.User
alias Mobilizon.Web.Email
@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,
actor: %Actor{} = follower,
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
Email.base_email(to: email, subject: subject)
|> assign(:locale, locale)
|> assign(:follower, follower)
|> assign(:subject, subject)
|> render(:instance_follow)
|> Email.Mailer.send_email_later()
:ok
end
defp send_notification_to_admin(_, _), do: :ok
end