Documentation
¶
Overview ¶
Package push sends notifications to people's phones and computers with standard Web Push: Safari on macOS, Home Screen web apps on iOS and iPadOS, and Chromium and Firefox, through one pipeline and with no Apple developer account.
A device subscribes once, on the portal, and then gets Outpost's own notifications and those of every app its user can open and hasn't muted. Apps don't see subscriptions. Each is started with OUTPOST_PUSH_URL, a loopback endpoint this package serves, and OUTPOST_PUSH_TOKEN, its own, and names who a notification is for; Outpost decides which devices that means. Tapping one opens the portal, which sends the device on to the app, so a notification's link is always the subscription's own origin, as Declarative Web Push requires. See docs/design/push.md.
Index ¶
- Constants
- Variables
- func IsBadRequest(err error) bool
- func ValidSubject(s string) error
- type Device
- type Gate
- type Notification
- type Options
- type Service
- func (s *Service) Close() error
- func (s *Service) Devices(username string) ([]Device, error)
- func (s *Service) Forget(username, id string) (bool, error)
- func (s *Service) Muted(username string) (map[string]bool, error)
- func (s *Service) PublicKey() string
- func (s *Service) ReserveBus(ctx context.Context, b *bus.Service) error
- func (s *Service) Resolve(service string) (map[string]string, string)
- func (s *Service) Run(ctx context.Context)
- func (s *Service) RunBus(ctx context.Context, b *bus.Service) error
- func (s *Service) Send(from string, to []string, n Notification) (int, error)
- func (s *Service) Serve(ctx context.Context, ln net.Listener) error
- func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Service) SetGate(g Gate)
- func (s *Service) SetMuted(username, service string, muted bool) error
- func (s *Service) Status() Stats
- func (s *Service) Subscribe(username, signIn, endpoint, p256dh, auth, userAgent string) error
- func (s *Service) URL() string
- func (s *Service) Unsubscribe(username, endpoint string) error
- type Stats
- type Subscription
Constants ¶
const ( BusSubject = "outpost.*.notification.>" BusConsumer = "outpost-push-notifications" )
Bus notification subscription constants. Apps publish CloudEvents under outpost.<app>.notification.<action> (for example outpost.domestique.notification.created), and the shared push service subscribes to deliver them.
const ( EnvURL = "OUTPOST_PUSH_URL" EnvToken = "OUTPOST_PUSH_TOKEN" )
Environment variables every app is started with.
const DefaultTTL = 24 * 60 * 60
DefaultTTL is how long a push service holds a notification for a device that's off, when the sender doesn't say.
const MaxPayload = recordSize - headerLen - 1 - 16
MaxPayload is the largest plaintext that still fits one 4096-byte record once the header, the delimiter and the GCM tag are added, which is the most a push service has to take (RFC 8030 §7.2).
const Outpost = ""
Outpost is the sender name of Outpost's own notifications. No app can be called it: a service's name is never empty.
Variables ¶
var ( ErrNotReady = errors.New("notifications aren't available: the login gateway isn't running") ErrLimit = fmt.Errorf("at most %d notifications an hour from one app", sendLimit) )
Errors Send returns besides a bad notification.
Functions ¶
func IsBadRequest ¶
IsBadRequest reports whether err is a fault in the notification itself.
func ValidSubject ¶
ValidSubject checks a VAPID contact: Apple refuses a request whose sub isn't a mailto: or https: URI.
Types ¶
type Device ¶
type Device struct {
ID string
SignIn string
UserAgent string
Platform string
CreatedAt time.Time
LastSuccess time.Time
Failing bool
}
Device is a subscription as its owner sees it: no keys, and no endpoint, which is as good as one.
type Gate ¶
type Gate interface {
// MayReceive reports whether username may be sent service's
// notifications; Outpost is Outpost's own.
MayReceive(service, username string) bool
// SignIns lists username's live sign-ins by ID; an error means none
// can be checked now, not that there are none.
SignIns(username string) (map[string]bool, error)
// from service about path opens.
Navigate(service, path string) string
}
Gate is what Send needs from the gateway: who may receive what, whether a device is still signed in, and where tapping a notification goes.
type Notification ¶
type Notification struct {
// Title is required; Body is the line under it.
Title string `json:"title"`
Body string `json:"body,omitempty"`
// Path is where on the sender tapping it goes: a path on the app's own
// host for an app, on the portal for Outpost's own. Unset is "/".
Path string `json:"path,omitempty"`
// Tag replaces an earlier notification with the same tag on a device,
// rather than stacking another.
Tag string `json:"tag,omitempty"`
// Badge, when set, is the number on the Home Screen or Dock icon; 0
// clears it.
Badge *int `json:"badge,omitempty"`
// Silent asks the device not to make a sound.
Silent bool `json:"silent,omitempty"`
// Urgency is very-low, low, normal (the default) or high: how far a
// device on low battery goes to deliver it.
Urgency string `json:"urgency,omitempty"`
// TTL is how long, in seconds, the push service holds it for a device
// that is off; 0 is the default, a day.
TTL int `json:"ttl,omitempty"`
// Topic replaces one still waiting at the push service with the same
// topic, so a device that was off gets only the latest.
Topic string `json:"topic,omitempty"`
}
Notification is one message for people's devices.
type Options ¶
type Options struct {
// Path is push.db.
Path string
// Listen is the loopback address the endpoint serves on, which apps are
// told as OUTPOST_PUSH_URL.
Listen string
// Subject is the VAPID contact a push service can reach the operator
// at: a mailto: or https: URI.
Subject string
// Logf receives a line per subscription change and per failed delivery.
Logf func(format string, v ...any)
}
Options configures a Service.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service holds the subscriptions and sends to them. It is safe for concurrent use.
func Open ¶
Open opens push.db, making the VAPID key the first time; Serve starts the endpoint and Run delivers.
func (*Service) Forget ¶
Forget removes one of username's subscriptions; it reports whether there was one.
func (*Service) ReserveBus ¶ added in v0.9.14
ReserveBus creates the push service's consumer on b ahead of RunBus. The daemon calls it before the first apply: an app's consumer name is its name and subject joined with '-', so an app called "outpost" subscribing to outpost.push.notifications would otherwise claim this name first, and notifications sent over the bus would go nowhere.
func (*Service) Resolve ¶
Resolve gives an app its OUTPOST_PUSH_URL and OUTPOST_PUSH_TOKEN. The token is an HMAC of its name, so it is the same on every start and needs no revision of its own.
func (*Service) Run ¶
Run delivers queued notifications until ctx ends. Deliveries still queued then are dropped: a notification is for now, and a restart is brief.
func (*Service) RunBus ¶ added in v0.9.14
RunBus subscribes the push service to notification events on b until ctx ends. Delivery is at-least-once and deduplicated on CloudEvents id in push.db.
func (*Service) Send ¶
Send queues n from from for to's devices, or, with no one named, for everyone with a device who may receive from's notifications. Someone who can't open the app, has muted it, or has no device gets nothing, and that isn't an error. It reports how many devices it was queued for; delivery happens in Run.
func (*Service) ServeHTTP ¶
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP is the endpoint apps call.
func (*Service) SetGate ¶
SetGate tells the Service who may receive what, once the gateway exists. Until then nothing is sent.
func (*Service) Subscribe ¶
Subscribe keeps a browser's subscription for username, made from the sign-in signIn. It replaces one with the same endpoint.
func (*Service) Unsubscribe ¶
Unsubscribe forgets username's subscription at endpoint, as a browser turning notifications off asks.
type Stats ¶
type Stats struct {
Delivered int `json:"delivered"`
// Gone are subscriptions the push service said no longer exist, which
// are forgotten.
Gone int `json:"gone"`
// Failed are deliveries given up on.
Failed int `json:"failed"`
// Dropped are deliveries that didn't fit the queue.
Dropped int `json:"dropped"`
}
Stats counts deliveries since the daemon started.
type Subscription ¶
type Subscription struct {
ID string
Username string
// SignIn is the sign-in it was made from; when that ends, so does this.
SignIn string
Endpoint string
P256dh string
Auth string
UserAgent string
Platform string
CreatedAt time.Time
// LastSuccess is the last push the service took for it, zero if none.
LastSuccess time.Time
FailureCount int
}
Subscription is one device's subscription, as a browser made it. Keys are credentials: anyone holding them and the endpoint can send to the device, so they are never logged or shown.