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.
Some notifications are about no item: a release whose tag reached GitHub, for one, or a run from the Scripts tab that failed, which names the run a click on it opens. The Dispatcher's Notify records such an Entry and shows its banner the same way.
Service is what the window binds: the log, marking entries read or unread, clearing it, the state of the banners, and a test banner. Opening the history marks nothing: an entry is marked read when the user opens it, clicks its banner, opens its item's sheet, or marks it done, and marked unread again only by the user. Nothing here leaves the machine: banners are posted to the operating system, and the log is a local table.
Index ¶
- Constants
- Variables
- func WakeDetail(r snooze.Reasons) string
- type Banner
- type BannerStatus
- type Config
- type Dispatcher
- type Entry
- type Log
- type Notification
- type Poster
- type Service
- func (s *Service) BannerStatus() BannerStatus
- func (s *Service) Clear(ctx context.Context) error
- func (s *Service) List(ctx context.Context) (Log, error)
- func (s *Service) MarkAllRead(ctx context.Context) error
- func (s *Service) MarkRead(ctx context.Context, ids []int64) error
- func (s *Service) MarkUnread(ctx context.Context, ids []int64) error
- func (s *Service) SendTest() error
- type SessionBanners
- type Store
- func (s *Store) Clear(ctx context.Context) error
- func (s *Store) Log(ctx context.Context) (Log, error)
- func (s *Store) MarkAllRead(ctx context.Context, now time.Time) error
- func (s *Store) MarkRead(ctx context.Context, now time.Time, ids []int64) (int64, error)
- func (s *Store) MarkUnread(ctx context.Context, ids []int64) (int64, error)
- func (s *Store) Record(ctx context.Context, now time.Time, arrivals []inbox.Arrival) ([]Notification, error)
- func (s *Store) RecordEvent(ctx context.Context, now time.Time, entry Entry) (Notification, error)
Constants ¶
const ( // KindReviewRequested is a pull request that newly asks for the user's review. KindReviewRequested = notifyprefs.Kind(inbox.ArrivalReviewRequested) // KindAssigned is an issue or pull request newly assigned to the user. KindAssigned = notifyprefs.Kind(inbox.ArrivalAssigned) // KindWoke is a snoozed item that new activity brought back early. KindWoke = notifyprefs.Kind(inbox.ArrivalWoke) // KindReleaseDone is a release whose tag reached GitHub. It is about no item. KindReleaseDone = notifyprefs.KindReleaseDone // KindReleasePublished is a release its workflow on GitHub published, and // KindReleaseFailed one whose workflow failed. Both are about no item. KindReleasePublished = notifyprefs.KindReleasePublished KindReleaseFailed = notifyprefs.KindReleaseFailed // KindRunFailed is a run from the Scripts tab that failed or ran past its time limit. It // is about no item, and names the run. KindRunFailed = notifyprefs.KindRunFailed // KindServiceStatus is a part of a service the user watches going down, or coming back. // It is about no item, and opens the incident or the status page. KindServiceStatus = notifyprefs.KindServiceStatus )
Kinds of notification, which notifyprefs lists for the settings: the inbox's kinds of arrival, and the kinds about no item.
const ( // KindSessionNeedsYou is an agent session waiting for the user's approval or input. KindSessionNeedsYou = notifyprefs.KindSessionNeedsYou // KindSessionFinished is an agent session that finished its turn. KindSessionFinished = notifyprefs.KindSessionFinished )
The agent sessions' kinds of notification. They start off, since the coding agents and the terminal can already notify the user themselves.
const GatherWindow = 2 * time.Second
GatherWindow is how long Gather holds the banner of a notification for those of others that arrive meanwhile: a burst, as Install all's failed runs or the runs a restart settles, then shows as one batch.
const Keep = 1000
Keep is how many notifications the log keeps; recording more drops the oldest.
const MaxBanners = 3
MaxBanners is how many banners one refresh shows one by one; more are summed up in one.
Variables ¶
var ( 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") )
Functions ¶
func WakeDetail ¶
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 ¶
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, or the page of an entry about no item.
ItemID int64
URL string
// RunID is the run a click on the banner opens in Scripts → Runs, or 0 for none, and
// RunStarted when it started, which the window checks first.
RunID int64
RunStarted string
// NotificationID is the log's entry the banner stands for, which a click on it marks
// read, or 0 for a banner that stands for none or many (a summary, a session's, the
// test banner). The window marks it only while the entry with that id is about the
// banner's item, page, or run, since the log hands an id out again once it is cleared.
NotificationID int64
}
Banner is one native notification.
func Banners ¶
func Banners(rows []Notification, off []notifyprefs.Kind) []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 SessionBanner ¶ added in v0.2.0
func SessionBanner(kind notifyprefs.Kind, s sessions.Session) Banner
SessionBanner is the banner for a session of kind: who and what as its title, its item as the subtitle, and what the agent last said, or the item's title, as its body. A click on it shows the item. Posting it again for the same session replaces it.
type BannerStatus ¶ added in v0.2.0
type BannerStatus string
BannerStatus says whether banners are shown.
const ( // StatusOn means banners are shown. StatusOn BannerStatus = "on" // StatusOff means the user has not allowed the app's banners, or turned them off in // the system's settings. StatusOff BannerStatus = "off" // no app bundle to own them, or an OS the shell does not post to yet. StatusUnavailable BannerStatus = "unavailable" )
Banner states, as Poster.Status reports them.
type Config ¶
type Config struct {
// Pool is the database's connection pool. It holds the log, and the prefs the
// Dispatcher reads which banners are on from.
Pool *sql.DB
Poster Poster
// Events hears an event.NotificationsUpdated whenever the log changed, so the window
// reads it again.
Events event.Sink
Clock clock.Clock
Logger *slog.Logger
}
Config holds the dependencies of a Dispatcher and a Service. Pool is required; Poster, Events, 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. It also records the notifications about no item, such as a release that reached GitHub, and shows theirs (Notify), or a run that failed, whose banners it gathers (Gather). It is not bound to the window.
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.
func (*Dispatcher) Close ¶ added in v0.2.0
func (d *Dispatcher) Close()
Close drops the banners Gather holds, and holds none after: the history keeps their entries, and what Gather records later shows no banner. A batch already showing is cut short, and Close returns once it has ended, so nothing reads the database after it: core calls it before it closes the database.
func (*Dispatcher) Gather ¶ added in v0.2.0
func (d *Dispatcher) Gather(ctx context.Context, entry Entry)
Gather records entry, a notification about no item, and tells the window at once, as Notify does, but holds its banner for GatherWindow with those of every entry gathered meanwhile, and then shows them as one batch: one banner each, or past MaxBanners one that sums them up, as one refresh's arrivals are. A failure to record is logged, and holds nothing.
func (*Dispatcher) Notify ¶ added in v0.2.0
func (d *Dispatcher) Notify(ctx context.Context, entry Entry)
Notify records entry, a notification about no item, tells the window, and shows its banner when its kind is on and the user allows banners. A failure is logged: what it tells of has happened anyway.
type Entry ¶ added in v0.2.0
type Entry struct {
// Kind is the kind of notification, one of notifyprefs.Kinds.
Kind notifyprefs.Kind
// Title is its sentence ("v1.4.3 is on GitHub"), Body what follows it, which may be "",
// and URL the page a click on it opens, which may be "".
Title string
Body string
URL string
// RepoOwner and RepoName are the repository it is about, or "" for none.
RepoOwner string
RepoName string
// RunID is the run it is about, which a click on it opens, or 0 for none, and RunStarted
// when that run started, as RFC 3339, which tells it from a later run given its id.
RunID int64
RunStarted string
}
Entry is a notification about no item, as a release whose tag reached GitHub or a run that failed: RecordEvent stores 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, KindWoke, one of a release's kinds
// (KindReleaseDone, KindReleasePublished, or KindReleaseFailed), KindRunFailed, or
// KindServiceStatus.
Kind notifyprefs.Kind `json:"kind"`
// CreatedAt is when it was recorded, as RFC 3339.
CreatedAt string `json:"createdAt"`
// Read reports whether the user is done with it: opened it, clicked its banner, opened
// its item, or marked it done. Opening the history marks nothing.
Read bool `json:"read"`
// ItemID is the item's id in the inbox snapshot, or 0 once the item is no longer
// stored, or for an entry about no item.
ItemID int64 `json:"itemId"`
// ItemKind is the item's kind, or "" for an entry about no item (an Entry), such as a
// release: its Number and Author are then 0 and "".
ItemKind item.Kind `json:"itemKind"`
// Repo is "owner/name", or "" when the repository is unknown. Title is the item's
// title, or an entry's own sentence ("v1.4.3 is on GitHub"), and URL the page a click
// opens: the item, or the entry's.
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; an entry's body ("Tagged a1b2c3d on master."); and "" otherwise.
Detail string `json:"detail"`
// RunID is the run an entry is about, which a click on it opens in Scripts → Runs, or 0
// for none: every entry but a KindRunFailed one. RunStarted is when that run started, as
// RFC 3339, or "": the window opens the run only while the run with that id started then,
// since the history hands an id out again once it is cleared.
RunID int64 `json:"runId"`
RunStarted string `json:"runStarted"`
}
Notification is one entry in the log, as the window shows it. Every field is a plain value.
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() BannerStatus
// 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 (*Service) BannerStatus ¶
func (s *Service) BannerStatus() BannerStatus
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) List ¶
List returns the notification history, newest first, and how many are unread.
func (*Service) MarkAllRead ¶
MarkAllRead marks every notification read, as the history's Mark all done does.
func (*Service) MarkRead ¶ added in v0.2.0
MarkRead marks the notifications with ids read: the user opened them, clicked their banner, opened their item, or marked them done. The window is told only when one was unread.
func (*Service) MarkUnread ¶ added in v0.2.0
MarkUnread marks the notifications with ids unread again, which puts them back in the bell's quick view. The window is told only when one was read.
type SessionBanners ¶ added in v0.2.0
type SessionBanners struct {
// contains filtered or unexported fields
}
SessionBanners shows a banner when an agent session needs the user or finished its turn, for each kind the user turned on. Unlike the inbox's notifications, these are not kept in the notification log: the session's card already says what it is doing. It is the sessions Watcher's AttentionSink, and is not bound to the window.
func NewSessionBanners ¶ added in v0.2.0
func NewSessionBanners(cfg Config) *SessionBanners
NewSessionBanners returns a SessionBanners.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store reads and writes the notification log.
func (*Store) Log ¶
Log returns the stored notifications, newest first, and how many are unread. A notification of a kind this build does not know, as another build may have written, is left out.
func (*Store) MarkAllRead ¶
MarkAllRead marks every unread notification read at now.
func (*Store) MarkRead ¶ added in v0.2.0
MarkRead marks the unread notifications among ids read at now, and reports how many it marked. An id the log doesn't hold, or one already read, is left alone.
func (*Store) MarkUnread ¶ added in v0.2.0
MarkUnread marks the read notifications among ids unread again, and reports how many it marked. An id the log doesn't hold, or one already unread, is left alone.
func (*Store) Record ¶
func (s *Store) Record(ctx context.Context, now time.Time, arrivals []inbox.Arrival) ([]Notification, error)
Record stores the notifications for arrivals at now, in order, and drops the oldest past Keep, in one transaction. It returns what it stored, with the ids.
func (*Store) RecordEvent ¶ added in v0.2.0
RecordEvent stores entry, a notification about no item, at now, and drops the oldest past Keep, in one transaction. It returns what it stored, with its id. A kind that isn't one of notifyprefs.Kinds is refused, and nothing is stored.