mirror of
https://github.com/iyear/tdl
synced 2025-01-09 04:17:35 +08:00
refactor(chat): use tRun to start session
This commit is contained in:
parent
3f65a374fb
commit
62077ff467
@ -10,16 +10,15 @@ import (
|
||||
"github.com/antonmedv/expr"
|
||||
"github.com/fatih/color"
|
||||
"github.com/go-faster/jx"
|
||||
"github.com/gotd/contrib/middleware/ratelimit"
|
||||
"github.com/gotd/td/telegram"
|
||||
"github.com/gotd/td/telegram/peers"
|
||||
"github.com/gotd/td/telegram/query"
|
||||
"github.com/gotd/td/telegram/query/messages"
|
||||
"github.com/gotd/td/tg"
|
||||
"github.com/jedib0t/go-pretty/v6/progress"
|
||||
"go.uber.org/multierr"
|
||||
"golang.org/x/time/rate"
|
||||
|
||||
"github.com/iyear/tdl/app/internal/tgc"
|
||||
"github.com/iyear/tdl/pkg/kv"
|
||||
"github.com/iyear/tdl/pkg/prog"
|
||||
"github.com/iyear/tdl/pkg/storage"
|
||||
"github.com/iyear/tdl/pkg/texpr"
|
||||
@ -29,11 +28,6 @@ import (
|
||||
|
||||
//go:generate go-enum --names --values --flag --nocase
|
||||
|
||||
const (
|
||||
rateInterval = 550 * time.Millisecond
|
||||
rateBucket = 2
|
||||
)
|
||||
|
||||
type ExportOptions struct {
|
||||
Type ExportType
|
||||
Chat string
|
||||
@ -60,12 +54,7 @@ type Message struct {
|
||||
// ENUM(time, id, last)
|
||||
type ExportType int
|
||||
|
||||
func Export(ctx context.Context, opts *ExportOptions) error {
|
||||
c, kvd, err := tgc.NoLogin(ctx, ratelimit.New(rate.Every(rateInterval), rateBucket))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
func Export(ctx context.Context, c *telegram.Client, kvd kv.KV, opts ExportOptions) (rerr error) {
|
||||
// only output available fields
|
||||
if opts.Filter == "-" {
|
||||
fg := texpr.NewFieldsGetter(nil)
|
||||
@ -84,7 +73,6 @@ func Export(ctx context.Context, opts *ExportOptions) error {
|
||||
return fmt.Errorf("failed to compile filter: %w", err)
|
||||
}
|
||||
|
||||
return tgc.RunWithAuth(ctx, c, func(ctx context.Context) (rerr error) {
|
||||
var peer peers.Peer
|
||||
|
||||
manager := peers.Options{Storage: storage.NewPeers(kvd)}.Build(c.API())
|
||||
@ -164,7 +152,7 @@ func Export(ctx context.Context, opts *ExportOptions) error {
|
||||
|
||||
count := int64(0)
|
||||
|
||||
loop:
|
||||
loop:
|
||||
for iter.Next(ctx) {
|
||||
msg := iter.Value()
|
||||
switch opts.Type {
|
||||
@ -234,5 +222,4 @@ func Export(ctx context.Context, opts *ExportOptions) error {
|
||||
tracker.MarkAsDone()
|
||||
prog.Wait(ctx, pw)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
@ -6,19 +6,17 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/antonmedv/expr"
|
||||
"github.com/gotd/contrib/middleware/ratelimit"
|
||||
"github.com/gotd/td/telegram"
|
||||
"github.com/gotd/td/telegram/message/peer"
|
||||
"github.com/gotd/td/telegram/peers"
|
||||
"github.com/gotd/td/telegram/query"
|
||||
"github.com/gotd/td/tg"
|
||||
"github.com/mattn/go-runewidth"
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/time/rate"
|
||||
|
||||
"github.com/iyear/tdl/app/internal/tgc"
|
||||
"github.com/iyear/tdl/pkg/kv"
|
||||
"github.com/iyear/tdl/pkg/logger"
|
||||
"github.com/iyear/tdl/pkg/storage"
|
||||
"github.com/iyear/tdl/pkg/texpr"
|
||||
@ -57,7 +55,7 @@ type ListOptions struct {
|
||||
Filter string
|
||||
}
|
||||
|
||||
func List(ctx context.Context, opts ListOptions) error {
|
||||
func List(ctx context.Context, c *telegram.Client, kvd kv.KV, opts ListOptions) error {
|
||||
log := logger.From(ctx)
|
||||
|
||||
// align output
|
||||
@ -81,12 +79,6 @@ func List(ctx context.Context, opts ListOptions) error {
|
||||
return fmt.Errorf("failed to compile filter: %w", err)
|
||||
}
|
||||
|
||||
c, kvd, err := tgc.NoLogin(ctx, ratelimit.New(rate.Every(time.Millisecond*400), 2))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tgc.RunWithAuth(ctx, c, func(ctx context.Context) error {
|
||||
dialogs, err := query.GetDialogs(c.API()).BatchSize(100).Collect(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -154,7 +146,6 @@ func List(ctx context.Context, opts ListOptions) error {
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func printTable(result []*Dialog) {
|
||||
|
@ -10,16 +10,15 @@ import (
|
||||
"github.com/fatih/color"
|
||||
"github.com/go-faster/errors"
|
||||
"github.com/go-faster/jx"
|
||||
"github.com/gotd/contrib/middleware/ratelimit"
|
||||
"github.com/gotd/td/telegram"
|
||||
"github.com/gotd/td/telegram/peers"
|
||||
"github.com/gotd/td/telegram/query/channels/participants"
|
||||
"github.com/gotd/td/tg"
|
||||
"github.com/gotd/td/tgerr"
|
||||
"github.com/jedib0t/go-pretty/v6/progress"
|
||||
"go.uber.org/multierr"
|
||||
"golang.org/x/time/rate"
|
||||
|
||||
"github.com/iyear/tdl/app/internal/tgc"
|
||||
"github.com/iyear/tdl/pkg/kv"
|
||||
"github.com/iyear/tdl/pkg/prog"
|
||||
"github.com/iyear/tdl/pkg/storage"
|
||||
"github.com/iyear/tdl/pkg/utils"
|
||||
@ -39,13 +38,7 @@ type User struct {
|
||||
LastName string `json:"last_name"`
|
||||
}
|
||||
|
||||
func Users(ctx context.Context, opts UsersOptions) error {
|
||||
c, kvd, err := tgc.NoLogin(ctx, ratelimit.New(rate.Every(rateInterval), rateBucket))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tgc.RunWithAuth(ctx, c, func(ctx context.Context) (rerr error) {
|
||||
func Users(ctx context.Context, c *telegram.Client, kvd kv.KV, opts UsersOptions) (rerr error) {
|
||||
manager := peers.Options{Storage: storage.NewPeers(kvd)}.Build(c.API())
|
||||
if opts.Chat == "" {
|
||||
return fmt.Errorf("missing domain id")
|
||||
@ -112,7 +105,6 @@ func Users(ctx context.Context, opts UsersOptions) error {
|
||||
|
||||
prog.Wait(ctx, pw)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func outputUsers(ctx context.Context,
|
||||
|
20
cmd/chat.go
20
cmd/chat.go
@ -1,16 +1,24 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gotd/contrib/middleware/ratelimit"
|
||||
"github.com/gotd/td/telegram"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/time/rate"
|
||||
|
||||
"github.com/iyear/tdl/app/chat"
|
||||
"github.com/iyear/tdl/pkg/kv"
|
||||
"github.com/iyear/tdl/pkg/logger"
|
||||
)
|
||||
|
||||
var limiter = ratelimit.New(rate.Every(500*time.Millisecond), 2)
|
||||
|
||||
func NewChat() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "chat",
|
||||
@ -29,7 +37,9 @@ func NewChatList() *cobra.Command {
|
||||
Use: "ls",
|
||||
Short: "List your chats",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return chat.List(logger.Named(cmd.Context(), "ls"), opts)
|
||||
return tRun(cmd.Context(), false, func(ctx context.Context, c *telegram.Client, kvd kv.KV) error {
|
||||
return chat.List(logger.Named(ctx, "ls"), c, kvd, opts)
|
||||
}, limiter)
|
||||
},
|
||||
}
|
||||
|
||||
@ -72,7 +82,9 @@ func NewChatExport() *cobra.Command {
|
||||
return fmt.Errorf("unknown export type: %s", opts.Type)
|
||||
}
|
||||
|
||||
return chat.Export(logger.Named(cmd.Context(), "export"), &opts)
|
||||
return tRun(cmd.Context(), false, func(ctx context.Context, c *telegram.Client, kvd kv.KV) error {
|
||||
return chat.Export(logger.Named(ctx, "export"), c, kvd, opts)
|
||||
}, limiter)
|
||||
},
|
||||
}
|
||||
|
||||
@ -125,7 +137,9 @@ func NewChatUsers() *cobra.Command {
|
||||
Use: "users",
|
||||
Short: "export users from (protected) channels",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return chat.Users(logger.Named(cmd.Context(), "users"), opts)
|
||||
return tRun(cmd.Context(), false, func(ctx context.Context, c *telegram.Client, kvd kv.KV) error {
|
||||
return chat.Users(logger.Named(ctx, "users"), c, kvd, opts)
|
||||
}, limiter)
|
||||
},
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user