129 lines
3.5 KiB
Go
129 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type notificationSettingsStore struct {
|
|
path string
|
|
mu sync.RWMutex
|
|
record notificationSettingsRecord
|
|
}
|
|
|
|
type notificationSettingsRecord struct {
|
|
TelegramEnabled bool `json:"telegram_enabled,omitempty"`
|
|
TelegramCommandsEnabled bool `json:"telegram_commands_enabled,omitempty"`
|
|
TelegramBotToken string `json:"telegram_bot_token,omitempty"`
|
|
TelegramChatID string `json:"telegram_chat_id,omitempty"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func newNotificationSettingsStore(path string) (*notificationSettingsStore, error) {
|
|
store := ¬ificationSettingsStore{path: path}
|
|
if err := store.loadLocked(); err != nil {
|
|
return nil, err
|
|
}
|
|
return store, nil
|
|
}
|
|
|
|
func (s *notificationSettingsStore) loadLocked() error {
|
|
if s == nil || s.path == "" {
|
|
return nil
|
|
}
|
|
data, err := os.ReadFile(s.path)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
if len(strings.TrimSpace(string(data))) == 0 {
|
|
return nil
|
|
}
|
|
var raw map[string]json.RawMessage
|
|
if err := json.Unmarshal(data, &raw); err != nil {
|
|
return fmt.Errorf("failed to parse notification settings file: %w", err)
|
|
}
|
|
var record notificationSettingsRecord
|
|
if err := json.Unmarshal(data, &record); err != nil {
|
|
return fmt.Errorf("failed to parse notification settings file: %w", err)
|
|
}
|
|
if _, ok := raw["telegram_commands_enabled"]; !ok {
|
|
record.TelegramCommandsEnabled = record.TelegramEnabled
|
|
}
|
|
s.record = record
|
|
return nil
|
|
}
|
|
|
|
func (s *notificationSettingsStore) saveLocked() error {
|
|
if s == nil || s.path == "" {
|
|
return nil
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(s.path), 0o755); err != nil {
|
|
return err
|
|
}
|
|
encoded, err := json.MarshalIndent(s.record, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
encoded = append(encoded, '\n')
|
|
return os.WriteFile(s.path, encoded, 0o600)
|
|
}
|
|
|
|
func (s *notificationSettingsStore) snapshot() notificationSettingsRecord {
|
|
if s == nil {
|
|
return notificationSettingsRecord{}
|
|
}
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.record
|
|
}
|
|
|
|
func (s *notificationSettingsStore) update(telegramEnabled *bool, telegramCommandsEnabled *bool, telegramBotToken *string, telegramChatID *string) (notificationSettingsRecord, error) {
|
|
if s == nil {
|
|
return notificationSettingsRecord{}, nil
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
if telegramEnabled != nil {
|
|
s.record.TelegramEnabled = *telegramEnabled
|
|
}
|
|
if telegramCommandsEnabled != nil {
|
|
s.record.TelegramCommandsEnabled = *telegramCommandsEnabled
|
|
}
|
|
if telegramBotToken != nil {
|
|
s.record.TelegramBotToken = strings.TrimSpace(*telegramBotToken)
|
|
}
|
|
if telegramChatID != nil {
|
|
s.record.TelegramChatID = strings.TrimSpace(*telegramChatID)
|
|
}
|
|
s.record.UpdatedAt = time.Now().UTC()
|
|
if err := s.saveLocked(); err != nil {
|
|
return notificationSettingsRecord{}, err
|
|
}
|
|
return s.record, nil
|
|
}
|
|
|
|
func (s *notificationSettingsStore) configured() bool {
|
|
record := s.snapshot()
|
|
return strings.TrimSpace(record.TelegramBotToken) != "" && strings.TrimSpace(record.TelegramChatID) != ""
|
|
}
|
|
|
|
func publicNotificationSettings(record notificationSettingsRecord) map[string]any {
|
|
return map[string]any{
|
|
"telegram_enabled": record.TelegramEnabled,
|
|
"telegram_commands_enabled": record.TelegramCommandsEnabled,
|
|
"telegram_bot_token": record.TelegramBotToken,
|
|
"telegram_bot_token_set": strings.TrimSpace(record.TelegramBotToken) != "",
|
|
"telegram_chat_id": record.TelegramChatID,
|
|
"updated_at": record.UpdatedAt,
|
|
}
|
|
}
|