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
}
// ValueRequirement defines a requirement on another configuraiton option.
// ValueRequirement defines a requirement on another configuration option.
type ValueRequirement struct {
// Key is the key of the configuration option that is required.
Key string

View file

@ -152,13 +152,9 @@ func notify(nType Type, id, msg string, actions ...Action) *Notification {
// Notify sends the given notification.
func Notify(n *Notification) *Notification {
// Derive missing information.
if n.Message == "" {
n.Title = n.Message
}
if n.EventID == "" {
n.EventID = utils.DerivedInstanceUUID(n.Message).String()
}
// 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
@ -174,7 +170,7 @@ func (n *Notification) Save() {
func (n *Notification) save(pushUpdate bool) {
var id string
// Delete notification after processing deletion.
// Save notification after pre-save processing.
defer func() {
if id != "" {
// Lock and save to notification storage.
@ -189,12 +185,26 @@ func (n *Notification) save(pushUpdate bool) {
n.lock.Lock()
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.EventID == "" || n.Message == "" {
log.Warning("notifications: ignoring notification without EventID and Message")
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
id = n.EventID