db

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package db implements PostgreSQL-backed storage for teled.

Index

Constants

This section is empty.

Variables

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

func DecodeDeleted(extra []byte) []int64

DecodeDeleted extracts deleted local ids from a delete log entry.

func DecodeRead

func DecodeRead(extra []byte) (peer, maxID int64)

DecodeRead extracts (peer, maxID) from a read log entry.

func Migrate

func Migrate(uri string) error

Migrate applies all pending migrations to the database at uri.

func Open

func Open(ctx context.Context, uri string) (*pgxpool.Pool, error)

Open creates a new connection pool to the given PostgreSQL DSN.

Types

type DB

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

DB is the PostgreSQL-backed storage implementation.

func New

func New(pool *pgxpool.Pool) *DB

New creates a DB over the given pool.

func (*DB) BindSession

func (db *DB) BindSession(ctx context.Context, keyID [8]byte, userID int64) error

BindSession binds an MTProto auth key to a logged-in user.

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

func (db *DB) FileByID(ctx context.Context, id int64) (teled.File, bool, error)

FileByID returns stored media by id.

func (*DB) GetDialogs

func (db *DB) GetDialogs(ctx context.Context, self int64, limit int) ([]teled.Dialog, error)

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

func (db *DB) GetState(ctx context.Context, self int64) (teled.State, error)

GetState returns the caller's current update state.

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

func (db *DB) ReadHistory(ctx context.Context, self, peer, maxID int64) (pts int, err error)

ReadHistory marks the caller's incoming messages from peer up to maxID as read and allocates a pts for the read event.

func (*DB) Ready

func (db *DB) Ready(ctx context.Context) error

Ready reports whether the database is reachable.

func (*DB) SaveFile

func (db *DB) SaveFile(ctx context.Context, f teled.File) (teled.File, error)

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

func (db *DB) SessionUserID(ctx context.Context, keyID [8]byte) (userID int64, ok bool, err error)

SessionUserID returns the user bound to the given auth key, if any.

func (*DB) Unbind

func (db *DB) Unbind(ctx context.Context, keyID [8]byte) error

Unbind removes the user binding for an auth key (logout).

func (*DB) UserByID

func (db *DB) UserByID(ctx context.Context, id int64) (*teled.User, bool, error)

UserByID returns a user by id.

func (*DB) UserByPhone

func (db *DB) UserByPhone(ctx context.Context, phone string) (*teled.User, bool, error)

UserByPhone returns a user by phone number.

func (*DB) UserByUsername

func (db *DB) UserByUsername(ctx context.Context, username string) (*teled.User, bool, error)

UserByUsername returns a user by username.

func (*DB) UsersByIDs

func (db *DB) UsersByIDs(ctx context.Context, ids []int64) ([]teled.User, error)

UsersByIDs returns the users with the given ids, in arbitrary order.

type KeyStore

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

KeyStore persists MTProto auth keys, implementing mtproto.KeyStorage.

func NewKeyStore

func NewKeyStore(pool *pgxpool.Pool) *KeyStore

NewKeyStore creates a KeyStore over the given pool.

func (*KeyStore) Get

func (s *KeyStore) Get(ctx context.Context, id [8]byte) (crypto.AuthKey, bool, error)

Get returns the key by its 8-byte ID. The boolean is false when absent.

func (*KeyStore) Save

func (s *KeyStore) Save(ctx context.Context, key crypto.AuthKey) error

Save persists an auth key. Saving an existing key is a no-op.

Jump to

Keyboard shortcuts

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