Skip to content

Commit

Permalink
queue emails
Browse files Browse the repository at this point in the history
  • Loading branch information
liltechnomancer committed May 25, 2024
1 parent 1cfcaa9 commit 51a1ab1
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 5 deletions.
5 changes: 5 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ config :logger, :console,
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

config :phoenix_00, Oban,
engine: Oban.Engines.Lite,
queues: [default: 10, mailers: 20],
repo: Phoenix00.Repo

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs"
2 changes: 2 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ config :phoenix, :plug_init_mode, :runtime
config :phoenix_live_view,
# Enable helpful, but potentially expensive runtime checks
enable_expensive_runtime_checks: true

config :phoenix_00, Oban, testing: :inline
8 changes: 8 additions & 0 deletions lib/mail_man.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ defmodule Phoenix00.MailMan do
|> put_provider_option(:configuration_set_name, "default")
end

def enqueue_worker(email) do
email_map = Phoenix00.Mailer.to_map(email)

%{email: email_map}
|> Phoenix00.Workers.SendEmail.new()
|> Oban.insert()
end

# defp render_markdown(markdown) do
# %{html: render_markdown_to_html(markdown)}
# end
Expand Down
1 change: 1 addition & 0 deletions lib/phoenix_00/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Phoenix00.Application do
children = [
Phoenix00Web.Telemetry,
Phoenix00.Repo,
{Oban, Application.fetch_env!(:phoenix_00, Oban)},
{DNSCluster, query: Application.get_env(:phoenix_00, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: Phoenix00.PubSub},
# Start the Finch HTTP client for sending emails
Expand Down
46 changes: 46 additions & 0 deletions lib/phoenix_00/mailer.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
defmodule Phoenix00.Mailer do
use Swoosh.Mailer, otp_app: :phoenix_00

def to_map(%Swoosh.Email{} = email) do
%{
"to" => contact_to_map(email.to),
"from" => contact_to_map(email.from),
"subject" => email.subject,
"text_body" => email.text_body,
"html_body" => email.html_body
}
end

def from_map(args) do
%{
"to" => to,
"from" => from,
"subject" => subject,
"text_body" => text_body,
"html_body" => html_body
} = args

opts = [
to: map_to_contact(to),
from: map_to_contact(from),
subject: subject,
text_body: text_body,
html_body: html_body
]

Swoosh.Email.new(opts) |> Swoosh.Email.put_provider_option(:configuration_set_name, "default")
end

defp contact_to_map(info) when is_list(info) do
Enum.map(info, &contact_to_map/1)
end

defp contact_to_map({name, email}) do
%{"name" => name, "email" => email}
end

defp map_to_contact(info) when is_list(info) do
Enum.map(info, &map_to_contact/1)
end

defp map_to_contact(%{"name" => name, "email" => email}) do
{name, email}
end
end
2 changes: 1 addition & 1 deletion lib/phoenix_00/messages/email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Phoenix00.Messages.Email do
def changeset(email, attrs) do
email
|> cast(attrs, [:aws_message_id, :to, :from, :status, :email_id])
|> validate_required([:aws_message_id, :to, :from, :status, :email_id])
|> validate_required([:to, :from, :status])
|> unique_constraint(:email_id)
end
end
6 changes: 3 additions & 3 deletions lib/phoenix_00/messages/services/send_email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ defmodule Phoenix00.Messages.Services.SendEmail do
end

defp proccess_and_send_email(email_req) do
with {:ok, mail} <-
with {:ok, job} <-
email_req
|> Map.new(fn {k, v} -> {String.to_atom(k), v} end)
|> MailMan.letter()
|> Mailer.deliver() do
EmailRepo.create_email(merge_aws_with_email(mail, email_req))
|> MailMan.enqueue_worker() do
EmailRepo.create_email(Map.merge(email_req, %{"status" => "pending"}))
else
_ -> :error
end
Expand Down
1 change: 0 additions & 1 deletion lib/phoenix_00_web/live/user_settings_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ defmodule Phoenix00Web.UserSettingsLive do
<p class="text-center">
Before you can get started you will need an API key.
</p>
<p class="w-full text-center">You have existing API keys</p>
<.simple_form
for={@token_form}
class="float-right"
Expand Down
13 changes: 13 additions & 0 deletions lib/workers/send_email.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Phoenix00.Workers.SendEmail do
use Oban.Worker, queue: :default

alias Phoenix00.Mailer

@impl Oban.Worker
def perform(%Oban.Job{args: %{"email" => email_args}}) do
with email <- Mailer.from_map(email_args),
{:ok, _metadata} <- Mailer.deliver(email) do
:ok
end
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ defmodule Phoenix00.MixProject do
{:dns_cluster, "~> 0.1.1"},
{:bandit, "~> 1.2"},
{:ecto_sqlite3, "~> 0.15"},
{:oban, "~> 2.17"},
{:mdex, "~> 0.1"}
]
end
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"mint": {:hex, :mint, "1.6.0", "88a4f91cd690508a04ff1c3e28952f322528934be541844d54e0ceb765f01d5e", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "3c5ae85d90a5aca0a49c0d8b67360bbe407f3b54f1030a111047ff988e8fefaa"},
"nimble_options": {:hex, :nimble_options, "1.1.0", "3b31a57ede9cb1502071fade751ab0c7b8dbe75a9a4c2b5bbb0943a690b63172", [:mix], [], "hexpm", "8bbbb3941af3ca9acc7835f5655ea062111c9c27bcac53e004460dfd19008a99"},
"nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"},
"oban": {:hex, :oban, "2.17.10", "c3e5bd739b5c3fdc38eba1d43ab270a8c6ca4463bb779b7705c69400b0d87678", [:mix], [{:ecto_sql, "~> 3.10", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:ecto_sqlite3, "~> 0.9", [hex: :ecto_sqlite3, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.16", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4afd027b8e2bc3c399b54318b4f46ee8c40251fb55a285cb4e38b5363f0ee7c4"},
"parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"},
"phoenix": {:hex, :phoenix, "1.7.12", "1cc589e0eab99f593a8aa38ec45f15d25297dd6187ee801c8de8947090b5a9d3", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.7", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:websock_adapter, "~> 0.5.3", [hex: :websock_adapter, repo: "hexpm", optional: false]}], "hexpm", "d646192fbade9f485b01bc9920c139bfdd19d0f8df3d73fd8eaf2dfbe0d2837c"},
"phoenix_ecto": {:hex, :phoenix_ecto, "4.6.1", "96798325fab2fed5a824ca204e877b81f9afd2e480f581e81f7b4b64a5a477f2", [:mix], [{:ecto, "~> 3.5", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.1", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.17", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm", "0ae544ff99f3c482b0807c5cec2c8289e810ecacabc04959d82c3337f4703391"},
Expand Down
13 changes: 13 additions & 0 deletions priv/repo/migrations/20240524230521_add_oban_jobs_table.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Phoenix00.Repo.Migrations.AddObanJobsTable do
use Ecto.Migration

def up do
Oban.Migration.up(version: 12)
end

# We specify `version: 1` in `down`, ensuring that we'll roll all the way back down if
# necessary, regardless of which version we've migrated `up` to.
def down do
Oban.Migration.down(version: 1)
end
end

0 comments on commit 51a1ab1

Please sign in to comment.