|
| 1 | +// Package report implements "tg contact report <ref>". |
| 2 | +package report |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/gotd/td/telegram/peers" |
| 9 | + "github.com/gotd/td/tg" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + |
| 12 | + actioncontact "github.com/vika2603/telegram-cli/internal/action/contact" |
| 13 | + "github.com/vika2603/telegram-cli/internal/command" |
| 14 | + "github.com/vika2603/telegram-cli/internal/runtime" |
| 15 | + "github.com/vika2603/telegram-cli/internal/telegram" |
| 16 | + "github.com/vika2603/telegram-cli/internal/telegram/peer" |
| 17 | + "github.com/vika2603/telegram-cli/internal/ui" |
| 18 | +) |
| 19 | + |
| 20 | +// Options holds the resolved flags and injected dependencies for Run. |
| 21 | +type Options struct { |
| 22 | + RawRef string |
| 23 | + Reason string |
| 24 | + Message string |
| 25 | + Block bool |
| 26 | + Yes bool |
| 27 | + |
| 28 | + IOStreams *ui.IOStreams |
| 29 | + Prompter ui.Prompter |
| 30 | + |
| 31 | + // Report is the closure that performs the actual Telegram call. Production |
| 32 | + // code sets it via newReport; tests stub it directly. |
| 33 | + Report actioncontact.ReportFunc |
| 34 | +} |
| 35 | + |
| 36 | +// New builds the cobra command for "tg contact report". |
| 37 | +func New(f *runtime.Invocation, runF func(*Options) error) *cobra.Command { |
| 38 | + opts := &Options{} |
| 39 | + cmd := &cobra.Command{ |
| 40 | + Use: "report <ref>", |
| 41 | + Short: "Report a user, bot, group, or channel to Telegram", |
| 42 | + Args: cobra.ExactArgs(1), |
| 43 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 44 | + opts.RawRef = args[0] |
| 45 | + opts.IOStreams = f.IOStreams |
| 46 | + opts.Prompter = f.Prompter |
| 47 | + if runF != nil { |
| 48 | + return runF(opts) |
| 49 | + } |
| 50 | + opts.Report = newReport(f) |
| 51 | + return Run(cmd.Context(), opts) |
| 52 | + }, |
| 53 | + } |
| 54 | + cmd.Flags().StringVar(&opts.Reason, "reason", "spam", "Report reason: spam, violence, porn, child-abuse, copyright, fake, drugs, personal-details, geo-irrelevant, other") |
| 55 | + cmd.Flags().StringVar(&opts.Message, "message", "", "Optional comment for report moderation") |
| 56 | + cmd.Flags().BoolVar(&opts.Block, "block", false, "Also block the peer after reporting") |
| 57 | + cmd.Flags().BoolVarP(&opts.Yes, "yes", "y", false, "Skip confirmation prompt") |
| 58 | + command.SetMeta(cmd, command.Meta{NeedsAccount: true, NeedsClient: true}) |
| 59 | + return cmd |
| 60 | +} |
| 61 | + |
| 62 | +// Run collects the raw request, delegates validation/mutation, and renders status. |
| 63 | +func Run(ctx context.Context, opts *Options) error { |
| 64 | + if err := actioncontact.Report(ctx, actioncontact.ReportRequest{ |
| 65 | + RawRef: opts.RawRef, |
| 66 | + Reason: opts.Reason, |
| 67 | + Message: opts.Message, |
| 68 | + Block: opts.Block, |
| 69 | + Yes: opts.Yes, |
| 70 | + Prompter: opts.Prompter, |
| 71 | + }, opts.Report); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + verb := "reported" |
| 75 | + if opts.Block { |
| 76 | + verb = "reported+blocked" |
| 77 | + } |
| 78 | + _, _ = fmt.Fprintf(opts.IOStreams.Out, "%s\t%s\n", verb, opts.RawRef) |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +// newReport returns the production Report closure that calls the Telegram API. |
| 83 | +func newReport(f *runtime.Invocation) actioncontact.ReportFunc { |
| 84 | + return func(ctx context.Context, q actioncontact.ReportQuery) error { |
| 85 | + acct, err := f.Account("") |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + if cl, _ := runtime.MaybeDialDaemon(ctx, f, acct); cl != nil { |
| 90 | + defer func() { _ = cl.Close() }() |
| 91 | + _, err := cl.Call(ctx, "contact.report", q) |
| 92 | + return err |
| 93 | + } |
| 94 | + return f.WithPeers(ctx, acct, runtime.ClientOptsFrom(f, acct), |
| 95 | + func(ctx context.Context, api *tg.Client, _ *peers.Manager, res *peer.Resolver) error { |
| 96 | + return telegram.ReportPeer(ctx, api, res, q) |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments