updates

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2021 License: MIT Imports: 11 Imported by: 0

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 says that we do not have a local state.

Functions

This section is empty.

Types

type Config

type Config struct {
	RawClient RawClient
	Handler   Handler
	SelfID    int
	IsBot     bool

	// Storage is the place whether the state is stored.
	// In-mem by default.
	Storage Storage
	// Number of workers which handles updates.
	// Min is 5.
	Concurrency int
	Forget      bool
	Logger      *zap.Logger
}

Config is an engine config.

type DiffUpdate

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

	Pending     []tg.UpdateClass
	PendingEnts *Entities
}

DiffUpdate contains updates received by getDifference.

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

func (e *Engine) Handle(u tg.UpdatesClass)

Handle update.

func (*Engine) Run

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

Run starts update handling.

type Entities

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

Entities contains Telegram's entities which comes with updates.

func (*Entities) AsChats

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

AsChats returns chat entities as tg.ChatClass slice.

func (*Entities) AsUsers

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

AsUsers returns user entities as tg.UserClass slice.

type Handler

type Handler interface {
	HandleDiff(DiffUpdate) error
	HandleUpdates(Updates) error
	ChannelTooLong(channelID int)
}

Handler interface.

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 interface contains Telegram RPC calls 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
	SetDateSeq(date, seq int) error

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

Storage interface.

type Updates

type Updates struct {
	Updates []tg.UpdateClass
	Ents    *Entities
}

Updates contains Telegram updates.

Directories

Path Synopsis
internal
sequence
Package sequence contains sequence box implementation.
Package sequence contains sequence box implementation.

Jump to

Keyboard shortcuts

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