feat(postcard): mail partials, built-in layouts and layout and partial overrides

- pact.HasMailPartials: plugins declare <plugin>::mail.partials.<name>,
  loaded from views/mail/partials/<name>.htm; HasMailTemplates is unchanged
- bodies and layouts call {{ partial "name" "key" value ... }}; a call in a
  Markdown body leaves a per-render random token swapped after goldmark, so
  raw HTML stays disabled for bodies
- built-in partials button, panel, table and subcopy, built-in layouts
  system and minimal next to the unchanged default, reachable by alias
  after a plugin's own aliases
- NewDBSource also reads customized and database-only layouts and
  customized partials; a layout's CSS goes before mail.css

Plan 35-01, task 3.
This commit is contained in:
Jakub Zych
2026-11-13 10:38:14 +01:00
parent 7e5d2c74af
commit 5df1ec27cf
17 changed files with 1273 additions and 71 deletions

View File

@@ -2,6 +2,7 @@ package postcard
import (
"context"
"database/sql"
"fmt"
"git.golem15.com/golem15/summercms/modules/backpack"
@@ -140,9 +141,11 @@ type Overrides struct {
// Template is the customized subject, body and layout of the template
// being sent, or nil.
Template *TemplateOverride
// Layouts holds layout content by layout code.
// Layouts holds layout content by layout code: the full name of a
// customized file layout, or the code of a database-only layout. The
// layout a render uses is taken from here when its code is present.
Layouts map[string]LayoutOverride
// Partials holds partial content by partial name.
// Partials holds the customized sections of partials by full name.
Partials map[string]PartialOverride
}
@@ -160,7 +163,8 @@ type TemplateOverride struct {
Layout string
}
// LayoutOverride replaces a layout's wrappers and adds its CSS.
// LayoutOverride is a layout's content from the database: the HTML and text
// wrappers, and CSS that is placed before mail.css in the layout's css slot.
type LayoutOverride struct {
Name string
HTML string
@@ -178,6 +182,10 @@ type PartialOverride struct {
// looks the *gorm.DB up on app at every call and never caches a row, so an
// edit is effective on the next send in every process that shares the
// database. With no database published it returns no overrides and no error.
//
// One call reads, in one read-only transaction, the customized row of the
// template, every customized file layout and every database-only layout, and
// every customized partial.
func NewDBSource(app *backpack.App) OverrideSource {
return &dbSource{app: app}
}
@@ -199,23 +207,60 @@ func (s *dbSource) MailOverrides(ctx context.Context, name string) (Overrides, e
// The mailer reports a failed lookup itself, rate-limited; GORM's own
// error line per send would repeat it for every message.
db := gdb.Session(&gorm.Session{Logger: gdb.Logger.LogMode(logger.Silent)}).WithContext(ctx)
if name == "" {
return out, nil
}
var rows []struct {
Subject string
Body string
Layout string
}
err := db.Raw(`SELECT t.subject, t.body, COALESCE(l.code, '') AS layout
err := db.Transaction(func(tx *gorm.DB) error {
if name != "" {
var rows []struct {
Subject string
Body string
Layout string
}
err := tx.Raw(`SELECT t.subject, t.body, COALESCE(l.code, '') AS layout
FROM `+TemplatesTable+` t
LEFT JOIN `+LayoutsTable+` l ON l.id = t.layout_id
WHERE t.code = ? AND t.is_custom`, name).Scan(&rows).Error
if err != nil {
return err
}
if len(rows) > 0 {
out.Template = &TemplateOverride{Subject: rows[0].Subject, Body: rows[0].Body, Layout: rows[0].Layout}
}
}
// is_custom is set on a customized file layout and on every
// database-only layout.
var layouts []struct {
Code string
Name string
ContentHTML string `gorm:"column:content_html"`
ContentText string `gorm:"column:content_text"`
ContentCSS string `gorm:"column:content_css"`
}
if err := tx.Raw(`SELECT code, name, content_html, content_text, content_css FROM ` + LayoutsTable + ` WHERE is_custom`).Scan(&layouts).Error; err != nil {
return err
}
for _, l := range layouts {
if out.Layouts == nil {
out.Layouts = make(map[string]LayoutOverride, len(layouts))
}
out.Layouts[l.Code] = LayoutOverride{Name: l.Name, HTML: l.ContentHTML, Text: l.ContentText, CSS: l.ContentCSS}
}
var partials []struct {
Code string
ContentHTML string `gorm:"column:content_html"`
ContentText string `gorm:"column:content_text"`
}
if err := tx.Raw(`SELECT code, content_html, content_text FROM ` + PartialsTable + ` WHERE is_custom`).Scan(&partials).Error; err != nil {
return err
}
for _, p := range partials {
if out.Partials == nil {
out.Partials = make(map[string]PartialOverride, len(partials))
}
out.Partials[p.Code] = PartialOverride{HTML: p.ContentHTML, Text: p.ContentText}
}
return nil
}, &sql.TxOptions{ReadOnly: true, Isolation: sql.LevelRepeatableRead})
if err != nil {
return Overrides{}, fmt.Errorf("postcard: mail overrides: %w", err)
}
if len(rows) > 0 {
out.Template = &TemplateOverride{Subject: rows[0].Subject, Body: rows[0].Body, Layout: rows[0].Layout}
}
return out, nil
}