feat(postcard): database overrides for mail templates with file fallback

- postcard owns the system_mail_* tables (postcard.Migrations), run by lagoon
  as the framework set summercms.postcard after system_files
- Send asks an OverrideSource once per message; NewDBSource reads the
  customized template row through the *gorm.DB published on the app
- a failed lookup, missing tables or an override that does not render or
  fails the safety checks sends the file version and is logged
- the caller's Message.Subject still wins; Mailer, Message, NewMailer,
  Catalog.Register and BootPlugin keep their signatures

Plan 35-01, task 2 (tracer).
This commit is contained in:
Jakub Zych
2026-11-13 10:26:10 +01:00
parent 3198bbe1aa
commit 7e5d2c74af
10 changed files with 932 additions and 30 deletions

View File

@@ -317,30 +317,63 @@ func parseINI(header string) (map[string]string, error) {
type renderOptions struct {
css string
brandCSS string
// overrides are the database overrides to apply; the zero value renders
// the file version.
overrides Overrides
// warn reports a problem with an override that the render recovered
// from. key identifies the problem for rate limiting. Nil drops it.
warn func(key, msg string, args ...any)
}
// renderSource is what one render works from: a file template, or a file
// template with its override applied.
type renderSource struct {
name string // template name, used in parse and execute errors
subject string
body string
layout string // full layout name, the default alias, or an override's layout code
// layoutFromOverride marks a layout an override chose: one that resolves
// to nothing renders with the default layout instead of failing.
layoutFromOverride bool
}
// render renders msg.Template: the file version, or the file version with
// opts.overrides applied. Both go through renderFrom.
func (c *Catalog) render(msg Message, opts renderOptions) (RenderedMessage, error) {
var out RenderedMessage
t, err := c.lookupTemplate(msg.Template)
if err != nil {
return out, err
return RenderedMessage{}, err
}
src := renderSource{name: t.name, subject: t.subject, body: t.body, layout: t.layout}
if ov := opts.overrides.Template; ov != nil {
src.subject, src.body = ov.Subject, ov.Body
if ov.Layout != "" {
src.layout, src.layoutFromOverride = ov.Layout, true
}
}
return c.renderFrom(msg, src, opts)
}
// renderFrom renders src for msg. The caller's non-empty msg.Subject wins
// over src.subject.
func (c *Catalog) renderFrom(msg Message, src renderSource, opts renderOptions) (RenderedMessage, error) {
var out RenderedMessage
vars := msg.Vars
if vars == nil {
vars = map[string]any{}
}
subjectSrc := t.subject
subjectSrc := src.subject
if strings.TrimSpace(msg.Subject) != "" {
subjectSrc = msg.Subject
}
subject, err := execHTML(t.name+".subject", subjectSrc, vars)
subject, err := execHTML(src.name+".subject", subjectSrc, vars)
if err != nil {
return out, err
}
if strings.ContainsAny(subject, "\r\n") {
return out, fmt.Errorf("postcard: subject contains CR/LF")
}
markdownBody, err := execHTML(t.name+".body", t.body, vars)
markdownBody, err := execHTML(src.name+".body", src.body, vars)
if err != nil {
return out, err
}
@@ -351,7 +384,7 @@ func (c *Catalog) render(msg Message, opts renderOptions) (RenderedMessage, erro
if err := validateHTML(htmlBody); err != nil {
return out, err
}
layout, err := c.lookupLayout(t.layout)
layout, err := c.layoutFor(src, opts)
if err != nil {
return out, err
}
@@ -388,6 +421,22 @@ func (c *Catalog) render(msg Message, opts renderOptions) (RenderedMessage, erro
return out, nil
}
// layoutFor returns the layout src renders in. A layout code from an
// override that names no layout is reported through opts.warn and replaced
// by the default layout.
func (c *Catalog) layoutFor(src renderSource, opts renderOptions) (*parsedLayout, error) {
layout, err := c.lookupLayout(src.layout)
if err == nil || !src.layoutFromOverride {
return layout, err
}
if opts.warn != nil {
opts.warn("layout:"+src.name+":"+src.layout,
"postcard: override names an unknown layout, using the default layout",
"template", src.name, "layout", src.layout)
}
return c.lookupLayout(defaultLayoutAlias)
}
func markdownHTML(src string) (string, error) {
var buf bytes.Buffer
if err := markdown.Convert([]byte(src), &buf); err != nil {