feat(04-03): register plugin mail layouts and publish the mailer at boot

- Map short layout names onto dotted layout files and wrap text/HTML
- party.Activate publishes postcard.Mailer before Boot and names missing files
- Hello plugin sends through the app-scoped mailer

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Jakub Zych
2026-09-18 13:59:52 +02:00
parent b4fd8339fe
commit 1e515971c9
9 changed files with 251 additions and 4 deletions

View File

@@ -8,6 +8,7 @@ import (
"git.golem15.com/golem15/summercms/backpack"
"git.golem15.com/golem15/summercms/pact"
"git.golem15.com/golem15/summercms/phrasebook"
"git.golem15.com/golem15/summercms/postcard"
)
// Plugin is the required descriptor every compiled plugin implements.
@@ -120,7 +121,13 @@ func activate(registered []Plugin, app *backpack.App, ids []string) ([]Plugin, e
if err := phrasebook.Activate(app, ordered); err != nil {
return nil, fmt.Errorf("party: lang: %w", err)
}
if err := postcard.Activate(app, ordered); err != nil {
return nil, fmt.Errorf("party: mail: %w", err)
}
for _, p := range ordered {
if err := postcard.BootPlugin(app, p); err != nil {
return nil, fmt.Errorf("party: boot %s: %w", p.ID(), err)
}
if err := p.Boot(app); err != nil {
return nil, fmt.Errorf("party: boot %s: %w", p.ID(), err)
}

View File

@@ -14,6 +14,7 @@ import (
"git.golem15.com/golem15/summercms/compass"
"git.golem15.com/golem15/summercms/pact"
"git.golem15.com/golem15/summercms/phrasebook"
"git.golem15.com/golem15/summercms/postcard"
)
type tracePlugin struct {
@@ -455,3 +456,65 @@ func TestActivateMalformedLangFailsWithPluginContext(t *testing.T) {
t.Fatalf("error %q should name the plugin and path problem", msg)
}
}
type mailPlugin struct {
id string
fsys fs.FS
templates []string
layouts map[string]string
sawMailer *bool
}
func (p *mailPlugin) ID() string { return p.id }
func (p *mailPlugin) Requires() []string { return nil }
func (p *mailPlugin) Register(*backpack.App) error { return nil }
func (p *mailPlugin) MailTemplatesFS() fs.FS { return p.fsys }
func (p *mailPlugin) MailTemplates() []string { return p.templates }
func (p *mailPlugin) MailLayouts() map[string]string { return p.layouts }
func (p *mailPlugin) Boot(app *backpack.App) error {
if p.sawMailer == nil {
return nil
}
_, ok := app.Lookup[postcard.Mailer]()
*p.sawMailer = ok
return nil
}
func TestActivatePublishesMailerBeforeBoot(t *testing.T) {
var saw bool
p := &mailPlugin{
id: "golem15.hello",
sawMailer: &saw,
templates: []string{"golem15.hello::mail.hello"},
layouts: map[string]string{"hello": "golem15.hello::mail.layouts.hello"},
fsys: fstest.MapFS{
"views/mail/hello.htm": {Data: []byte("subject = \"Hi\"\nlayout = \"hello\"\n==\nHi.\n")},
"views/mail/layouts/hello.htm": {Data: []byte("name = \"Hello\"\n==\n{{ .Content }}\n==\n<div>{{ .Content }}</div>\n")},
},
}
app := backpack.New(&compass.Config{})
if _, err := activate([]Plugin{p}, app, []string{"golem15.hello"}); err != nil {
t.Fatalf("activate: %v", err)
}
if !saw {
t.Fatal("mailer must be published before Boot")
}
}
func TestActivateMissingMailTemplateFailsBootWithName(t *testing.T) {
p := &mailPlugin{
id: "golem15.hello",
templates: []string{"golem15.hello::mail.hello"},
fsys: fstest.MapFS{},
}
app := backpack.New(&compass.Config{})
_, err := activate([]Plugin{p}, app, []string{"golem15.hello"})
if err == nil {
t.Fatal("expected missing template error")
}
msg := err.Error()
if !strings.Contains(msg, "party: boot golem15.hello") || !strings.Contains(msg, "golem15.hello::mail.hello") {
t.Fatalf("error %q should name the boot plugin and missing template", msg)
}
}