Documentation
¶
Overview ¶
Package notifications provides multi-channel notification delivery modelled on Laravel's Notification facade.
A Notification describes what to send (subject, body, etc.) and which channels it should go through. A Notifiable is the recipient (typically a User struct) and knows its own routing per channel (email address, phone number, push token, …).
The Manager dispatches a notification to every requested channel, collecting errors via errors.Join so partial failure is surfaced without aborting the rest.
Channels shipped in-box:
- MailChannel — adapts a mail.Mailer (this module's mail package).
Add custom channels (SMS, Slack, push) by implementing Channel.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Channel ¶
type Channel interface {
// Name returns the channel identifier ("mail", "sms", …).
Name() string
// Send delivers n to the recipient address returned by Notifiable.
Send(ctx context.Context, recipient string, n Notification) error
}
Channel is the transport interface. Implementations should validate that they can handle the notification (typically via a type assertion to the channel-specific interface).
type MailChannel ¶
type MailChannel struct {
// contains filtered or unexported fields
}
MailChannel routes notifications through a mail.Mailer.
func NewMailChannel ¶
func NewMailChannel(m mail.Mailer) *MailChannel
NewMailChannel wraps a mail.Mailer as a notification Channel.
func (*MailChannel) Send ¶
func (c *MailChannel) Send(ctx context.Context, recipient string, n Notification) error
Send adapts the notification (which must implement MailNotification) to the mailer's Message.
type MailNotification ¶
type MailNotification interface {
// ToMail builds the mail.Message for the given recipient address.
ToMail(recipient string) mail.Message
}
MailNotification is the contract a Notification must satisfy to be dispatched through MailChannel.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager wires Notifiable → Channels.
func NewManager ¶
func NewManager() *Manager
NewManager returns an empty Manager. Register channels with Register or RegisterMany.
func (*Manager) Register ¶
Register attaches a channel. Re-registering replaces the previous channel under the same name.
func (*Manager) RegisterMany ¶
RegisterMany is a convenience for multiple channels.
func (*Manager) Send ¶
func (m *Manager) Send(ctx context.Context, to Notifiable, n Notification) error
Send dispatches n to every channel listed in n.Channels(). Per- channel errors are collected with errors.Join and returned together; if every channel succeeded, the return value is nil.
Channels not registered on the Manager are silently skipped (with a nil error per channel) so a notification declaring extra channels doesn't fail when running in a stripped-down environment (e.g. tests without SMS configured).
func (*Manager) SendNow ¶
func (m *Manager) SendNow(ctx context.Context, to Notifiable, n Notification) error
SendNow is an alias for Send — kept for parity with Laravel terminology where SendNow bypasses the queue. The queue-backed path lives in a sibling package; from this package's perspective every dispatch is synchronous.
type Notifiable ¶
type Notifiable interface {
// RouteFor returns the address to use on the named channel.
// Returning "" means "skip this channel for this user".
RouteFor(channel string) string
}
Notifiable is a recipient of notifications. Implementations expose a per-channel routing address: an email for "mail", a phone for "sms", etc.
type Notification ¶
type Notification interface {
// Channels returns the channel names this notification targets.
Channels() []string
}
Notification is the message to be sent. Implementations expose the channels they support and produce a per-channel payload.
Channels names are arbitrary strings ("mail", "sms", "slack", "database", …). The Manager routes by name; only registered channels receive a delivery attempt.