storage

package
v1.0.0-beta25 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	DefaultUsername   = ""
	DefaultAccessHash = 0
	DefaultPhone      = ""
)
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

func ExtractChatPhotoID

func ExtractChatPhotoID(photo tg.ChatPhotoClass) int64

ExtractChatPhotoID extracts the photo_id from a ChatPhotoClass. Returns 0 if the photo is nil or not a *tg.ChatPhoto.

func ExtractPhotoID

func ExtractPhotoID(photo tg.UserProfilePhotoClass) int64

ExtractPhotoID extracts the photo_id from a UserProfilePhotoClass. Returns 0 if the photo is nil or not a *tg.UserProfilePhoto.

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)
	GetPeerByPhoneNumber(phone string) (*Peer, error)

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

	AutoMigrate() error
	Close() error

	// DeleteStalePeers removes peers whose last_updated is older than the
	// given unix timestamp. Returns the number of deleted peers.
	DeleteStalePeers(olderThan int64) (int64, error)
}

type ConvState

type ConvState struct {
	Key       string
	ChatID    int64
	UserID    int64
	Step      string
	Payload   []byte
	ExpiresAt time.Time
	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 Peer

type Peer struct {
	ID          int64
	AccessHash  int64
	Type        int
	Username    string
	Usernames   Usernames
	PhoneNumber string
	IsBot       bool
	PhotoID     int64
	Language    string
	LastUpdated int64
}

func (*Peer) GetID

func (p *Peer) GetID() int64

type PeerStorage

type PeerStorage struct {
	// 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 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) AddPeerWithUsernames

func (p *PeerStorage) AddPeerWithUsernames(iD, accessHash int64, peerType EntityType, userName string, usernames Usernames, phoneNumber string, isBot bool, photoID int64)

AddPeerWithUsernames adds a peer with its primary username and all usernames (active + collectible).

func (*PeerStorage) Close

func (p *PeerStorage) Close()

Close closes the write channel, waits for the writer and pruner goroutines to drain, and closes the adapter to release database connections. Safe to call multiple times (idempotent).

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

func (p *PeerStorage) GetPeerByPhoneNumber(phone string) *Peer

GetPeerByPhoneNumber finds a peer by phone number. Uses phone index for O(1) lookup. On cache+DB miss, falls back to contacts.ResolvePhone RPC if a resolver is set. Returns nil if the peer is not 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. Strips leading @ and normalizes to lowercase before lookup. Uses username index for O(1) lookup; stale usernames are automatically cleaned up. On cache+DB miss, falls back to contacts.ResolveUsername RPC if a resolver is set. 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) SetPhoneResolver

func (p *PeerStorage) SetPhoneResolver(fn func(ctx context.Context, phone string) ([]tg.UserClass, []tg.ChatClass, error))

SetPhoneResolver injects a callback that resolves a phone number via the Telegram API (contacts.ResolvePhone RPC). Called automatically by GetPeerByPhoneNumber on cache miss.

func (*PeerStorage) SetResolver

func (p *PeerStorage) SetResolver(fn func(ctx context.Context, username string) ([]tg.UserClass, []tg.ChatClass, error))

SetResolver injects a callback that resolves a username via the Telegram API (contacts.ResolveUsername RPC). Called automatically by GetPeerByUsername on cache miss.

func (*PeerStorage) UpdateSession

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

type Session

type Session struct {
	Version int
	Data    []byte
}

Session stores the MTProto session data (auth key and related state). The Data field contains the raw 256-byte auth key. Anyone with database access can use this key to impersonate the Telegram session. Consider encrypting Data at the application layer before persisting.

type Username

type Username struct {
	Username string
	Active   bool
	Editable bool
}

Username stores a Telegram username with its metadata (active, editable). Mirrors tg.Username but is JSON-safe for persistent storage adapters.

func (Username) MarshalJSON

func (u Username) MarshalJSON() ([]byte, error)

MarshalJSON serializes Username as an object.

func (*Username) UnmarshalJSON

func (u *Username) UnmarshalJSON(data []byte) error

UnmarshalJSON deserializes Username from either:

  • new format: {"Username":"alice","Active":true,"Editable":true}
  • legacy format: "alice" (plain string from old gotg versions)

type Usernames

type Usernames []Username

Usernames is a named slice of Username, enabling database serialization.

func ConvertUsernames

func ConvertUsernames(usernames []tg.Username) Usernames

ConvertUsernames converts []tg.Username to Usernames with lowercase normalization, preserving Active and Editable metadata.

func (*Usernames) Scan

func (u *Usernames) Scan(val interface{}) error

Scan implements sql.Scanner for database retrieval (JSON deserialization).

func (Usernames) Value

func (u Usernames) Value() (driver.Value, error)

Value implements driver.Valuer for database storage (JSON serialization).

Directories

Path Synopsis
Package memory provides an in-memory Backend implementation.
Package memory provides an in-memory Backend implementation.

Jump to

Keyboard shortcuts

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