Implement review suggestions

This commit is contained in:
Daniel 2020-10-30 12:05:50 +01:00
parent 4a1ed12598
commit 505fcc8913
2 changed files with 21 additions and 11 deletions

View file

@ -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

View file

@ -152,13 +152,9 @@ func notify(nType Type, id, msg string, actions ...Action) *Notification {
// Notify sends the given notification. // Notify sends the given notification.
func Notify(n *Notification) *Notification { func Notify(n *Notification) *Notification {
// Derive missing information. // While this function is very similar to Save(), it is much nicer to use in
if n.Message == "" { // order to just fire off one notification, as it does not require some more
n.Title = n.Message // uncommon Go syntax.
}
if n.EventID == "" {
n.EventID = utils.DerivedInstanceUUID(n.Message).String()
}
n.save(true) n.save(true)
return n return n
@ -174,7 +170,7 @@ func (n *Notification) Save() {
func (n *Notification) save(pushUpdate bool) { 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() {
if id != "" { if id != "" {
// Lock and save to notification storage. // Lock and save to notification storage.
@ -189,12 +185,26 @@ func (n *Notification) save(pushUpdate bool) {
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. // Check if required data is present.
if n.EventID == "" || n.Message == "" { if n.Message == "" {
log.Warning("notifications: ignoring notification without EventID and Message") log.Warning("notifications: ignoring notification without Message")
return 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