Documentation
¶
Overview ¶
Package peer conatains some peer resolving and extracting helpers.
Index ¶
- Variables
- func MergeMinChannel(full, min *tg.Channel) *tg.Channel
- func MergeMinUser(full, min *tg.User) *tg.User
- func ResolveInputPeer(ctx context.Context, r Resolver, input InputPeer) (tg.InputPeerClass, error)
- func ToInputChannel(channel tg.InputPeerClass) (tg.InputChannelClass, bool)
- func ToInputUser(user tg.InputPeerClass) (tg.InputUserClass, bool)
- type ConstraintError
- type Entities
- func (ent Entities) Channel(id int64) (*tg.Channel, bool)
- func (ent Entities) Channels() map[int64]*tg.Channel
- func (ent Entities) Chat(id int64) (*tg.Chat, bool)
- func (ent Entities) Chats() map[int64]*tg.Chat
- func (ent Entities) ExtractPeer(peerID tg.PeerClass) (tg.InputPeerClass, error)
- func (ent Entities) ExtractPeerFromMessage(peerID tg.PeerClass, msgPeer tg.InputPeerClass, msgID int) (tg.InputPeerClass, error)
- func (ent Entities) Fill(users map[int64]*tg.User, chats map[int64]*tg.Chat, ...)
- func (ent Entities) FillFromResult(r EntitySearchResult)
- func (ent Entities) FillFromUpdate(uctx tg.Entities)
- func (ent Entities) OffsetPeer(peerID tg.PeerClass) tg.InputPeerClass
- func (ent Entities) User(id int64) (*tg.User, bool)
- func (ent Entities) Users() map[int64]*tg.User
- type EntitySearchResult
- type InputPeer
- type LRUResolver
- func (l *LRUResolver) Evict(key string) (tg.InputPeerClass, bool)
- func (l *LRUResolver) ResolveDomain(ctx context.Context, domain string) (tg.InputPeerClass, error)
- func (l *LRUResolver) ResolvePhone(ctx context.Context, phone string) (tg.InputPeerClass, error)
- func (l *LRUResolver) WithClock(c clock.Clock) *LRUResolver
- func (l *LRUResolver) WithExpiration(expiration time.Duration) *LRUResolver
- type Promise
- type PromiseDecorator
- type Resolved
- type Resolver
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrMinPeer = errors.New("min peer has no usable access hash")
ErrMinPeer means that peer is known only as min constructor, which has no usable access hash, so input peer can be built only from a message that mentions it, see Entities.ExtractPeerFromMessage and https://core.telegram.org/api/min.
Functions ¶
func MergeMinChannel ¶
MergeMinChannel returns a copy of full channel updated with fields of min channel that min constructors carry reliably, following TDLib (ChatManager::on_get_channel).
Access hash, participant status (creator, left, admin and banned rights), date, signatures, participant count and colors are kept from full, and the photo too if the current user is banned from the channel.
func MergeMinUser ¶
MergeMinUser returns a copy of full user updated with fields of min user that min constructors carry reliably, following TDLib (UserManager::on_get_user).
Access hash, name, usernames, phone and contact state are kept from full. Photo is taken from min only if full has ApplyMinPhoto set, and status only if full has none. A deleted min copy of self is ignored, as in TDLib.
func ResolveInputPeer ¶ added in v0.157.0
ResolveInputPeer resolves input to a concrete tg.InputPeerClass.
Concrete tg.InputPeerClass values are returned as-is; lazy references created by Resolve are resolved using r.
func ToInputChannel ¶ added in v0.33.0
func ToInputChannel(channel tg.InputPeerClass) (tg.InputChannelClass, bool)
ToInputChannel converts given peer to input channel if possible.
func ToInputUser ¶ added in v0.33.0
func ToInputUser(user tg.InputPeerClass) (tg.InputUserClass, bool)
ToInputUser converts given peer to input user if possible.
Types ¶
type ConstraintError ¶ added in v0.35.0
type ConstraintError struct {
Expected string
Got tg.InputPeerClass
}
ConstraintError is a peer resolve constraint error.
func (*ConstraintError) Error ¶ added in v0.35.0
func (c *ConstraintError) Error() string
Error implements error.
type Entities ¶
type Entities struct {
// contains filtered or unexported fields
}
Entities is simple peer entities storage.
func EntitiesFromResult ¶
func EntitiesFromResult(r EntitySearchResult) Entities
EntitiesFromResult fills Entities struct using given context.
func EntitiesFromUpdate ¶
EntitiesFromUpdate fills Entities struct using given context.
func NewEntities ¶
func NewEntities( users map[int64]*tg.User, chats map[int64]*tg.Chat, channels map[int64]*tg.Channel, ) Entities
NewEntities creates new Entities struct.
func (Entities) Channels ¶ added in v0.38.0
Channels returns map of channels. Notice that returned map is not a copy.
func (Entities) Chats ¶ added in v0.38.0
Chats returns map of chats. Notice that returned map is not a copy.
func (Entities) ExtractPeer ¶
ExtractPeer finds and creates InputPeerClass using given PeerClass.
It returns an error wrapping ErrMinPeer if the user or channel is known only as a min constructor, as its access hash is not usable for requests. Up to v0.163.0 it returned an input peer with the min access hash; use ExtractPeerFromMessage to get a usable input peer for min peers.
func (Entities) ExtractPeerFromMessage ¶
func (ent Entities) ExtractPeerFromMessage( peerID tg.PeerClass, msgPeer tg.InputPeerClass, msgID int, ) (tg.InputPeerClass, error)
ExtractPeerFromMessage is like ExtractPeer, but for min users and channels returns inputPeerUserFromMessage or inputPeerChannelFromMessage, built from message msgID in chat msgPeer that mentions the peer (e.g. its author).
func (Entities) Fill ¶
func (ent Entities) Fill( users map[int64]*tg.User, chats map[int64]*tg.Chat, channels map[int64]*tg.Channel, )
Fill adds and updates all entities from given maps.
Min users and channels (see https://core.telegram.org/api/min) do not replace full ones, as they have no usable access hash. Instead, the fields a min constructor is known to carry are merged into the full one, like TDLib does, see MergeMinUser and MergeMinChannel.
func (Entities) FillFromResult ¶
func (ent Entities) FillFromResult(r EntitySearchResult)
FillFromResult adds and updates all entities from given result.
Min users and channels do not replace full ones.
func (Entities) FillFromUpdate ¶
FillFromUpdate adds and updates all entities from given updates.
func (Entities) OffsetPeer ¶
func (ent Entities) OffsetPeer(peerID tg.PeerClass) tg.InputPeerClass
OffsetPeer returns input peer to use as offset_peer of paginated requests (messages.searchGlobal, messages.getDialogs), never failing.
Like ExtractPeer, but like TDLib (get_input_peer_force) it is built even for min users and channels, as the server uses offset_peer only to order results, and for unknown peers, with zero access hash. Do not use it for other requests: min and zero access hashes are rejected there.
type EntitySearchResult ¶
type EntitySearchResult interface {
MapChats() tg.ChatClassArray
MapUsers() tg.UserClassArray
}
EntitySearchResult is abstraction for different RPC responses which contains entities.
type InputPeer ¶ added in v0.157.0
type InputPeer interface {
// Zero reports whether the value has a zero value.
Zero() bool
// String implements fmt.Stringer.
String() string
}
InputPeer is a peer input accepted by helpers that need a peer.
It is implemented both by concrete tg.InputPeerClass values and by lazy references created with Resolve, allowing peers to be passed by username/phone/deeplink and resolved lazily.
type LRUResolver ¶
type LRUResolver struct {
// contains filtered or unexported fields
}
LRUResolver is simple decorator for Resolver to cache result in LRU.
Example (Cache) ¶
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/message"
"github.com/gotd/td/telegram/message/peer"
"github.com/gotd/td/tg"
)
func resolveLRU(ctx context.Context) error {
client, err := telegram.ClientFromEnvironment(telegram.Options{})
if err != nil {
return err
}
return client.Run(ctx, func(ctx context.Context) error {
raw := tg.NewClient(client)
resolver := peer.NewLRUResolver(peer.Plain(raw), 16).WithExpiration(time.Minute)
sender := message.NewSender(raw).WithResolver(resolver)
// "durovschat" will be resolved by Plain resolver.
if _, err := sender.Resolve("@durovschat").Dice(ctx); err != nil {
return err
}
// "durovschat" will be resolved by cache.
if _, err := sender.Resolve("https://t.me/durovschat").Darts(ctx); err != nil {
return err
}
// Evict and delete record.
resolver.Evict("durovschat")
return nil
})
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
if err := resolveLRU(ctx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(2)
}
}
Output:
func NewLRUResolver ¶
func NewLRUResolver(next Resolver, capacity int) *LRUResolver
NewLRUResolver creates new LRUResolver.
func (*LRUResolver) Evict ¶ added in v0.36.0
func (l *LRUResolver) Evict(key string) (tg.InputPeerClass, bool)
Evict deletes record from cache.
func (*LRUResolver) ResolveDomain ¶ added in v0.31.0
func (l *LRUResolver) ResolveDomain(ctx context.Context, domain string) (tg.InputPeerClass, error)
ResolveDomain implements Resolver.
func (*LRUResolver) ResolvePhone ¶ added in v0.30.0
func (l *LRUResolver) ResolvePhone(ctx context.Context, phone string) (tg.InputPeerClass, error)
ResolvePhone implements Resolver.
func (*LRUResolver) WithClock ¶ added in v0.36.0
func (l *LRUResolver) WithClock(c clock.Clock) *LRUResolver
WithClock sets clock to use when counting expiration.
func (*LRUResolver) WithExpiration ¶ added in v0.36.0
func (l *LRUResolver) WithExpiration(expiration time.Duration) *LRUResolver
WithExpiration sets expiration timeout for records in cache. If zero, expiration will be disabled. Default value is a minute.
type Promise ¶ added in v0.27.0
type Promise func(ctx context.Context) (tg.InputPeerClass, error)
Promise is a peer promise.
func OnlyChannel ¶ added in v0.30.0
OnlyChannel returns Promise which returns error if resolved peer is not a channel.
func OnlyChat ¶ added in v0.30.0
OnlyChat returns Promise which returns error if resolved peer is not a chat.
func OnlyUser ¶ added in v0.30.0
OnlyUser returns Promise which returns error if resolved peer is not a user.
func OnlyUserID ¶ added in v0.35.0
OnlyUserID returns Promise which returns error if resolved peer is not a user object with ID. Unlike OnlyUser, it returns error if resolved peer is tg.InputPeerSelf.
func ResolveDeeplink ¶ added in v0.27.0
ResolveDeeplink uses given deeplink to create new peer promise. Deeplink is a URL like https://t.me/telegram. It resolves peer of message using given Resolver. Input examples:
t.me/telegram https://t.me/telegram tg:resolve?domain=telegram tg://resolve?domain=telegram
func ResolveDomain ¶ added in v0.27.0
ResolveDomain uses given domain to create new peer promise. It resolves peer of message using given Resolver. Can has prefix with @ or not. Input examples:
@telegram telegram
func ResolvePhone ¶ added in v0.30.0
ResolvePhone uses given phone to create new peer promise. It resolves peer of message using given Resolver. Input example:
+13115552368 +1 (311) 555-0123 +1 311 555-6162 13115556162
Note that Telegram represents phone numbers according to the E.164 standard without the plus sign (”+”) prefix. The resolver therefore takes an easy route and just deletes any non-digit symbols from phone number string.
type PromiseDecorator ¶ added in v0.30.0
PromiseDecorator is a decorator of peer promise.
type Resolved ¶ added in v0.157.0
type Resolved struct {
// contains filtered or unexported fields
}
Resolved is a lazily-resolved peer reference produced by Resolve.
It implements InputPeer, so it can be passed directly to helpers that accept a peer. The actual resolution is deferred until the helper runs, using the resolver provided by that helper.
func Resolve ¶ added in v0.27.0
Resolve creates a lazy peer reference from the given text.
It auto-detects domain, phone and deeplink forms. The reference is resolved lazily, using the resolver of the helper it is passed to. Input examples:
@telegram telegram t.me/telegram https://t.me/telegram tg:resolve?domain=telegram tg://resolve?domain=telegram +13115552368 +1 (311) 555-0123 +1 311 555-6162 13115556162
func (*Resolved) Bind ¶ added in v0.157.0
Bind binds the given resolver to the reference, producing a Promise.
type Resolver ¶
type Resolver interface {
ResolveDomain(ctx context.Context, domain string) (tg.InputPeerClass, error)
ResolvePhone(ctx context.Context, phone string) (tg.InputPeerClass, error)
}
Resolver is an abstraction to resolve domains and Telegram deeplinks.
func DefaultResolver ¶
DefaultResolver creates and returns default resolver.
func SingleflightResolver ¶ added in v0.36.0
SingleflightResolver is a simple resolver decorator which prevents duplicate resolve calls.