Skip to content

Commit

Permalink
don't try to spam-check supers
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Oct 9, 2023
1 parent 8165e4d commit afc35f2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
15 changes: 10 additions & 5 deletions app/bot/spam.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (

// SpamFilter bot, checks if user is a spammer using CAS API
type SpamFilter struct {
casAPI string
dry bool
client HTTPClient
casAPI string
dry bool
client HTTPClient
superUser SuperUser

approvedUsers map[int64]bool
}
Expand All @@ -22,9 +23,9 @@ type SpamFilter struct {
var permanentBanDuration = time.Hour * 24 * 400

// NewSpamFilter makes a spam detecting bot
func NewSpamFilter(api string, client HTTPClient, dry bool) *SpamFilter {
func NewSpamFilter(api string, client HTTPClient, superUser SuperUser, dry bool) *SpamFilter {
log.Printf("[INFO] Spam bot with %s", api)
return &SpamFilter{casAPI: api, client: client, dry: dry, approvedUsers: map[int64]bool{}}
return &SpamFilter{casAPI: api, client: client, dry: dry, approvedUsers: map[int64]bool{}, superUser: superUser}
}

// OnMessage checks if user already approved and if not checks if user is a spammer
Expand All @@ -33,6 +34,10 @@ func (s *SpamFilter) OnMessage(msg Message) (response Response) {
return Response{}
}

if s.superUser.IsSuper(msg.From.Username) {
return Response{} // don't check super users for spam
}

reqURL := fmt.Sprintf("%s/check?user_id=%d", s.casAPI, msg.From.ID)
req, err := http.NewRequest("GET", reqURL, http.NoBody)
if err != nil {
Expand Down
26 changes: 23 additions & 3 deletions app/bot/spam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,28 @@ import (
)

func TestNewSpamFilter(t *testing.T) {
su := &mocks.SuperUser{IsSuperFunc: func(userName string) bool {
if userName == "super" || userName == "admin" {
return true
}
return false
}}
client := &mocks.HTTPClient{}
sf := NewSpamFilter("http://localhost", client, false)
sf := NewSpamFilter("http://localhost", client, su, false)
assert.NotNil(t, sf)
assert.Equal(t, "http://localhost", sf.casAPI)
assert.Equal(t, client, sf.client)
assert.False(t, sf.dry)
}

func TestSpamFilter_OnMessage(t *testing.T) {
su := &mocks.SuperUser{IsSuperFunc: func(userName string) bool {
if userName == "super" || userName == "admin" {
return true
}
return false
}}

tests := []struct {
name string
mockResp string
Expand Down Expand Up @@ -79,7 +92,7 @@ func TestSpamFilter_OnMessage(t *testing.T) {
},
}

s := NewSpamFilter("http://localhost", mockedHTTPClient, tt.dryMode)
s := NewSpamFilter("http://localhost", mockedHTTPClient, su, tt.dryMode)

msg := Message{
From: User{
Expand Down Expand Up @@ -107,7 +120,14 @@ func TestSpamFilter_OnMessageCheckOnce(t *testing.T) {
},
}

s := NewSpamFilter("http://localhost", mockedHTTPClient, false)
su := &mocks.SuperUser{IsSuperFunc: func(userName string) bool {
if userName == "super" || userName == "admin" {
return true
}
return false
}}

s := NewSpamFilter("http://localhost", mockedHTTPClient, su, false)
res := s.OnMessage(Message{From: User{ID: 1, Username: "testuser"}, ID: 1, Text: "Hello"})
assert.Equal(t, Response{}, res)
assert.Len(t, mockedHTTPClient.DoCalls(), 1, "Do should be called once")
Expand Down
2 changes: 1 addition & 1 deletion app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func main() {
log.Printf("[INFO] spam filter enabled, api=%s, timeout=%s, dry=%v",
opts.SpamFilter.API, opts.SpamFilter.TimeOut, opts.SpamFilter.Dry)
httpCasClient := &http.Client{Timeout: opts.SpamFilter.TimeOut}
multiBot = append(multiBot, bot.NewSpamFilter(opts.SpamFilter.API, httpCasClient, opts.SpamFilter.Dry))
multiBot = append(multiBot, bot.NewSpamFilter(opts.SpamFilter.API, httpCasClient, opts.SuperUsers, opts.SpamFilter.Dry))
} else {
log.Print("[INFO] spam filter disabled")
}
Expand Down

0 comments on commit afc35f2

Please sign in to comment.