push

package
v1.57.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 20 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

type Message struct {
	Title string
	Body  string
	URL   string
	Tag   string
}

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

func NewService(stateDir string) (*Service, error)

NewService loads or creates the push state in stateDir: the VAPID identity, the device subscriptions, and the per-channel configuration.

func (*Service) BaseURL

func (s *Service) BaseURL() string

BaseURL returns the configured public address of the cockpit.

func (*Service) SetBaseURL

func (s *Service) SetBaseURL(base string)

SetBaseURL stores the public address, "" turns absolute links off.

func (*Service) Start

func (s *Service) Start(n Notifier)

Start subscribes to the notifier fan-out before returning, so no event published afterwards can be missed, and drains it in a goroutine for the process lifetime: news still unread after the delay goes to every channel.

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

func (w *WebPush) Deliver(msg Message) error

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

func (w *WebPush) LiveCount() int

LiveCount returns how many registered devices can currently receive pushes.

func (*WebPush) PublicKey

func (w *WebPush) PublicKey() string

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

func (w *WebPush) RemoveDevice(id string) bool

RemoveDevice drops a subscription by id (the settings page device list) and reports whether it existed.

func (*WebPush) RemoveEndpoint

func (w *WebPush) RemoveEndpoint(endpoint string) bool

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

type Webhook struct {
	ID  string `json:"id"`
	URL string `json:"url"`
}

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.

func (*Webhooks) Add

func (w *Webhooks) Add(url string) error

Add registers a webhook URL. Duplicates are rejected.

func (*Webhooks) Deliver

func (w *Webhooks) Deliver(msg Message) error

Deliver posts msg to every registered webhook.

func (*Webhooks) List

func (w *Webhooks) List() []Webhook

List returns the registered webhooks.

func (*Webhooks) Remove

func (w *Webhooks) Remove(id string) bool

Remove drops a webhook by id and reports whether it existed.

func (*Webhooks) Test

func (w *Webhooks) Test(id string, msg Message) error

Test posts msg to one webhook by id.

Jump to

Keyboard shortcuts

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