Documentation
¶
Overview ¶
Package notify decides, server-side, who gets a Web Push for a message or an alert (plan pipeline steps 6 and 7). In-app, browser and tray notifications are decided by the clients with the same pkg/notifyrules; the server only owns the case where no window is looking: it applies the rules to every channel member and hands the notifiable ones, who are not focused on a bubbles window, to the push sender.
Index ¶
Constants ¶
const FocusWindow = 60 * time.Second
FocusWindow is how recently a user must have had a visible, focused window for the server to hold back Web Push (plan pipeline step 7): the browser already shows the message, and a push on top of it would be noise. It covers the gap while a window is hidden, a device sleeps or a socket reconnects.
const QueueSize = 10000
QueueSize bounds the events waiting for the dispatcher. The writer goroutine never waits for it: when the queue is full an event is dropped and counted (a missed push is better than a stalled write path).
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dispatcher ¶
type Dispatcher struct {
// contains filtered or unexported fields
}
Dispatcher turns published events into push notifications. It implements events.Publisher: install it next to the hub with events.NewMultiPublisher.
func New ¶
func New(o Options) *Dispatcher
New returns a stopped Dispatcher; Start runs it. Events published before Start wait in the queue.
func (*Dispatcher) Close ¶
func (d *Dispatcher) Close()
Close stops the dispatcher, discarding queued events. It is idempotent.
func (*Dispatcher) Dropped ¶
func (d *Dispatcher) Dropped() int64
Dropped is the number of events discarded because the queue was full.
func (*Dispatcher) Handle ¶
func (d *Dispatcher) Handle(ctx context.Context, e events.Event)
Handle processes one event synchronously. The dispatch goroutine calls it for every queued event; tests call it directly.
Only message.created (text and alert messages) and alert.updated notify. A new alert message counts as the alert's "created" transition; alert.updated carries its own transition (protocol.AlertUpdatedEvent).
func (*Dispatcher) Publish ¶
func (d *Dispatcher) Publish(e events.Event)
Publish implements events.Publisher. It never blocks: events that cannot notify anyone are ignored, the rest are queued in publish (= seq) order, and a full queue drops the event and counts it.
func (*Dispatcher) Start ¶
func (d *Dispatcher) Start()
Start launches the dispatch goroutine. It is idempotent.
type Options ¶
type Options struct {
// DB supplies channel members and their notification preferences.
DB *store.DB
// Settings, Presence and Pusher are required.
Settings Settings
Presence Presence
Pusher Pusher
// Now is the clock of the mute check; nil means time.Now.
Now func() time.Time
// Logger receives drops and failures; it never sees message text.
Logger *slog.Logger
// QueueSize defaults to the package QueueSize.
QueueSize int
}
Options configures New.
type Presence ¶
type Presence interface {
// Online reports whether the user is online (active) now; it decides
// whether @here reaches them.
Online(userID string) bool
// UserFocusedRecently reports whether the user has a connection whose
// window is visible now, or had one within the given duration.
UserFocusedRecently(userID string, within time.Duration) bool
}
Presence is what the dispatcher needs to know about live connections. The realtime hub implements it: connections report window visibility with `focus` frames, and presence follows connection activity.
type Pusher ¶
type Pusher interface {
// HasSubscriptions reports whether the user has a push subscription.
HasSubscriptions(ctx context.Context, userID string) bool
// Send queues a notification for every subscription of the user.
Send(userID string, n push.Notification)
}
Pusher is the Web Push sender (*push.Service).