Skip to content

Commit

Permalink
clean dbl atempt to avoid multiple unbans
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Dec 11, 2023
1 parent acee7f1 commit a95ed80
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
23 changes: 5 additions & 18 deletions app/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,31 +142,18 @@ func (s *SpamWeb) unbanHandler(w http.ResponseWriter, r *http.Request) {
}

// check if user is already unbanned
isAlreadyUnbanned := func() bool {
isAlreadyUnbanned, tsPrevUnban := func() (bool, time.Time) {
s.unbanned.RLock()
defer s.unbanned.RUnlock()
_, ok := s.unbanned.users[userID]
return ok
ts, ok := s.unbanned.users[userID]
return ok, ts
}()

if isAlreadyUnbanned {
log.Printf("[WARN] user %d already unbanned", userID)
log.Printf("[WARN] user %d already unbanned ", userID)
resp := htmlResponse{
Title: "Error",
Message: fmt.Sprintf("user %d already unbanned", userID),
Background: "#ff6347",
Foreground: "#ffffff",
StatusCode: http.StatusBadRequest,
}
s.sendHTML(w, resp)
return
}

if ts, ok := s.unbanned.users[userID]; ok {
log.Printf("[WARN] user %d already unbanned", userID)
resp := htmlResponse{
Title: "Error",
Message: fmt.Sprintf("user %d already unbanned %v ago", userID, time.Since(ts).Round(time.Second)),
Message: fmt.Sprintf("user %d already unbanned %v ago", userID, time.Since(tsPrevUnban).Round(time.Second)),
Background: "#ff6347",
Foreground: "#ffffff",
StatusCode: http.StatusBadRequest,
Expand Down
1 change: 1 addition & 0 deletions app/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ func TestSpamWeb_Run(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
body, err = io.ReadAll(resp.Body)
require.NoError(t, err)
t.Logf("body: %s", body)
assert.Contains(t, string(body), "user 1239 already unbanned")

})
Expand Down

0 comments on commit a95ed80

Please sign in to comment.