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
- Variables
- func DefaultTypes() []string
- func KnownType(t string) bool
- func Mask(kind string, settings json.RawMessage) json.RawMessage
- func Merge(kind string, stored, incoming json.RawMessage) json.RawMessage
- func SendOne(ctx context.Context, ch Channel, e Event) error
- func Sign(secret string, body []byte) string
- type Channel
- type Event
- type Field
- type FieldType
- type HealthWatcher
- type Hub
- type Kind
- type LegacyChannel
- type Level
- type Outbox
- type Problem
- type Result
- type Route
- type Sender
- type Sent
- type Store
- func (s *Store) Count(ctx context.Context) (total, enabled int)
- func (s *Store) Create(ctx context.Context, c Channel) (Channel, error)
- func (s *Store) Delete(ctx context.Context, id string) error
- func (s *Store) Get(ctx context.Context, id string) (Channel, error)
- func (s *Store) HasEnabled(ctx context.Context) bool
- func (s *Store) ImportLegacy(ctx context.Context, old []LegacyChannel) (imported int, skipped []string, err error)
- func (s *Store) List(ctx context.Context) ([]Channel, error)
- func (s *Store) Mode(ctx context.Context) string
- func (s *Store) SetMode(ctx context.Context, mode string) error
- func (s *Store) Update(ctx context.Context, id string, c Channel) (Channel, error)
- type TypeInfo
Constants ¶
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.
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.
const Masked = "***"
Masked is what secrets look like when settings are read back.
const SendTimeout = 20 * time.Second
SendTimeout bounds each delivery, so one slow service can't hold up the rest.
Variables ¶
var ErrNotFound = errors.New("notify: channel not found")
ErrNotFound is returned for a channel that doesn't exist.
var ErrUnknownKind = errors.New("notify: unknown channel type")
ErrUnknownKind is returned for a provider this version doesn't have (including the retired "command" type).
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 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.
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.
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.
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.
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.
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 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 ¶
Deliver sends an event that arrived from elsewhere (Pulse receiving from an app) to this app's own channels, waiting for the result.
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.
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 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 (*Store) HasEnabled ¶
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.
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. |