store

package
v0.0.0-...-fce5618 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const BroadcastChannelType = "broadcast"

BroadcastChannelType is the channel type recorded for broadcast deliveries.

Variables

View Source
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.

func Now

func Now() int64

Types

type Channel

type Channel struct {
	ID        int64
	UserID    int64
	Type      string
	Enabled   bool
	Config    string // raw JSON
	CreatedAt int64
	UpdatedAt int64
}

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 DeliveryLog struct {
	ID          int64
	UserID      int64
	ChannelType string
	Sender      string
	Status      string
	Detail      string
	CreatedAt   int64
}

type Device

type Device struct {
	ID        int64
	UserID    int64
	TokenHash string
	Label     string
	CreatedAt int64
}

type DeviceRow

type DeviceRow struct {
	ID         int64
	Label      string
	CreatedAt  int64
	LastSeenAt int64
	Revoked    bool
}

type OutboxItem

type OutboxItem struct {
	ID          int64
	UserID      int64
	ChannelID   int64
	Sender      string
	Content     string
	SmsTs       int64
	ClientMsgID string
	Attempts    int
	NextAt      int64
	CreatedAt   int64
}

type Stats

type Stats struct {
	Users     int
	Channels  int
	OutboxLen int
	DeliverOK int
	DeliverNG int
}

type Store

type Store struct {
	// contains filtered or unexported fields
}

func Open

func Open(path string) (*Store, error)

Open opens (and migrates) the SQLite database at path.

func (*Store) BroadcastChatID

func (s *Store) BroadcastChatID(ctx context.Context, fallback int64) int64

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

func (s *Store) ClearAllTelegramBindings(ctx context.Context) (int64, error)

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

func (s *Store) ClearChannelBinding(ctx context.Context, id int64) error

ClearChannelBinding empties one channel's config (used by the 403 self-heal).

func (*Store) ClearOutbox

func (s *Store) ClearOutbox(ctx context.Context) (int64, error)

ClearOutbox drops all pending deliveries (undelivered messages are lost); count.

func (*Store) Close

func (s *Store) Close() error

func (*Store) Counts

func (s *Store) Counts(ctx context.Context) Counts

func (*Store) CreateBindCode

func (s *Store) CreateBindCode(ctx context.Context, code string, userID, channelID, expiresAt int64) error

func (*Store) CreateChannel

func (s *Store) CreateChannel(ctx context.Context, userID int64, typ string, enabled bool, config string) (*Channel, error)

func (*Store) CreateDevice

func (s *Store) CreateDevice(ctx context.Context, userID int64, tokenHash, label string) (*Device, error)

func (*Store) CreateUser

func (s *Store) CreateUser(ctx context.Context, username, passwordHash string) (*User, error)

func (*Store) DeleteAllDeliveryLogs

func (s *Store) DeleteAllDeliveryLogs(ctx context.Context) (int64, error)

DeleteAllDeliveryLogs clears the delivery log; returns count.

func (*Store) DeleteChannel

func (s *Store) DeleteChannel(ctx context.Context, userID, id int64) error

func (*Store) DeleteDeliveryLogsOlderThan

func (s *Store) DeleteDeliveryLogsOlderThan(ctx context.Context, cutoff int64) (int64, error)

DeleteDeliveryLogsOlderThan deletes logs created before cutoff (unix s); returns count.

func (*Store) DeleteDevice

func (s *Store) DeleteDevice(ctx context.Context, id int64) error

DeleteDevice hard-deletes a single device row.

func (*Store) DeleteOutbox

func (s *Store) DeleteOutbox(ctx context.Context, id int64) error

func (*Store) DeleteRevokedDevices

func (s *Store) DeleteRevokedDevices(ctx context.Context, userID int64) (int64, error)

DeleteRevokedDevices hard-deletes all revoked devices of a user; returns count.

func (*Store) DeleteUser

func (s *Store) DeleteUser(ctx context.Context, id int64) error

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) DueOutbox

func (s *Store) DueOutbox(ctx context.Context, now int64, limit int) ([]OutboxItem, error)

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

func (s *Store) GetChannel(ctx context.Context, userID, id int64) (*Channel, error)

GetChannel returns a channel only if it belongs to userID.

func (*Store) GetSetting

func (s *Store) GetSetting(ctx context.Context, key string) (string, error)

func (*Store) GetUserByUsername

func (s *Store) GetUserByUsername(ctx context.Context, username string) (*User, error)

func (*Store) InsertDeliveryLog

func (s *Store) InsertDeliveryLog(ctx context.Context, l DeliveryLog) error

func (*Store) ListChannels

func (s *Store) ListChannels(ctx context.Context, userID int64) ([]Channel, error)

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 (s *Store) ListDevices(ctx context.Context, userID int64) ([]DeviceRow, error)

func (*Store) ListUsersWithCounts

func (s *Store) ListUsersWithCounts(ctx context.Context) ([]UserRow, error)

func (*Store) PurgeBindCodes

func (s *Store) PurgeBindCodes(ctx context.Context, now int64) (int64, error)

PurgeBindCodes deletes used or expired one-time bind codes; returns count.

func (*Store) RescheduleOutbox

func (s *Store) RescheduleOutbox(ctx context.Context, id int64, attempts int, nextAt int64) error

RescheduleOutbox bumps attempts and sets the next retry time.

func (*Store) RevokeDevice

func (s *Store) RevokeDevice(ctx context.Context, deviceID int64) error

func (*Store) RevokeUserDevices

func (s *Store) RevokeUserDevices(ctx context.Context, userID int64) error

func (*Store) SetChannelConfig

func (s *Store) SetChannelConfig(ctx context.Context, id int64, config string) error

SetChannelConfig overwrites just the config JSON (used by the TG bind flow).

func (*Store) SetSetting

func (s *Store) SetSetting(ctx context.Context, key, value string) error

func (*Store) SetUserStatus

func (s *Store) SetUserStatus(ctx context.Context, id int64, status string) error

func (*Store) Stats

func (s *Store) Stats(ctx context.Context) (Stats, error)

func (*Store) UpdateChannel

func (s *Store) UpdateChannel(ctx context.Context, userID, id int64, enabled bool, config string) error

func (*Store) UserByDeviceToken

func (s *Store) UserByDeviceToken(ctx context.Context, tokenHash string) (*User, int64, error)

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.

type User

type User struct {
	ID           int64
	Username     string
	PasswordHash string
	Status       string
	CreatedAt    int64
}

type UserRow

type UserRow struct {
	User
	Channels int
	Devices  int
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL