From aaa7ad2e14ddc6f5e19dfa37511cd66289c4826a Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Sun, 6 Oct 2024 23:57:27 +0300 Subject: [PATCH] fix: update message helpers after API change Report API changed in backward incompatible way. --- telegram/message/report.go | 47 +++++---------------------------- telegram/message/report_test.go | 42 +++++------------------------ telegram/message/sender.go | 2 +- 3 files changed, 14 insertions(+), 77 deletions(-) diff --git a/telegram/message/report.go b/telegram/message/report.go index 5660c9c595..ee2c2c50f9 100644 --- a/telegram/message/report.go +++ b/telegram/message/report.go @@ -22,58 +22,23 @@ func (b *ReportBuilder) Message(msg string) *ReportBuilder { return b } -func (b *ReportBuilder) send(ctx context.Context, reason tg.ReportReasonClass) (bool, error) { +func (b *ReportBuilder) send(ctx context.Context, option []byte) (tg.ReportResultClass, error) { p, err := b.peer(ctx) if err != nil { - return false, errors.Wrap(err, "peer") + return nil, errors.Wrap(err, "peer") } return b.sender.report(ctx, &tg.MessagesReportRequest{ Peer: p, ID: b.ids, - Reason: reason, + Option: option, Message: b.message, }) } -// Spam sends report for spam. -func (b *ReportBuilder) Spam(ctx context.Context) (bool, error) { - return b.send(ctx, &tg.InputReportReasonSpam{}) -} - -// Violence sends report for violence. -func (b *ReportBuilder) Violence(ctx context.Context) (bool, error) { - return b.send(ctx, &tg.InputReportReasonViolence{}) -} - -// Pornography sends report for pornography. -func (b *ReportBuilder) Pornography(ctx context.Context) (bool, error) { - return b.send(ctx, &tg.InputReportReasonPornography{}) -} - -// ChildAbuse sends report for child abuse. -func (b *ReportBuilder) ChildAbuse(ctx context.Context) (bool, error) { - return b.send(ctx, &tg.InputReportReasonChildAbuse{}) -} - -// Other sends report for other. -func (b *ReportBuilder) Other(ctx context.Context) (bool, error) { - return b.send(ctx, &tg.InputReportReasonOther{}) -} - -// Copyright sends report for copyrighted content. -func (b *ReportBuilder) Copyright(ctx context.Context) (bool, error) { - return b.send(ctx, &tg.InputReportReasonCopyright{}) -} - -// GeoIrrelevant sends report for irrelevant geogroup. -func (b *ReportBuilder) GeoIrrelevant(ctx context.Context) (bool, error) { - return b.send(ctx, &tg.InputReportReasonGeoIrrelevant{}) -} - -// Fake sends report for fake. -func (b *ReportBuilder) Fake(ctx context.Context) (bool, error) { - return b.send(ctx, &tg.InputReportReasonFake{}) +// Option sends report with provided option field. +func (b *ReportBuilder) Option(ctx context.Context, option []byte) (tg.ReportResultClass, error) { + return b.send(ctx, option) } // Report reports messages in a chat for violation of Telegram's Terms of Service. diff --git a/telegram/message/report_test.go b/telegram/message/report_test.go index 3251689168..db91c1140a 100644 --- a/telegram/message/report_test.go +++ b/telegram/message/report_test.go @@ -14,16 +14,16 @@ import ( "github.com/gotd/td/tgmock" ) -func expectSendReport(t *testing.T, reason tg.ReportReasonClass, mock *tgmock.Mock, id int, msg string) { +func expectSendReport(t *testing.T, option []byte, mock *tgmock.Mock, id int, msg string) { mock.ExpectFunc(func(b bin.Encoder) { req, ok := b.(*tg.MessagesReportRequest) require.True(t, ok) require.Equal(t, &tg.InputPeerSelf{}, req.Peer) - require.Equal(t, reason, req.Reason) + require.Equal(t, option, req.Option) require.NotZero(t, req.ID) require.Equal(t, id, req.ID[0]) require.Equal(t, msg, req.Message) - }).ThenTrue() + }).ThenResult(&tg.ReportResultReported{}) } func TestRequestBuilder_Report(t *testing.T) { @@ -37,39 +37,11 @@ func TestRequestBuilder_Report(t *testing.T) { report := sender.Self().Report(id).Message(msg) - var r bool - expectSendReport(t, &tg.InputReportReasonSpam{}, mock, id, msg) - r, err = report.Spam(ctx) - require.NoError(t, err) - require.True(t, r) - expectSendReport(t, &tg.InputReportReasonViolence{}, mock, id, msg) - r, err = report.Violence(ctx) - require.NoError(t, err) - require.True(t, r) - expectSendReport(t, &tg.InputReportReasonPornography{}, mock, id, msg) - r, err = report.Pornography(ctx) - require.NoError(t, err) - require.True(t, r) - expectSendReport(t, &tg.InputReportReasonChildAbuse{}, mock, id, msg) - r, err = report.ChildAbuse(ctx) - require.NoError(t, err) - require.True(t, r) - expectSendReport(t, &tg.InputReportReasonOther{}, mock, id, msg) - r, err = report.Other(ctx) - require.NoError(t, err) - require.True(t, r) - expectSendReport(t, &tg.InputReportReasonCopyright{}, mock, id, msg) - r, err = report.Copyright(ctx) - require.NoError(t, err) - require.True(t, r) - expectSendReport(t, &tg.InputReportReasonGeoIrrelevant{}, mock, id, msg) - r, err = report.GeoIrrelevant(ctx) - require.NoError(t, err) - require.True(t, r) - expectSendReport(t, &tg.InputReportReasonFake{}, mock, id, msg) - r, err = report.Fake(ctx) + option := []byte{1, 2, 3} + expectSendReport(t, option, mock, id, msg) + r, err := report.Option(ctx, option) require.NoError(t, err) - require.True(t, r) + require.NotNil(t, r) } func TestRequestBuilder_ReportSpam(t *testing.T) { diff --git a/telegram/message/sender.go b/telegram/message/sender.go index 21238ee8b1..2df3b83109 100644 --- a/telegram/message/sender.go +++ b/telegram/message/sender.go @@ -160,7 +160,7 @@ func (s *Sender) setTyping(ctx context.Context, req *tg.MessagesSetTypingRequest } // report reports a message in a chat for violation of Telegram's Terms of Service. -func (s *Sender) report(ctx context.Context, req *tg.MessagesReportRequest) (bool, error) { +func (s *Sender) report(ctx context.Context, req *tg.MessagesReportRequest) (tg.ReportResultClass, error) { return s.raw.MessagesReport(ctx, req) }