updates

package
v0.66.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2022 License: MIT Imports: 10 Imported by: 30

Documentation

Overview

Package updates provides a Telegram's state synchronization manager.

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

Limitations:

  1. Manager 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, manager cannot guarantee the correctness of these operations. We rely on the server here.

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

TODO: Write implementation details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChannelAccessHasher added in v0.51.0

type ChannelAccessHasher interface {
	SetChannelAccessHash(userID, channelID, accessHash int64) error
	GetChannelAccessHash(userID, channelID int64) (accessHash int64, found bool, err error)
}

ChannelAccessHasher stores users channel access hashes.

type Config

type Config struct {
	// Handler where updates will be passed.
	Handler telegram.UpdateHandler
	// Callback called if manager cannot
	// recover channel gap (optional).
	OnChannelTooLong func(channelID int64)
	// State storage.
	// In-mem used if not provided.
	Storage StateStorage
	// Channel access hash storage.
	// In-mem used if not provided.
	AccessHasher ChannelAccessHasher
	// Logger (optional).
	Logger *zap.Logger
}

Config of the manager.

type Manager added in v0.51.0

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

Manager deals with gaps.

Important: Updates produced by this manager may contain negative Pts/Qts/Seq values in tg.UpdateClass/tg.UpdatesClass (does not affects to the tg.MessageClass).

This is because telegram server does not return these sequences for getDifference/getChannelDifference results. You SHOULD NOT use them in update handlers at all.

func New

func New(cfg Config) *Manager

New creates new manager.

func (*Manager) Auth added in v0.51.0

func (m *Manager) Auth(ctx context.Context, client RawClient, userID int64, isBot, forget bool) error

Auth notifies manager about user authentication on the telegram server.

If forget is true, local state (if exist) will be overwritten with remote state.

func (*Manager) Handle added in v0.51.0

func (m *Manager) Handle(ctx context.Context, u tg.UpdatesClass) error

Handle handles updates.

Important: If Auth method not called, all updates will be passed to the provided handler as-is without any order verification or short updates transformation.

func (*Manager) Logout added in v0.51.0

func (m *Manager) Logout() error

Logout notifies manager about user logout.

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 manager for state synchronization.

type State

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

State is the user state.

type StateStorage added in v0.51.0

type StateStorage interface {
	GetState(userID int64) (state State, found bool, err error)
	SetState(userID int64, state State) error
	SetPts(userID int64, pts int) error
	SetQts(userID int64, qts int) error
	SetDate(userID int64, date int) error
	SetSeq(userID int64, seq int) error
	SetDateSeq(userID int64, date, seq int) error
	GetChannelPts(userID, channelID int64) (pts int, found bool, err error)
	SetChannelPts(userID, channelID int64, pts int) error
	ForEachChannels(userID int64, f func(channelID int64, pts int) error) error
}

StateStorage is the users state storage.

Note: SetPts, SetQts, SetDate, SetSeq, SetDateSeq should return error if user state does not exist.

Directories

Path Synopsis
Package hook contains telegram update hook middleware.
Package hook contains telegram update hook middleware.
internal
e2e
Package e2e contains end-to-end updates processing test.
Package e2e contains end-to-end updates processing test.

Jump to

Keyboard shortcuts

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