From 737656ccf45e38d0c348b954a33f888e4754bf8d Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 18 Dec 2023 16:59:33 -0600 Subject: [PATCH] don't filter reply in admin chat --- app/events/events.go | 25 ++++++-------- app/events/events_test.go | 70 --------------------------------------- 2 files changed, 10 insertions(+), 85 deletions(-) diff --git a/app/events/events.go b/app/events/events.go index 754cf514..2d0c11af 100644 --- a/app/events/events.go +++ b/app/events/events.go @@ -256,7 +256,8 @@ func (l *TelegramListener) adminChatMsgHandler(update tbapi.Update) error { } return string([]rune(inp)[:max]) + "..." } - log.Printf("[DEBUG] message from admin chat: %+v", update) + log.Printf("[DEBUG] message from admin chat: msg id: %d, update id: %d, from: %s, sender: %s", + update.Message.MessageID, update.UpdateID, update.Message.From.UserName, update.Message.ForwardSenderName) if update.Message.ForwardSenderName == "" && update.FromChat() == nil { // this is a regular message from admin chat, not the forwarded one, ignore it @@ -264,26 +265,12 @@ func (l *TelegramListener) adminChatMsgHandler(update tbapi.Update) error { return nil } - if update.Message.ReplyToMessage != nil { - // this is a reply to a message, ignore it - log.Printf("[DEBUG] message from admin chat, but reply to a message, ignore it, %+v", update.Message) - return nil - } - // this is a forwarded message from super to admin chat, it is an example of missed spam // we need to update spam filter with this message msgTxt := strings.ReplaceAll(update.Message.Text, "\n", " ") log.Printf("[DEBUG] forwarded message from superuser %q to admin chat %d: %q", update.Message.From.UserName, l.adminChatID, msgTxt) - // update spam samples - if !l.Dry { - if err := l.Bot.UpdateSpam(msgTxt); err != nil { - return fmt.Errorf("failed to update spam for %q: %w", msgTxt, err) - } - log.Printf("[INFO] spam updated with %q", shrink(update.Message.Text, 50)) - } - // it would be nice to ban this user right away, but we don't have forwarded user ID here due to tg privacy limitation. // it is empty in update.Message. To ban this user, we need to get the match on the message from the locator and ban from there. info, ok := l.Locator.Message(update.Message.Text) @@ -296,6 +283,14 @@ func (l *TelegramListener) adminChatMsgHandler(update tbapi.Update) error { // remove user from the approved list l.Bot.RemoveApprovedUsers(info.userID) + // update spam samples + if !l.Dry { + if err := l.Bot.UpdateSpam(msgTxt); err != nil { + return fmt.Errorf("failed to update spam for %q: %w", msgTxt, err) + } + log.Printf("[INFO] spam updated with %q", shrink(update.Message.Text, 50)) + } + if l.Dry || l.TrainingMode { return nil } diff --git a/app/events/events_test.go b/app/events/events_test.go index 290926d4..26cf8228 100644 --- a/app/events/events_test.go +++ b/app/events/events_test.go @@ -434,76 +434,6 @@ func TestTelegramListener_DoWithForwarded(t *testing.T) { assert.Equal(t, int64(88), b.RemoveApprovedUsersCalls()[0].ID) } -func TestTelegramListener_DoWithForwarded_Reply(t *testing.T) { - mockLogger := &mocks.SpamLoggerMock{SaveFunc: func(msg *bot.Message, response *bot.Response) {}} - mockAPI := &mocks.TbAPIMock{ - GetChatFunc: func(config tbapi.ChatInfoConfig) (tbapi.Chat, error) { - return tbapi.Chat{ID: 123}, nil - }, - SendFunc: func(c tbapi.Chattable) (tbapi.Message, error) { - return tbapi.Message{Text: c.(tbapi.MessageConfig).Text, From: &tbapi.User{UserName: "user"}}, nil - }, - RequestFunc: func(c tbapi.Chattable) (*tbapi.APIResponse, error) { - return &tbapi.APIResponse{Ok: true}, nil - }, - GetChatAdministratorsFunc: func(config tbapi.ChatAdministratorsConfig) ([]tbapi.ChatMember, error) { return nil, nil }, - } - b := &mocks.BotMock{ - OnMessageFunc: func(msg bot.Message) bot.Response { - t.Logf("on-message: %+v", msg) - if msg.Text == "text 123" && msg.From.Username == "user" { - return bot.Response{Send: true, Text: "bot's answer"} - } - return bot.Response{} - }, - UpdateSpamFunc: func(msg string) error { - t.Logf("update-spam: %s", msg) - return nil - }, - } - - l := TelegramListener{ - SpamLogger: mockLogger, - TbAPI: mockAPI, - Bot: b, - Group: "gr", - AdminGroup: "123", - StartupMsg: "startup", - SuperUsers: SuperUser{"umputun"}, - Locator: NewLocator(10*time.Minute, 0), - } - - ctx, cancel := context.WithTimeout(context.Background(), 500*time.Minute) - defer cancel() - - l.Locator.AddMessage("text 123", 123, 88, 999999) // add message to locator - - updMsg := tbapi.Update{ - Message: &tbapi.Message{ - Chat: &tbapi.Chat{ID: 123}, - Text: "text 123", - From: &tbapi.User{UserName: "umputun", ID: 77}, - Date: int(time.Date(2020, 2, 11, 19, 35, 55, 9, time.UTC).Unix()), - ForwardSenderName: "forwarded_name", - MessageID: 999999, - ReplyToMessage: &tbapi.Message{}, // reply to message - }, - } - - updChan := make(chan tbapi.Update, 1) - updChan <- updMsg - close(updChan) - mockAPI.GetUpdatesChanFunc = func(config tbapi.UpdateConfig) tbapi.UpdatesChannel { return updChan } - - err := l.Do(ctx) - assert.EqualError(t, err, "telegram update chan closed") - assert.Equal(t, 0, len(mockLogger.SaveCalls())) - require.Equal(t, 1, len(mockAPI.SendCalls())) - assert.Equal(t, "startup", mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).Text) - require.Equal(t, 0, len(b.UpdateSpamCalls()), "no spam should be updated") - assert.Equal(t, 0, len(mockAPI.RequestCalls()), "no requests should be made") -} - func TestTelegramListener_DoWithAdminUnBan(t *testing.T) { mockLogger := &mocks.SpamLoggerMock{} mockAPI := &mocks.TbAPIMock{