peer

package
v0.0.0-...-87e9d67 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package peer conatains some peer resolving and extracting helpers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToInputChannel

func ToInputChannel(channel tg.InputPeerClass) (tg.InputChannelClass, bool)

ToInputChannel converts given peer to input channel if possible.

func ToInputUser

func ToInputUser(user tg.InputPeerClass) (tg.InputUserClass, bool)

ToInputUser converts given peer to input user if possible.

Types

type ConstraintError

type ConstraintError struct {
	Expected string
	Got      tg.InputPeerClass
}

ConstraintError is a peer resolve constraint error.

func (*ConstraintError) Error

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

func EntitiesFromUpdate(uctx tg.Entities) Entities

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

func (ent Entities) Channel(id int64) (*tg.Channel, bool)

Channel finds channel by given ID.

func (Entities) Channels

func (ent Entities) Channels() map[int64]*tg.Channel

Channels returns map of channels. Notice that returned map is not a copy.

func (Entities) Chat

func (ent Entities) Chat(id int64) (*tg.Chat, bool)

Chat finds chat by given ID.

func (Entities) Chats

func (ent Entities) Chats() map[int64]*tg.Chat

Chats returns map of chats. Notice that returned map is not a copy.

func (Entities) ExtractPeer

func (ent Entities) ExtractPeer(peerID tg.PeerClass) (tg.InputPeerClass, error)

ExtractPeer finds and creates InputPeerClass using given PeerClass.

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.

func (Entities) FillFromResult

func (ent Entities) FillFromResult(r EntitySearchResult)

FillFromResult adds and updates all entities from given result.

func (Entities) FillFromUpdate

func (ent Entities) FillFromUpdate(uctx tg.Entities)

FillFromUpdate adds and updates all entities from given updates.

func (Entities) User

func (ent Entities) User(id int64) (*tg.User, bool)

User finds user by given ID.

func (Entities) Users

func (ent Entities) Users() map[int64]*tg.User

Users returns map of users. Notice that returned map is not a copy.

type EntitySearchResult

type EntitySearchResult interface {
	MapChats() tg.ChatClassArray
	MapUsers() tg.UserClassArray
}

EntitySearchResult is abstraction for different RPC responses which contains entities.

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"

	"bitbucket.org/hokego/hokego-td/telegram"
	"bitbucket.org/hokego/hokego-td/telegram/message"
	"bitbucket.org/hokego/hokego-td/telegram/message/peer"
	"bitbucket.org/hokego/hokego-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

func (l *LRUResolver) Evict(key string) (tg.InputPeerClass, bool)

Evict deletes record from cache.

func (*LRUResolver) ResolveDomain

func (l *LRUResolver) ResolveDomain(ctx context.Context, domain string) (tg.InputPeerClass, error)

ResolveDomain implements Resolver.

func (*LRUResolver) ResolvePhone

func (l *LRUResolver) ResolvePhone(ctx context.Context, phone string) (tg.InputPeerClass, error)

ResolvePhone implements Resolver.

func (*LRUResolver) WithClock

func (l *LRUResolver) WithClock(c clock.Clock) *LRUResolver

WithClock sets clock to use when counting expiration.

func (*LRUResolver) WithExpiration

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

type Promise func(ctx context.Context) (tg.InputPeerClass, error)

Promise is a peer promise.

func OnlyChannel

func OnlyChannel(p Promise) Promise

OnlyChannel returns Promise which returns error if resolved peer is not a channel.

func OnlyChat

func OnlyChat(p Promise) Promise

OnlyChat returns Promise which returns error if resolved peer is not a chat.

func OnlyUser

func OnlyUser(p Promise) Promise

OnlyUser returns Promise which returns error if resolved peer is not a user.

func OnlyUserID

func OnlyUserID(p Promise) Promise

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 Resolve

func Resolve(r Resolver, from string) Promise

Resolve uses given string to create new peer promise. It resolves peer of message using given Resolver. 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 ResolveDeeplink(r Resolver, u string) Promise

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

func ResolveDomain(r Resolver, domain string) Promise

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

func ResolvePhone(r Resolver, phone string) Promise

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

type PromiseDecorator = func(Promise) Promise

PromiseDecorator is a decorator of peer 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

func DefaultResolver(raw *tg.Client) Resolver

DefaultResolver creates and returns default resolver.

func Plain

func Plain(raw *tg.Client) Resolver

Plain creates plain resolver.

func SingleflightResolver

func SingleflightResolver(next Resolver) Resolver

SingleflightResolver is a simple resolver decorator which prevents duplicate resolve calls.

Jump to

Keyboard shortcuts

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