feat: add custom username suggestions pool (#1644)

*  add custom username suggestions pool

* 🚸 add minimum of 10 suggested usernames until custom pool is used
This commit is contained in:
Jannik
2022-01-12 19:18:08 +01:00
committed by GitHub
parent 814c65eeb7
commit d3cfc40b5c
6 changed files with 71 additions and 11 deletions

View File

@@ -42,6 +42,7 @@ const externalActionsKey = "external_actions"
const customStylesKey = "custom_styles"
const videoCodecKey = "video_codec"
const blockedUsernamesKey = "blocked_usernames"
const suggestedUsernamesKey = "suggested_usernames"
// GetExtraPageBodyContent will return the user-supplied body content.
func GetExtraPageBodyContent() string {
@@ -601,3 +602,23 @@ func SetForbiddenUsernameList(usernames []string) error {
usernameListString := strings.Join(usernames, ",")
return _datastore.SetString(blockedUsernamesKey, usernameListString)
}
// GetSuggestedUsernamesList will return the suggested usernames as a comma separated string.
// If the number of suggested usernames is smaller than 10, the number pool is not used (see code in the CreateAnonymousUser function).
func GetSuggestedUsernamesList() []string {
usernameString, err := _datastore.GetString(suggestedUsernamesKey)
if err != nil || usernameString == "" {
return []string{}
}
suggestionList := strings.Split(usernameString, ",")
return suggestionList
}
// SetSuggestedUsernamesList sets the username suggestion list as a comma separated string.
func SetSuggestedUsernamesList(usernames []string) error {
usernameListString := strings.Join(usernames, ",")
return _datastore.SetString(suggestedUsernamesKey, usernameListString)
}

View File

@@ -17,6 +17,7 @@ import (
var _datastore *data.Datastore
const moderatorScopeKey = "MODERATOR"
const minSuggestedUsernamePoolLength = 10
// User represents a single chat user.
type User struct {
@@ -48,7 +49,7 @@ func SetupUsers() {
}
// CreateAnonymousUser will create a new anonymous user with the provided display name.
func CreateAnonymousUser(username string) (*User, error) {
func CreateAnonymousUser(displayName string) (*User, error) {
id := shortid.MustGenerate()
accessToken, err := utils.GenerateAccessToken()
if err != nil {
@@ -56,9 +57,15 @@ func CreateAnonymousUser(username string) (*User, error) {
return nil, err
}
displayName := username
if displayName == "" {
displayName = utils.GeneratePhrase()
suggestedUsernamesList := data.GetSuggestedUsernamesList()
if len(suggestedUsernamesList) >= minSuggestedUsernamePoolLength {
index := utils.RandomIndex(len(suggestedUsernamesList))
displayName = suggestedUsernamesList[index]
} else {
displayName = utils.GeneratePhrase()
}
}
displayColor := utils.GenerateRandomDisplayColor()
@@ -287,8 +294,8 @@ func GetModeratorUsers() []*User {
substr(rest, instr(rest, ',')+1)
FROM split
WHERE rest <> '')
SELECT id, display_name, scopes, display_color, created_at, disabled_at, previous_names, namechanged_at, scope
FROM split
SELECT id, display_name, scopes, display_color, created_at, disabled_at, previous_names, namechanged_at, scope
FROM split
WHERE scope <> ''
ORDER BY created_at
) AS token WHERE token.scope = ?`