penpot/backend/src/app/email.clj
Juan de la Cruz ea61730f61
✨ Add review mode, review comments and tasks to design reviews (#12939)
* ✨ Add review mode, review comments and tasks to design reviews

A design review now opens a read-only review mode. The left panel shows
the pages and layers with the View only badge, the toolbar offers the
review tools (move, comment, comment an area, create a task, show the
changes) and the right panel holds the inspect of the selection, the
comments of the review and its tasks. A bar at the bottom names the
review, shows the progress of its tasks and opens its details.

Comments can point at a spot, a layer or an area, and a comment written
in a review says so with its own mark, wherever the file is open. The
layers panel counts the open comments of each layer.

Tasks are new: work on a file, assigned to someone and tied to a layer
or to a spot of the canvas. A comment can become a task and stays
attached to it. A task moves through open, done, verified, reopened and
won't do; anybody may move it, and each move is an event that records
who made it. Deleting a layer with open tasks moves them to its board
and offers to undo the deletion or to mark them done; a task can then
be linked to another layer or get its layer back. Assignees are told by
email. Merging a branch moves its tasks and their threads into main:
the open ones keep going, the closed ones stay as its history, and the
merge dialog lists them and can tell their assignees.

The details of a review show its overview, its tasks and its activity:
what the saves of the branch did to each layer since the review opened,
by person and day, with the values before and after. The changes can be
shown on the canvas one after another.

The backend adds the review fields of a comment thread, the task and
task event tables, the activity of a review (recorded by each save of a
branch under review) and the commands of the tasks.

* ♻️ Bring the design review styles and components up to the DS

Move what the review UI repeated into the design system: layer-chip*,
progress-bar*, comment-card*, link-button* and textarea*, each with its
story and docs. The screens keep the look they had: the same review states
compare equal, pixel by pixel, before and after.

The stylesheets follow the rules of the UI guide: local variables for the
colors that change by state, the right tokens, no unused imports and fewer
element and nested selectors. What has no DS token or typography says so in
a TODO. The aliases left in comments.scss resolve to their tokens.

The components get complete schemas and a single owner for the copyright.
Translations take literal keys and templates instead of dynamic keys and
glued strings, and comments.cljs no longer depends on the review tasks: the
workspace passes it what a thread shows about its task.

Fix three bugs of the review:

- The inspect took the width of a narrower panel and its height from the
  window, so it looked squeezed and cut its last rows. It now fills the
  panel that holds it.
- Enter on the menu of a task opened the task. The row now reacts only to
  keys on itself, and the menu keeps Enter from the workspace shortcuts.
- The resolved and lost marks ignored the pointer: hover now wins over the
  state of a mark.

* ✨ Turn each review message into a task and restore the filter dropdown

Each message of a thread can now become a task, so one thread can hold
several. The new file_task.comment_id column records the message, the
migration fills it with the first message of the thread for existing tasks,
and the backend rejects a message from another thread or one that already
has a task.

Comments and tasks of a review behave like normal ones. The canvas marks
show a short preview on hover and there is one mark per task. A blue label
marks what belongs to a review, in the thread and in the sidebar, and a
selected card takes a blue border there and the primary color elsewhere.
The new review-label* component holds the label.

The sidebar and the review panel go back to a filter dropdown in place of
the chips. Comments offer the views all, yours and mentions, with an option
to hide the resolved ones. Tasks offer all, yours, open and to verify, with
an option to hide the closed ones. Resolved comments now show by default.

"New task" outside a review now arms the task tool: it shows the task cursor
and the next click opens the task form, not a comment. Then the comment tool
returns, and Esc leaves the tool.

The details modal shows changed properties in the format of the compare
dialogs, and showing a task on the canvas syncs the WASM viewport. The sidebar
cards put the layer chip on the line of the replies, and task states lose the
dot and take a color each.

Fix these bugs:

- The hover card of the activity tab crashed on a change with no token.
- The assignee and reviewer dropdowns came out cut. The select now portals
  into the closest dialog, because the modal makes the rest of the page inert.
- The review label cut the header of the thread. It now sits below it.

* ✨ Group overlapping review marks and let tasks change assignee

Comments and tasks of a review that fall on the same spot now show as one
mark with a counter in a white bubble. A click fans them out, as the comment
bubbles do, and the user picks one. They group on the same layer and, by
proximity, at a low zoom. The card of the mark lists up to five items.

Hovering a mark outlines its layer, or its board when the layer is gone. The
WASM viewport draws no outlines under the comments tool, so it now draws the
highlighted ones there. Opening a comment from a mark closes the open task.

A task can change assignee from the selector in its state and from "Assign
to" in its menu, which holds the team in the same popover. The change shows
right away and goes to the history of the task. A closed task keeps its
assignee.

Deleting a task or a comment now asks first, with the usual confirmation. A
task deleted from the details of a review brings the dialog back on its tab.

The state of a task takes two rows, in its panel and in a thread: state and
layer, then assignee and the review it comes from, which a review omits. The
text of tasks and comments sits in a box. The history no longer says that the
task was created.

* ✨ Keep the branch bar and the palettes at one level on a branch

The open branch bar covered the horizontal scroll of the canvas. It now sits
as far from the bottom as the review bar. Folded, it stays flush.

On a branch the palettes no longer lift over the branch bar: both sit at the
same level and work as a toggle. Opening one folds the other, also through
the palette shortcuts, and both can stay folded. The new `:bottom-bar` state
says which one is open. It exists only while the branch bar does, so a file
that is not a branch keeps the palettes on their layout flags and at their
usual place, and what happens on a branch does not reach them.

The branch bar comes open on a branch and centers on the canvas, not on the
window, so that it clears the folded palettes at usual widths. The room it
published for the palettes goes away with the lifting.

* ✨ Give every task a thread and tidy the design review screens

Every task now lives in a message of a comment thread, so anyone can go on
commenting on it. A task made from scratch gets a thread whose first message
is its title, on its layer and in its review, created in the same
transaction. A comment turned into a task keeps its thread, as before. The
panel of a task with no thread is gone: a task always opens as its thread.
Migration 0814 gives a thread to the tasks that had none. Deleting a thread
deletes its tasks, after a confirmation that says so.

The anchor of a task was a plain map, not a point, so a thread on a layer
refused replies. It is now a point.

The details of a review filter the tasks by state with the chips of the
activity, in the colour of each state, and show how many are closed on the
right. The "New task" button leaves the dialog.

Descriptions look the same everywhere: the box of the branch info dialog,
an "Edit" button and a dashed box when empty. Read, they grow up to 400px
and scroll inside; edited, the textarea starts at 400px. The text of a
review in the sidebar does the same once unfolded.

In the review bar, the author gets a "Publish latest changes" button when
the branch moved past the review, with a hint that approvals must be
renewed. The others keep a badge, now "Unpublished changes", with a hint.
The bar refreshes once the publish lands.

Smaller changes: the resolve checkbox lines up with the task button and has
a tooltip, messages sit in a filled box, task events take an outline, and
the change navigator sits right above the review bar.

* 💄 Sort the test runner requires that cljfmt rejects

The Frontend Tests lint step fails on runner.cljs: the requires for
workspace-tasks-test, review-details-test and review-markers-test sat
out of alphabetical order. This is the output of cljfmt fix.

AI-assisted-by: claude-opus-5-5
Signed-off-by: Álvaro Tejero Cantero <alvorithm@teje.ro>

* 🐛 Test the design review mode in the pull request spec

The review bar replaced the branch banner, so four tests still drove
the branch bar: a visible Compare button, the rectangle tool and the
Review button. They now drive the design review instead.

* Opening a file with a pr-id enters the review: the bar names the
  review, the header trades the file name for the review title, and
  comparing, merging and cancelling sit in the bar's options menu.
* The compare dialog opens from that menu.
* The review is read-only rather than an editable file: the review
  toolbar offers its five tools, the drawing tools are gone, the
  sidebar says View only, and the right panel is the review's own
  Inspect, Comments and Tasks tabs.
* The verdict comes from the bar's Submit review button and the
  dialog it opens, scoped by role so the bar action and the dialog
  action do not collide.

The tests also reach the review bar by its region label instead of
walking up three parents from the Reviewing the design review text,
which broke as soon as the bar's markup changed.

AI-assisted-by: claude-opus-5-5
Signed-off-by: Álvaro Tejero Cantero <alvorithm@teje.ro>

* 🐛 Count only the test thread's acquisitions in the climit test

acquires-the-command-own-limits-before-the-root-limits failed on CI
with an extra "root/global" ahead of the expected order. The test
records metrics through with-redefs, which is global, so a call of
another thread of the test JVM, such as an RPC an earlier test left
running, can take a root limiter inside the window and land in the
same atom.

Record the thread with each metrics call and keep, in
acquired-in-order, only the acquisitions of the calling thread. Both
tests that read it acquire their limiters on the test thread. The
same class of flake was fixed on develop for the waiting-count test
(12d73d0f63); this one is still open there.

AI-assisted-by: claude-opus-5-5
Signed-off-by: Álvaro Tejero Cantero <alvorithm@teje.ro>

* 📚 Describe the task moves the server allows in the table comment

The comment on file_task said a task is reopened only from done and
that editors move tasks. files_task.clj::transitions also reopens a
verified or won't-do task, and set-task-status lets anybody who can
read the file move one. Say the same in the schema.

AI-assisted-by: claude-opus-5-5
Signed-off-by: Álvaro Tejero Cantero <alvorithm@teje.ro>

---------

Signed-off-by: Álvaro Tejero Cantero <alvorithm@teje.ro>
Co-authored-by: Álvaro Tejero Cantero <alvorithm@teje.ro>
2026-11-17 22:30:22 +01:00

827 lines
28 KiB
Clojure

;; 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/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns app.email
"Main api for send emails."
(:refer-clojure :exclude [send])
(:require
[app.common.data :as d]
[app.common.data.macros :as dm]
[app.common.exceptions :as ex]
[app.common.logging :as l]
[app.common.pprint :as pp]
[app.common.schema :as sm]
[app.config :as cf]
[app.db :as db]
[app.db.sql :as sql]
[app.jobs :as jobs]
[app.util.template :as tmpl]
[clojure.java.io :as io]
[cuerdas.core :as str]
[integrant.core :as ig])
(:import
jakarta.mail.internet.InternetAddress
jakarta.mail.internet.MimeBodyPart
jakarta.mail.internet.MimeMessage
jakarta.mail.internet.MimeMultipart
jakarta.mail.Message$RecipientType
jakarta.mail.Session
jakarta.mail.Transport
java.util.Properties))
(defn clean
"Clean and normalizes email address string"
[email]
(let [email (str/lower email)
email (if (str/starts-with? email "mailto:")
(subs email 7)
email)
email (if (or (str/starts-with? email "<")
(str/ends-with? email ">"))
(str/trim email "<>")
email)]
email))
(defn get-domain
[email]
(let [email (clean email)
[_ domain] (str/split email "@" 2)]
domain))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; EMAIL IMPL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- parse-address
^"[Ljakarta.mail.internet.InternetAddress;"
[v]
(InternetAddress/parse ^String v))
(defn- resolve-recipient-type
^Message$RecipientType
[type]
(case type
:to Message$RecipientType/TO
:cc Message$RecipientType/CC
:bcc Message$RecipientType/BCC))
(defn- assign-recipient
[^MimeMessage mmsg type address]
(if (sequential? address)
(reduce #(assign-recipient %1 type %2) mmsg address)
(let [address (parse-address address)
type (resolve-recipient-type type)]
(.addRecipients mmsg type address)
mmsg)))
(defn- assign-recipients
[mmsg {:keys [to cc bcc] :as params}]
(cond-> mmsg
(some? to) (assign-recipient :to to)
(some? cc) (assign-recipient :cc cc)
(some? bcc) (assign-recipient :bcc bcc)))
(defn- assign-from
[mmsg {:keys [::default-from] :as cfg} {:keys [from] :as params}]
(let [from (or from default-from)]
(when from
(let [from (parse-address from)]
(.addFrom ^MimeMessage mmsg from)))))
(defn- assign-reply-to
[mmsg {:keys [::default-reply-to] :as cfg} {:keys [reply-to] :as params}]
(let [reply-to (or reply-to default-reply-to)]
(when reply-to
(let [reply-to (parse-address reply-to)]
(.setReplyTo ^MimeMessage mmsg reply-to)))))
(defn- assign-subject
[mmsg {:keys [subject charset] :or {charset "utf-8"} :as params}]
(assert (string? subject) "subject is mandatory")
(.setSubject ^MimeMessage mmsg
^String subject
^String charset))
(defn- assign-extra-headers
[^MimeMessage mmsg {:keys [headers extra-data] :as params}]
(let [headers (assoc headers "X-Penpot-Data" extra-data)]
(reduce-kv (fn [^MimeMessage mmsg k v]
(doto mmsg
(.addHeader (name k) (str v))))
mmsg
headers)))
(defn- assign-body
[^MimeMessage mmsg {:keys [body charset attachments] :or {charset "utf-8"}}]
(let [mixed-mpart (MimeMultipart. "mixed")]
(cond
(string? body)
(let [text-part (MimeBodyPart.)]
(.setText text-part ^String body ^String charset)
(.addBodyPart mixed-mpart text-part))
(map? body)
(let [content-part (MimeBodyPart.)
alternative-mpart (MimeMultipart. "alternative")]
(when-let [content (get body "text/plain")]
(let [text-part (MimeBodyPart.)]
(.setText text-part ^String content ^String charset)
(.addBodyPart alternative-mpart text-part)))
(when-let [content (get body "text/html")]
(let [html-part (MimeBodyPart.)]
(.setContent html-part ^String content
(str "text/html; charset=" charset))
(.addBodyPart alternative-mpart html-part)))
(.setContent content-part alternative-mpart)
(.addBodyPart mixed-mpart content-part))
:else
(throw (IllegalArgumentException. "invalid email body provided")))
(doseq [[name content] attachments]
(let [attachment-part (MimeBodyPart.)]
(.setFileName attachment-part ^String name)
(.setContent attachment-part ^String content (str "text/plain; charset=" charset))
(.addBodyPart mixed-mpart attachment-part)))
(.setContent mmsg mixed-mpart)
mmsg))
(defn- opts->props
[{:keys [::username ::tls ::host ::port ::timeout ::default-from]
:or {timeout 30000}}]
(reduce-kv
(fn [^Properties props k v]
(if (nil? v)
props
(doto props (.put ^String k ^String (str v)))))
(Properties.)
{"mail.user" username
"mail.host" host
"mail.debug" (contains? cf/flags :smtp-debug)
"mail.from" default-from
"mail.smtp.auth" (boolean username)
"mail.smtp.starttls.enable" tls
"mail.smtp.starttls.required" tls
"mail.smtp.host" host
"mail.smtp.port" port
"mail.smtp.user" username
"mail.smtp.timeout" timeout
"mail.smtp.connectiontimeout" timeout}))
(def ^:private schema:smtp-config
[:map
[::username {:optional true} :string]
[::password {:optional true} :string]
[::tls {:optional true} ::sm/boolean]
[::ssl {:optional true} ::sm/boolean]
[::host {:optional true} :string]
[::port {:optional true} ::sm/int]
[::default-from {:optional true} :string]
[::default-reply-to {:optional true} :string]])
(def valid-smtp-config?
(sm/check-fn schema:smtp-config))
(defn- create-smtp-session
^Session
[cfg]
(dm/assert!
"expected valid smtp config"
(valid-smtp-config? cfg))
(let [props (opts->props cfg)]
(Session/getInstance props)))
(defn- create-smtp-message
^MimeMessage
[cfg session params]
(let [mmsg (MimeMessage. ^Session session)]
(assign-recipients mmsg params)
(assign-from mmsg cfg params)
(assign-reply-to mmsg cfg params)
(assign-subject mmsg params)
(assign-extra-headers mmsg params)
(assign-body mmsg params)
(.saveChanges ^MimeMessage mmsg)
mmsg))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TEMPLATE EMAIL IMPL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ^:private email-path "app/email/%(id)s/%(lang)s.%(type)s")
(defn- render-email-template-part
[type id context]
(let [lang (:lang context :en)
path (str/format email-path {:id (name id)
:lang (name lang)
:type (name type)})]
(some-> (io/resource path)
(tmpl/render context))))
(defn- build-email-template
[id context]
(let [subj (render-email-template-part :subj id context)
text (render-email-template-part :txt id context)
html (render-email-template-part :html id context)]
(when (or (not subj)
(and (not text)
(not html)))
(ex/raise :type :internal
:code :missing-email-templates))
{:subject subj
:body (d/without-nils
{"text/plain" text
"text/html" html})}))
(def ^:private schema:params
[:map {:title "Email Params"}
[:to [:or ::sm/email [::sm/vec ::sm/email]]]
[:reply-to {:optional true} ::sm/email]
[:from {:optional true} ::sm/email]
[:lang {:optional true} ::sm/text]
[:subject {:optional true} ::sm/text]
[:priority {:optional true} [:enum :high :low]]
[:extra-data {:optional true} ::sm/text]
[:body {:optional true}
[:or :string [:map-of :string :string]]]
[:attachments {:optional true}
[:map-of :string :string]]])
(def ^:private check-params
(sm/check-fn schema:params))
(defn template-factory
[& {:keys [id schema]}]
(assert (keyword? id) "id should be provided and it should be a keyword")
(let [check-fn (if schema
(sm/check-fn schema)
(constantly nil))]
(fn [params]
(let [params (-> params check-params check-fn)
email (build-email-template id params)]
(when-not email
(ex/raise :type :internal
:code :email-template-does-not-exists
:hint "seems like the template is wrong or does not exists."
:template-id id))
(cond-> (assoc email :id (name id))
(:extra-data params)
(assoc :extra-data (:extra-data params))
(seq (:attachments params))
(assoc :attachments (:attachments params))
(:from params)
(assoc :from (:from params))
(:reply-to params)
(assoc :reply-to (:reply-to params))
(:to params)
(assoc :to (:to params)))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PUBLIC HIGH-LEVEL API
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn render
[email-factory params]
(email-factory params))
(defn send
"Schedule an already defined email to be sent asynchronously
using the unified jobs machinery. The first `cfg` parameter is the
connectable context that provides the `::jobs/defs` registry (an RPC
method cfg or the system) and can provide a default connection; the
second `params` provides the email data and optionally `::factory`
and `::reuse-conn`. When `::reuse-conn` is true the submission
reuses the caller's existing `::db/conn` (from cfg) instead of
acquiring a fresh one from the pool.
NOTE: this renders the email before submitting, so the full subject
and body are stored as job params. Prefer `send-template` for
templated emails: it stores only the template id plus the raw data
and renders inside the worker. `send` is kept for raw (non
templated) emails and for draining jobs submitted in the old
format."
[cfg {:keys [::reuse-conn ::factory] :as params}]
(when (and reuse-conn (nil? (::db/conn cfg)))
(ex/raise :type :validation
:code :missing-connection
:hint "reuse-conn requires the caller cfg to carry ::db/conn (call inside a transaction)"))
(let [email (if factory
(factory params)
(-> params
(dissoc ::reuse-conn ::factory)
(check-params)))]
(jobs/submit (cond-> (dissoc cfg ::db/conn)
reuse-conn (assoc ::db/conn (::db/conn cfg)))
{::jobs/name :sendmail
::jobs/delay 0
::jobs/max-retries 4
::jobs/priority 200
::jobs/params email})))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SENDMAIL FN / TASK HANDLER
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(declare send-to-logger!)
(defn- sendmail-impl
[cfg params]
(when (contains? cf/flags :smtp)
(let [session (create-smtp-session cfg)]
(with-open [transport (.getTransport session (if (::ssl cfg) "smtps" "smtp"))]
(.connect ^Transport transport
^String (::host cfg)
^String (::port cfg)
^String (::username cfg)
^String (::password cfg))
(let [^MimeMessage message (create-smtp-message cfg session params)]
(l/dbg :hint "sendmail"
:id (:id params)
:to (:to params)
:subject (str/trim (:subject params)))
(.sendMessage ^Transport transport
^MimeMessage message
(.getAllRecipients message))))))
(when (contains? cf/flags :log-emails)
(send-to-logger! cfg params)))
(defmethod ig/init-key ::sendmail
[_ cfg]
;; the job handler: [context params]. The smtp deps are closed over
;; here, and this job has no use for the context.
(fn [_context params]
(sendmail-impl cfg params)))
(defmethod ig/init-key ::job-def
[_ {sendmail ::sendmail}]
{::jobs/name :sendmail
::jobs/schema schema:params
::jobs/handler sendmail
::jobs/decoder (sm/decoder schema:params sm/json-transformer)
::jobs/validator (sm/validator schema:params)})
(defn- send-to-logger!
[_ email]
(let [body (:body email)
out (with-out-str
(println "email console dump:")
(println "******** start email" (:id email) "**********")
(pp/pprint (dissoc email :body))
(if (string? body)
(println body)
(println (->> body
(filter #(= "text/plain" (:type %)))
(map :content)
first)))
(println "******** end email" (:id email) "**********"))]
(l/raw! :info out)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; EMAIL FACTORIES
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ^:private schema:feedback
[:map
[:feedback-subject ::sm/text]
[:feedback-type ::sm/text]
[:feedback-content ::sm/text]
[:profile [:map [:id ::sm/uuid]]]])
(def user-feedback
"A profile feedback email."
(template-factory
:id ::feedback
:schema schema:feedback))
(def ^:private schema:register
[:map [:name ::sm/text]])
(def register
"A new profile registration welcome email."
(template-factory
:id ::register
:schema schema:register))
(def ^:private schema:password-recovery
[:map
[:name ::sm/text]
[:token ::sm/text]])
(def password-recovery
"A password recovery notification email."
(template-factory
:id ::password-recovery
:schema schema:password-recovery))
(def ^:private schema:password-changed
[:map
[:name ::sm/text]])
(def password-changed
"A password changed notification email."
(template-factory
:id ::password-changed
:schema schema:password-changed))
(def ^:private schema:change-email
[:map
[:name ::sm/text]
[:pending-email ::sm/email]
[:token ::sm/text]])
(def change-email
"Password change confirmation email"
(template-factory
:id ::change-email
:schema schema:change-email))
(def ^:private schema:organization-data
[:map
[:name ::sm/text]
[:initials {:optional true} [:maybe :string]]
[:logo {:optional true} [:maybe ::sm/uri]]
[:avatar-bg-url {:optional true} [:maybe ::sm/uri]]
[:sso-active {:optional true} [:maybe ::sm/boolean]]])
(def ^:private schema:invite-to-team
[:map
[:invited-by ::sm/text]
[:team ::sm/text]
[:organization {:optional true} [:maybe schema:organization-data]]
[:token ::sm/text]])
(def invite-to-team
"Teams member invitation email."
(template-factory
:id ::invite-to-team
:schema schema:invite-to-team))
(def ^:private schema:invite-to-organization
[:map
[:invited-by ::sm/text]
[:user-name {:optional true} [:maybe ::sm/text]]
[:token ::sm/text]
[:organization schema:organization-data]])
(def invite-to-organization
"Organization member invitation email."
(template-factory
:id ::invite-to-organization
:schema schema:invite-to-organization))
(def ^:private schema:organization-setup-sso
[:map
[:organization-name ::sm/text]])
(def organization-setup-sso
"Email when an organization set up SSO"
(template-factory
:id ::organization-setup-sso
:schema schema:organization-setup-sso))
(def ^:private schema:renewal-notice
[:map
[:user-name {:optional true} [:maybe :string]]
[:renewal-date ::sm/text]
[:estimated-amount ::sm/text]
[:organizations [:vector schema:organization-data]]])
(def renewal-notice
"Enterprise subscription renewal notice email."
(template-factory
:id ::renewal-notice
:schema schema:renewal-notice))
(def ^:private schema:join-team
[:map
[:invited-by ::sm/text]
[:team ::sm/text]
[:team-id ::sm/uuid]])
(def join-team
"Teams member joined after request email."
(template-factory
:id ::join-team
:schema schema:join-team))
(def ^:private schema:request-file-access
[:map
[:requested-by ::sm/text]
[:requested-by-email ::sm/text]
[:team-name ::sm/text]
[:team-id ::sm/uuid]
[:file-name ::sm/text]
[:file-id ::sm/uuid]
[:page-id ::sm/uuid]])
(def request-file-access
"File access request email."
(template-factory
:id ::request-file-access
:schema schema:request-file-access))
(def request-file-access-yourpenpot
"File access on Personal Projects request email."
(template-factory
:id ::request-file-access-yourpenpot
:schema schema:request-file-access))
(def request-file-access-yourpenpot-view
"File access on Personal Projects view mode request email."
(template-factory
:id ::request-file-access-yourpenpot-view
:schema schema:request-file-access))
(def ^:private schema:request-team-access
[:map
[:requested-by ::sm/text]
[:requested-by-email ::sm/text]
[:team-name ::sm/text]
[:team-id ::sm/uuid]])
(def request-team-access
"Team access request email."
(template-factory
:id ::request-team-access
:schema schema:request-team-access))
(def ^:private schema:review-request
[:map
[:name ::sm/text]
[:source-user ::sm/text]
[:pull-request-title ::sm/text]
[:pull-request-route ::sm/text]
;; plain :string on purpose: the description is optional and may be empty
[:pull-request-description [:string {:max 4000}]]
[:pull-request-url ::sm/text]])
(def review-request
"Email sent to the requested reviewers of a pull request."
(template-factory
:id ::review-request
:schema schema:review-request))
(def ^:private schema:task-assigned
[:map
[:name ::sm/text]
[:source-user ::sm/text]
[:task-title ::sm/text]
[:task-url ::sm/text]])
(def task-assigned
"Email sent to the person a task is assigned to."
(template-factory
:id ::task-assigned
:schema schema:task-assigned))
(def ^:private schema:tasks-moved
[:map
[:name ::sm/text]
[:source-user ::sm/text]
[:branch-name ::sm/text]
[:file-name ::sm/text]
[:task-titles ::sm/text]
[:file-url ::sm/text]])
(def tasks-moved
"Email sent to the assignees of the open tasks of a branch when the
merge of the branch moves them into main."
(template-factory
:id ::tasks-moved
:schema schema:tasks-moved))
(def ^:private schema:comment-mention
[:map
[:name ::sm/text]
[:source-user ::sm/text]
[:comment-reference ::sm/text]
[:comment-content ::sm/text]
[:comment-url ::sm/text]])
(def comment-mention
(template-factory
:id ::comment-mention
:schema schema:comment-mention))
(def ^:private schema:comment-thread
[:map
[:name ::sm/text]
[:source-user ::sm/text]
[:comment-reference ::sm/text]
[:comment-content ::sm/text]
[:comment-url ::sm/text]])
(def comment-thread
(template-factory
:id ::comment-thread
:schema schema:comment-thread))
(def ^:private schema:comment-notification
[:map
[:name ::sm/text]
[:source-user ::sm/text]
[:comment-reference ::sm/text]
[:comment-content ::sm/text]
[:comment-url ::sm/text]])
(def comment-notification
(template-factory
:id ::comment-notification
:schema schema:comment-notification))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TEMPLATED SEND (render inside the worker)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- template-entry
"Registry entry for a templated email: the factory that renders it and
the JSON encoder/decoder of its data schema. The job `params` column
is plain JSON, so values without a JSON form (e.g. the URIs of an
organization) are encoded to strings before submit and decoded back
to their types before the factory validates and renders them."
[factory schema]
{::factory factory
::encode (sm/encoder schema sm/json-transformer)
::decode (sm/decoder schema sm/json-transformer)})
;; Registry of template id to entry. This map is the single source of
;; truth for templated mail: `send-template` validates membership and
;; encodes the data here before submitting (so a job that could never
;; be rendered is rejected up front), and the worker decodes the data
;; and renders it with the entry factory. Callers pass the id string
;; itself (e.g. `"invite-to-team"`); keys stay `::eml/`-namespaced and
;; values stay plain data, following the params convention.
(def templates
{"feedback" (template-entry user-feedback schema:feedback)
"register" (template-entry register schema:register)
"password-recovery" (template-entry password-recovery schema:password-recovery)
"password-changed" (template-entry password-changed schema:password-changed)
"change-email" (template-entry change-email schema:change-email)
"invite-to-team" (template-entry invite-to-team schema:invite-to-team)
"invite-to-organization" (template-entry invite-to-organization schema:invite-to-organization)
"organization-setup-sso" (template-entry organization-setup-sso schema:organization-setup-sso)
"renewal-notice" (template-entry renewal-notice schema:renewal-notice)
"join-team" (template-entry join-team schema:join-team)
"request-file-access" (template-entry request-file-access schema:request-file-access)
"request-file-access-yourpenpot" (template-entry request-file-access-yourpenpot schema:request-file-access)
"request-file-access-yourpenpot-view" (template-entry request-file-access-yourpenpot-view schema:request-file-access)
"request-team-access" (template-entry request-team-access schema:request-team-access)
"comment-mention" (template-entry comment-mention schema:comment-mention)
"comment-thread" (template-entry comment-thread schema:comment-thread)
"comment-notification" (template-entry comment-notification schema:comment-notification)
"review-request" (template-entry review-request schema:review-request)
"task-assigned" (template-entry task-assigned schema:task-assigned)
"tasks-moved" (template-entry tasks-moved schema:tasks-moved)})
(defn render-template
"Render the templated email `template-id` from its JSON-decoded data,
as the worker does before sending it."
[template-id params]
(let [{:keys [::factory ::decode]} (get templates template-id)]
(factory (decode params))))
;; Envelope of the job params only: the template data travels encoded
;; by the template schema and is decoded by `render-template`.
(def ^:private schema:templated-params
[:map {:title "Templated Email Params"}
[:template (into [:enum] (sort (keys templates)))]
[:to [:or ::sm/email [::sm/vec ::sm/email]]]
[:reply-to {:optional true} ::sm/email]
[:from {:optional true} ::sm/email]
[:lang {:optional true} ::sm/text]
[:priority {:optional true} [:enum :high :low]]
[:extra-data {:optional true} ::sm/text]
[:attachments {:optional true}
[:map-of :string :string]]])
(defn send-template
"Schedule a templated email to be sent asynchronously using the
unified jobs machinery. Unlike `send`, this does NOT render the
email: it stores only the template id plus the raw template data as
`:send-templated-mail` job params, and the worker renders and sends
the email. The `::template` param is the template id string (one of
the keys of `templates`, e.g. `invite-to-team`);
`::reuse-conn` behaves like in `send`."
[cfg {:keys [::template ::reuse-conn] :as params}]
(when (and reuse-conn (nil? (::db/conn cfg)))
(ex/raise :type :validation
:code :missing-connection
:hint "reuse-conn requires the caller cfg to carry ::db/conn (call inside a transaction)"))
(when (nil? template)
(ex/raise :type :validation
:code :missing-template
:hint "template is mandatory for templated emails"))
(when-not (contains? templates template)
(ex/raise :type :validation
:code :unknown-email-template
:hint (str/ffmt "unknown email template: %", template)))
(let [encode (get-in templates [template ::encode])
recipe (-> params
(dissoc ::reuse-conn ::template)
;; Present-but-nil extras (e.g. `:file-id` when there is
;; no file) would fail the job schema, while the old
;; inline render simply ignored them.
(d/without-nils)
(encode))]
(jobs/submit (cond-> (dissoc cfg ::db/conn)
reuse-conn (assoc ::db/conn (::db/conn cfg)))
{::jobs/name :send-templated-mail
::jobs/delay 0
::jobs/max-retries 4
::jobs/priority 200
::jobs/params (cond-> (assoc recipe :template template)
;; `:public-uri` arrives as a URI record
;; from config, which plain JSON cannot
;; roundtrip; the templates only need its
;; string form.
(some? (:public-uri recipe))
(update :public-uri str))})))
(defmethod ig/init-key ::templated-job-def
[_ {sendmail ::sendmail}]
{::jobs/name :send-templated-mail
::jobs/schema schema:templated-params
::jobs/handler (fn [context params]
(let [template-id (:template params)]
(when-not (contains? templates template-id)
(ex/raise :type :internal
:code :unknown-email-template
:template-id template-id))
(sendmail context (render-template template-id (dissoc params :template)))))
::jobs/decoder (sm/decoder schema:templated-params sm/json-transformer)
::jobs/validator (sm/validator schema:templated-params)})
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; BOUNCE/COMPLAINS HELPERS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def sql:profile-complaint-report
"select (select count(*)
from profile_complaint_report
where type = 'complaint'
and profile_id = ?
and created_at > now() - ?::interval) as complaints,
(select count(*)
from profile_complaint_report
where type = 'bounce'
and profile_id = ?
and created_at > now() - ?::interval) as bounces;")
(defn allow-send-emails?
[conn profile]
(when-not (:is-muted profile false)
(let [complaint-threshold (cf/get :profile-complaint-threshold)
complaint-max-age (cf/get :profile-complaint-max-age)
bounce-threshold (cf/get :profile-bounce-threshold)
bounce-max-age (cf/get :profile-bounce-max-age)
{:keys [complaints bounces] :as result}
(db/exec-one! conn [sql:profile-complaint-report
(:id profile)
(db/interval complaint-max-age)
(:id profile)
(db/interval bounce-max-age)])]
(and (< (or complaints 0) complaint-threshold)
(< (or bounces 0) bounce-threshold)))))
(defn has-complaint-reports?
([conn email] (has-complaint-reports? conn email nil))
([conn email {:keys [threshold] :or {threshold 1}}]
(let [reports (db/exec! conn (sql/select :global-complaint-report
{:email email :type "complaint"}
{:limit 10}))]
(>= (count reports) threshold))))
(defn has-bounce-reports?
([conn email] (has-bounce-reports? conn email nil))
([conn email {:keys [threshold] :or {threshold 1}}]
(let [reports (db/exec! conn (sql/select :global-complaint-report
{:email email :type "bounce"}
{:limit 10}))]
(>= (count reports) threshold))))
(defn has-reports?
([conn email] (has-reports? conn email nil))
([conn email {:keys [threshold] :or {threshold 1}}]
(let [reports (db/exec! conn (sql/select :global-complaint-report
{:email email}
{:limit 10}))]
(>= (count reports) threshold))))