Add Title and Category fields to Notification

Also, improve on the notification handling a bit
This commit is contained in:
Daniel 2020-10-29 22:50:35 +01:00
parent a9ac02f68e
commit 4a1ed12598

View file

@ -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,53 @@ 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) // Derive missing information.
if n.Message == "" {
n.Title = n.Message
}
if n.EventID == "" {
n.EventID = utils.DerivedInstanceUUID(n.Message).String()
}
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. // Delete notification after processing deletion.
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 +189,12 @@ func (n *Notification) save(pushUpdate bool) *Notification {
n.lock.Lock() n.lock.Lock()
defer n.lock.Unlock() defer n.lock.Unlock()
// Check if required data is present.
if n.EventID == "" || n.Message == "" {
log.Warning("notifications: ignoring notification without EventID and Message")
return
}
// Save ID for deletion // Save ID for deletion
id = n.EventID id = n.EventID
@ -209,8 +231,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.