notification

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package notification provides a unified abstraction for sending notifications across multiple channels (email, SMS, Slack, push, etc.).

Usage:

type WelcomeNotification struct {
    User schema.User
}

func (n *WelcomeNotification) Via() []string { return []string{"mail"} }

func (n *WelcomeNotification) ToMail() *mail.Message {
    return mail.NewMessage().
        To(n.User.Email).
        Subject("Welcome to Astra!").
        Text("Hi " + n.User.Name + ", welcome!")
}

// Send:
notifier.Send(ctx, &WelcomeNotification{User: user})

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 (e.g. "mail", "sms").
	Name() string
	// Send delivers the notification.
	Send(ctx context.Context, n Notification) error
}

Channel is a pluggable delivery backend.

type DatabaseChannel

type DatabaseChannel struct {
	// contains filtered or unexported fields
}

DatabaseChannel persists notifications to a database via a user-supplied writer.

func NewDatabaseChannel

func NewDatabaseChannel(writer DatabaseWriter) *DatabaseChannel

NewDatabaseChannel creates a DatabaseChannel.

func (*DatabaseChannel) Name

func (c *DatabaseChannel) Name() string

func (*DatabaseChannel) Send

type DatabaseNotification

type DatabaseNotification interface {
	Notification
	// ToDatabase returns the data to store in the notifications table.
	ToDatabase() map[string]any
}

DatabaseNotification is implemented by notifications that persist to a DB.

type DatabaseWriter

type DatabaseWriter interface {
	Write(ctx context.Context, data map[string]any) error
}

DatabaseWriter is implemented by the application's notification repository.

type MailChannel

type MailChannel struct {
	// contains filtered or unexported fields
}

MailChannel delivers notifications over email.

func NewMailChannel

func NewMailChannel(mailer mail.Mailer) *MailChannel

NewMailChannel creates a MailChannel backed by the given Mailer.

func (*MailChannel) Name

func (c *MailChannel) Name() string

func (*MailChannel) Send

func (c *MailChannel) Send(ctx context.Context, n Notification) error

type MailableNotification

type MailableNotification interface {
	Notification
	ToMail() *mail.Message
}

MailableNotification is implemented by notifications that send an email.

type Notification

type Notification interface {
	Via() []string
}

Notification is the interface every notification must implement. Via() returns the list of channels to send on (e.g. "mail", "sms", "slack").

type Notifier

type Notifier struct {
	// contains filtered or unexported fields
}

Notifier is the central notification dispatcher. Register channels via AddChannel, then call Send.

Example:

n := notification.New()
n.AddChannel(notification.NewMailChannel(mailer))
n.Send(ctx, &WelcomeNotification{User: user})

func New

func New() *Notifier

New creates a new Notifier with no channels.

func (*Notifier) AddChannel

func (n *Notifier) AddChannel(ch Channel)

AddChannel registers a delivery channel.

func (*Notifier) Notify

func (n *Notifier) Notify(ctx context.Context, notif any) error

Notify implements engine.Notifier.

func (*Notifier) Send

func (n *Notifier) Send(ctx context.Context, notif Notification) error

Send dispatches a notification on all channels returned by n.Via(). Errors are logged but do not cause Send to fail fast — all channels are tried.

func (*Notifier) SendAll

func (n *Notifier) SendAll(ctx context.Context, notif Notification) error

SendAll dispatches notifications to a list of recipients. Each recipient is passed to recipientFn to produce the Notification.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL