Move useSystemNotifications to notifications module, apply on save

This commit is contained in:
Daniel 2021-05-18 14:09:08 +02:00
parent 412a393bca
commit 8a9ee6e2ca
3 changed files with 46 additions and 2 deletions

32
notifications/config.go Normal file
View file

@ -0,0 +1,32 @@
package notifications
import (
"github.com/safing/portbase/config"
)
// Configuration Keys.
var (
CfgUseSystemNotificationsKey = "core/useSystemNotifications"
useSystemNotifications config.BoolOption
)
func registerConfig() error {
if err := config.Register(&config.Option{
Name: "Desktop Notifications",
Key: CfgUseSystemNotificationsKey,
Description: "In addition to showing notifications in the Portmaster App, also send them to the Desktop. This requires the Portmaster Notifier to be running.",
OptType: config.OptTypeBool,
ExpertiseLevel: config.ExpertiseLevelUser,
ReleaseLevel: config.ReleaseLevelStable,
DefaultValue: true, // TODO: turn off by default on unsupported systems
Annotations: config.Annotations{
config.DisplayOrderAnnotation: -15,
config.CategoryAnnotation: "User Interface",
},
}); err != nil {
return err
}
useSystemNotifications = config.Concurrent.GetAsBool(CfgUseSystemNotificationsKey, true)
return nil
}

View file

@ -11,7 +11,11 @@ var (
)
func init() {
module = modules.Register("notifications", nil, start, nil, "database", "base")
module = modules.Register("notifications", prep, start, nil, "database", "base")
}
func prep() error {
return registerConfig()
}
func start() error {

View file

@ -75,6 +75,8 @@ type Notification struct {
// ShowOnSystem specifies if the notification should be also shown on the
// operating system. Notifications shown on the operating system level are
// more focus-intrusive and should only be used for important notifications.
// If the configuration option "Desktop Notifications" is switched off, this
// will be forced to false on the first save.
ShowOnSystem bool
// EventData contains an additional payload for the notification. This payload
// may contain contextual data and may be used by a localization framework
@ -319,9 +321,15 @@ func (n *Notification) save(pushUpdate bool) {
n.State = Active
}
// check key
// Initialize on first save.
if !n.KeyIsSet() {
// Set database key.
n.SetKey(fmt.Sprintf("notifications:all/%s", n.EventID))
// Check if notifications should be shown on the system at all.
if !useSystemNotifications() {
n.ShowOnSystem = false
}
}
// Update meta data.