mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Fixed updating mention keys when a user's username changes (#2774)
This commit is contained in:
committed by
Christopher Speller
parent
f73daebb61
commit
d8df66c00c
@@ -186,13 +186,28 @@ func (u *User) SetDefaultNotifications() {
|
||||
u.NotifyProps["desktop"] = USER_NOTIFY_ALL
|
||||
u.NotifyProps["desktop_sound"] = "true"
|
||||
u.NotifyProps["mention_keys"] = u.Username + ",@" + u.Username
|
||||
u.NotifyProps["first_name"] = "false"
|
||||
u.NotifyProps["all"] = "true"
|
||||
u.NotifyProps["channel"] = "true"
|
||||
splitName := strings.Split(u.Nickname, " ")
|
||||
if len(splitName) > 0 && splitName[0] != "" {
|
||||
|
||||
if u.FirstName == "" {
|
||||
u.NotifyProps["first_name"] = "false"
|
||||
} else {
|
||||
u.NotifyProps["first_name"] = "true"
|
||||
u.NotifyProps["mention_keys"] += "," + splitName[0]
|
||||
}
|
||||
}
|
||||
|
||||
func (user *User) UpdateMentionKeysFromUsername(oldUsername string) {
|
||||
nonUsernameKeys := []string{}
|
||||
splitKeys := strings.Split(user.NotifyProps["mention_keys"], ",")
|
||||
for _, key := range splitKeys {
|
||||
if key != oldUsername && key != "@"+oldUsername {
|
||||
nonUsernameKeys = append(nonUsernameKeys, key)
|
||||
}
|
||||
}
|
||||
|
||||
user.NotifyProps["mention_keys"] = user.Username + ",@" + user.Username
|
||||
if len(nonUsernameKeys) > 0 {
|
||||
user.NotifyProps["mention_keys"] += "," + strings.Join(nonUsernameKeys, ",")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +41,33 @@ func TestUserPreUpdate(t *testing.T) {
|
||||
user.PreUpdate()
|
||||
}
|
||||
|
||||
func TestUserUpdateMentionKeysFromUsername(t *testing.T) {
|
||||
user := User{Username: "user"}
|
||||
user.SetDefaultNotifications()
|
||||
|
||||
if user.NotifyProps["mention_keys"] != "user,@user" {
|
||||
t.Fatal("default mention keys are invalid: %v", user.NotifyProps["mention_keys"])
|
||||
}
|
||||
|
||||
user.Username = "person"
|
||||
user.UpdateMentionKeysFromUsername("user")
|
||||
if user.NotifyProps["mention_keys"] != "person,@person" {
|
||||
t.Fatal("mention keys are invalid after changing username: %v", user.NotifyProps["mention_keys"])
|
||||
}
|
||||
|
||||
user.NotifyProps["mention_keys"] += ",mention"
|
||||
user.UpdateMentionKeysFromUsername("person")
|
||||
if user.NotifyProps["mention_keys"] != "person,@person,mention" {
|
||||
t.Fatal("mention keys are invalid after adding extra mention keyword: %v", user.NotifyProps["mention_keys"])
|
||||
}
|
||||
|
||||
user.Username = "user"
|
||||
user.UpdateMentionKeysFromUsername("person")
|
||||
if user.NotifyProps["mention_keys"] != "user,@user,mention" {
|
||||
t.Fatal("mention keys are invalid after changing username with extra mention keyword: %v", user.NotifyProps["mention_keys"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserIsValid(t *testing.T) {
|
||||
user := User{}
|
||||
|
||||
|
||||
@@ -141,14 +141,7 @@ func (us SqlUserStore) Update(user *model.User, allowActiveUpdate bool) StoreCha
|
||||
}
|
||||
|
||||
if user.Username != oldUser.Username {
|
||||
nonUsernameKeys := []string{}
|
||||
splitKeys := strings.Split(user.NotifyProps["mention_keys"], ",")
|
||||
for _, key := range splitKeys {
|
||||
if key != oldUser.Username && key != "@"+oldUser.Username {
|
||||
nonUsernameKeys = append(nonUsernameKeys, key)
|
||||
}
|
||||
}
|
||||
user.NotifyProps["mention_keys"] = strings.Join(nonUsernameKeys, ",") + "," + user.Username + ",@" + user.Username
|
||||
user.UpdateMentionKeysFromUsername(oldUser.Username)
|
||||
}
|
||||
|
||||
if count, err := us.GetMaster().Update(user); err != nil {
|
||||
|
||||
@@ -214,7 +214,6 @@ function highlightCurrentMentions(text, tokens) {
|
||||
}
|
||||
|
||||
for (const mention of UserStore.getCurrentMentionKeys()) {
|
||||
// occasionally we get an empty mention which matches a bunch of empty strings
|
||||
if (!mention) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user