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:
@@ -3,6 +3,10 @@ package postcard
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.golem15.com/golem15/summercms/backpack"
|
||||
"git.golem15.com/golem15/summercms/pact"
|
||||
)
|
||||
|
||||
// Message is the Send contract for plugin callers.
|
||||
@@ -70,3 +74,44 @@ func (m *mailer) Send(ctx context.Context, msg Message) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Activate publishes an app-scoped Mailer before plugin Boot. Catalog files
|
||||
// are validated later at each plugin's Boot transition.
|
||||
func Activate[P interface{ ID() string }](app *backpack.App, _ []P) error {
|
||||
if app == nil {
|
||||
return fmt.Errorf("postcard: app is nil")
|
||||
}
|
||||
opts := Options{}
|
||||
if app.Config != nil {
|
||||
opts.From = strings.TrimSpace(app.Config.String("mail.from"))
|
||||
opts.CSS = app.Config.String("mail.css")
|
||||
opts.BrandCSS = app.Config.String("mail.brandCss")
|
||||
}
|
||||
m := &mailer{
|
||||
catalog: NewCatalog(),
|
||||
driver: NewMemoryDriver(),
|
||||
opts: opts,
|
||||
}
|
||||
if err := app.Publish[Mailer](m); err != nil {
|
||||
return fmt.Errorf("postcard: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BootPlugin registers a plugin's declared mail templates and layouts.
|
||||
// Missing files and unknown layout aliases fail with the dotted name.
|
||||
func BootPlugin[P interface{ ID() string }](app *backpack.App, p P) error {
|
||||
hm, ok := any(p).(pact.HasMailTemplates)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
got, ok := app.Lookup[Mailer]()
|
||||
if !ok {
|
||||
return fmt.Errorf("postcard: mailer not published")
|
||||
}
|
||||
m, ok := got.(*mailer)
|
||||
if !ok || m == nil || m.catalog == nil {
|
||||
return fmt.Errorf("postcard: mailer not published")
|
||||
}
|
||||
return m.catalog.Register(p.ID(), hm.MailTemplatesFS(), hm.MailTemplates(), hm.MailLayouts())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user