~sbaildon/yemma

b8c0bd684cc10e9821970c712dca9f18d632ecee — Sean Baildon 8 months ago fec5865
require sender for mailers
M lib/yemma/config.ex => lib/yemma/config.ex +3 -1
@@ 23,7 23,9 @@ defmodule Yemma.Config do
            name: Yemma,
            notifier:
              {Yemma.Notifiers.NaiveMailer,
               mailer: Yemma.Mailer, builder: Yemma.Mail.UnbrandedBuilder},
               mailer: Yemma.Mailer,
               builder: Yemma.Mail.UnbrandedBuilder,
               sender: "notifications@yemma.test"},
            user: nil,
            token: nil,
            endpoint: nil

M lib/yemma/mail/builder.ex => lib/yemma/mail/builder.ex +8 -6
@@ 1,15 1,17 @@
defmodule Yemma.Mail.Builder do
  alias Swoosh.Email.Recipient

  @type user :: map()
  @type link :: String.t()

  @callback create_magic_link_email(user(), link()) :: Swoosh.Email.t()
  @callback create_update_email_instructions(user(), link()) :: Swoosh.Email.t()
  @callback create_magic_link_email(user(), Recipient.t(), link()) :: Swoosh.Email.t()
  @callback create_update_email_instructions(user(), Recipient.t(), link()) :: Swoosh.Email.t()

  def create_magic_link_email(builder, user, link) do
    builder.create_magic_link_email(user, link)
  def create_magic_link_email(builder, recipient, sender, link) do
    builder.create_magic_link_email(recipient, sender, link)
  end

  def create_update_email_instructions(builder, user, link) do
    builder.create_update_email_instructions(user, link)
  def create_update_email_instructions(builder, recipient, sender, link) do
    builder.create_update_email_instructions(recipient, sender, link)
  end
end

M lib/yemma/mail/unbranded_builder.ex => lib/yemma/mail/unbranded_builder.ex +6 -5
@@ 5,10 5,11 @@ defmodule Yemma.Mail.UnbrandedBuilder do
  alias Yemma.Mail.Builder

  @impl Builder
  def create_magic_link_email(%{email: email}, link) do
  def create_magic_link_email(recipient, sender, link) do
    base_email()
    |> subject("Sign in link")
    |> to(email)
    |> to(recipient)
    |> from(sender)
    |> text_body("""
      Sign in with your magic link



@@ 21,10 22,11 @@ defmodule Yemma.Mail.UnbrandedBuilder do
  end

  @impl Builder
  def create_update_email_instructions(%{email: email}, link) do
  def create_update_email_instructions(recipient, sender, link) do
    base_email()
    |> subject("Update email instructions")
    |> to(email)
    |> to(recipient)
    |> from(sender)
    |> text_body("""
    Update your email by visiting the link below:



@@ 41,6 43,5 @@ defmodule Yemma.Mail.UnbrandedBuilder do

  defp base_email do
    new()
    |> from("something@yemma.test")
  end
end

M lib/yemma/notifications/naive_mailer.ex => lib/yemma/notifications/naive_mailer.ex +26 -9
@@ 6,17 6,17 @@ defmodule Yemma.Notifiers.NaiveMailer do

  @impl Notifier
  def deliver_magic_link_instructions(recipient, link, opts) do
    {mailer, builder} = mailer_and_builder(opts)
    {mailer, builder, sender} = notifier_opts(opts)

    MailBuilder.create_magic_link_email(builder, recipient, link)
    MailBuilder.create_magic_link_email(builder, recipient.email, sender, link)
    |> deliver_with(mailer)
  end

  @impl Notifier
  def deliver_update_email_instructions(recipient, link, opts) do
    {mailer, builder} = mailer_and_builder(opts)
    {mailer, builder, sender} = notifier_opts(opts)

    MailBuilder.create_update_email_instructions(builder, recipient, link)
    MailBuilder.create_update_email_instructions(builder, recipient.email, sender, link)
    |> deliver_with(mailer)
  end



@@ 26,7 26,7 @@ defmodule Yemma.Notifiers.NaiveMailer do
    end
  end

  defp mailer_and_builder(opts) do
  defp notifier_opts(opts) do
    builder =
      opts[:builder] ||
        raise ArgumentError, """


@@ 35,7 35,9 @@ defmodule Yemma.Notifiers.NaiveMailer do
        config :my_app, Yemma,
          notifier:
            {Yemma.Notifiers.NaiveMailer,
             mailer: Yemma.Mailer, builder: Yemma.Mail.UnbrandedBuilder}
             mailer: Yemma.Mailer,
             builder: Yemma.Mail.UnbrandedBuilder,
             sender: {"Notifications", "notifications@yemma.test"}}
        """

    mailer =


@@ 45,10 47,25 @@ defmodule Yemma.Notifiers.NaiveMailer do

        config :my_app, Yemma,
          notifier:
            {Yemma.Notifiers.ObanMailer,
             mailer: Yemma.Mailer, builder: Yemma.Mail.UnbrandedBuilder}
            {Yemma.Notifiers.NaiveMailer,
             mailer: Yemma.Mailer,
             builder: Yemma.Mail.UnbrandedBuilder,
             sender: {"Notifications", "notifications@yemma.test"}}
        """

    sender =
      opts[:sender] ||
        raise ArgumentError, """
        #{__MODULE__} needs a :sender to send emails, eg.

        config :my_app, Yemma,
          notifier:
            {Yemma.Notifiers.NaiveMailer,
             mailer: Yemma.Mailer,
             builder: Yemma.Mail.UnbrandedBuilder,
             sender: {"Notifications", "notifications@yemma.test"}}
        """

    {mailer, builder}
    {mailer, builder, sender}
  end
end

M lib/yemma/notifications/oban_mailer.ex => lib/yemma/notifications/oban_mailer.ex +28 -11
@@ 42,10 42,10 @@ defmodule Yemma.Notifiers.ObanMailer do
    Keyword.get(opts, :oban, Oban)
  end

  def yemma_to_module(%{"yemma" => name}) when is_binary(name),
  defp yemma_to_module(%{"yemma" => name}) when is_binary(name),
    do: String.to_existing_atom(name)

  def yemma_to_module(%{"yemma" => name}) when is_atom(name),
  defp yemma_to_module(%{"yemma" => name}) when is_atom(name),
    do: name

  @impl Oban.Worker


@@ 56,8 56,8 @@ defmodule Yemma.Notifiers.ObanMailer do
    with name <- yemma_to_module(meta),
         conf <- Yemma.config(name),
         user <- Yemma.get_user!(conf.name, user_id),
         {mailer, builder} <- mailer_and_builder(conf.notifier) do
      MailBuilder.create_magic_link_email(builder, user, link)
         {mailer, builder, sender} <- notifier_opts(conf.notifier) do
      MailBuilder.create_magic_link_email(builder, user.email, sender, link)
      |> deliver_with(mailer)
    end
  end


@@ 70,8 70,8 @@ defmodule Yemma.Notifiers.ObanMailer do
    with name <- yemma_to_module(meta),
         conf <- Yemma.config(name),
         user <- Yemma.get_user!(conf.name, user_id),
         {mailer, builder} <- mailer_and_builder(conf.notifier) do
      MailBuilder.create_update_email_instructions(builder, user, link)
         {mailer, builder, sender} <- notifier_opts(conf.notifier) do
      MailBuilder.create_update_email_instructions(builder, user.email, sender, link)
      |> deliver_with(mailer)
    end
  end


@@ 82,7 82,7 @@ defmodule Yemma.Notifiers.ObanMailer do
    end
  end

  defp mailer_and_builder({_notifier, opts}) do
  defp notifier_opts({_notifer, opts}) do
    builder =
      opts[:builder] ||
        raise ArgumentError, """


@@ 90,8 90,10 @@ defmodule Yemma.Notifiers.ObanMailer do

        config :my_app, Yemma,
          notifier:
            {Yemma.Notifiers.NaiveMailer,
             mailer: Yemma.Mailer, builder: Yemma.Mail.UnbrandedBuilder}
            {Yemma.Notifiers.ObanMailer,
             mailer: Yemma.Mailer,
             builder: Yemma.Mail.UnbrandedBuilder,
             sender: {"Notifications", "notifications@yemma.test"}}
        """

    mailer =


@@ 102,9 104,24 @@ defmodule Yemma.Notifiers.ObanMailer do
        config :my_app, Yemma,
          notifier:
            {Yemma.Notifiers.ObanMailer,
             mailer: Yemma.Mailer, builder: Yemma.Mail.UnbrandedBuilder}
             mailer: Yemma.Mailer,
             builder: Yemma.Mail.UnbrandedBuilder,
             sender: {"Notifications", "notifications@yemma.test"}}
        """

    sender =
      opts[:sender] ||
        raise ArgumentError, """
        #{__MODULE__} needs a :sender to send emails, eg.

        config :my_app, Yemma,
          notifier:
            {Yemma.Notifiers.ObanMailer,
             mailer: Yemma.Mailer,
             builder: Yemma.Mail.UnbrandedBuilder,
             sender: {"Notifications", "notifications@yemma.test"}}
        """

    {mailer, builder}
    {mailer, builder, sender}
  end
end

M test/yemma/notifications/naive_mailer_test.exs => test/yemma/notifications/naive_mailer_test.exs +3 -1
@@ 10,7 10,9 @@ defmodule Yemma.Notifiers.NaiveMailerTest do
      yemma_config(
        notifier:
          {Yemma.Notifiers.NaiveMailer,
           mailer: Yemma.Mailer, builder: Yemma.Mail.UnbrandedBuilder}
           mailer: Yemma.Mailer,
           builder: Yemma.Mail.UnbrandedBuilder,
           sender: {"YemmaTest", "notifications@yemma.test"}}
      )

    %{conf: conf, user: user_fixture(conf)}

M test/yemma/notifications/oban_mailer_test.exs => test/yemma/notifications/oban_mailer_test.exs +5 -1
@@ 17,7 17,11 @@ defmodule Yemma.Notifiers.ObanMailerTest do
      start_supervised_yemma!(
        notifier:
          {Yemma.Notifiers.ObanMailer,
           oban: @name, mailer: Yemma.Mailer, builder: Yemma.Mail.UnbrandedBuilder, yemma: Yemma}
           oban: @name,
           mailer: Yemma.Mailer,
           builder: Yemma.Mail.UnbrandedBuilder,
           yemma: Yemma,
           sender: {"YemmaTest", "notifications@yemma.test"}}
      )

    %{conf: conf, user: user_fixture(conf)}