Documentation
¶
Overview ¶
Package push forwards notification news to external channels: Web Push to registered devices (the installed web app on iPhone, or any browser) and registered webhooks. It listens on the notifier fan-out, waits a short delay, and re-checks the unread state before sending, so news read on a visibly open page never rings a phone.
Index ¶
- Variables
- type Message
- type Notifier
- type Service
- type Subscription
- type TestChannel
- type WebPush
- func (w *WebPush) Deliver(msg Message) error
- func (w *WebPush) Devices() []Subscription
- func (w *WebPush) LiveCount() int
- func (w *WebPush) PublicKey() string
- func (w *WebPush) Register(sub Subscription) error
- func (w *WebPush) RemoveDevice(id string) bool
- func (w *WebPush) RemoveEndpoint(endpoint string) bool
- func (w *WebPush) Stale(sub Subscription) bool
- type Webhook
- type Webhooks
Constants ¶
This section is empty.
Variables ¶
var ErrUnknownWebhook = errors.New("unknown webhook")
ErrUnknownWebhook reports a send for a webhook id that is not registered anymore.
Functions ¶
This section is empty.
Types ¶
type Message ¶
Message is one delivery to an external channel. Tag carries the target id, so channels that support it collapse to one notification per target, matching the one unread entry per target model.
func TestMessage ¶
func TestMessage(channel TestChannel) Message
TestMessage is what a test button on the settings page sends. It names the channel it went out on, because both buttons sit on the same page and a notification that only says that something arrived leaves the reader guessing which of the two just worked.
type Notifier ¶
type Notifier interface {
Subscribe() (<-chan notify.Event, func())
UnreadTargets() map[string]bool
}
Notifier is the slice of notify.Service the dispatcher needs.
type Service ¶
type Service struct {
WebPush *WebPush
Webhooks *Webhooks
// contains filtered or unexported fields
}
Service bundles the push channels and their state.
func NewService ¶
NewService loads or creates the push state in stateDir: the VAPID identity, the device subscriptions, and the per-channel configuration.
func (*Service) SetBaseURL ¶
SetBaseURL stores the public address, "" turns absolute links off.
type Subscription ¶
type Subscription struct {
ID string `json:"id"`
Endpoint string `json:"endpoint"`
P256dh string `json:"p256dh"`
Auth string `json:"auth"`
Label string `json:"label"`
VAPIDKey string `json:"vapidKey"`
CreatedAt time.Time `json:"createdAt"`
}
Subscription is one registered Web Push device. VAPIDKey records the public key the device subscribed with, so a later key change can tell which entries are dead.
type TestChannel ¶
type TestChannel string
TestChannel is which of the settings page test buttons was pressed.
const ( // TestWebPush is the button next to the registered devices. TestWebPush TestChannel = "webpush" // TestWebhook is the button on a single webhook. TestWebhook TestChannel = "webhook" )
type WebPush ¶
type WebPush struct {
// contains filtered or unexported fields
}
WebPush sends system notifications to the registered devices. The payload is rendered by the service worker (static/sw.js), which shows one notification per target via the tag.
func (*WebPush) Deliver ¶
Deliver sends msg to every live device. Subscriptions the push service reports gone (404 or 410) prune themselves.
func (*WebPush) Devices ¶
func (w *WebPush) Devices() []Subscription
Devices returns the registered subscriptions.
func (*WebPush) LiveCount ¶
LiveCount returns how many registered devices can currently receive pushes.
func (*WebPush) PublicKey ¶
PublicKey is the VAPID application server key the client subscribes with.
func (*WebPush) Register ¶
func (w *WebPush) Register(sub Subscription) error
Register stores a device subscription posted by the settings page.
func (*WebPush) RemoveDevice ¶
RemoveDevice drops a subscription by id (the settings page device list) and reports whether it existed.
func (*WebPush) RemoveEndpoint ¶
RemoveEndpoint drops a subscription by endpoint, used when a device unsubscribes itself.
func (*WebPush) Stale ¶
func (w *WebPush) Stale(sub Subscription) bool
Stale reports whether the subscription was made against older VAPID keys. The push service rejects sends to it, so delivery skips it and the settings page presents it as dead.
type Webhook ¶
Webhook is one registered notification webhook. The URL is a bearer credential, so the list lives in the 0600 channel config.
type Webhooks ¶
type Webhooks struct {
// contains filtered or unexported fields
}
Webhooks posts every notification to each registered webhook. The JSON payload carries text, title, body, and url; the text field makes Slack incoming webhooks work as is, extra fields are ignored there.