notifications

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2019 License: GPL-3.0 Imports: 14 Imported by: 2

Documentation

Overview

Package notifications provides a notification system.

Notification Lifecycle

1. Create Notification with an ID and Message. 2. Set possible actions and save it. 3. When the user responds, the action is executed.

Example

// create notification
n := notifications.New("update-available", "A new update is available. Restart to upgrade.")
// set actions and save
n.AddAction("later", "Later").AddAction("restart", "Restart now!").Save()

// wait for user action
selectedAction := <-n.Response()
switch selectedAction {
case "later":
  log.Infof("user wants to upgrade later.")
case "restart":
  log.Infof("user wants to restart now.")
}

Index

Constants

View Source
const (
	Info    uint8 = 0
	Warning uint8 = 1
	Prompt  uint8 = 2
)

Notification types

Variables

View Source
var (
	ErrInvalidData = errors.New("invalid data, must be a notification object")
	ErrInvalidPath = errors.New("invalid path")
	ErrNoDelete    = errors.New("notifications may not be deleted, they must be handled")
)

Storage interface errors

Functions

func SetPersistenceBasePath

func SetPersistenceBasePath(dbBasePath string)

SetPersistenceBasePath sets the base path for persisting persistent notifications.

func UpdateNotification added in v0.3.0

func UpdateNotification(n *Notification, key string)

UpdateNotification updates a notification with input from a database action. Notification will not be saved/propagated if there is no valid change.

Types

type Action

type Action struct {
	ID   string
	Text string
}

Action describes an action that can be taken for a notification.

type Notification

type Notification struct {
	record.Base

	ID   string
	GUID string

	Message string
	// MessageTemplate string
	// MessageData []string
	DataSubject sync.Locker
	Type        uint8

	Persistent bool  // this notification persists until it is handled and survives restarts
	Created    int64 // creation timestamp, notification "starts"
	Expires    int64 // expiry timestamp, notification is expected to be canceled at this time and may be cleaned up afterwards
	Responded  int64 // response timestamp, notification "ends"
	Executed   int64 // execution timestamp, notification will be deleted soon

	AvailableActions []*Action
	SelectedActionID string
	// contains filtered or unexported fields
}

Notification represents a notification that is to be delivered to the user.

func EnsureNotification

func EnsureNotification(r record.Record) (*Notification, error)

EnsureNotification ensures that the given record is a Notification and returns it.

func Get

func Get(id string) *Notification

Get returns the notification identifed by the given id or nil if it doesn't exist.

func (*Notification) AddDataSubject

func (n *Notification) AddDataSubject(ds sync.Locker)

AddDataSubject adds the data subject to the notification. This is the only way how a data subject should be added - it avoids locking problems.

func (*Notification) Delete added in v0.3.0

func (n *Notification) Delete() error

Delete (prematurely) cancels and deletes a notification.

func (*Notification) Expired added in v0.3.0

func (n *Notification) Expired() <-chan struct{}

Expired notifies the caller when the notification has expired.

func (*Notification) Lock

func (n *Notification) Lock()

Lock locks the Notification and the DataSubject, if available.

func (*Notification) MakeAck

func (n *Notification) MakeAck() *Notification

MakeAck sets a default "OK" action and a no-op action function.

func (*Notification) Response

func (n *Notification) Response() <-chan string

Response waits for the user to respond to the notification and returns the selected action.

func (*Notification) Save

func (n *Notification) Save() *Notification

Save saves the notification and returns it.

func (*Notification) SetActionFunction

func (n *Notification) SetActionFunction(fn func(*Notification)) *Notification

SetActionFunction sets a trigger function to be executed when the user reacted on the notification. The provided function will be started as its own goroutine and will have to lock everything it accesses, even the provided notification.

func (*Notification) Unlock

func (n *Notification) Unlock()

Unlock unlocks the Notification and the DataSubject, if available.

func (*Notification) Update added in v0.3.0

func (n *Notification) Update(expires int64)

Update updates/resends a notification if it was not already responded to.

type StorageInterface

type StorageInterface struct {
	storage.InjectBase
}

StorageInterface provices a storage.Interface to the configuration manager.

func (*StorageInterface) Delete

func (s *StorageInterface) Delete(key string) error

Delete deletes a record from the database.

func (*StorageInterface) Get

func (s *StorageInterface) Get(key string) (record.Record, error)

Get returns a database record.

func (*StorageInterface) Put

func (s *StorageInterface) Put(r record.Record) error

Put stores a record in the database.

func (*StorageInterface) Query

func (s *StorageInterface) Query(q *query.Query, local, internal bool) (*iterator.Iterator, error)

Query returns a an iterator for the supplied query.

func (*StorageInterface) ReadOnly

func (s *StorageInterface) ReadOnly() bool

ReadOnly returns whether the database is read only.

Jump to

Keyboard shortcuts

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