From c23c00e1c8ba59ec329c303cd0510c5dde31a534 Mon Sep 17 00:00:00 2001 From: miffigriffi Date: Tue, 17 Sep 2019 02:45:32 +0200 Subject: [PATCH] Move Email context under MobilizonWeb --- config/dev.exs | 2 +- config/prod.exs | 2 +- config/test.exs | 2 +- lib/mobilizon/email/email.ex | 17 ----------------- .../email/admin.ex | 8 +++++--- lib/mobilizon_web/email/email.ex | 17 +++++++++++++++++ .../email/mailer.ex | 2 +- lib/{mobilizon => mobilizon_web}/email/user.ex | 8 +++++--- lib/mobilizon_web/views/email_view.ex | 2 +- lib/service/activity_pub/utils.ex | 6 +++--- lib/service/users/activation.ex | 8 ++++---- lib/service/users/reset_password.ex | 8 ++++---- mix.exs | 6 +++--- .../resolvers/user_resolver_test.exs | 18 +++++++++++------- 14 files changed, 57 insertions(+), 49 deletions(-) delete mode 100644 lib/mobilizon/email/email.ex rename lib/{mobilizon => mobilizon_web}/email/admin.ex (84%) create mode 100644 lib/mobilizon_web/email/email.ex rename lib/{mobilizon => mobilizon_web}/email/mailer.ex (69%) rename lib/{mobilizon => mobilizon_web}/email/user.ex (91%) diff --git a/config/dev.exs b/config/dev.exs index c7345d110..ae2ab7e5e 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -61,7 +61,7 @@ config :phoenix, :stacktrace_depth, 20 # Initialize plugs at runtime for faster development compilation config :phoenix, :plug_init_mode, :runtime -config :mobilizon, Mobilizon.Email.Mailer, adapter: Bamboo.LocalAdapter +config :mobilizon, MobilizonWeb.Email.Mailer, adapter: Bamboo.LocalAdapter # Configure your database config :mobilizon, Mobilizon.Storage.Repo, diff --git a/config/prod.exs b/config/prod.exs index d580935ca..334790b5e 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -21,7 +21,7 @@ config :mobilizon, Mobilizon.Storage.Repo, port: System.get_env("MOBILIZON_DATABASE_PORT") || "5432", pool_size: 15 -config :mobilizon, Mobilizon.Email.Mailer, +config :mobilizon, MobilizonWeb.Email.Mailer, adapter: Bamboo.SMTPAdapter, server: "localhost", hostname: "localhost", diff --git a/config/test.exs b/config/test.exs index 8d174e40b..c72f44b4d 100644 --- a/config/test.exs +++ b/config/test.exs @@ -30,7 +30,7 @@ config :mobilizon, Mobilizon.Storage.Repo, hostname: System.get_env("MOBILIZON_DATABASE_HOST") || "localhost", pool: Ecto.Adapters.SQL.Sandbox -config :mobilizon, Mobilizon.Email.Mailer, adapter: Bamboo.TestAdapter +config :mobilizon, MobilizonWeb.Email.Mailer, adapter: Bamboo.TestAdapter config :mobilizon, MobilizonWeb.Upload, filters: [], link_name: false diff --git a/lib/mobilizon/email/email.ex b/lib/mobilizon/email/email.ex deleted file mode 100644 index 0a849583c..000000000 --- a/lib/mobilizon/email/email.ex +++ /dev/null @@ -1,17 +0,0 @@ -defmodule Mobilizon.Email do - @moduledoc """ - The Email context. - """ - - use Bamboo.Phoenix, view: Mobilizon.EmailView - - alias Mobilizon.Config - - @spec base_email :: Bamboo.Email.t() - def base_email do - new_email() - |> from(Config.instance_email_from()) - |> put_html_layout({Mobilizon.EmailView, "email.html"}) - |> put_text_layout({Mobilizon.EmailView, "email.text"}) - end -end diff --git a/lib/mobilizon/email/admin.ex b/lib/mobilizon_web/email/admin.ex similarity index 84% rename from lib/mobilizon/email/admin.ex rename to lib/mobilizon_web/email/admin.ex index 5a2f78cf7..b50059c9d 100644 --- a/lib/mobilizon/email/admin.ex +++ b/lib/mobilizon_web/email/admin.ex @@ -1,18 +1,20 @@ -defmodule Mobilizon.Email.Admin do +defmodule MobilizonWeb.Email.Admin do @moduledoc """ Handles emails sent to admins. """ - use Bamboo.Phoenix, view: Mobilizon.EmailView + use Bamboo.Phoenix, view: MobilizonWeb.EmailView import Bamboo.{Email, Phoenix} import MobilizonWeb.Gettext - alias Mobilizon.{Config, Email} + alias Mobilizon.Config alias Mobilizon.Reports.Report alias Mobilizon.Users.User + alias MobilizonWeb.Email + @spec report(User.t(), Report.t(), String.t()) :: Bamboo.Email.t() def report(%User{email: email}, %Report{} = report, locale \\ "en") do Gettext.put_locale(locale) diff --git a/lib/mobilizon_web/email/email.ex b/lib/mobilizon_web/email/email.ex new file mode 100644 index 000000000..7277f6f56 --- /dev/null +++ b/lib/mobilizon_web/email/email.ex @@ -0,0 +1,17 @@ +defmodule MobilizonWeb.Email do + @moduledoc """ + The Email context. + """ + + use Bamboo.Phoenix, view: MobilizonWeb.EmailView + + alias Mobilizon.Config + + @spec base_email :: Bamboo.Email.t() + def base_email do + new_email() + |> from(Config.instance_email_from()) + |> put_html_layout({MobilizonWeb.EmailView, "email.html"}) + |> put_text_layout({MobilizonWeb.EmailView, "email.text"}) + end +end diff --git a/lib/mobilizon/email/mailer.ex b/lib/mobilizon_web/email/mailer.ex similarity index 69% rename from lib/mobilizon/email/mailer.ex rename to lib/mobilizon_web/email/mailer.ex index 027647609..246d20d05 100644 --- a/lib/mobilizon/email/mailer.ex +++ b/lib/mobilizon_web/email/mailer.ex @@ -1,4 +1,4 @@ -defmodule Mobilizon.Email.Mailer do +defmodule MobilizonWeb.Email.Mailer do @moduledoc """ Mobilizon Mailer. """ diff --git a/lib/mobilizon/email/user.ex b/lib/mobilizon_web/email/user.ex similarity index 91% rename from lib/mobilizon/email/user.ex rename to lib/mobilizon_web/email/user.ex index a05778254..c4895be2a 100644 --- a/lib/mobilizon/email/user.ex +++ b/lib/mobilizon_web/email/user.ex @@ -1,17 +1,19 @@ -defmodule Mobilizon.Email.User do +defmodule MobilizonWeb.Email.User do @moduledoc """ Handles emails sent to users. """ - use Bamboo.Phoenix, view: Mobilizon.EmailView + use Bamboo.Phoenix, view: MobilizonWeb.EmailView import Bamboo.{Email, Phoenix} import MobilizonWeb.Gettext - alias Mobilizon.{Config, Email} + alias Mobilizon.Config alias Mobilizon.Users.User + alias MobilizonWeb.Email + @spec confirmation_email(User.t(), String.t()) :: Bamboo.Email.t() def confirmation_email( %User{email: email, confirmation_token: confirmation_token}, diff --git a/lib/mobilizon_web/views/email_view.ex b/lib/mobilizon_web/views/email_view.ex index 7c2ecb679..d34589acd 100644 --- a/lib/mobilizon_web/views/email_view.ex +++ b/lib/mobilizon_web/views/email_view.ex @@ -1,3 +1,3 @@ -defmodule Mobilizon.EmailView do +defmodule MobilizonWeb.EmailView do use MobilizonWeb, :view end diff --git a/lib/service/activity_pub/utils.ex b/lib/service/activity_pub/utils.ex index ff7655231..49e1da753 100644 --- a/lib/service/activity_pub/utils.ex +++ b/lib/service/activity_pub/utils.ex @@ -25,8 +25,8 @@ defmodule Mobilizon.Service.ActivityPub.Utils do alias Mobilizon.Storage.Repo alias Mobilizon.Users + alias MobilizonWeb.{Email, Endpoint} alias MobilizonWeb.Router.Helpers, as: Routes - alias MobilizonWeb.Endpoint require Logger @@ -165,8 +165,8 @@ defmodule Mobilizon.Service.ActivityPub.Utils do {:ok, %Report{} = report} <- Reports.create_report(data) do Enum.each(Users.list_moderators(), fn moderator -> moderator - |> Mobilizon.Email.Admin.report(moderator, report) - |> Mobilizon.Email.Mailer.deliver_later() + |> Email.Admin.report(moderator, report) + |> Email.Mailer.deliver_later() end) {:ok, report} diff --git a/lib/service/users/activation.ex b/lib/service/users/activation.ex index a111c5698..1d03da1a2 100644 --- a/lib/service/users/activation.ex +++ b/lib/service/users/activation.ex @@ -1,12 +1,12 @@ defmodule Mobilizon.Service.Users.Activation do @moduledoc false - alias Mobilizon.Email.Mailer - alias Mobilizon.Email.User, as: UserEmail alias Mobilizon.Users alias Mobilizon.Users.User alias Mobilizon.Service.Users.Tools + alias MobilizonWeb.Email + require Logger @doc false @@ -40,7 +40,7 @@ defmodule Mobilizon.Service.Users.Activation do def send_confirmation_email(%User{} = user, locale \\ "en") do user - |> UserEmail.confirmation_email(locale) - |> Mailer.deliver_later() + |> Email.User.confirmation_email(locale) + |> Email.Mailer.deliver_later() end end diff --git a/lib/service/users/reset_password.ex b/lib/service/users/reset_password.ex index dd6341877..1fa744f54 100644 --- a/lib/service/users/reset_password.ex +++ b/lib/service/users/reset_password.ex @@ -1,13 +1,13 @@ defmodule Mobilizon.Service.Users.ResetPassword do @moduledoc false - alias Mobilizon.Email.Mailer - alias Mobilizon.Email.User, as: UserEmail alias Mobilizon.Service.Users.Tools alias Mobilizon.Storage.Repo alias Mobilizon.Users alias Mobilizon.Users.User + alias MobilizonWeb.Email + require Logger @doc """ @@ -51,8 +51,8 @@ defmodule Mobilizon.Service.Users.ResetPassword do ) do mail = user_updated - |> UserEmail.reset_password_email(locale) - |> Mailer.deliver_later() + |> Email.User.reset_password_email(locale) + |> Email.Mailer.deliver_later() {:ok, mail} else diff --git a/mix.exs b/mix.exs index 34fd71674..920da69ea 100644 --- a/mix.exs +++ b/mix.exs @@ -315,9 +315,9 @@ defmodule Mobilizon.Mixfile do Tools: [ Mobilizon.Application, Mobilizon.Factory, - Mobilizon.Email.Mailer, - Mobilizon.EmailView, - Mobilizon.Email.User + MobilizonWeb.Email.Mailer, + MobilizonWeb.Email.User, + MobilizonWeb.EmailView ] ] end diff --git a/test/mobilizon_web/resolvers/user_resolver_test.exs b/test/mobilizon_web/resolvers/user_resolver_test.exs index 52e37c6e9..5f483e9b8 100644 --- a/test/mobilizon_web/resolvers/user_resolver_test.exs +++ b/test/mobilizon_web/resolvers/user_resolver_test.exs @@ -1,15 +1,19 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do use MobilizonWeb.ConnCase - alias Mobilizon.{Actors, Config, Users} - alias Mobilizon.Actors.Actor - alias Mobilizon.Users.User - alias Mobilizon.Users - alias MobilizonWeb.AbsintheHelpers - alias Mobilizon.Service.Users.ResetPassword + import Mobilizon.Factory import Mock + use Bamboo.Test + alias Mobilizon.{Actors, Config, Users} + alias Mobilizon.Actors.Actor + alias Mobilizon.Service.Users.ResetPassword + alias Mobilizon.Users.User + alias Mobilizon.Users + + alias MobilizonWeb.{AbsintheHelpers, Email} + @valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"} @valid_single_actor_params %{preferred_username: "test2", keys: "yolo"} @@ -503,7 +507,7 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do |> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) assert json_response(res, 200)["data"]["resendConfirmationEmail"] == user.email - assert_delivered_email(Mobilizon.Email.User.confirmation_email(user)) + assert_delivered_email(Email.User.confirmation_email(user)) end test "test resend_confirmation_email/3 with invalid email resends an validation email",