Documentation
¶
Overview ¶
Package store is the SQLite persistence layer. Every query that touches user-owned data is scoped by user_id (mirrors the secretary project).
Index ¶
- Constants
- Variables
- func IsBroadcast(it OutboxItem) bool
- func Now() int64
- type Channel
- type Counts
- type DeliveryLog
- type Device
- type DeviceRow
- type OutboxItem
- type Stats
- type Store
- func (s *Store) BroadcastChatID(ctx context.Context, fallback int64) int64
- func (s *Store) ClaimBindCode(ctx context.Context, code string, now int64) (userID, channelID int64, err error)
- func (s *Store) ClearAllTelegramBindings(ctx context.Context) (int64, error)
- func (s *Store) ClearChannelBinding(ctx context.Context, id int64) error
- func (s *Store) ClearOutbox(ctx context.Context) (int64, error)
- func (s *Store) Close() error
- func (s *Store) Counts(ctx context.Context) Counts
- func (s *Store) CreateBindCode(ctx context.Context, code string, userID, channelID, expiresAt int64) error
- func (s *Store) CreateChannel(ctx context.Context, userID int64, typ string, enabled bool, config string) (*Channel, error)
- func (s *Store) CreateDevice(ctx context.Context, userID int64, tokenHash, label string) (*Device, error)
- func (s *Store) CreateUser(ctx context.Context, username, passwordHash string) (*User, error)
- func (s *Store) DeleteAllDeliveryLogs(ctx context.Context) (int64, error)
- func (s *Store) DeleteChannel(ctx context.Context, userID, id int64) error
- func (s *Store) DeleteDeliveryLogsOlderThan(ctx context.Context, cutoff int64) (int64, error)
- func (s *Store) DeleteDevice(ctx context.Context, id int64) error
- func (s *Store) DeleteOutbox(ctx context.Context, id int64) error
- func (s *Store) DeleteRevokedDevices(ctx context.Context, userID int64) (int64, error)
- func (s *Store) DeleteUser(ctx context.Context, id int64) error
- func (s *Store) DueOutbox(ctx context.Context, now int64, limit int) ([]OutboxItem, error)
- func (s *Store) EnqueueBroadcast(ctx context.Context, sender, content string, smsTs int64, clientMsgID string) (bool, error)
- func (s *Store) EnqueueOutbox(ctx context.Context, userID, channelID int64, sender, content string, ...) (bool, error)
- func (s *Store) GetChannel(ctx context.Context, userID, id int64) (*Channel, error)
- func (s *Store) GetSetting(ctx context.Context, key string) (string, error)
- func (s *Store) GetUserByUsername(ctx context.Context, username string) (*User, error)
- func (s *Store) InsertDeliveryLog(ctx context.Context, l DeliveryLog) error
- func (s *Store) ListChannels(ctx context.Context, userID int64) ([]Channel, error)
- func (s *Store) ListDeliveryLogs(ctx context.Context, status string, userID int64, limit int) ([]DeliveryLog, error)
- func (s *Store) ListDevices(ctx context.Context, userID int64) ([]DeviceRow, error)
- func (s *Store) ListUsersWithCounts(ctx context.Context) ([]UserRow, error)
- func (s *Store) PurgeBindCodes(ctx context.Context, now int64) (int64, error)
- func (s *Store) RescheduleOutbox(ctx context.Context, id int64, attempts int, nextAt int64) error
- func (s *Store) RevokeDevice(ctx context.Context, deviceID int64) error
- func (s *Store) RevokeUserDevices(ctx context.Context, userID int64) error
- func (s *Store) SetChannelConfig(ctx context.Context, id int64, config string) error
- func (s *Store) SetSetting(ctx context.Context, key, value string) error
- func (s *Store) SetUserStatus(ctx context.Context, id int64, status string) error
- func (s *Store) Stats(ctx context.Context) (Stats, error)
- func (s *Store) UpdateChannel(ctx context.Context, userID, id int64, enabled bool, config string) error
- func (s *Store) UserByDeviceToken(ctx context.Context, tokenHash string) (*User, int64, error)
- type User
- type UserRow
Constants ¶
const BroadcastChannelType = "broadcast"
BroadcastChannelType is the channel type recorded for broadcast deliveries.
Variables ¶
var ErrNotFound = errors.New("not found")
Functions ¶
func IsBroadcast ¶
func IsBroadcast(it OutboxItem) bool
IsBroadcast reports whether an outbox row is the global broadcast item.
Types ¶
type Counts ¶
type Counts struct {
Users, Devices, Channels, BindCodes, Outbox, DeliveryLogs int
}
Counts returns per-table row counts for the admin Data page.
type DeliveryLog ¶
type OutboxItem ¶
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
func (*Store) BroadcastChatID ¶
BroadcastChatID resolves the global broadcast target chat id: the DB setting "publish_chat_id" (admin-editable) wins; fallback (env) applies when unset. 0 = disabled. A channel/group chat id is negative.
func (*Store) ClaimBindCode ¶
func (s *Store) ClaimBindCode(ctx context.Context, code string, now int64) (userID, channelID int64, err error)
ClaimBindCode atomically marks a valid, unexpired, unused code as used and returns its (user_id, channel_id). Returns ErrNotFound otherwise.
func (*Store) ClearAllTelegramBindings ¶
ClearAllTelegramBindings resets every telegram channel's config to empty, so every user must re-bind (used when the shared bot is changed/replaced). Count.
func (*Store) ClearChannelBinding ¶
ClearChannelBinding empties one channel's config (used by the 403 self-heal).
func (*Store) ClearOutbox ¶
ClearOutbox drops all pending deliveries (undelivered messages are lost); count.
func (*Store) CreateBindCode ¶
func (*Store) CreateChannel ¶
func (*Store) CreateDevice ¶
func (*Store) CreateUser ¶
func (*Store) DeleteAllDeliveryLogs ¶
DeleteAllDeliveryLogs clears the delivery log; returns count.
func (*Store) DeleteChannel ¶
func (*Store) DeleteDeliveryLogsOlderThan ¶
DeleteDeliveryLogsOlderThan deletes logs created before cutoff (unix s); returns count.
func (*Store) DeleteDevice ¶
DeleteDevice hard-deletes a single device row.
func (*Store) DeleteRevokedDevices ¶
DeleteRevokedDevices hard-deletes all revoked devices of a user; returns count.
func (*Store) DeleteUser ¶
DeleteUser removes the user and all owned rows. devices/channels/tg_bind_codes cascade via FK; outbox/delivery_logs have no FK, so clear them explicitly.
func (*Store) EnqueueBroadcast ¶
func (s *Store) EnqueueBroadcast(ctx context.Context, sender, content string, smsTs int64, clientMsgID string) (bool, error)
EnqueueBroadcast inserts a delivery task for the global broadcast target (a configured Telegram channel/group). It uses the sentinel (user_id=0, channel_id=0) and the same (channel_id, client_msg_id) dedup as per-user rows, so retried SMS are not broadcast twice.
func (*Store) EnqueueOutbox ¶
func (s *Store) EnqueueOutbox(ctx context.Context, userID, channelID int64, sender, content string, smsTs int64, clientMsgID string) (bool, error)
EnqueueOutbox inserts a delivery task. When clientMsgID is non-empty the (channel_id, client_msg_id) unique index makes re-inserts a no-op (idempotent). Returns true if a new row was actually inserted.
func (*Store) GetChannel ¶
GetChannel returns a channel only if it belongs to userID.
func (*Store) GetSetting ¶
func (*Store) GetUserByUsername ¶
func (*Store) InsertDeliveryLog ¶
func (s *Store) InsertDeliveryLog(ctx context.Context, l DeliveryLog) error
func (*Store) ListChannels ¶
func (*Store) ListDeliveryLogs ¶
func (s *Store) ListDeliveryLogs(ctx context.Context, status string, userID int64, limit int) ([]DeliveryLog, error)
ListDeliveryLogs returns recent logs, optionally filtered by status ("ok"/"fail") and/or userID (0 = any).
func (*Store) ListDevices ¶
func (*Store) ListUsersWithCounts ¶
func (*Store) PurgeBindCodes ¶
PurgeBindCodes deletes used or expired one-time bind codes; returns count.
func (*Store) RescheduleOutbox ¶
RescheduleOutbox bumps attempts and sets the next retry time.
func (*Store) RevokeDevice ¶
func (*Store) RevokeUserDevices ¶
func (*Store) SetChannelConfig ¶
SetChannelConfig overwrites just the config JSON (used by the TG bind flow).
func (*Store) SetUserStatus ¶
func (*Store) UpdateChannel ¶
func (*Store) UserByDeviceToken ¶
UserByDeviceToken resolves an active device token hash to its (active) user, and bumps last_seen_at. Returns ErrNotFound if the token is unknown/revoked or the user is not active.