Revert "feat(config): implement loading and saving of bot node configuration with default notification settings"

This reverts commit ec1cf20347.
This commit is contained in:
2026-09-06 21:36:50 +08:00
parent b5b042b46d
commit 5d349df2ba
3 changed files with 36 additions and 63 deletions
+28 -44
View File
@@ -5,45 +5,25 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"sync"
) )
// LoadBotNodeConfig reads and parses bot_node_config.json and stores it as the // botNodeConfigMu guards botNodeConfig, the in-memory mirror of
// global C_botNodeConfig singleton, mirroring LoadGlobalConfig. Unlike the // bot_node_config.json.
// other config files this one is auto-generated and optional: a missing or var (
// empty file yields an empty map, so every node falls back to its default botNodeConfigMu sync.RWMutex
// enableStatusNotify value (true). botNodeConfig BotNodeMembers
func LoadBotNodeConfig(configPath string) error { )
cfg := BotNodeMembers{}
data, err := os.ReadFile(configPath)
if err != nil {
if os.IsNotExist(err) {
C_botNodeConfig = cfg
return nil
}
return fmt.Errorf("failed to read bot node config file: %w", err)
}
if len(bytes.TrimSpace(data)) > 0 {
if err := json.Unmarshal(data, &cfg); err != nil {
return fmt.Errorf("failed to parse bot node config file: %w", err)
}
}
C_botNodeConfig = cfg
return nil
}
// NodeStatusNotifyEnabled reports whether the node identified by uuid should // NodeStatusNotifyEnabled reports whether the node identified by uuid should
// broadcast status-change notifications, per bot_node_config.json. // broadcast status-change notifications, per bot_node_config.json. The flag is
// enableStatusNotify defaults to true: a node notifies unless its entry // allow-list only: a node notifies when its entry has enableStatusNotify set to
// explicitly sets the flag to false. // true, and stays silent when the entry is absent or not enabled.
func NodeStatusNotifyEnabled(uuid string) bool { func NodeStatusNotifyEnabled(uuid string) bool {
if C_botNodeConfig == nil { botNodeConfigMu.RLock()
return true opts, ok := botNodeConfig[uuid]
} botNodeConfigMu.RUnlock()
opts, ok := C_botNodeConfig[uuid] return ok && opts.EnableStatusNotify
if !ok || opts.EnableStatusNotify == nil {
return true
}
return *opts.EnableStatusNotify
} }
// LoadGlobalConfig reads and parses the configuration file, applies defaults, // LoadGlobalConfig reads and parses the configuration file, applies defaults,
@@ -155,16 +135,16 @@ func LoadBotUserConfig(configPath string) (*BotUserConfig, error) {
return &cfg, nil return &cfg, nil
} }
// SaveBotNodeConfig persists the given node UUIDs to the bot node config file, // SaveBotNodeConfig persists the given node UUIDs to the bot node config file
// which is a plain registry of the nodes Komari reports (read back at startup // and refreshes the in-memory mirror read by NodeStatusNotifyEnabled. Entries
// by LoadBotNodeConfig). Entries already present are always preserved: a UUID // already present in the file are always preserved: a UUID Komari no longer
// Komari no longer reports on a given fetch is kept rather than deleted, and // reports on a given fetch is kept rather than deleted, so per-node settings
// any enableStatusNotify a user set by hand is left untouched. UUIDs seen for // for stale nodes survive a node-list refresh that does not include them. UUIDs
// the first time are added as an empty object, i.e. no parameter is written, so // seen for the first time are added disabled (enableStatusNotify defaults to
// they inherit the enableStatusNotify default (true). The resulting JSON has // false); enable a node's status notifications by setting the flag to true in
// its object keys emitted in sorted order by encoding/json, keeping the file // the file. The resulting JSON has its object keys emitted in sorted order by
// deterministic across writes. The path is supplied by the caller (typically // encoding/json, keeping the file deterministic across writes. The path is
// global.ConfigPath.BotNodeConfig). // supplied by the caller (typically global.ConfigPath.BotNodeConfig).
func SaveBotNodeConfig(configPath string, uuids []string) error { func SaveBotNodeConfig(configPath string, uuids []string) error {
// Start from whatever is already on disk so nothing is dropped. An empty or // Start from whatever is already on disk so nothing is dropped. An empty or
// missing file is treated as an empty map. // missing file is treated as an empty map.
@@ -192,6 +172,10 @@ func SaveBotNodeConfig(configPath string, uuids []string) error {
if err := os.WriteFile(configPath, data, 0o644); err != nil { if err := os.WriteFile(configPath, data, 0o644); err != nil {
return fmt.Errorf("failed to write bot node config %s: %w", configPath, err) return fmt.Errorf("failed to write bot node config %s: %w", configPath, err)
} }
botNodeConfigMu.Lock()
botNodeConfig = members
botNodeConfigMu.Unlock()
return nil return nil
} }
+8 -12
View File
@@ -175,18 +175,14 @@ var C_botUserConfig *BotUserConfig
// file is auto-populated by the node tracker for every node Komari reports; // file is auto-populated by the node tracker for every node Komari reports;
// per-node options are edited by hand in the JSON file. // per-node options are edited by hand in the JSON file.
type BotNodeOptions struct { type BotNodeOptions struct {
// EnableStatusNotify controls whether this node broadcasts status-change // EnableStatusNotify is an allow-list switch: a node broadcasts status-change
// notifications. It defaults to true: only an explicit false in the JSON // notifications only when this is true. Nodes default to false (silent)
// file disables a node's notifications. The pointer (rather than a plain // unless explicitly enabled.
// bool) lets an absent field be told apart from an explicit false, and EnableStatusNotify bool `json:"enableStatusNotify"`
// omitempty keeps untouched nodes stored as {}.
EnableStatusNotify *bool `json:"enableStatusNotify,omitempty"`
} }
// BotNodeMembers maps a node UUID (as reported by Komari) to its per-node // BotNodeMembers maps a node UUID (as reported by Komari) to its per-node
// options. // options, e.g. bot_node_config.json:
type BotNodeMembers map[string]BotNodeOptions //
// {"<uuid1>": {"enableStatusNotify": true}, "<uuid2>": {}}
// C_botNodeConfig is the global singleton mirroring bot_node_config.json, type BotNodeMembers map[string]BotNodeOptions
// populated by LoadBotNodeConfig.
var C_botNodeConfig BotNodeMembers
-7
View File
@@ -47,13 +47,6 @@ func main() {
log.Fatalf("Failed to load bot user config: %v", err) log.Fatalf("Failed to load bot user config: %v", err)
} }
// Load the node registry config. Unlike the other files it is optional:
// missing or empty bot_node_config.json simply means every node keeps its
// default enableStatusNotify (true).
if err := config.LoadBotNodeConfig(global.ConfigPath.BotNodeConfig); err != nil {
log.Fatalf("Failed to load bot node config: %v", err)
}
// Initialize logging. // Initialize logging.
postLog.SetDebugMode(cfg.System.DebugMode) postLog.SetDebugMode(cfg.System.DebugMode)
postLog.InitLogBroadcaster() postLog.InitLogBroadcaster()