Files
tenant-registry/internal/mail/render.go
T
Sharang Parnerkar afe62e4795 feat(onprem): mails come from the installation, not from us
On APP_ENV=onprem the registry's mails (new findings, published documents)
no longer carry our company signature, address, register entry, contact
address or legal pages. They say the installation sent them, name the
customer's contact (MAIL_CONTACT) and link the customer's imprint and
privacy notice (LEGAL_IMPRINT_URL, LEGAL_PRIVACY_URL), each only when set.

mail.View gets a Site; the zero value is the cloud, so cloud mails stay as
they were (checked byte for byte against the previous templates) apart from
the spelling "Breakpilot", also in the default sender name. The legal line
moved from every text template into signature.tmpl.

On-prem the registry also refuses to start when MAIL_FROM, PORTAL_URL,
INVITE_REDIRECT_URI or LEGAL_* point at breakpilot.com.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 6058a94254)
2026-11-20 17:08:48 +01:00

110 lines
3.2 KiB
Go

package mail
import (
"bytes"
"embed"
"fmt"
htmltpl "html/template"
"strings"
texttpl "text/template"
)
//go:embed templates/*
var templates embed.FS
// Kinds the registry can send.
const (
KindDocumentPublished = "document.published"
KindFindingsNew = "findings.new"
)
// Kinds lists every kind products may request (POST /v1/notifications).
var Kinds = map[string]bool{KindDocumentPublished: true, KindFindingsNew: true}
// Registry-only kinds (portal#64): German only, sent directly, never queued
// by a product.
const (
KindTrialReceived = "trial.received"
KindTrialActivated = "trial.activated"
)
var internalKinds = map[string]bool{KindTrialReceived: true, KindTrialActivated: true}
// View is what a template sees.
type View struct {
Lang string
Tenant string
URL string
Subject string
D map[string]any
// Site is who sends the mail; the zero value is the cloud.
Site Site
}
// Site decides the signature and the legal links of every mail.
//
// In the cloud the mail comes from Breakpilot GmbH: the company signature
// (no-reply sender, address, register entry) and our imprint and privacy
// notice. On a customer's own installation (APP_ENV=onprem) it comes from
// their system, through their relay and from their address: no company
// signature of ours, their imprint and privacy notice if they name one, and
// whom to ask (their IT) instead of our contact address.
type Site struct {
OnPrem bool
Imprint string // legal notice URL; on-prem empty = left out
Privacy string // privacy notice URL; on-prem empty = left out
Contact string // on-prem: whom to ask (an address or a short text); empty = left out
}
// Cloud legal pages, used when a cloud Site leaves them empty.
const (
CloudImprint = "https://breakpilot.com/impressum"
CloudPrivacy = "https://app.breakpilot.com/datenschutz"
)
// Render builds the subject, text and HTML body of kind in locale ("de"
// unless "en"). HTML values are escaped by html/template; the text body and
// subject are plain.
func Render(kind, locale string, v View) (Message, error) {
if !Kinds[kind] && !internalKinds[kind] {
return Message{}, fmt.Errorf("mail: unknown kind %q", kind)
}
if locale != "en" || internalKinds[kind] {
locale = "de"
}
v.Lang = locale
if !v.Site.OnPrem {
if v.Site.Imprint == "" {
v.Site.Imprint = CloudImprint
}
if v.Site.Privacy == "" {
v.Site.Privacy = CloudPrivacy
}
}
if v.D == nil {
v.D = map[string]any{}
}
file := "templates/" + locale + "_" + kind + ".tmpl"
tt, err := texttpl.ParseFS(templates, "templates/signature.tmpl", file)
if err != nil {
return Message{}, err
}
var subj, text bytes.Buffer
if err := tt.ExecuteTemplate(&subj, "subject", v); err != nil {
return Message{}, err
}
if err := tt.ExecuteTemplate(&text, "text", v); err != nil {
return Message{}, err
}
v.Subject = strings.Join(strings.Fields(subj.String()), " ")
ht, err := htmltpl.ParseFS(templates, "templates/layout.html", "templates/signature.tmpl", file)
if err != nil {
return Message{}, err
}
var html bytes.Buffer
if err := ht.ExecuteTemplate(&html, "layout", v); err != nil {
return Message{}, err
}
return Message{Subject: v.Subject, Text: strings.TrimSpace(text.String()) + "\n", HTML: html.String()}, nil
}