Documentation
¶
Overview ¶
Package push sends the web push notifications of the runtime: a finished turn or a question of an agent, to the person's subscribed browsers (RFC 8030 delivery, RFC 8291 message encryption, RFC 8292 VAPID). The subscriptions and the key pair are files in the data folder, shared with the page's subscribe route.
Index ¶
- Constants
- func Clip(text string, max int) string
- func Decrypt(body []byte, uaPrivate *ecdh.PrivateKey, authSecret []byte) ([]byte, error)
- func Encrypt(plaintext, uaPublic, authSecret []byte) ([]byte, error)
- func EncryptWith(plaintext, uaPublic, authSecret []byte, asPrivate *ecdh.PrivateKey, ...) ([]byte, error)
- func StripMarkdown(text string) string
- func Topic(tag string) string
- type Config
- type Message
- type Sender
- type Store
- func (s *Store) Add(endpoint string, keys any, deviceID string, mobile *bool) error
- func (s *Store) DeviceEndpoints() []any
- func (s *Store) MarkHidden(deviceID string)
- func (s *Store) MarkVisible(deviceID string)
- func (s *Store) Remove(endpoint string) error
- func (s *Store) Subscriptions() []Subscription
- func (s *Store) Visible() map[string]bool
- type Subscription
- type VAPIDKeys
Constants ¶
const SubscriptionsFile = "push-subscriptions.json"
SubscriptionsFile is the browsers' push subscriptions in the data folder.
const TTL = 6 * time.Hour
TTL is how long a push service keeps an undelivered notification.
const VAPIDFile = "vapid-keys.json"
VAPIDFile is the key pair's file in the data folder.
const VisibilityTTL = 60 * time.Second
VisibilityTTL: a device that has not said it is visible for this long counts as not looked at.
Variables ¶
This section is empty.
Functions ¶
func Decrypt ¶
Decrypt is the user agent's side, for tests and a fake push service: it opens an aes128gcm body with the subscription's private key and auth secret.
func EncryptWith ¶
func EncryptWith(plaintext, uaPublic, authSecret []byte, asPrivate *ecdh.PrivateKey, salt []byte) ([]byte, error)
EncryptWith is RFC 8291 message encryption with a given sender key and salt (the test vector's); Encrypt draws them. uaPublic is the subscription's p256dh key (65 bytes, uncompressed), authSecret its auth secret (16 bytes). The result is the aes128gcm body: salt, record size, key id (the sender's public key) and one record.
func StripMarkdown ¶
StripMarkdown is an answer as plain text for a notification: code blocks dropped, links and emphasis unwrapped, headings, quotes and list marks removed, everything on one line.
Types ¶
type Config ¶
type Config struct {
// Dir is the data folder holding the subscriptions and the key pair.
Dir string
// Subject is the VAPID subject: a reachable https: or mailto: URL (Apple refuses others).
Subject string
// TagPrefix starts a notification's tag (prefix + tab id): a newer push of a conversation replaces the older.
TagPrefix string
Client *http.Client
Now func() time.Time
Log func(level, msg string, kv ...any)
}
Config is what the sender needs; the deployment names the product parts.
type Message ¶
type Message struct {
Kind string `json:"kind"`
Title string `json:"title"`
Body string `json:"body"`
ConversationTitle string `json:"conversationTitle"`
Detail string `json:"detail"`
Provider string `json:"provider"`
TabID string `json:"tabId"`
WorkspaceID string `json:"workspaceId"`
AgentSessionID *string `json:"agentSessionId"`
Tag string `json:"tag"`
At int64 `json:"at"`
// URL is where a tap leads (a pushed design board); absent for the others.
URL string `json:"url,omitempty"`
}
Message is what the page's service worker and its in-app toast render; the keys are in the order the page's writer put them.
func BuildMessage ¶
BuildMessage writes the notification of a finished turn ("done") or a question ("question"): the conversation's title, what the person has to do, and the words of the answer or the question from the transcript's tail.
type Sender ¶
type Sender struct {
Store *Store
// contains filtered or unexported fields
}
Sender sends pushes and implements status.Pusher.
func (*Sender) PublicKey ¶
PublicKey is the VAPID public key the page subscribes with (created with the pair when missing).
func (*Sender) Push ¶
Push sends a turn's notification ("done" or "question") to the devices it is for, dropping subscriptions the push service says are gone (404, 410). It implements status.Pusher.
type Store ¶
Store reads and edits the subscriptions file and remembers which devices are on screen. Every writer of this process goes through one Store and its lock (the sender dropping a gone subscription, the page's subscribe route); a writer in another process (the previous runtime's routes, while their slice is off) replaces the file whole, as these do.
func (*Store) Add ¶
Add stores a browser's subscription ({endpoint, keys} as the browser gave them) with the device that made it. A device subscribing again replaces its earlier subscription; an unchanged record is not written again (every page load registers).
func (*Store) DeviceEndpoints ¶
DeviceEndpoints is {endpoint} of every stored subscription, in the file's order.
func (*Store) MarkHidden ¶
MarkHidden: the device hid the app.
func (*Store) MarkVisible ¶
MarkVisible: the device shows the app now.
func (*Store) Remove ¶
Remove drops the subscriptions of an endpoint (one the push service said is gone, or the page unsubscribed); the others are written back as they were.
func (*Store) Subscriptions ¶
func (s *Store) Subscriptions() []Subscription
Subscriptions are every stored subscription; none when the file is missing or unreadable.
type Subscription ¶
type Subscription struct {
Endpoint string `json:"endpoint"`
Keys struct {
P256dh string `json:"p256dh"`
Auth string `json:"auth"`
} `json:"keys"`
DeviceID string `json:"deviceId,omitempty"`
Mobile *bool `json:"mobile,omitempty"`
}
Subscription is one browser's push subscription, with the device that made it.
func SelectTargets ¶
func SelectTargets(subs []Subscription, visible map[string]bool) []Subscription
SelectTargets: while someone has the app open on a device, only that device is notified (its page turns the push into a toast and the phone in the pocket stays quiet); nobody looking, every device is.
type VAPIDKeys ¶
VAPIDKeys are the application server's key pair as the file holds it: base64url without padding, the public key uncompressed (65 bytes), the private key the raw scalar (32 bytes). Every runtime of a person must use the same pair: a browser subscribes once, against one key.
func LoadOrCreateVAPID ¶
LoadOrCreateVAPID reads the key pair from dir, or creates and stores one when the file is missing or unreadable (0600, through a temporary file).