Add a user_settings realtime channel so clients learn when a setting
changed on another device without polling. The channel is modeled on
user_state: non-admin subscribable, per-user addressed envelopes, null
snapshot.
SettingValuesHandler gains an EventsHub and publishes
user_settings.changed after every successful PUT and DELETE on
/settings/values/{key}. The payload carries only key, scope and
profile_id — never the value. Admins receive every user's user-scoped
events, so a value in the payload would leak private settings to
admins; interested clients re-fetch over the scoped REST API instead.
The payload is always non-empty because an empty Data falls back to a
null snapshot in the hub. A nil hub (tests) skips publishing.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
112 lines
3.3 KiB
Go
112 lines
3.3 KiB
Go
package events
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
type EventChannel string
|
|
|
|
const (
|
|
ChannelCatalog EventChannel = "catalog"
|
|
ChannelJobs EventChannel = "jobs"
|
|
ChannelSessions EventChannel = "sessions"
|
|
ChannelTasks EventChannel = "tasks"
|
|
ChannelScans EventChannel = "scans"
|
|
ChannelHistoryImport EventChannel = "history_import"
|
|
ChannelUserState EventChannel = "user_state"
|
|
ChannelUserSettings EventChannel = "user_settings"
|
|
ChannelSettings EventChannel = "settings"
|
|
ChannelPlugins EventChannel = "plugins"
|
|
// ChannelNotifications carries profile-scoped user notifications
|
|
// (inbox deliveries). Subscriptions require a websocket ticket binding
|
|
// the connection to a (user, profile).
|
|
ChannelNotifications EventChannel = "notifications"
|
|
)
|
|
|
|
var AllChannels = []EventChannel{
|
|
ChannelCatalog,
|
|
ChannelJobs,
|
|
ChannelSessions,
|
|
ChannelTasks,
|
|
ChannelScans,
|
|
ChannelHistoryImport,
|
|
ChannelUserState,
|
|
ChannelUserSettings,
|
|
ChannelSettings,
|
|
ChannelPlugins,
|
|
ChannelNotifications,
|
|
}
|
|
|
|
type Envelope struct {
|
|
Channel EventChannel `json:"channel"`
|
|
Event string `json:"event"`
|
|
EventID string `json:"event_id"`
|
|
Timestamp time.Time `json:"timestamp"`
|
|
SourceID string `json:"source_id,omitempty"`
|
|
Data json.RawMessage `json:"data,omitempty"`
|
|
UserID int `json:"user_id,omitempty"`
|
|
ProfileID string `json:"profile_id,omitempty"`
|
|
AdminOnly bool `json:"admin_only,omitempty"`
|
|
// TargetPluginID, when non-empty, restricts dispatch to a single installed
|
|
// plugin (and only if it is already a subscriber). Used by
|
|
// RuntimeHostServer.PublishEventTo.
|
|
TargetPluginID string `json:"target_plugin_id,omitempty"`
|
|
}
|
|
|
|
type PublishOptions struct {
|
|
EventID string
|
|
UserID int
|
|
ProfileID string
|
|
AdminOnly bool
|
|
}
|
|
|
|
type EventsHelloMessage struct {
|
|
Type string `json:"type"`
|
|
SchemaVersion int `json:"schema_version"`
|
|
ConnectionID string `json:"connection_id"`
|
|
AvailableChannels []EventChannel `json:"available_channels"`
|
|
RequiredAction string `json:"required_action"`
|
|
}
|
|
|
|
type EventsSubscribeMessage struct {
|
|
Type string `json:"type"`
|
|
RequestID string `json:"request_id,omitempty"`
|
|
Channels []EventChannel `json:"channels"`
|
|
}
|
|
|
|
type EventsRejectedChannel struct {
|
|
Channel EventChannel `json:"channel"`
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
type EventsSubscribedMessage struct {
|
|
Type string `json:"type"`
|
|
RequestID string `json:"request_id,omitempty"`
|
|
Channels []EventChannel `json:"channels"`
|
|
Rejected []EventsRejectedChannel `json:"rejected,omitempty"`
|
|
}
|
|
|
|
type EventsSnapshotMessage struct {
|
|
Type string `json:"type"`
|
|
Channel EventChannel `json:"channel"`
|
|
Timestamp string `json:"timestamp"`
|
|
Data json.RawMessage `json:"data"`
|
|
}
|
|
|
|
type EventsEventMessage struct {
|
|
Type string `json:"type"`
|
|
Channel EventChannel `json:"channel"`
|
|
Event string `json:"event"`
|
|
EventID string `json:"event_id"`
|
|
Timestamp string `json:"timestamp"`
|
|
Data json.RawMessage `json:"data"`
|
|
}
|
|
|
|
type EventsErrorMessage struct {
|
|
Type string `json:"type"`
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
}
|