Skip to content

Commit

Permalink
extract username as unbanned
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Dec 29, 2023
1 parent ea6908e commit 7718777
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
27 changes: 26 additions & 1 deletion app/events/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -280,7 +281,12 @@ func (a *admin) callbackUnbanConfirmed(query *tbapi.CallbackQuery) error {
}

// add user to the approved list
if err := a.bot.AddApprovedUser(userID, ""); err != nil { // name is not available here
name, err := a.extractUsername(query.Message.Text) // try to extract username from the message
if err != nil {
log.Printf("[DEBUG] failed to extract username from %q: %v", query.Message.Text, err)
name = ""
}
if err := a.bot.AddApprovedUser(userID, name); err != nil { // name is not available here
return fmt.Errorf("failed to add user %d to approved list: %w", userID, err)
}

Expand Down Expand Up @@ -455,3 +461,22 @@ func (a *admin) parseCallbackData(data string) (userID int64, msgID int, err err

return userID, msgID, nil
}

// extractUsername tries to extract the username from a ban message
func (a *admin) extractUsername(text string) (string, error) {
// regex for markdown format: [username](tg://user?id=123456)
markdownRegex := regexp.MustCompile(`\[(.*?)\]\(tg://user\?id=\d+\)`)
matches := markdownRegex.FindStringSubmatch(text)
if len(matches) > 1 {
return matches[1], nil
}

// regex for plain format: {200312168 umputun Umputun U}
plainRegex := regexp.MustCompile(`\{\d+ (\S+) .+?\}`)
matches = plainRegex.FindStringSubmatch(text)
if len(matches) > 1 {
return matches[1], nil
}

return "", errors.New("username not found")
}
40 changes: 40 additions & 0 deletions app/events/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,43 @@ func TestAdmin_parseCallbackData(t *testing.T) {
})
}
}

func TestAdmin_extractUsername(t *testing.T) {
tests := []struct {
name string
banMessage string
expectedResult string
expectError bool
}{
{
name: "Markdown Format",
banMessage: `**permanently banned [John_Doe](tg://user?id=123456)** some message text`,
expectedResult: "John_Doe",
expectError: false,
},
{
name: "Plain Format",
banMessage: `permanently banned {200312168 umputun Umputun U} some message text`,
expectedResult: "umputun",
expectError: false,
},
{
name: "Invalid Format",
banMessage: `permanently banned John_Doe some message text`,
expectError: true,
},
}

a := admin{}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
username, err := a.extractUsername(test.banMessage)
if test.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, test.expectedResult, username)
}
})
}
}

0 comments on commit 7718777

Please sign in to comment.