feat(postcard): mail branding and the logo URL

- Brand, DefaultBrand and CompileBrandCSS: thirteen colours, each checked
  as a six-digit hex colour, compiled into CSS for plain elements and the
  built-in layouts and partials
- once the branding row exists its compiled CSS replaces mail.brandCss;
  mail.css still follows, and the file version sent after a failing
  override keeps the Branding
- layouts get logoUrl, built per send from storage.uploads.public_url or
  app.url through attach.AbsolutePublicURL; a logo that cannot be shown is
  left out with a rate-limited warning
- Editor previews and validates a Branding draft (rule colour)

Plan 35-01, task 5.
This commit is contained in:
Jakub Zych
2026-11-13 11:06:43 +01:00
parent 935e4bf64f
commit 2bf9081c1b
10 changed files with 974 additions and 27 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/lagoon/attach"
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
"gorm.io/gorm/logger"
@@ -147,9 +148,17 @@ type Overrides struct {
Layouts map[string]LayoutOverride
// Partials holds the customized sections of partials by full name.
Partials map[string]PartialOverride
// Brand is the saved Branding, or nil while it has never been saved.
// Its LogoURL is already absolute.
Brand *Brand
// warnings are problems the source worked around, for the mailer to
// log: a logo that cannot be shown, for example.
warnings []string
}
// empty reports whether o changes nothing about a render.
// empty reports whether o holds no override of a file. Branding is not one:
// it applies to the file version too.
func (o Overrides) empty() bool {
return o.Template == nil && len(o.Layouts) == 0 && len(o.Partials) == 0
}
@@ -184,8 +193,10 @@ type PartialOverride struct {
// 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.
// template, every customized file layout and every database-only layout,
// every customized partial, and the Branding row. The logo's URL is built
// here, from the application's configuration (attach.AbsolutePublicURL), so
// it is the same in every process.
func NewDBSource(app *backpack.App) OverrideSource {
return &dbSource{app: app}
}
@@ -257,7 +268,7 @@ WHERE t.code = ? AND t.is_custom`, name).Scan(&rows).Error
}
out.Partials[p.Code] = PartialOverride{HTML: p.ContentHTML, Text: p.ContentText}
}
return nil
return s.brand(tx, &out)
}, &sql.TxOptions{ReadOnly: true, Isolation: sql.LevelRepeatableRead})
if err != nil {
return Overrides{}, fmt.Errorf("postcard: mail overrides: %w", err)
@@ -265,6 +276,67 @@ WHERE t.code = ? AND t.is_custom`, name).Scan(&rows).Error
return out, nil
}
// brand reads the Branding row into out.Brand: nil while the row does not
// exist, which is what keeps mail.brandCss in use until Branding is saved.
func (s *dbSource) brand(tx *gorm.DB, out *Overrides) error {
var rows []struct {
LogoFileID *int64
BodyBg string
ContentBg string
HeaderColor string
FooterColor string
HeadingColor string
TextColor string
LinkColor string
ButtonTextColor string
ButtonPrimaryBg string
ButtonPositiveBg string
ButtonNegativeBg string
PanelBg string
TableBorderColor string
}
err := tx.Raw(`SELECT logo_file_id, body_bg, content_bg, header_color, footer_color, heading_color, text_color,
link_color, button_text_color, button_primary_bg, button_positive_bg, button_negative_bg, panel_bg, table_border_color
FROM ` + BrandTable + ` WHERE id = 1`).Scan(&rows).Error
if err != nil {
return err
}
if len(rows) == 0 {
return nil
}
r := rows[0]
out.Brand = &Brand{
BodyBg: r.BodyBg, ContentBg: r.ContentBg, HeaderColor: r.HeaderColor, FooterColor: r.FooterColor,
HeadingColor: r.HeadingColor, TextColor: r.TextColor, LinkColor: r.LinkColor, ButtonTextColor: r.ButtonTextColor,
ButtonPrimaryBg: r.ButtonPrimaryBg, ButtonPositiveBg: r.ButtonPositiveBg, ButtonNegativeBg: r.ButtonNegativeBg,
PanelBg: r.PanelBg, TableBorderColor: r.TableBorderColor,
}
if r.LogoFileID == nil {
return nil
}
// A logo that cannot be shown is left out and reported; the mail and
// the other overrides are not affected. The files table is read under
// a savepoint, so a failure there does not abort the transaction.
var disks []string
err = tx.Transaction(func(sp *gorm.DB) error {
return sp.Raw(`SELECT disk_name FROM system_files WHERE id = ? AND is_public`, *r.LogoFileID).Scan(&disks).Error
})
switch {
case err != nil:
out.warnings = append(out.warnings, "mail branding logo: the file cannot be read: "+err.Error())
case len(disks) == 0:
out.warnings = append(out.warnings, "mail branding logo: the file is missing or is not public")
default:
logo, err := attach.AbsolutePublicURL(s.app.Config, attach.BlobKey(disks[0]))
if err != nil {
out.warnings = append(out.warnings, "mail branding logo: "+err.Error())
break
}
out.Brand.LogoURL = logo
}
return nil
}
// syncLockKey names the advisory lock that serializes SyncIndex runs.
const syncLockKey = "summercms.postcard.sync"