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

@@ -44,6 +44,9 @@ type Draft struct {
HTML string
Text string
CSS string
// Brand is a branding draft. Nil is the default brand.
Brand *Brand
}
// The rules a DraftError reports.
@@ -55,6 +58,7 @@ const (
ruleLayout = "layout"
rulePartial = "partial"
ruleNoContent = "no_content"
ruleColour = "colour"
)
// The fields a DraftError names: the columns an admin form binds.
@@ -71,10 +75,10 @@ const (
type DraftError struct {
// Field is the form field at fault: subject, layout or body for a
// template, content_html, content_text or content_css for a layout or a
// partial.
// partial, the colour's column such as button_primary_bg for Branding.
Field string
// Rule is what failed: parse, render, unsafe, subject_line, layout,
// partial or no_content.
// partial, no_content or colour.
Rule string
// Message is the parser's, renderer's or safety check's own text, and
// for the unsafe rule the text that matched. It carries nothing else.
@@ -144,8 +148,9 @@ func (e *Editor) Validate(ctx context.Context, d Draft) *DraftError {
// Preview renders a draft: a template with its sample values and visible
// [name] placeholders, a layout around postcard's sample message, a partial
// called with placeholder arguments. Layouts, partials and other pieces the
// draft does not carry come from the mailer's override source, as in a send.
// called with placeholder arguments, Branding as the sample message in the
// default layout. Layouts, partials, Branding and other pieces the draft
// does not carry come from the mailer's override source, as in a send.
// A problem with the draft is returned as a *DraftError.
func (e *Editor) Preview(ctx context.Context, d Draft) (Preview, error) {
msg, err := e.render(ctx, d)
@@ -187,8 +192,10 @@ func (e *Editor) render(ctx context.Context, d Draft) (RenderedMessage, error) {
// stored override of a template being edited is replaced by the draft.
ov := m.overrides(ctx, "")
ov.Template = nil
opts := renderOptions{css: m.opts.CSS, brandCSS: m.opts.BrandCSS}
opts := m.options(ov.Brand)
switch d.Kind {
case KindBrand:
return e.renderBrand(d, ov, opts)
case KindTemplate:
return e.renderTemplate(d, ov, opts)
case KindLayout:
@@ -301,6 +308,25 @@ func (e *Editor) renderPartial(d Draft, ov Overrides, opts renderOptions) (Rende
return msg, nil
}
// renderBrand renders the sample message in the default layout with the
// draft's colours and logo in place of the saved Branding.
func (e *Editor) renderBrand(d Draft, ov Overrides, opts renderOptions) (RenderedMessage, error) {
brand := DefaultBrand()
if d.Brand != nil {
brand = *d.Brand
}
css, err := CompileBrandCSS(brand)
if err != nil {
return RenderedMessage{}, err
}
opts.brandCSS, opts.logoURL = css, brand.LogoURL
opts.overrides = ov
sample := sampleMessage()
return e.m.catalog.renderFrom(Message{
Vars: placeholderVars(sample.samples, sample.subject, sample.body),
}, renderSource{name: sample.name, subject: sample.subject, body: sample.body}, opts)
}
// usesContent reports whether a layout wrapper reads .Content, and whether
// the wrapper parsed at all.
func usesContent(src string) (uses, parsed bool) {
@@ -366,6 +392,10 @@ func draftErrorFor(d Draft, err error) *DraftError {
if errors.As(err, &de) {
return de
}
var colour *brandColourError
if errors.As(err, &colour) {
return &DraftError{Field: colour.column, Rule: ruleColour}
}
part := ""
var re *renderError
if errors.As(err, &re) {