storage

package
v1.0.0-beta24 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultUsername   = ""
	DefaultAccessHash = 0
)
View Source
const LatestVersion = 1

Variables

This section is empty.

Functions

func AddPeersFromDialogs

func AddPeersFromDialogs(ctx context.Context, raw *tg.Client, peerStorage *PeerStorage) error

AddPeersFromDialogs iterates all dialogs via the Telegram API and adds every encountered user, chat and channel to peerStorage. It returns any error from the underlying RPC pagination.

func ConvKey

func ConvKey(chatID, userID int64) string

Types

type Adapter

type Adapter interface {
	GetSession(version int) (*Session, error)
	UpdateSession(s *Session) error

	SavePeer(p *Peer) error
	GetPeerByID(id int64) (*Peer, error)
	GetPeerByUsername(username string) (*Peer, error)

	SaveConvState(state *ConvState) error
	LoadConvState(key string) (*ConvState, error)
	DeleteConvState(key string) error
	ListConvStates() ([]ConvState, error)

	AutoMigrate() error
	Close() error
}

type ConvState

type ConvState struct {
	Key       string `gorm:"primaryKey"`
	ChatID    int64  `gorm:"index"`
	UserID    int64  `gorm:"index"`
	Step      string
	Payload   []byte
	ExpiresAt time.Time `gorm:"index"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type EntityType

type EntityType int
const (
	TypeUser EntityType
	TypeChat
	TypeChannel
)

func (EntityType) GetInt

func (e EntityType) GetInt() int

type GormDB

type GormDB interface {
	DB() *gorm.DB
}

GormDB is an optional interface that GORM-based backends can implement to expose the underlying *gorm.DB for backward compatibility.

type Peer

type Peer struct {
	ID         int64 `gorm:"primary_key"`
	AccessHash int64
	Type       int
	Username   string `gorm:"index"`
	Language   string
}

func (*Peer) GetID

func (p *Peer) GetID() int64

type PeerStorage

type PeerStorage struct {
	SqlSession *gorm.DB // Non-nil only for GORM backends (backward compat).
	// contains filtered or unexported fields
}

PeerStorage provides a two-tier cache system for Telegram peers (users, chats, channels). It maintains an in-memory hot cache backed by a pluggable Adapter for persistence.

func NewPeerStorage

func NewPeerStorage(dialector gorm.Dialector, inMemory bool) (*PeerStorage, error)

NewPeerStorage creates PeerStorage from a GORM dialector (backward-compatible API).

func NewPeerStorageWithAdapter

func NewPeerStorageWithAdapter(db Adapter, inMemory bool) (*PeerStorage, error)

NewPeerStorageWithAdapter creates PeerStorage with a pluggable Adapter.

func (*PeerStorage) AddPeer

func (p *PeerStorage) AddPeer(iD, accessHash int64, peerType EntityType, userName string)

func (*PeerStorage) Close

func (p *PeerStorage) Close()

Close closes the write channel and waits for the writer goroutine to drain. Safe to call on in-memory storage (no-op).

func (*PeerStorage) DeleteConversationState

func (p *PeerStorage) DeleteConversationState(key string) error

func (*PeerStorage) GetAdapter

func (p *PeerStorage) GetAdapter() Adapter

func (*PeerStorage) GetInputPeerByID

func (p *PeerStorage) GetInputPeerByID(iD int64) tg.InputPeerClass

GetInputPeerByID finds the provided id in the peer storage and return its tg.InputPeerClass if found.

func (*PeerStorage) GetInputPeerByUsername

func (p *PeerStorage) GetInputPeerByUsername(userName string) tg.InputPeerClass

GetInputPeerByUsername finds the provided username in the peer storage and return its tg.InputPeerClass if found.

func (*PeerStorage) GetPeerByID

func (p *PeerStorage) GetPeerByID(peerID int64) *Peer

GetPeerByID finds the provided id in the peer storage and return it if found.

func (*PeerStorage) GetPeerByUsername

func (p *PeerStorage) GetPeerByUsername(username string) *Peer

GetPeerByUsername finds the provided username in the peer storage and return it if found. Returns nil if the peer is not found.

func (*PeerStorage) GetSession

func (p *PeerStorage) GetSession() *Session

func (*PeerStorage) ListConversationStates

func (p *PeerStorage) ListConversationStates() ([]ConvState, error)

func (*PeerStorage) LoadConversationState

func (p *PeerStorage) LoadConversationState(key string) (*ConvState, error)

func (*PeerStorage) SaveConversationState

func (p *PeerStorage) SaveConversationState(state *ConvState) error

func (*PeerStorage) SetPeerLanguage

func (p *PeerStorage) SetPeerLanguage(userID int64, lang string)

func (*PeerStorage) UpdateSession

func (p *PeerStorage) UpdateSession(session *Session)

type Session

type Session struct {
	Version int `gorm:"primary_key"`
	Data    []byte
}

Directories

Path Synopsis
backend
gormdb
Package gormdb provides a GORM-based Backend implementation.
Package gormdb provides a GORM-based Backend implementation.
memory
Package memory provides an in-memory Backend implementation.
Package memory provides an in-memory Backend implementation.
redisdb
Package redisdb provides a Redis-based Backend implementation.
Package redisdb provides a Redis-based Backend implementation.

Jump to

Keyboard shortcuts

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