notify

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 16 Imported by: 0 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 tells the user about the items that newly need them, and keeps a log of everything it told.

After each refresh, the inbox reports its arrivals (see inbox.Arrival): pull requests that newly ask for the user's review, items newly assigned to them, and snoozes that new activity ended early. The Dispatcher records each one in the notifications table, then shows a native banner for each kind the user has not turned off, through the desktop shell's Poster. More than MaxBanners at once are shown as one banner that sums them up, so a morning's backlog does not bury the screen. The log is written whether or not a banner is shown, and whether or not the shell can show banners at all, so the window's notification history always has every one. Only the newest Keep entries are kept.

Service is what the window binds: the log, marking it read, clearing it, the state of the banners, and a test banner. Nothing here leaves the machine: banners are posted to the operating system, and the log is a local table.

Index

Constants

View Source
const (
	// StatusOn means banners are shown.
	StatusOn = "on"

	// StatusOff means the user has not allowed the app's banners, or turned them off in
	// the system's settings.
	StatusOff = "off"

	// StatusUnavailable means this build cannot show banners: a development run, which has
	// no app bundle to own them, or an OS the shell does not post to yet.
	StatusUnavailable = "unavailable"
)

Banner states, as Poster.Status reports them.

View Source
const (
	// KindReviewRequested is a pull request that newly asks for the user's review.
	KindReviewRequested = inbox.ArrivalReviewRequested

	// KindAssigned is an issue or pull request newly assigned to the user.
	KindAssigned = inbox.ArrivalAssigned

	// KindWoke is a snoozed item that new activity brought back early.
	KindWoke = inbox.ArrivalWoke
)

Kinds of notification. They are the inbox's kinds of arrival.

View Source
const Keep = 200

Keep is how many notifications the log keeps; recording more drops the oldest.

View Source
const MaxBanners = 3

MaxBanners is how many banners one refresh shows one by one; more are summed up in one.

View Source
const PrefBannersOff = "notify.bannersOff"

PrefBannersOff is the prefs key listing, as a JSON array, the kinds of notification whose banners are off. Kinds are on unless listed, so a kind added later starts on.

Variables

View Source
var (
	// ErrBannersUnavailable is returned by SendTest when this build cannot show banners.
	ErrBannersUnavailable = errors.New("this build cannot show notifications; they need the packaged app")

	// ErrBannersOff is returned by SendTest when the user has not allowed banners.
	ErrBannersOff = errors.New("notifications are off for this app in System Settings")
)
View Source
var ErrUnknownKind = errors.New("unknown kind of notification")

ErrUnknownKind is returned for a kind of notification that is not one of Kinds.

Functions

func DecodeBannersOff

func DecodeBannersOff(stored map[string]string) []string

DecodeBannersOff reads the kinds whose banners are off from the stored prefs. A value that cannot be read turns none off, and kinds that are not known are dropped.

func EncodeBannersOff

func EncodeBannersOff(off []string) string

EncodeBannersOff writes the kinds whose banners are off for the prefs table.

func Kinds

func Kinds() []string

Kinds returns the kinds of notification in the order Settings lists them.

func SetBanner

func SetBanner(off []string, kind string, on bool) ([]string, error)

SetBanner turns a kind's banners on or off in off, the kinds whose banners are off, and returns the new list in the order of Kinds.

func WakeDetail

func WakeDetail(r snooze.Reasons) string

WakeDetail says what ended a snooze early, most decisive first, as the inbox card does: "changes requested, 2 new commits, 3 new comments". It is "" when nothing did.

Types

type Banner struct {
	// ID identifies the banner to the system; posting the same ID again replaces it.
	ID string

	Title    string
	Subtitle string
	Body     string

	// Thread groups the banners of one kind in the system's notification list.
	Thread string

	// ItemID is the item a click on the banner shows, or 0 for none. URL is the item on
	// GitHub, for when it is no longer in the inbox.
	ItemID int64
	URL    string
}

Banner is one native notification.

func Banners

func Banners(rows []model.Notification, off []string) []Banner

Banners returns what to show for recorded notifications: one banner each, or a single summary when there are more than MaxBanners. It shows none of the kinds in off.

func TestBanner

func TestBanner() Banner

TestBanner is the banner Service.SendTest shows.

type Clock

type Clock interface {
	Now() time.Time
}

Clock tells the time.

type Config

type Config struct {
	DB       *gorm.DB
	Poster   Poster
	Notifier Notifier
	Clock    Clock
	Logger   *slog.Logger
}

Config holds the dependencies of a Dispatcher and a Service. Poster, Notifier, Clock, and Logger are optional: without a Poster no banner is shown, and the log is still kept.

type Dispatcher

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

Dispatcher records the inbox's arrivals and shows their banners. It is the inbox's inbox.ArrivalSink, and is not bound to the window.

func NewDispatcher

func NewDispatcher(cfg Config) *Dispatcher

NewDispatcher returns a Dispatcher.

func (*Dispatcher) Arrived

func (d *Dispatcher) Arrived(ctx context.Context, arrivals []inbox.Arrival)

Arrived records a notification for each arrival, tells the window, and shows banners for the kinds that are on, if the user allows banners. A failure is logged: the refresh that found the arrivals has already been stored, and nothing is gained by failing it.

type Log

type Log struct {
	Items  []Notification `json:"items"`
	Unread int            `json:"unread"`
}

Log is the notification history: the newest first, and how many are unread.

type Notification

type Notification struct {
	ID int64 `json:"id"`

	// Kind is KindReviewRequested, KindAssigned, or KindWoke.
	Kind string `json:"kind"`

	// CreatedAt is when it was recorded, as RFC 3339.
	CreatedAt string `json:"createdAt"`

	// Read reports whether the user has seen it in the history.
	Read bool `json:"read"`

	// ItemID is the item's id in the inbox snapshot, or 0 once the item is no longer
	// stored.
	ItemID int64 `json:"itemId"`

	// ItemKind is "pr" or "issue".
	ItemKind string `json:"itemKind"`

	// Repo is "owner/name".
	Repo   string `json:"repo"`
	Number int    `json:"number"`
	Title  string `json:"title"`
	URL    string `json:"url"`
	Author string `json:"author"`

	// Detail is what ended a snooze early, in a few words ("approved, 2 new commits"), for
	// KindWoke, and "" otherwise.
	Detail string `json:"detail"`
}

Notification is one entry in the log, as the window shows it. Every field is a plain value.

type Notifier

type Notifier interface {
	NotificationsUpdated()
}

Notifier is told when the log changed, so the window reads it again.

type Poster

type Poster interface {
	// Post shows a banner. It never waits on the user: a banner the user has not allowed
	// is not shown, and Post reports why.
	Post(b Banner) error

	// Status reports StatusOn, StatusOff, or StatusUnavailable.
	Status() string

	// Authorize asks the user to allow banners, if they have not been asked, and reports
	// whether banners are allowed. It may wait for the user's answer.
	Authorize() (bool, error)
}

Poster shows banners. The desktop shell implements it over the system's notification center.

type Service

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

Service is the notification log as the window binds it.

func NewService

func NewService(cfg Config) *Service

NewService returns a Service.

func (*Service) BannerStatus

func (s *Service) BannerStatus() string

BannerStatus reports whether banners are shown: StatusOn, StatusOff when the user has not allowed them, or StatusUnavailable when this build cannot show them.

func (*Service) Clear

func (s *Service) Clear(ctx context.Context) error

Clear forgets every notification.

func (*Service) List

func (s *Service) List(ctx context.Context) (Log, error)

List returns the notification history, newest first, and how many are unread.

func (*Service) MarkAllRead

func (s *Service) MarkAllRead(ctx context.Context) error

MarkAllRead marks every notification read, as the history does once it is opened.

func (*Service) SendTest

func (s *Service) SendTest() error

SendTest shows a sample banner, asking the user to allow banners first if they have not been asked, so the user can see what banners look like and that they arrive.

type Store

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

Store reads and writes the notification log.

func NewStore

func NewStore(db *gorm.DB) *Store

NewStore returns a Store over db.

func (*Store) Clear

func (s *Store) Clear(ctx context.Context) error

Clear forgets every notification.

func (*Store) Log

func (s *Store) Log(ctx context.Context) (Log, error)

Log returns the stored notifications, newest first, and how many are unread.

func (*Store) MarkAllRead

func (s *Store) MarkAllRead(ctx context.Context, now time.Time) error

MarkAllRead marks every unread notification read at now.

func (*Store) Record

func (s *Store) Record(ctx context.Context, now time.Time, arrivals []inbox.Arrival) ([]model.Notification, error)

Record stores the notifications for arrivals at now, in order, and drops the oldest past Keep, in one transaction. It returns the stored rows, with their ids.

Directories

Path Synopsis
Package model holds the persisted record of the notifications the app gave.
Package model holds the persisted record of the notifications the app gave.

Jump to

Keyboard shortcuts

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