2016-11-20 20:04:52 +01:00
|
|
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
;;
|
2019-11-18 11:52:57 +01:00
|
|
|
;; Copyright (c) 2016-2019 Andrey Antukh <niwi@niwi.nz>
|
2016-11-20 20:04:52 +01:00
|
|
|
|
|
|
|
(ns uxbox.emails
|
|
|
|
"Main api for send emails."
|
2019-11-18 11:52:57 +01:00
|
|
|
(:require
|
|
|
|
[clojure.spec.alpha :as s]
|
|
|
|
[promesa.core :as p]
|
|
|
|
[uxbox.config :as cfg]
|
2020-01-11 20:25:50 +01:00
|
|
|
[uxbox.common.exceptions :as ex]
|
|
|
|
[uxbox.common.spec :as us]
|
2019-11-18 11:52:57 +01:00
|
|
|
[uxbox.db :as db]
|
2020-01-23 17:53:26 +01:00
|
|
|
[uxbox.tasks :as tasks]
|
2019-11-18 11:52:57 +01:00
|
|
|
[uxbox.media :as media]
|
2020-01-23 17:53:26 +01:00
|
|
|
[uxbox.util.emails :as emails]))
|
2016-11-20 20:04:52 +01:00
|
|
|
|
2019-11-26 14:17:10 +01:00
|
|
|
;; --- Defaults
|
|
|
|
|
2019-11-18 11:52:57 +01:00
|
|
|
(def default-context
|
|
|
|
{:static media/resolve-asset
|
|
|
|
:comment (constantly nil)})
|
2016-11-20 20:04:52 +01:00
|
|
|
|
2019-11-26 14:17:10 +01:00
|
|
|
;; --- Public API
|
|
|
|
|
2019-11-22 18:08:27 +01:00
|
|
|
(defn render
|
|
|
|
[email context]
|
2020-05-14 13:49:11 +02:00
|
|
|
(let [defaults {:from (:sendmail-from cfg/config)
|
|
|
|
:reply-to (:sendmail-reply-to cfg/config)}]
|
2019-11-25 12:34:35 +01:00
|
|
|
(email (merge defaults context))))
|
2019-11-18 11:52:57 +01:00
|
|
|
|
|
|
|
(defn send!
|
|
|
|
"Schedule the email for sending."
|
2020-01-23 17:53:26 +01:00
|
|
|
([email context] (send! db/pool email context))
|
2020-04-20 13:44:42 +02:00
|
|
|
([conn email-factory context]
|
|
|
|
(us/verify fn? email-factory)
|
2020-01-31 19:08:45 +01:00
|
|
|
(us/verify map? context)
|
2020-04-20 13:44:42 +02:00
|
|
|
(let [defaults {:from (:sendmail-from cfg/config)
|
|
|
|
:reply-to (:sendmail-reply-to cfg/config)}
|
|
|
|
data (merge defaults context)
|
|
|
|
email (email-factory data)]
|
2020-05-15 13:23:23 +02:00
|
|
|
(tasks/submit! conn {:name "sendmail"
|
|
|
|
:delay 0
|
|
|
|
:props email}))))
|
2020-01-13 23:52:31 +01:00
|
|
|
|
|
|
|
;; --- Emails
|
|
|
|
|
|
|
|
(s/def ::name ::us/string)
|
|
|
|
(s/def ::register
|
|
|
|
(s/keys :req-un [::name]))
|
|
|
|
|
|
|
|
(def register
|
|
|
|
"A new profile registration welcome email."
|
|
|
|
(emails/build ::register default-context))
|
|
|
|
|
|
|
|
(s/def ::token ::us/string)
|
|
|
|
(s/def ::password-recovery
|
|
|
|
(s/keys :req-un [::name ::token]))
|
|
|
|
|
|
|
|
(def password-recovery
|
|
|
|
"A password recovery notification email."
|
|
|
|
(emails/build ::password-recovery default-context))
|
2020-05-22 13:48:21 +02:00
|
|
|
|
|
|
|
(s/def ::pending-email ::us/string)
|
|
|
|
(s/def ::change-email
|
|
|
|
(s/keys :req-un [::name ::pending-email ::token]))
|
|
|
|
|
|
|
|
(def change-email
|
|
|
|
"Password change confirmation email"
|
|
|
|
(emails/build ::change-email default-context))
|