Skip to content

Commit

Permalink
Merge pull request #139 from gotd/feat/schema-123
Browse files Browse the repository at this point in the history
feat: update schema to 123
  • Loading branch information
ernado authored Feb 6, 2021
2 parents 16e7f7d + 1b192cf commit 2f105b7
Show file tree
Hide file tree
Showing 35 changed files with 2,637 additions and 546 deletions.
60 changes: 40 additions & 20 deletions _schema/telegram.tl

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion cmd/gotdchats/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"golang.org/x/net/proxy"
"golang.org/x/xerrors"

"github.com/gotd/td/mtproto"
"github.com/gotd/td/telegram"
"github.com/gotd/td/tg"
"github.com/gotd/td/transport"
Expand Down Expand Up @@ -67,7 +68,7 @@ func run(ctx context.Context) error {
for range time.NewTicker(time.Second * 5).C {
chats, err := c.MessagesGetAllChats(ctx, nil)

if d, ok := telegram.AsFloodWait(err); ok {
if d, ok := mtproto.AsFloodWait(err); ok {
// Server told us to wait N seconds before sending next message.
logger.Info("Sleeping", zap.Duration("duration", d))
time.Sleep(d)
Expand Down
6 changes: 2 additions & 4 deletions telegram/error.go → mtproto/flood_wait.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package telegram
package mtproto

import (
"time"

"github.com/gotd/td/mtproto"
)

// ErrFloodWait is error type of "FLOOD_WAIT" error.
Expand All @@ -15,7 +13,7 @@ const ErrFloodWait = "FLOOD_WAIT"
// Client should wait for that duration before issuing new requests with
// same method.
func AsFloodWait(err error) (d time.Duration, ok bool) {
if rpcErr, ok := mtproto.AsTypeErr(err, ErrFloodWait); ok {
if rpcErr, ok := AsTypeErr(err, ErrFloodWait); ok {
return time.Second * time.Duration(rpcErr.Argument), true
}
return 0, false
Expand Down
21 changes: 21 additions & 0 deletions mtproto/flood_wait_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mtproto

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"golang.org/x/xerrors"
)

func TestAsFloodWait(t *testing.T) {
err := func() error {
return xerrors.Errorf("failed to perform operation: %w",
NewError(400, "FLOOD_WAIT_10"),
)
}()

d, ok := AsFloodWait(err)
assert.True(t, ok)
assert.Equal(t, time.Second*10, d)
}
2 changes: 1 addition & 1 deletion telegram/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (c *conn) init(ctx context.Context) error {
LangCode: c.device.LangCode,
Query: c.wrapRequest(&tg.HelpGetConfigRequest{}),
})
var req bin.Object = c.wrapRequest(&tg.InvokeWithLayerRequest{
req := c.wrapRequest(&tg.InvokeWithLayerRequest{
Layer: tg.Layer,
Query: q,
})
Expand Down
14 changes: 0 additions & 14 deletions telegram/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package telegram

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"

Expand All @@ -23,15 +21,3 @@ func TestError(t *testing.T) {
}()
require.True(t, mtproto.IsErr(err, tg.ErrAccessTokenExpired))
}

func TestAsFloodWait(t *testing.T) {
err := func() error {
return xerrors.Errorf("failed to perform operation: %w",
mtproto.NewError(400, "FLOOD_WAIT_10"),
)
}()

d, ok := AsFloodWait(err)
assert.True(t, ok)
assert.Equal(t, time.Second*10, d)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import (
"context"
"time"

"golang.org/x/xerrors"

"github.com/gotd/td/mtproto"
)

// floodWait sleeps required duration and true if err is FLOOD_WAIT
// or false and context or original error otherwise.
func floodWait(ctx context.Context, err error) (bool, error) {
var rpcErr *mtproto.Error
if xerrors.As(err, &rpcErr) && rpcErr.Type == "FLOOD_WAIT" {
if d, ok := mtproto.AsFloodWait(err); ok {
select {
case <-time.After(time.Duration(rpcErr.Argument) * time.Second):
case <-time.After(d):
return true, err
case <-ctx.Done():
return false, ctx.Err()
Expand Down
25 changes: 25 additions & 0 deletions tg/tl_channels_create_channel_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2f105b7

Please sign in to comment.