Documentation
¶
Index ¶
- Constants
- func AddPeersFromDialogs(ctx context.Context, raw *tg.Client, peerStorage *PeerStorage) error
- func ConvKey(chatID, userID int64) string
- func ExtractChatPhotoID(photo tg.ChatPhotoClass) int64
- func ExtractPhotoID(photo tg.UserProfilePhotoClass) int64
- type Adapter
- type ConvState
- type EntityType
- type Peer
- type PeerStorage
- func (p *PeerStorage) AddPeer(iD, accessHash int64, peerType EntityType, userName string)
- func (p *PeerStorage) AddPeerWithUsernames(iD, accessHash int64, peerType EntityType, userName string, ...)
- func (p *PeerStorage) Close()
- func (p *PeerStorage) DeleteConversationState(key string) error
- func (p *PeerStorage) GetAdapter() Adapter
- func (p *PeerStorage) GetInputPeerByID(iD int64) tg.InputPeerClass
- func (p *PeerStorage) GetInputPeerByUsername(userName string) tg.InputPeerClass
- func (p *PeerStorage) GetPeerByID(peerID int64) *Peer
- func (p *PeerStorage) GetPeerByPhoneNumber(phone string) *Peer
- func (p *PeerStorage) GetPeerByUsername(username string) *Peer
- func (p *PeerStorage) GetSession() *Session
- func (p *PeerStorage) ListConversationStates() ([]ConvState, error)
- func (p *PeerStorage) LoadConversationState(key string) (*ConvState, error)
- func (p *PeerStorage) SaveConversationState(state *ConvState) error
- func (p *PeerStorage) SetPeerLanguage(userID int64, lang string)
- func (p *PeerStorage) SetPhoneResolver(...)
- func (p *PeerStorage) SetResolver(...)
- func (p *PeerStorage) UpdateSession(session *Session)
- type Session
- type Username
- type Usernames
Constants ¶
const ( DefaultUsername = "" DefaultAccessHash = 0 DefaultPhone = "" )
const LatestVersion = 1
Variables ¶
This section is empty.
Functions ¶
func AddPeersFromDialogs ¶
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 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 EntityType ¶
type EntityType int
const ( TypeUser EntityType TypeChat TypeChannel )
func (EntityType) GetInt ¶
func (e EntityType) GetInt() int
type Peer ¶
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 ¶
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 ¶
Username stores a Telegram username with its metadata (active, editable). Mirrors tg.Username but is JSON-safe for persistent storage adapters.
func (Username) MarshalJSON ¶
MarshalJSON serializes Username as an object.
func (*Username) UnmarshalJSON ¶
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 ¶
ConvertUsernames converts []tg.Username to Usernames with lowercase normalization, preserving Active and Editable metadata.