Skip to content

Commit

Permalink
Merge pull request #672 from tdakkota/fix/participants-iterator
Browse files Browse the repository at this point in the history
fix(participants): checking of the last batch
  • Loading branch information
ernado committed Jan 20, 2022
2 parents 974834f + d629f73 commit 5209c86
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
67 changes: 67 additions & 0 deletions telegram/peers/manager_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package peers_test

import (
"context"
"fmt"

"go.uber.org/zap"

"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/telegram/updates"
"github.com/gotd/td/tg"
)

func ExampleManager() {
logger := zap.NewExample()

var (
dispatcher = tg.NewUpdateDispatcher()
h telegram.UpdateHandler
)
client, err := telegram.ClientFromEnvironment(telegram.Options{
Logger: logger.Named("client"),
UpdateHandler: telegram.UpdateHandlerFunc(func(ctx context.Context, u tg.UpdatesClass) error {
return h.Handle(ctx, u)
}),
})
if err != nil {
panic(err)
}
peerManager := peers.Options{
Logger: logger,
}.Build(client.API())
gaps := updates.New(updates.Config{
Handler: dispatcher,
AccessHasher: peerManager,
Logger: logger.Named("gaps"),
})
h = peerManager.UpdateHook(gaps)

if err := client.Run(context.TODO(), func(ctx context.Context) error {
if err := peerManager.Init(ctx); err != nil {
return err
}
u, err := peerManager.Self(ctx)
if err != nil {
return err
}

_, isBot := u.ToBot()
if err := gaps.Auth(ctx, client.API(), u.ID(), isBot, false); err != nil {
return err
}
defer gaps.Logout()

p, err := peerManager.Resolve(ctx, "durov")
if err != nil {
return err
}

username, _ := p.Username()
fmt.Println(username)
return nil
}); err != nil {
panic(err)
}
}
5 changes: 3 additions & 2 deletions telegram/query/channels/participants/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (m *Iterator) apply(r tg.ChannelsChannelParticipantsClass) error {
entities = peer.NewEntities(prts.MapUsers().UserToMap(), map[int64]*tg.Chat{}, map[int64]*tg.Channel{})

m.count = prts.Count
m.lastBatch = len(participants) < m.limit
m.lastBatch = len(participants) < 1
default: // channels.channelParticipantsNotModified#f0173fe9
return errors.Errorf("unexpected type %T", r)
}
Expand Down Expand Up @@ -135,7 +135,8 @@ func (m *Iterator) FetchTotal(ctx context.Context) (int, error) {
}

// Next prepares the next message for reading with the Value method.
// It returns true on success, or false if there is no next message or an error happened while preparing it.
//
// Returns true on success, or false if there is no next message or an error happened while preparing it.
// Err should be consulted to distinguish between the two cases.
func (m *Iterator) Next(ctx context.Context) bool {
if m.lastErr != nil {
Expand Down

0 comments on commit 5209c86

Please sign in to comment.