feat(forward): forward message in reverse order with --desc flag. close #456

This commit is contained in:
iyear 2024-02-10 18:33:44 +08:00
parent a0474f6936
commit c2535e6d79
2 changed files with 12 additions and 2 deletions

View File

@ -35,6 +35,7 @@ type Options struct {
Silent bool
DryRun bool
Single bool
Desc bool
}
func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr error) {
@ -59,7 +60,7 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr
ctx = tctx.WithPool(ctx, pool)
dialogs, err := collectDialogs(ctx, opts.From)
dialogs, err := collectDialogs(ctx, opts.From, opts.Desc)
if err != nil {
return errors.Wrap(err, "collect dialogs")
}
@ -98,7 +99,7 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr
return fw.Forward(ctx)
}
func collectDialogs(ctx context.Context, input []string) ([]*tmessage.Dialog, error) {
func collectDialogs(ctx context.Context, input []string, desc bool) ([]*tmessage.Dialog, error) {
var dialogs []*tmessage.Dialog
for _, p := range input {
@ -120,6 +121,14 @@ func collectDialogs(ctx context.Context, input []string) ([]*tmessage.Dialog, er
}
}
if desc {
for _, dd := range d {
for i, j := 0, len(dd.Messages)-1; i < j; i, j = i+1, j-1 {
dd.Messages[i], dd.Messages[j] = dd.Messages[j], dd.Messages[i]
}
}
}
dialogs = append(dialogs, d...)
}

View File

@ -33,6 +33,7 @@ func NewForward() *cobra.Command {
cmd.Flags().BoolVar(&opts.Silent, "silent", false, "send messages silently")
cmd.Flags().BoolVar(&opts.DryRun, "dry-run", false, "do not actually send messages, just show how they would be sent")
cmd.Flags().BoolVar(&opts.Single, "single", false, "do not automatically detect and forward grouped messages")
cmd.Flags().BoolVar(&opts.Desc, "desc", false, "forward messages in reverse order for each input peer")
return cmd
}