notify

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 24 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package notify sends Beacon notifications to Discord, Slack, Telegram, ntfy, Pushover, Gotify, email and webhooks.

Every Beacon app uses it: Pulse to broadcast on the apps' behalf, and Pilot, Prism and Haul to send on their own when they run without Pulse. One event vocabulary, one set of providers and one settings format, so a channel set up in one place behaves the same everywhere.

Index

Constants

View Source
const (
	TypeAdded           = "added"            // a show or movie was added to the library
	TypeGrabbed         = "grabbed"          // a release was sent to the download client
	TypeDownloaded      = "downloaded"       // a download finished
	TypeImported        = "imported"         // a file landed in the library
	TypeImportFailed    = "import_failed"    // a finished download couldn't be imported
	TypeDownloadProblem = "download_problem" // a download failed or stalled
	TypeHealthIssue     = "health_issue"     // something needs attention
	TypeHealthOK        = "health_ok"        // a problem cleared
	TypeTest            = "test"             // the Send test button
)

Event types shared by every app. Apps translate their internal events into these; anything else is not sent.

View Source
const (
	// ModeAuto sends through Pulse when the app is connected to a Pulse
	// that has channels, and through the app's own channels otherwise.
	ModeAuto = "auto"
	// ModeLocal always uses the app's own channels.
	ModeLocal = "local"
)

Routing choices for an app.

View Source
const Masked = "***"

Masked is what secrets look like when settings are read back.

View Source
const SendTimeout = 20 * time.Second

SendTimeout bounds each delivery, so one slow service can't hold up the rest.

Variables

View Source
var ErrNotFound = errors.New("notify: channel not found")

ErrNotFound is returned for a channel that doesn't exist.

View Source
var ErrUnknownKind = errors.New("notify: unknown channel type")

ErrUnknownKind is returned for a provider this version doesn't have (including the retired "command" type).

View Source
var Types = []TypeInfo{
	{TypeImported, "Imported", "A show or movie is in your library, ready to watch.", []string{"pilot", "prism"}, true},
	{TypeImportFailed, "Import failed", "A finished download couldn't be added to the library.", []string{"pilot", "prism"}, true},
	{TypeDownloaded, "Download finished", "A download completed.", []string{"pilot", "prism", "haul"}, false},
	{TypeDownloadProblem, "Download problem", "A download failed or stopped making progress.", []string{"pilot", "prism", "haul"}, true},
	{TypeGrabbed, "Release grabbed", "A release was sent to the download client.", []string{"pilot", "prism"}, false},
	{TypeAdded, "Added to library", "A show or movie was added to be tracked.", []string{"pilot", "prism"}, false},
	{TypeHealthIssue, "Health problem", "Something needs attention, like an unreachable indexer or a full disk.", []string{"pilot", "prism", "haul", "pulse"}, true},
	{TypeHealthOK, "Health restored", "A health problem cleared.", []string{"pilot", "prism", "haul", "pulse"}, false},
}

Types lists the event types in the order settings screens show them.

Functions

func DefaultTypes

func DefaultTypes() []string

DefaultTypes is the quiet, useful set new channels start with.

func KnownType

func KnownType(t string) bool

KnownType reports whether t is one of the shared event types.

func Mask

func Mask(kind string, settings json.RawMessage) json.RawMessage

Mask replaces secret settings with "***" so they can be shown or returned by an API.

func Merge

func Merge(kind string, stored, incoming json.RawMessage) json.RawMessage

Merge applies edited settings over stored ones. A secret sent back as "***" (or left out) keeps the stored value, so editing a channel's name doesn't wipe its token.

func SendOne

func SendOne(ctx context.Context, ch Channel, e Event) error

SendOne delivers e to one channel regardless of its event filter (for the Send test button).

func Sign

func Sign(secret string, body []byte) string

Sign returns the hex sha256 HMAC of body, as sent in X-Beacon-Signature.

Types

type Channel

type Channel struct {
	ID       string          `json:"id"`
	Name     string          `json:"name"`
	Kind     string          `json:"kind"`
	Enabled  bool            `json:"enabled"`
	Settings json.RawMessage `json:"settings"`
	Events   []string        `json:"events"` // empty: every shared type
	Apps     []string        `json:"apps"`   // lower-case app names; empty: every app
}

Channel is a configured destination plus what it wants to hear about.

func (Channel) Wants

func (c Channel) Wants(e Event) bool

Wants reports whether the channel should receive e.

type Event

type Event struct {
	App      string            `json:"app"`                 // "Pilot", "Prism", "Haul", "Pulse"
	Type     string            `json:"type"`                // one of the Type constants
	Title    string            `json:"title"`               // one line: "Imported: Andor S02E03"
	Message  string            `json:"message,omitempty"`   // optional detail
	Level    Level             `json:"level"`               // info, success, warning, error
	Time     time.Time         `json:"time"`                // when it happened
	URL      string            `json:"url,omitempty"`       // a link for more (TMDB, IMDb, the app)
	ImageURL string            `json:"image_url,omitempty"` // a poster
	Fields   map[string]string `json:"fields,omitempty"`    // key facts: Quality, Size, Indexer
}

Event is one notification.

func TestEvent

func TestEvent(app string) Event

TestEvent is what the Send test button sends.

func (Event) Validate

func (e Event) Validate() error

Validate checks the fields every provider relies on.

type Field

type Field struct {
	Key         string    `json:"key"`
	Label       string    `json:"label"`
	Type        FieldType `json:"type"`
	Required    bool      `json:"required,omitempty"`
	Placeholder string    `json:"placeholder,omitempty"`
	Help        string    `json:"help,omitempty"`
}

Field is one setting of a provider.

func (Field) Secret

func (f Field) Secret() bool

Secret reports whether the field holds something that must not be shown.

type FieldType

type FieldType string

FieldType tells a settings screen which input to show.

const (
	FieldText     FieldType = "text"
	FieldPassword FieldType = "password" // a secret: masked when read back
	FieldURL      FieldType = "url"
	FieldNumber   FieldType = "number"
	FieldCheckbox FieldType = "checkbox"
	FieldList     FieldType = "list" // comma-separated in the form, a JSON array in settings
)

type HealthWatcher

type HealthWatcher struct {
	Outbox  *Outbox
	Check   func(ctx context.Context) []Problem
	Every   time.Duration // default 5 minutes
	Confirm int           // default 2
	// contains filtered or unexported fields
}

HealthWatcher runs an app's health check on a schedule and announces problems as they appear and clear.

  • The first check only learns what's already wrong, so restarts and deploys don't repeat old news.
  • A change has to hold for Confirm checks in a row before it's announced, so something that flickers (an indexer behind Cloudflare) doesn't send "problem" and "restored" all day.

func (*HealthWatcher) Run

func (w *HealthWatcher) Run(ctx context.Context)

Run checks until ctx ends.

type Hub

type Hub interface {
	// Notify hands an event to Pulse.
	Notify(ctx context.Context, e Event) error
	// Channels reports how many enabled channels Pulse has.
	Channels(ctx context.Context) (int, error)
}

Hub is Pulse as seen by an app: something that broadcasts on its behalf.

type Kind

type Kind struct {
	ID     string  `json:"id"`
	Name   string  `json:"name"`
	Help   string  `json:"help,omitempty"`
	Fields []Field `json:"fields"`
	// contains filtered or unexported fields
}

Kind is a provider: its settings and how to build a Sender.

func Kinds

func Kinds() []Kind

Kinds lists the providers, sorted by name, for settings screens.

type LegacyChannel

type LegacyChannel struct {
	Name     string
	Kind     string
	Enabled  bool
	Settings json.RawMessage
	Events   []string // old event names
}

LegacyChannel is a channel as Pilot and Prism stored it before.

type Level

type Level string

Level says how an event should look: colour, priority, emoji.

const (
	LevelInfo    Level = "info"
	LevelSuccess Level = "success"
	LevelWarning Level = "warning"
	LevelError   Level = "error"
)

type Outbox

type Outbox struct {
	App   string
	Store *Store
	// Hub returns Pulse, or nil when the app isn't connected to one.
	Hub func() Hub
	Log *slog.Logger

	// HoldFor is how long an event waits for Pulse before falling back.
	HoldFor time.Duration
	// RetryEvery is how often held events are retried.
	RetryEvery time.Duration
	// contains filtered or unexported fields
}

Outbox decides where each event goes and keeps a short history.

  • Mode auto, connected to a Pulse with channels: send to Pulse. If Pulse doesn't answer, hold the event and retry; after HoldFor, use the app's own channels instead (or drop it, logged, if it has none).
  • Otherwise: send to the app's own channels.

It never sends the same event both ways.

func (*Outbox) Deliver

func (o *Outbox) Deliver(ctx context.Context, e Event) Sent

Deliver sends an event that arrived from elsewhere (Pulse receiving from an app) to this app's own channels, waiting for the result.

func (*Outbox) Held

func (o *Outbox) Held() int

Held reports how many events are waiting for Pulse.

func (*Outbox) Publish

func (o *Outbox) Publish(e Event)

Publish sends an event without blocking the caller.

func (*Outbox) Recent

func (o *Outbox) Recent() []Sent

Recent returns the latest sends, newest first.

func (*Outbox) Route

func (o *Outbox) Route(ctx context.Context) Route

Route works out where events go now. Pulse's channel count is cached for a minute.

func (*Outbox) Run

func (o *Outbox) Run(ctx context.Context)

Run retries held events until ctx ends. Start it once per app.

type Problem

type Problem struct {
	// Key identifies the problem across checks ("indexer:1337x"), so a
	// reworded message for the same thing isn't announced again.
	Key     string
	Message string
	Level   Level // warning or error
}

Problem is one thing an app's health check found wrong.

type Result

type Result struct {
	ChannelID   string `json:"channel_id"`
	ChannelName string `json:"channel_name"`
	Error       string `json:"error,omitempty"`
}

Result is the outcome of sending to one channel.

func Send

func Send(ctx context.Context, channels []Channel, e Event) []Result

Send delivers e to every channel that wants it, in parallel, and reports each outcome. Channels that don't want e are skipped silently.

func (Result) OK

func (r Result) OK() bool

OK reports whether the send succeeded.

type Route

type Route struct {
	Mode           string `json:"mode"`            // auto or local
	ViaPulse       bool   `json:"via_pulse"`       // events go to Pulse
	PulseConnected bool   `json:"pulse_connected"` // the app is connected to Pulse
	PulseChannels  int    `json:"pulse_channels"`  // enabled channels in Pulse
}

Route says where an app's events go right now.

type Sender

type Sender interface {
	Send(ctx context.Context, e Event) error
}

Sender delivers events to one configured channel.

func New

func New(kind string, settings json.RawMessage) (Sender, error)

New builds a Sender for a provider from its JSON settings, checking required settings.

type Sent

type Sent struct {
	Time    time.Time `json:"time"`
	App     string    `json:"app"`
	Type    string    `json:"type"`
	Title   string    `json:"title"`
	Level   Level     `json:"level"`
	Via     string    `json:"via"`     // "Pulse" or "this app"
	Results []Result  `json:"results"` // per channel (local sends)
	Error   string    `json:"error,omitempty"`
	Pending bool      `json:"pending,omitempty"` // waiting for Pulse
}

Sent is one entry in the "recently sent" list.

type Store

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

Store keeps an app's notification channels and its routing choice in the app's own SQLite database. Every Beacon app uses the same tables, created here, so the feature is identical everywhere.

func NewStore

func NewStore(ctx context.Context, db *sql.DB) (*Store, error)

NewStore opens the store, creating its tables if needed.

func (*Store) Count

func (s *Store) Count(ctx context.Context) (total, enabled int)

Count returns how many channels exist and how many are on.

func (*Store) Create

func (s *Store) Create(ctx context.Context, c Channel) (Channel, error)

Create saves a new channel.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, id string) error

Delete removes a channel.

func (*Store) Get

func (s *Store) Get(ctx context.Context, id string) (Channel, error)

Get returns one channel.

func (*Store) HasEnabled

func (s *Store) HasEnabled(ctx context.Context) bool

HasEnabled reports whether any channel is switched on.

func (*Store) ImportLegacy

func (s *Store) ImportLegacy(ctx context.Context, old []LegacyChannel) (imported int, skipped []string, err error)

ImportLegacy copies an app's old channels in, once. Old event names are translated; a channel that listed none keeps "every event". Channels of a retired type (the old "command" script type) are skipped and returned, so the app can say so.

func (*Store) List

func (s *Store) List(ctx context.Context) ([]Channel, error)

List returns every channel, by name, with settings as stored.

func (*Store) Mode

func (s *Store) Mode(ctx context.Context) string

Mode returns the app's routing choice (ModeAuto unless set).

func (*Store) SetMode

func (s *Store) SetMode(ctx context.Context, mode string) error

SetMode stores the routing choice.

func (*Store) Update

func (s *Store) Update(ctx context.Context, id string, c Channel) (Channel, error)

Update replaces a channel's fields. Secrets sent back masked are kept.

type TypeInfo

type TypeInfo struct {
	ID          string   `json:"id"`
	Label       string   `json:"label"`
	Description string   `json:"description"`
	Apps        []string `json:"apps"`    // which apps send it
	Default     bool     `json:"default"` // on for new channels
}

TypeInfo describes an event type for settings screens.

Directories

Path Synopsis
Package notifyapi is the notifications settings API, the same in every Beacon app: channels, test sends, routing and the recent list.
Package notifyapi is the notifications settings API, the same in every Beacon app: channels, test sends, routing and the recent list.

Jump to

Keyboard shortcuts

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