github.com/safing/portbase@v0.19.5/notifications/module.go (about)

     1  package notifications
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/safing/portbase/config"
     8  	"github.com/safing/portbase/modules"
     9  )
    10  
    11  var module *modules.Module
    12  
    13  func init() {
    14  	module = modules.Register("notifications", prep, start, nil, "database", "config", "base")
    15  }
    16  
    17  func prep() error {
    18  	return registerConfig()
    19  }
    20  
    21  func start() error {
    22  	err := registerAsDatabase()
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	showConfigLoadingErrors()
    28  
    29  	go module.StartServiceWorker("cleaner", 1*time.Second, cleaner)
    30  	return nil
    31  }
    32  
    33  func showConfigLoadingErrors() {
    34  	validationErrors := config.GetLoadedConfigValidationErrors()
    35  	if len(validationErrors) == 0 {
    36  		return
    37  	}
    38  
    39  	// Trigger a module error for more awareness.
    40  	module.Error(
    41  		"config:validation-errors-on-load",
    42  		"Invalid Settings",
    43  		"Some current settings are invalid. Please update them and restart the Portmaster.",
    44  	)
    45  
    46  	// Send one notification per invalid setting.
    47  	for _, validationError := range config.GetLoadedConfigValidationErrors() {
    48  		NotifyError(
    49  			fmt.Sprintf("config:validation-error:%s", validationError.Option.Key),
    50  			fmt.Sprintf("Invalid Setting for %s", validationError.Option.Name),
    51  			fmt.Sprintf(`Your current setting for %s is invalid: %s
    52  
    53  Please update the setting and restart the Portmaster, until then the default value is used.`,
    54  				validationError.Option.Name,
    55  				validationError.Err.Error(),
    56  			),
    57  			Action{
    58  				Text: "Change",
    59  				Type: ActionTypeOpenSetting,
    60  				Payload: &ActionTypeOpenSettingPayload{
    61  					Key: validationError.Option.Key,
    62  				},
    63  			},
    64  		)
    65  	}
    66  }