updates

package
v0.48.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2021 License: MIT Imports: 11 Imported by: 29

Documentation

Overview

Package updates provides a Telegram's state synchronization engine.

It guarantees that all state-sensitive updates will be performed in correct order.

Limitations:

  1. Engine cannot verify stateless types of updates (tg.UpdatesClass without Seq, or tg.UpdateClass without Pts or Qts).

  2. Due to the fact that updates.getDifference and updates.getChannelDifference do not return event sequences, the engine cannot guarantee the correctness of these operations. We rely on the server here.

  3. Engine cannot recover the channel gap if there is a ChannelDifferenceTooLong error. Restoring the state in such situation is not the prerogative of this engine. See: https://core.telegram.org/constructor/updates.channelDifferenceTooLong

TODO: Write implementation details.

Index

Constants

This section is empty.

Variables

View Source
var ErrStateNotFound = xerrors.Errorf("state not found")

ErrStateNotFound means that state is not found in storage.

Functions

This section is empty.

Types

type AccessHasher

type AccessHasher interface {
	GetChannelHash(channelID int) (hash int64, found bool, err error)
	SetChannelHash(channelID int, accessHash int64) error
}

AccessHasher stores channel access hashes.

type Config

type Config struct {
	RawClient    RawClient
	Handler      Handler
	Storage      Storage
	AccessHasher AccessHasher
	SelfID       int
	IsBot        bool
	Forget       bool
	Logger       *zap.Logger
}

Config of the engine.

type DiffUpdate

type DiffUpdate struct {
	NewMessages          []tg.MessageClass
	NewEncryptedMessages []tg.EncryptedMessageClass
	Users                []tg.UserClass
	Chats                []tg.ChatClass
}

DiffUpdate struct.

type Engine

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

Engine deals with gaps.

func New

func New(cfg Config) *Engine

New creates new engine.

func (*Engine) HandleUpdates

func (e *Engine) HandleUpdates(u tg.UpdatesClass) error

HandleUpdates handles updates.

func (*Engine) Run

func (e *Engine) Run(ctx context.Context) error

Run engine.

type Entities

type Entities struct {
	Users             map[int]*tg.User
	Chats             map[int]*tg.Chat
	Channels          map[int]*tg.Channel
	ChannelsForbidden map[int]*tg.ChannelForbidden
}

Entities contains update entities.

func NewEntities

func NewEntities() *Entities

NewEntities creates new Entities.

func (*Entities) AsChats

func (e *Entities) AsChats() []tg.ChatClass

AsChats returns chats as tg.ChatClass slice.

func (*Entities) AsUsers

func (e *Entities) AsUsers() []tg.UserClass

AsUsers returns users as tg.UserClass slice.

func (*Entities) Merge

func (e *Entities) Merge(from *Entities)

Merge merges entities.

type Handler

type Handler interface {
	HandleDiff(DiffUpdate) error
	HandleUpdates(*Entities, []tg.UpdateClass) error
	ChannelTooLong(channelID int)
}

Handler interface.

type MemStorage

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

MemStorage is a in-memory sequence storage.

func NewMemStorage

func NewMemStorage() *MemStorage

NewMemStorage creates new MemStorage.

func (*MemStorage) Channels

func (s *MemStorage) Channels(f func(channelID, pts int)) error

Channels iterates through channels.

func (*MemStorage) ForgetAll

func (s *MemStorage) ForgetAll() error

ForgetAll clears all sequence info.

func (*MemStorage) GetState

func (s *MemStorage) GetState() (State, error)

GetState returns the state.

func (*MemStorage) SetChannelPts

func (s *MemStorage) SetChannelPts(channelID, pts int) error

SetChannelPts sets channel pts.

func (*MemStorage) SetDate

func (s *MemStorage) SetDate(date int) error

SetDate sets date.

func (*MemStorage) SetPts

func (s *MemStorage) SetPts(pts int) error

SetPts sets pts.

func (*MemStorage) SetQts

func (s *MemStorage) SetQts(qts int) error

SetQts sets qts.

func (*MemStorage) SetSeq

func (s *MemStorage) SetSeq(seq int) error

SetSeq sets seq.

func (*MemStorage) SetState

func (s *MemStorage) SetState(state State) error

SetState sets the state.

type RawClient

type RawClient interface {
	UpdatesGetState(ctx context.Context) (*tg.UpdatesState, error)
	UpdatesGetDifference(ctx context.Context, request *tg.UpdatesGetDifferenceRequest) (tg.UpdatesDifferenceClass, error)
	UpdatesGetChannelDifference(ctx context.Context, request *tg.UpdatesGetChannelDifferenceRequest) (tg.UpdatesChannelDifferenceClass, error)
}

RawClient is the interface which contains Telegram RPC methods used by the engine for state synchronization.

type State

type State struct {
	Pts  int
	Qts  int
	Date int
	Seq  int
}

State contains common sequence state.

type Storage

type Storage interface {
	GetState() (State, error)
	SetState(s State) error
	SetPts(pts int) error
	SetQts(qts int) error
	SetSeq(seq int) error
	SetDate(date int) error

	SetChannelPts(channelID, pts int) error
	Channels(iter func(channelID, pts int)) error

	ForgetAll() error
}

Storage interface.

Directories

Path Synopsis
internal
e2e
Package e2e contains updates.Engine tests.
Package e2e contains updates.Engine tests.

Jump to

Keyboard shortcuts

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