Documentation
¶
Overview ¶
Package db implements PostgreSQL-backed storage for teled.
Index ¶
- Variables
- func DecodeDeleted(extra []byte) []int64
- func DecodeRead(extra []byte) (peer, maxID int64)
- func Migrate(uri string) error
- func Open(ctx context.Context, uri string, providers obs.Providers) (*pgxpool.Pool, error)
- type DB
- func (db *DB) BindSession(ctx context.Context, keyID [8]byte, userID int64) error
- func (db *DB) BotByToken(ctx context.Context, token string) (*teled.User, bool, error)
- func (db *DB) BotCommands(ctx context.Context, botUserID int64, scope, langCode string) ([]teled.BotCommand, error)
- func (db *DB) BotFatherState(ctx context.Context, userID int64) (teled.BotFatherState, error)
- func (db *DB) BotsByOwner(ctx context.Context, ownerID int64) ([]teled.User, error)
- func (db *DB) ClearBotFatherState(ctx context.Context, userID int64) error
- func (db *DB) CreateBot(ctx context.Context, token, username, firstName string) (teled.User, error)
- func (db *DB) CreateOwnedBot(ctx context.Context, username, firstName string, ownerID int64, secret string) (teled.User, error)
- func (db *DB) CreateUser(ctx context.Context, phone, firstName, lastName string) (teled.User, error)
- func (db *DB) DeleteMessages(ctx context.Context, self int64, localIDs []int64) (teled.DeleteResult, error)
- func (db *DB) EditMessage(ctx context.Context, self, localID int64, text string) (teled.EditResult, error)
- func (db *DB) FileByID(ctx context.Context, id int64) (teled.File, bool, error)
- func (db *DB) GetDialogs(ctx context.Context, self int64, limit int) ([]teled.Dialog, error)
- func (db *DB) GetDifference(ctx context.Context, self int64, sincePts, limit int) ([]teled.UpdateLogEntry, int, error)
- func (db *DB) GetHistory(ctx context.Context, self, peer, offsetID int64, limit int) ([]teled.Message, error)
- func (db *DB) GetState(ctx context.Context, self int64) (teled.State, error)
- func (db *DB) MessageByGlobal(ctx context.Context, self, globalID int64) (teled.Message, bool, error)
- func (db *DB) PhoneCode(ctx context.Context, phone, codeHash string) (code string, ok bool, err error)
- func (db *DB) ReadHistory(ctx context.Context, self, peer, maxID int64) (pts int, err error)
- func (db *DB) Ready(ctx context.Context) error
- func (db *DB) ResetBotCommands(ctx context.Context, botUserID int64, scope, langCode string) error
- func (db *DB) SaveFile(ctx context.Context, f teled.File) (teled.File, error)
- func (db *DB) SavePhoneCode(ctx context.Context, phone, codeHash, code string, ttl time.Duration) error
- func (db *DB) SendMessage(ctx context.Context, fromID, peerID int64, text string, ...) (teled.SentMessage, error)
- func (db *DB) SessionUserID(ctx context.Context, keyID [8]byte) (userID int64, ok bool, err error)
- func (db *DB) SetBotCommands(ctx context.Context, botUserID int64, scope, langCode string, ...) error
- func (db *DB) SetBotFatherState(ctx context.Context, userID int64, s teled.BotFatherState) error
- func (db *DB) SetBotToken(ctx context.Context, botID int64, token string) error
- func (db *DB) Unbind(ctx context.Context, keyID [8]byte) error
- func (db *DB) UserByID(ctx context.Context, id int64) (*teled.User, bool, error)
- func (db *DB) UserByPhone(ctx context.Context, phone string) (*teled.User, bool, error)
- func (db *DB) UserByUsername(ctx context.Context, username string) (*teled.User, bool, error)
- func (db *DB) UsersByIDs(ctx context.Context, ids []int64) ([]teled.User, error)
- type KeyStore
Constants ¶
This section is empty.
Variables ¶
var ErrMessageID = errors.New("message id invalid")
ErrMessageID indicates a referenced message does not exist or is not editable by the caller.
Functions ¶
func DecodeDeleted ¶
DecodeDeleted extracts deleted local ids from a delete log entry.
func DecodeRead ¶
DecodeRead extracts (peer, maxID) from a read log entry.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is the PostgreSQL-backed storage implementation.
func (*DB) BindSession ¶
BindSession binds an MTProto auth key to a logged-in user.
func (*DB) BotByToken ¶ added in v0.4.0
BotByToken returns the bot account holding token, if any.
func (*DB) BotCommands ¶ added in v0.4.0
func (db *DB) BotCommands(ctx context.Context, botUserID int64, scope, langCode string) ([]teled.BotCommand, error)
BotCommands returns the command list published for the given scope and language. A missing row yields an empty slice and no error.
func (*DB) BotFatherState ¶ added in v0.4.0
BotFatherState returns the user's pending BotFather flow. A zero-value state (empty Step) and no error means no flow is in progress.
func (*DB) BotsByOwner ¶ added in v0.4.0
BotsByOwner returns the bots created by ownerID, oldest first.
func (*DB) ClearBotFatherState ¶ added in v0.4.0
ClearBotFatherState ends any pending BotFather flow for the user.
func (*DB) CreateBot ¶ added in v0.4.0
CreateBot inserts a new bot account authenticated by token. username may be empty.
func (*DB) CreateOwnedBot ¶ added in v0.4.0
func (db *DB) CreateOwnedBot(ctx context.Context, username, firstName string, ownerID int64, secret string) (teled.User, error)
CreateOwnedBot creates a bot owned by ownerID, mints its token as "<bot_id>:<secret>" once the id is known, and returns the persisted account (with BotToken populated).
func (*DB) CreateUser ¶
func (db *DB) CreateUser(ctx context.Context, phone, firstName, lastName string) (teled.User, error)
CreateUser inserts a new user with the given phone and name.
func (*DB) DeleteMessages ¶
func (db *DB) DeleteMessages(ctx context.Context, self int64, localIDs []int64) (teled.DeleteResult, error)
DeleteMessages marks the caller's messages (by local id) deleted and allocates a pts covering them.
func (*DB) EditMessage ¶
func (db *DB) EditMessage(ctx context.Context, self, localID int64, text string) (teled.EditResult, error)
EditMessage updates the text of a message the caller sent, returning the data needed to emit edit updates to both participants.
func (*DB) GetDialogs ¶
GetDialogs returns the caller's conversations, newest activity first.
func (*DB) GetDifference ¶
func (db *DB) GetDifference(ctx context.Context, self int64, sincePts, limit int) ([]teled.UpdateLogEntry, int, error)
GetDifference returns log entries with pts greater than sincePts (up to limit) and the caller's current pts.
func (*DB) GetHistory ¶
func (db *DB) GetHistory(ctx context.Context, self, peer, offsetID int64, limit int) ([]teled.Message, error)
GetHistory returns up to limit messages between self and peer, newest first. When offsetID > 0 only messages with a smaller local id are returned.
func (*DB) MessageByGlobal ¶
func (db *DB) MessageByGlobal(ctx context.Context, self, globalID int64) (teled.Message, bool, error)
MessageByGlobal returns the caller's view of a message by its canonical id.
func (*DB) PhoneCode ¶
func (db *DB) PhoneCode(ctx context.Context, phone, codeHash string) (code string, ok bool, err error)
PhoneCode returns the unexpired code stored for (phone, codeHash).
func (*DB) ReadHistory ¶
ReadHistory marks the caller's incoming messages from peer up to maxID as read and allocates a pts for the read event.
func (*DB) ResetBotCommands ¶ added in v0.4.0
ResetBotCommands removes the command list for the given scope and language, restoring the bot's default (empty) commands there.
func (*DB) SaveFile ¶
SaveFile records uploaded media, generating an access hash and file reference, and returns the populated File.
func (*DB) SavePhoneCode ¶
func (db *DB) SavePhoneCode(ctx context.Context, phone, codeHash, code string, ttl time.Duration) error
SavePhoneCode stores (or replaces) a login code for phone under codeHash, valid for ttl.
func (*DB) SendMessage ¶
func (db *DB) SendMessage(ctx context.Context, fromID, peerID int64, text string, randomID, mediaFileID int64) (teled.SentMessage, error)
SendMessage persists a DM atomically: the canonical message plus a per-account ref (with its own local id and pts) for sender and recipient. A non-zero mediaFileID attaches stored media so the message re-renders in history.
func (*DB) SessionUserID ¶
SessionUserID returns the user bound to the given auth key, if any.
func (*DB) SetBotCommands ¶ added in v0.4.0
func (db *DB) SetBotCommands(ctx context.Context, botUserID int64, scope, langCode string, commands []teled.BotCommand) error
SetBotCommands replaces the command list a bot publishes for the given scope and language. An empty commands slice stores an empty list (distinct from having no row, which ResetBotCommands produces).
func (*DB) SetBotFatherState ¶ added in v0.4.0
SetBotFatherState upserts the user's pending BotFather flow.
func (*DB) SetBotToken ¶ added in v0.4.0
SetBotToken replaces a bot's auth token, e.g. on /revoke. A missing bot is a no-op.
func (*DB) UserByPhone ¶
UserByPhone returns a user by phone number.
func (*DB) UserByUsername ¶
UserByUsername returns a user by username.
type KeyStore ¶
type KeyStore struct {
// contains filtered or unexported fields
}
KeyStore persists MTProto auth keys, implementing mtproto.KeyStorage.
func NewKeyStore ¶
NewKeyStore creates a KeyStore over the given pool.