Skip to content

Commit

Permalink
support for removal users from aproved list
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Dec 18, 2023
1 parent 280ae7c commit 9e4b8dc
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 24 deletions.
71 changes: 63 additions & 8 deletions app/bot/mocks/detector.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions app/bot/spam.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Detector interface {
UpdateSpam(msg string) error
UpdateHam(msg string) error
AddApprovedUsers(ids ...string)
RemoveApprovedUsers(ids ...string)
}

// NewSpamFilter creates new spam filter
Expand Down Expand Up @@ -122,6 +123,17 @@ func (s *SpamFilter) AddApprovedUsers(id int64, ids ...int64) {
s.director.AddApprovedUsers(sids...)
}

// RemoveApprovedUsers removes users from the list of approved users
func (s *SpamFilter) RemoveApprovedUsers(id int64, ids ...int64) {
combinedIDs := append([]int64{id}, ids...)
log.Printf("[DEBUG] remove aproved users: %v", combinedIDs)
sids := make([]string, len(combinedIDs))
for i, id := range combinedIDs {
sids[i] = strconv.FormatInt(id, 10)
}
s.director.RemoveApprovedUsers(sids...)
}

// watch watches for changes in samples files and reloads them
// delay is a time to wait after the last change before reloading to avoid multiple reloads
func (s *SpamFilter) watch(ctx context.Context, delay time.Duration) error {
Expand Down
28 changes: 28 additions & 0 deletions app/bot/spam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,31 @@ func TestAddApprovedUsers(t *testing.T) {
assert.Equal(t, []string{"1", "2", "3"}, mockDirector.AddApprovedUsersCalls()[0].Ids)
})
}

func TestRemoveApprovedUsers(t *testing.T) {
mockDirector := &mocks.DetectorMock{RemoveApprovedUsersFunc: func(ids ...string) {}}

t.Run("remove single approved user", func(t *testing.T) {
mockDirector.ResetCalls()
sf := SpamFilter{director: mockDirector}
sf.RemoveApprovedUsers(1)
require.Equal(t, 1, len(mockDirector.RemoveApprovedUsersCalls()))
assert.Equal(t, []string{"1"}, mockDirector.RemoveApprovedUsersCalls()[0].Ids)
})

t.Run("remove multiple approved users", func(t *testing.T) {
mockDirector.ResetCalls()
sf := SpamFilter{director: mockDirector}
sf.RemoveApprovedUsers(1, 2, 3)
require.Equal(t, 1, len(mockDirector.RemoveApprovedUsersCalls()))
assert.Equal(t, []string{"1", "2", "3"}, mockDirector.RemoveApprovedUsersCalls()[0].Ids)
})

t.Run("remove empty list of approved users", func(t *testing.T) {
mockDirector.ResetCalls()
sf := SpamFilter{director: mockDirector}
sf.RemoveApprovedUsers(1, 2, 3)
require.Equal(t, 1, len(mockDirector.RemoveApprovedUsersCalls()))
assert.Equal(t, []string{"1", "2", "3"}, mockDirector.RemoveApprovedUsersCalls()[0].Ids)
})
}
5 changes: 5 additions & 0 deletions app/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type Bot interface {
UpdateSpam(msg string) error
UpdateHam(msg string) error
AddApprovedUsers(id int64, ids ...int64)
RemoveApprovedUsers(id int64, ids ...int64)
}

// SpamWeb is an interface for the web component
Expand Down Expand Up @@ -305,6 +306,10 @@ func (l *TelegramListener) adminChatMsgHandler(update tbapi.Update) error {
if err := l.banUserOrChannel(bot.PermanentBanDuration, l.chatID, info.userID, 0); err != nil {
return fmt.Errorf("failed to ban user %d: %w", info.userID, err)
}

// remove user from the approved list
l.Bot.RemoveApprovedUsers(info.userID)

log.Printf("[INFO] user %q (%d) banned", update.Message.ForwardSenderName, info.userID)
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions app/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ func TestTelegramListener_DoWithForwarded(t *testing.T) {
t.Logf("update-spam: %s", msg)
return nil
},
RemoveApprovedUsersFunc: func(id int64, ids ...int64) {},
}

l := TelegramListener{
Expand Down Expand Up @@ -428,6 +429,9 @@ func TestTelegramListener_DoWithForwarded(t *testing.T) {

assert.Equal(t, int64(123), mockAPI.RequestCalls()[1].C.(tbapi.RestrictChatMemberConfig).ChatID)
assert.Equal(t, int64(88), mockAPI.RequestCalls()[1].C.(tbapi.RestrictChatMemberConfig).UserID)

assert.Equal(t, 1, len(b.RemoveApprovedUsersCalls()))
assert.Equal(t, int64(88), b.RemoveApprovedUsersCalls()[0].ID)
}

func TestTelegramListener_DoWithForwarded_Reply(t *testing.T) {
Expand Down
77 changes: 69 additions & 8 deletions app/events/mocks/bot.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/events/mocks/spam_logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/events/mocks/spam_web.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9e4b8dc

Please sign in to comment.