mirror of
https://github.com/safing/portbase
synced 2025-09-02 02:29:59 +00:00
Merge pull request #97 from safing/feature/notification-title-category
Add Title and Category fields to Notification
This commit is contained in:
commit
9c3b240825
2 changed files with 52 additions and 22 deletions
|
@ -135,7 +135,7 @@ type QuickSetting struct {
|
||||||
Action QuickSettingsAction
|
Action QuickSettingsAction
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValueRequirement defines a requirement on another configuraiton option.
|
// ValueRequirement defines a requirement on another configuration option.
|
||||||
type ValueRequirement struct {
|
type ValueRequirement struct {
|
||||||
// Key is the key of the configuration option that is required.
|
// Key is the key of the configuration option that is required.
|
||||||
Key string
|
Key string
|
||||||
|
|
|
@ -60,6 +60,12 @@ type Notification struct {
|
||||||
GUID string
|
GUID string
|
||||||
// Type is the notification type. It can be one of Info, Warning or Prompt.
|
// Type is the notification type. It can be one of Info, Warning or Prompt.
|
||||||
Type Type
|
Type Type
|
||||||
|
// Title is an optional and very short title for the message that gives a
|
||||||
|
// hint about what the notification is about.
|
||||||
|
Title string
|
||||||
|
// Category is an optional category for the notification that allows for
|
||||||
|
// tagging and grouping notifications by category.
|
||||||
|
Category string
|
||||||
// Message is the default message shown to the user if no localized version
|
// Message is the default message shown to the user if no localized version
|
||||||
// of the notification is available. Note that the message should already
|
// of the notification is available. Note that the message should already
|
||||||
// have any paramerized values replaced.
|
// have any paramerized values replaced.
|
||||||
|
@ -129,43 +135,49 @@ func NotifyPrompt(id, msg string, actions ...Action) *Notification {
|
||||||
return notify(Prompt, id, msg, actions...)
|
return notify(Prompt, id, msg, actions...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func notify(nType Type, id string, msg string, actions ...Action) *Notification {
|
func notify(nType Type, id, msg string, actions ...Action) *Notification {
|
||||||
acts := make([]*Action, len(actions))
|
acts := make([]*Action, len(actions))
|
||||||
for idx := range actions {
|
for idx := range actions {
|
||||||
a := actions[idx]
|
a := actions[idx]
|
||||||
acts[idx] = &a
|
acts[idx] = &a
|
||||||
}
|
}
|
||||||
|
|
||||||
if id == "" {
|
return Notify(&Notification{
|
||||||
id = utils.DerivedInstanceUUID(msg).String()
|
|
||||||
}
|
|
||||||
|
|
||||||
n := Notification{
|
|
||||||
EventID: id,
|
EventID: id,
|
||||||
Message: msg,
|
|
||||||
Type: nType,
|
Type: nType,
|
||||||
|
Message: msg,
|
||||||
AvailableActions: acts,
|
AvailableActions: acts,
|
||||||
}
|
})
|
||||||
|
|
||||||
return n.Save()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save saves the notification and returns it.
|
// Notify sends the given notification.
|
||||||
func (n *Notification) Save() *Notification {
|
func Notify(n *Notification) *Notification {
|
||||||
return n.save(true)
|
// While this function is very similar to Save(), it is much nicer to use in
|
||||||
|
// order to just fire off one notification, as it does not require some more
|
||||||
|
// uncommon Go syntax.
|
||||||
|
|
||||||
|
n.save(true)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save saves the notification.
|
||||||
|
func (n *Notification) Save() {
|
||||||
|
n.save(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// save saves the notification to the internal storage. It locks the
|
// save saves the notification to the internal storage. It locks the
|
||||||
// notification, so it must not be locked when save is called.
|
// notification, so it must not be locked when save is called.
|
||||||
func (n *Notification) save(pushUpdate bool) *Notification {
|
func (n *Notification) save(pushUpdate bool) {
|
||||||
var id string
|
var id string
|
||||||
|
|
||||||
// Delete notification after processing deletion.
|
// Save notification after pre-save processing.
|
||||||
defer func() {
|
defer func() {
|
||||||
// Lock and save to notification storage.
|
if id != "" {
|
||||||
notsLock.Lock()
|
// Lock and save to notification storage.
|
||||||
defer notsLock.Unlock()
|
notsLock.Lock()
|
||||||
nots[id] = n
|
defer notsLock.Unlock()
|
||||||
|
nots[id] = n
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// We do not access EventData here, so it is enough to just lock the
|
// We do not access EventData here, so it is enough to just lock the
|
||||||
|
@ -173,6 +185,26 @@ func (n *Notification) save(pushUpdate bool) *Notification {
|
||||||
n.lock.Lock()
|
n.lock.Lock()
|
||||||
defer n.lock.Unlock()
|
defer n.lock.Unlock()
|
||||||
|
|
||||||
|
// Move Title to Message, as that is the required field.
|
||||||
|
if n.Message == "" {
|
||||||
|
n.Message = n.Title
|
||||||
|
n.Title = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if required data is present.
|
||||||
|
if n.Message == "" {
|
||||||
|
log.Warning("notifications: ignoring notification without Message")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Derive EventID from Message if not given.
|
||||||
|
if n.EventID == "" {
|
||||||
|
n.EventID = fmt.Sprintf(
|
||||||
|
"unknown:%s",
|
||||||
|
utils.DerivedInstanceUUID(n.Message).String(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Save ID for deletion
|
// Save ID for deletion
|
||||||
id = n.EventID
|
id = n.EventID
|
||||||
|
|
||||||
|
@ -209,8 +241,6 @@ func (n *Notification) save(pushUpdate bool) *Notification {
|
||||||
log.Tracef("notifications: pushing update for %s to subscribers", n.Key())
|
log.Tracef("notifications: pushing update for %s to subscribers", n.Key())
|
||||||
dbController.PushUpdate(n)
|
dbController.PushUpdate(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
return n
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetActionFunction sets a trigger function to be executed when the user reacted on the notification.
|
// SetActionFunction sets a trigger function to be executed when the user reacted on the notification.
|
||||||
|
|
Loading…
Add table
Reference in a new issue