dal

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Q       = new(Query)
	Alarm   *alarm
	History *history
	Keyword *keyword
)
View Source
var (
	// ErrNotFound reports an id with no users row.
	ErrNotFound = errors.New("dal: user not found")

	// ErrSelfTarget reports an operation that would remove or restrict the
	// actor's own access: deleting themselves, disabling themselves, or
	// demoting themselves from administrator.
	ErrSelfTarget = errors.New("dal: an actor cannot remove or restrict their own access")

	// ErrEnvManaged reports a row whose role/enabled state is owned by
	// MANAGER_ID or ALLOWED_USER_IDS. Only the remark may be edited by hand.
	ErrEnvManaged = errors.New("dal: user's role and access are managed by environment variables")

	// ErrLastAdmin reports an operation that would leave the deployment without
	// any enabled administrator, which would make the bot unadministrable.
	ErrLastAdmin = errors.New("dal: operation would leave no enabled administrator")
)

Sentinel errors of the access-control guards. They are exported so the transport layer can map them to a status code without matching on message text: ErrNotFound -> 404, the other three -> 409 (or 403).

View Source
var User = &user{}

User is the hand-written DAO for the access-control tables. It is a package singleton rather than a field of Query because the Query type is generated and must not be edited; every method reads the current Q.db, so SetDefault binds it exactly like the generated DAOs.

Functions

func ClaimAttachment added in v1.4.10

func ClaimAttachment(userID int64, url string, now, leaseUntil time.Time) (bool, error)

ClaimAttachment takes the lease on one pending attachment. It reports whether this caller won the row; a loser must leave it alone. The conditional update is what keeps a shared database free of duplicate attachments.

func CompleteAttachment added in v1.4.10

func CompleteAttachment(userID int64, url string) error

CompleteAttachment drops the row once the attachment has been delivered.

func DueAttachments added in v1.4.10

func DueAttachments(now time.Time, limit int) ([]model.PendingAttachment, error)

DueAttachments returns the pending attachments that are due at now and not claimed by another worker, oldest deadline first.

func EnqueueAttachment added in v1.4.10

func EnqueueAttachment(a *model.PendingAttachment) error

EnqueueAttachment records a notice whose PDF attachment still has to be delivered.

The (user_id, url) primary key makes this an upsert: a notice that fails again — a forced re-push, for instance — refreshes the existing row, resets its retry budget and points at the newest announced message instead of queueing a second attachment for the same page.

func EnsureBootstrap added in v1.5.0

func EnsureBootstrap(managers, allowed []int64) (string, error)

EnsureBootstrap seeds users from the environment and makes sure an access mode exists. It runs once at startup, before the bot serves requests.

The mode is only written when the settings row is absent or empty: an operator who switched the bot to "off" (or back to "restricted") must keep that choice across restarts. A database whose first start already has an administrator row starts restricted; an empty one starts open, so a fresh deployment cannot lock itself out before MANAGER_ID is configured.

func GetSetting added in v1.5.0

func GetSetting(key string) (string, bool, error)

GetSetting reads one settings row. The boolean is false when the key has never been written, which lets a caller distinguish "not configured" from an empty value without treating a missing row as an error.

A failed read is returned: silently treating it as "not configured" would let a broken database flip the access mode, and the access cache must not cache a guess.

func InsertIfAbsent added in v1.4.9

func InsertIfAbsent(db *gorm.DB, value any, conflictColumns ...string) (bool, error)

InsertIfAbsent inserts value only when no row conflicting on the given unique columns exists, reporting whether the row was actually created.

The table's unique constraint therefore acts as a distributed lock: concurrent callers — even across bot processes sharing the same DB — can never both claim the same row. The caller that gets a true return is the only one allowed to proceed (e.g. send a message); everyone else must skip.

conflictColumns must form (a subset of) a unique index on the table, e.g. "user_id", "credit_code" for alarms or "user_id", "url" for histories.

func MarkKeywordMatch added in v1.4.10

func MarkKeywordMatch(keywordID int32, url string) (bool, error)

MarkKeywordMatch records that keywordID matched url and reports whether the pair is new. The primary key makes it idempotent, so the caller can count distinct notices per keyword without a read-modify-write race.

func PruneKeywordMatches added in v1.4.10

func PruneKeywordMatches(now time.Time) (int64, error)

PruneKeywordMatches drops dedupe rows older than the retention window and reports how many were removed.

func RescheduleAttachment added in v1.4.10

func RescheduleAttachment(userID int64, url string, next time.Time, attempts int, lastErr string) error

RescheduleAttachment records a failed attempt and releases the lease so the row becomes due again at next.

func SetDefault

func SetDefault(db *gorm.DB, opts ...gen.DOOption)

func SetSetting added in v1.5.0

func SetSetting(key, value string) error

SetSetting writes one settings row, creating it on first use.

The write invalidates the access cache: the access mode is cached for the same TTL as user rows, and switching it must take effect on the next request rather than up to TTL later.

Types

type IAlarmDo

type IAlarmDo interface {
	gen.SubQuery
	Debug() IAlarmDo
	WithContext(ctx context.Context) IAlarmDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IAlarmDo
	WriteDB() IAlarmDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IAlarmDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IAlarmDo
	Not(conds ...gen.Condition) IAlarmDo
	Or(conds ...gen.Condition) IAlarmDo
	Select(conds ...field.Expr) IAlarmDo
	Where(conds ...gen.Condition) IAlarmDo
	Order(conds ...field.Expr) IAlarmDo
	Distinct(cols ...field.Expr) IAlarmDo
	Omit(cols ...field.Expr) IAlarmDo
	Join(table schema.Tabler, on ...field.Expr) IAlarmDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IAlarmDo
	RightJoin(table schema.Tabler, on ...field.Expr) IAlarmDo
	Group(cols ...field.Expr) IAlarmDo
	Having(conds ...gen.Condition) IAlarmDo
	Limit(limit int) IAlarmDo
	Offset(offset int) IAlarmDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IAlarmDo
	Unscoped() IAlarmDo
	Create(values ...*model.Alarm) error
	CreateInBatches(values []*model.Alarm, batchSize int) error
	Save(values ...*model.Alarm) error
	First() (*model.Alarm, error)
	Take() (*model.Alarm, error)
	Last() (*model.Alarm, error)
	Find() ([]*model.Alarm, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Alarm, err error)
	FindInBatches(result *[]*model.Alarm, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.Alarm) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IAlarmDo
	Assign(attrs ...field.AssignExpr) IAlarmDo
	Joins(fields ...field.RelationField) IAlarmDo
	Preload(fields ...field.RelationField) IAlarmDo
	FirstOrInit() (*model.Alarm, error)
	FirstOrCreate() (*model.Alarm, error)
	FindByPage(offset int, limit int) (result []*model.Alarm, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Rows() (*sql.Rows, error)
	Row() *sql.Row
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IAlarmDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

type IHistoryDo

type IHistoryDo interface {
	gen.SubQuery
	Debug() IHistoryDo
	WithContext(ctx context.Context) IHistoryDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IHistoryDo
	WriteDB() IHistoryDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IHistoryDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IHistoryDo
	Not(conds ...gen.Condition) IHistoryDo
	Or(conds ...gen.Condition) IHistoryDo
	Select(conds ...field.Expr) IHistoryDo
	Where(conds ...gen.Condition) IHistoryDo
	Order(conds ...field.Expr) IHistoryDo
	Distinct(cols ...field.Expr) IHistoryDo
	Omit(cols ...field.Expr) IHistoryDo
	Join(table schema.Tabler, on ...field.Expr) IHistoryDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IHistoryDo
	RightJoin(table schema.Tabler, on ...field.Expr) IHistoryDo
	Group(cols ...field.Expr) IHistoryDo
	Having(conds ...gen.Condition) IHistoryDo
	Limit(limit int) IHistoryDo
	Offset(offset int) IHistoryDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IHistoryDo
	Unscoped() IHistoryDo
	Create(values ...*model.History) error
	CreateInBatches(values []*model.History, batchSize int) error
	Save(values ...*model.History) error
	First() (*model.History, error)
	Take() (*model.History, error)
	Last() (*model.History, error)
	Find() ([]*model.History, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.History, err error)
	FindInBatches(result *[]*model.History, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.History) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IHistoryDo
	Assign(attrs ...field.AssignExpr) IHistoryDo
	Joins(fields ...field.RelationField) IHistoryDo
	Preload(fields ...field.RelationField) IHistoryDo
	FirstOrInit() (*model.History, error)
	FirstOrCreate() (*model.History, error)
	FindByPage(offset int, limit int) (result []*model.History, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Rows() (*sql.Rows, error)
	Row() *sql.Row
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IHistoryDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

type IKeywordDo

type IKeywordDo interface {
	gen.SubQuery
	Debug() IKeywordDo
	WithContext(ctx context.Context) IKeywordDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IKeywordDo
	WriteDB() IKeywordDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IKeywordDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IKeywordDo
	Not(conds ...gen.Condition) IKeywordDo
	Or(conds ...gen.Condition) IKeywordDo
	Select(conds ...field.Expr) IKeywordDo
	Where(conds ...gen.Condition) IKeywordDo
	Order(conds ...field.Expr) IKeywordDo
	Distinct(cols ...field.Expr) IKeywordDo
	Omit(cols ...field.Expr) IKeywordDo
	Join(table schema.Tabler, on ...field.Expr) IKeywordDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IKeywordDo
	RightJoin(table schema.Tabler, on ...field.Expr) IKeywordDo
	Group(cols ...field.Expr) IKeywordDo
	Having(conds ...gen.Condition) IKeywordDo
	Limit(limit int) IKeywordDo
	Offset(offset int) IKeywordDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IKeywordDo
	Unscoped() IKeywordDo
	Create(values ...*model.Keyword) error
	CreateInBatches(values []*model.Keyword, batchSize int) error
	Save(values ...*model.Keyword) error
	First() (*model.Keyword, error)
	Take() (*model.Keyword, error)
	Last() (*model.Keyword, error)
	Find() ([]*model.Keyword, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Keyword, err error)
	FindInBatches(result *[]*model.Keyword, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.Keyword) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IKeywordDo
	Assign(attrs ...field.AssignExpr) IKeywordDo
	Joins(fields ...field.RelationField) IKeywordDo
	Preload(fields ...field.RelationField) IKeywordDo
	FirstOrInit() (*model.Keyword, error)
	FirstOrCreate() (*model.Keyword, error)
	FindByPage(offset int, limit int) (result []*model.Keyword, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Rows() (*sql.Rows, error)
	Row() *sql.Row
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IKeywordDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

type Query

type Query struct {
	Alarm   alarm
	History history
	Keyword keyword
	// contains filtered or unexported fields
}

func Use

func Use(db *gorm.DB, opts ...gen.DOOption) *Query

func (*Query) Available

func (q *Query) Available() bool

func (*Query) Begin

func (q *Query) Begin(opts ...*sql.TxOptions) *QueryTx

func (*Query) ReadDB

func (q *Query) ReadDB() *Query

func (*Query) ReplaceDB

func (q *Query) ReplaceDB(db *gorm.DB) *Query

func (*Query) Transaction

func (q *Query) Transaction(fc func(tx *Query) error, opts ...*sql.TxOptions) error

func (*Query) WithContext

func (q *Query) WithContext(ctx context.Context) *queryCtx

func (*Query) WriteDB

func (q *Query) WriteDB() *Query

type QueryTx

type QueryTx struct {
	*Query
	Error error
}

func (*QueryTx) Commit

func (q *QueryTx) Commit() error

func (*QueryTx) Rollback

func (q *QueryTx) Rollback() error

func (*QueryTx) RollbackTo

func (q *QueryTx) RollbackTo(name string) error

func (*QueryTx) SavePoint

func (q *QueryTx) SavePoint(name string) error

type UserPatch added in v1.5.0

type UserPatch struct {
	Remark  *string
	Role    *int32
	Enabled *bool
}

UserPatch carries the fields an administrator may change. A nil field means "leave as is", which is why these are pointers rather than zero values: false and 0 are meaningful updates.

Jump to

Keyboard shortcuts

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