push

package
v0.80.1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package push persists Web Push subscriptions + the in-app notification feed and delivers notifications to both. The VAPID key pair is generated once and stored alongside the subscriptions, so reinstalling the container keeps the browser subscriptions valid as long as the state dir survives.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Notification

type Notification struct {
	ID        int       `json:"id"`
	UserID    int       `json:"userId"`
	Title     string    `json:"title"`
	Body      string    `json:"body"`
	Magnet    string    `json:"magnet,omitempty"`
	Read      bool      `json:"read"`
	CreatedAt time.Time `json:"createdAt"`
}

Notification is one entry of the in-app feed (the bell).

type Sender

type Sender struct {
	// contains filtered or unexported fields
}

Sender fans a notification out to the user's in-app feed and every Web Push subscription they registered. Failures are best-effort: a dead endpoint (404/410 from the push service) is dropped, anything else is just logged.

func NewSender

func NewSender(store *Store) (*Sender, error)

NewSender loads (or generates) the VAPID pair and returns a ready Sender.

func (*Sender) NotifyUser

func (s *Sender) NotifyUser(ctx context.Context, userID int, title, body, magnet string) error

NotifyUser records the notification in the in-app feed and pushes it to all of the user's browser subscriptions. The feed write is the source of truth — push delivery is opportunistic.

func (*Sender) PublicKey

func (s *Sender) PublicKey() string

PublicKey exposes the VAPID public key for PushManager.subscribe.

type Store

type Store struct {
	// contains filtered or unexported fields
}

func New

func New(pool *sql.DB) (*Store, error)

New wires the push store onto the shared Postgres pool. Schema is applied centrally (internal/db migrations).

func (*Store) AddNotification

func (s *Store) AddNotification(userID int, title, body, magnet string) error

AddNotification appends to the in-app feed, trimming the oldest entries past maxFeedPerUser so the table can't grow unbounded.

func (*Store) Close

func (s *Store) Close()

Close is a no-op: the shared pool's lifecycle is owned by main.

func (*Store) DeleteEndpoint

func (s *Store) DeleteEndpoint(endpoint string) error

DeleteEndpoint drops an endpoint regardless of owner — used when the push service reports it gone (404/410).

func (*Store) LoadOrCreateVAPID

func (s *Store) LoadOrCreateVAPID() (publicKey, privateKey string, err error)

LoadOrCreateVAPID returns the persisted VAPID pair, generating it on first boot. Rotating the pair would invalidate every browser subscription, so it is generated exactly once.

func (*Store) MarkAllRead

func (s *Store) MarkAllRead(userID int) error

MarkAllRead flags the whole feed as read.

func (*Store) Notifications

func (s *Store) Notifications(userID, limit int) ([]Notification, error)

Notifications returns the newest entries of the user's feed.

func (*Store) Subscribe

func (s *Store) Subscribe(userID int, endpoint, p256dh, auth string) error

Subscribe upserts a browser endpoint for the user. An endpoint that changes hands (browser profile re-login as another user) is re-owned, not duplicated.

func (*Store) SubscriptionsFor

func (s *Store) SubscriptionsFor(userID int) ([]Subscription, error)

SubscriptionsFor lists the user's registered endpoints.

func (*Store) UnreadCount

func (s *Store) UnreadCount(userID int) (int, error)

UnreadCount returns how many feed entries the user hasn't read.

func (*Store) Unsubscribe

func (s *Store) Unsubscribe(userID int, endpoint string) error

Unsubscribe removes the endpoint when owned by the user.

type Subscription

type Subscription struct {
	UserID   int
	Endpoint string
	P256dh   string
	Auth     string
}

Subscription is one browser push endpoint owned by a user.

Jump to

Keyboard shortcuts

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