package server import ( "context" "log" "git.a-wan.net/alan/pipelines/internal/protocol" ) // notificationSettingsStore persists the operator's global notification // choices. *pipeline.Store implements it. type notificationSettingsStore interface { NotificationSettings(context.Context) (protocol.NotificationSettings, error) UpdateNotificationSettings(context.Context, map[string]any) (protocol.NotificationSettings, error) } // handleNotificationSettings answers notification_settings and // set_notification_settings. It reports whether request was one of them. func handleNotificationSettings(ctx context.Context, request protocol.Message, store notificationSettingsStore, write func(protocol.Message) error) bool { if request.Type != protocol.GetNotificationSettings && request.Type != protocol.SetNotificationSettings { return false } response := protocol.Message{Type: protocol.NotificationSettingsReply, ID: request.ID} var settings protocol.NotificationSettings var err error switch { case store == nil: response.Type = protocol.Error response.Error = "notification settings are unavailable: no store" case request.Type == protocol.GetNotificationSettings: settings, err = store.NotificationSettings(ctx) default: settings, err = store.UpdateNotificationSettings(ctx, request.NotificationSettings) } if err != nil { response.Type = protocol.Error response.Error = err.Error() } else if response.Type != protocol.Error { response.NotificationSettings = settings.Map() } _ = write(response) return true } // chatNotificationSettings reads the chat-related choices for a pending // publication. Without a store the defaults apply; a read failure is logged and // also falls back to the defaults so a transient database error neither // silences the operator nor fails chat handling. func chatNotificationSettings(ctx context.Context, store notificationSettingsStore) protocol.NotificationSettings { if store == nil { return protocol.DefaultNotificationSettings() } settings, err := store.NotificationSettings(ctx) if err != nil { log.Printf("read notification settings: %v", err) return protocol.DefaultNotificationSettings() } return settings }