Skip to content

Commit

Permalink
fix: update message helpers after API change
Browse files Browse the repository at this point in the history
Report API changed in backward incompatible way.
  • Loading branch information
ernado committed Oct 6, 2024
1 parent 2691892 commit aaa7ad2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 77 deletions.
47 changes: 6 additions & 41 deletions telegram/message/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 7 additions & 35 deletions telegram/message/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion telegram/message/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit aaa7ad2

Please sign in to comment.