From 12905ce1ad39106251e401fec81b871799708cc4 Mon Sep 17 00:00:00 2001
From: Ivan Tashkinov <ivantashkinov@gmail.com>
Date: Tue, 11 Dec 2018 14:59:25 +0300
Subject: [PATCH] [#114] Added /dev/mailbox dev-only route (emails preview).
 Added mailer config examples.

---
 config/config.md          | 25 ++++++++++++++++++++++++
 config/prod.exs           | 41 ---------------------------------------
 lib/pleroma/web/router.ex | 18 +++++++++++++++++
 3 files changed, 43 insertions(+), 41 deletions(-)

diff --git a/config/config.md b/config/config.md
index dbbfa9194..2e7f6244c 100644
--- a/config/config.md
+++ b/config/config.md
@@ -30,6 +30,31 @@ This filter replaces the filename (not the path) of an upload. For complete obfu
 
 * `text`: Text to replace filenames in links. If empty, `{random}.extension` will be used.
 
+## Pleroma.Mailer
+* `adapter`: one of the mail adapters listed in [Swoosh readme](https://github.com/swoosh/swoosh#adapters), or `Swoosh.Adapters.Local` for in-memory mailbox.
+* `api_key` / `password` and / or other adapter-specific settings, per the above documentation. 
+
+An example for Sendgrid adapter:
+
+```
+config :pleroma, Pleroma.Mailer,
+  adapter: Swoosh.Adapters.Sendgrid,
+  api_key: "YOUR_API_KEY"
+```
+
+An example for SMTP adapter:
+```
+config :pleroma, Pleroma.Mailer,
+  adapter: Swoosh.Adapters.SMTP,
+  relay: "smtp.gmail.com",
+  username: "YOUR_USERNAME@gmail.com",
+  password: "YOUR_SMTP_PASSWORD",
+  port: 465,
+  ssl: true,
+  tls: :always,
+  auth: :always
+```
+
 ## :uri_schemes
 * `valid_schemes`: List of the scheme part that is considered valid to be an URL
 
diff --git a/config/prod.exs b/config/prod.exs
index e281a4a03..d0cfd1ac2 100644
--- a/config/prod.exs
+++ b/config/prod.exs
@@ -17,47 +17,6 @@ config :pleroma, Pleroma.Web.Endpoint,
   http: [port: 4000],
   protocol: "http"
 
-# Supported adapters: https://github.com/swoosh/swoosh#adapters
-mailer_settings =
-  case String.downcase(System.get_env("PLEROMA_SWOOSH_ADAPTER") || "") do
-    "mailgun" ->
-      [
-        adapter: Swoosh.Adapters.Mailgun,
-        api_key: System.get_env("PLEROMA_MAILGUN_API_KEY"),
-        domain: System.get_env("PLEROMA_MAILGUN_DOMAIN")
-      ]
-
-    "mandrill" ->
-      [
-        adapter: Swoosh.Adapters.Mandrill,
-        api_key: System.get_env("PLEROMA_MANDRILL_API_KEY")
-      ]
-
-    "sendgrid" ->
-      [
-        adapter: Swoosh.Adapters.Sendgrid,
-        api_key: System.get_env("PLEROMA_SENDGRID_API_KEY")
-      ]
-
-    "smtp" ->
-      [
-        adapter: Swoosh.Adapters.SMTP,
-        relay: System.get_env("PLEROMA_SMTP_RELAY"),
-        username: System.get_env("PLEROMA_SMTP_USERNAME"),
-        password: System.get_env("PLEROMA_SMTP_PASSWORD"),
-        port: System.get_env("PLEROMA_SMTP_PORT") || 1025,
-        ssl: true,
-        tls: :always,
-        auth: :always,
-        retries: 3
-      ]
-
-    _ ->
-      [adapter: Swoosh.Adapters.Local]
-  end
-
-config :pleroma, Pleroma.Mailer, mailer_settings
-
 # Do not print debug messages in production
 config :logger, level: :info
 
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 9c06fac4f..19b8750fc 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -85,6 +85,15 @@ defmodule Pleroma.Web.Router do
     plug(:accepts, ["html", "json"])
   end
 
+  pipeline :mailbox_preview do
+    plug(:accepts, ["html"])
+
+    plug(:put_secure_browser_headers, %{
+      "content-security-policy" =>
+        "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline' 'unsafe-eval'"
+    })
+  end
+
   scope "/api/pleroma", Pleroma.Web.TwitterAPI do
     pipe_through(:pleroma_api)
     get("/password_reset/:token", UtilController, :show_password_reset)
@@ -268,6 +277,7 @@ defmodule Pleroma.Web.Router do
     get("/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation)
 
     post("/account/register", TwitterAPI.Controller, :register)
+    post("/account/reset_password", TwitterAPI.Controller, :reset_password)
 
     get("/search", TwitterAPI.Controller, :search)
     get("/statusnet/tags/timeline/:tag", TwitterAPI.Controller, :public_and_external_timeline)
@@ -424,6 +434,14 @@ defmodule Pleroma.Web.Router do
     get("/:sig/:url/:filename", MediaProxyController, :remote)
   end
 
+  if Mix.env() == :dev do
+    scope "/dev" do
+      pipe_through([:mailbox_preview])
+
+      forward("/mailbox", Plug.Swoosh.MailboxPreview, base_path: "/dev/mailbox")
+    end
+  end
+
   scope "/", Fallback do
     get("/registration/:token", RedirectController, :registration_page)
     get("/*path", RedirectController, :redirector)