Add @ prefix for sender username in push notifications (#11934)
* Add @ prefix for sender username in push notifications * de-duplicating code
Этот коммит содержится в:
коммит произвёл
Harrison Healey
родитель
f753819a66
Коммит
24e0d6f00d
@@ -640,7 +640,7 @@ type postNotification struct {
|
|||||||
func (n *postNotification) GetChannelName(userNameFormat string, excludeId string) string {
|
func (n *postNotification) GetChannelName(userNameFormat string, excludeId string) string {
|
||||||
switch n.channel.Type {
|
switch n.channel.Type {
|
||||||
case model.CHANNEL_DIRECT:
|
case model.CHANNEL_DIRECT:
|
||||||
return n.sender.GetDisplayName(userNameFormat)
|
return n.sender.GetDisplayNameWithPrefix(userNameFormat, "@")
|
||||||
case model.CHANNEL_GROUP:
|
case model.CHANNEL_GROUP:
|
||||||
names := []string{}
|
names := []string{}
|
||||||
for _, user := range n.profileMap {
|
for _, user := range n.profileMap {
|
||||||
@@ -670,7 +670,7 @@ func (n *postNotification) GetSenderName(userNameFormat string, overridesAllowed
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return n.sender.GetDisplayName(userNameFormat)
|
return n.sender.GetDisplayNameWithPrefix(userNameFormat, "@")
|
||||||
}
|
}
|
||||||
|
|
||||||
// addMentionedUsers will add the mentioned user id in the struct's list for mentioned users
|
// addMentionedUsers will add the mentioned user id in the struct's list for mentioned users
|
||||||
|
|||||||
@@ -1207,12 +1207,12 @@ func TestPostNotificationGetChannelName(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"direct channel, unspecified": {
|
"direct channel, unspecified": {
|
||||||
channel: &model.Channel{Type: model.CHANNEL_DIRECT},
|
channel: &model.Channel{Type: model.CHANNEL_DIRECT},
|
||||||
expected: "sender",
|
expected: "@sender",
|
||||||
},
|
},
|
||||||
"direct channel, username": {
|
"direct channel, username": {
|
||||||
channel: &model.Channel{Type: model.CHANNEL_DIRECT},
|
channel: &model.Channel{Type: model.CHANNEL_DIRECT},
|
||||||
nameFormat: model.SHOW_USERNAME,
|
nameFormat: model.SHOW_USERNAME,
|
||||||
expected: "sender",
|
expected: "@sender",
|
||||||
},
|
},
|
||||||
"direct channel, full name": {
|
"direct channel, full name": {
|
||||||
channel: &model.Channel{Type: model.CHANNEL_DIRECT},
|
channel: &model.Channel{Type: model.CHANNEL_DIRECT},
|
||||||
@@ -1290,11 +1290,11 @@ func TestPostNotificationGetSenderName(t *testing.T) {
|
|||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
"name format unspecified": {
|
"name format unspecified": {
|
||||||
expected: sender.Username,
|
expected: "@" + sender.Username,
|
||||||
},
|
},
|
||||||
"name format username": {
|
"name format username": {
|
||||||
nameFormat: model.SHOW_USERNAME,
|
nameFormat: model.SHOW_USERNAME,
|
||||||
expected: sender.Username,
|
expected: "@" + sender.Username,
|
||||||
},
|
},
|
||||||
"name format full name": {
|
"name format full name": {
|
||||||
nameFormat: model.SHOW_FULLNAME,
|
nameFormat: model.SHOW_FULLNAME,
|
||||||
@@ -1317,12 +1317,12 @@ func TestPostNotificationGetSenderName(t *testing.T) {
|
|||||||
channel: &model.Channel{Type: model.CHANNEL_DIRECT},
|
channel: &model.Channel{Type: model.CHANNEL_DIRECT},
|
||||||
post: overriddenPost,
|
post: overriddenPost,
|
||||||
allowOverrides: true,
|
allowOverrides: true,
|
||||||
expected: sender.Username,
|
expected: "@" + sender.Username,
|
||||||
},
|
},
|
||||||
"overridden username, overrides disabled": {
|
"overridden username, overrides disabled": {
|
||||||
post: overriddenPost,
|
post: overriddenPost,
|
||||||
allowOverrides: false,
|
allowOverrides: false,
|
||||||
expected: sender.Username,
|
expected: "@" + sender.Username,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
|
|||||||
@@ -539,8 +539,8 @@ func (u *User) GetFullName() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) GetDisplayName(nameFormat string) string {
|
func (u *User) getDisplayName(baseName, nameFormat string) string {
|
||||||
displayName := u.Username
|
displayName := baseName
|
||||||
|
|
||||||
if nameFormat == SHOW_NICKNAME_FULLNAME {
|
if nameFormat == SHOW_NICKNAME_FULLNAME {
|
||||||
if len(u.Nickname) > 0 {
|
if len(u.Nickname) > 0 {
|
||||||
@@ -557,6 +557,18 @@ func (u *User) GetDisplayName(nameFormat string) string {
|
|||||||
return displayName
|
return displayName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *User) GetDisplayName(nameFormat string) string {
|
||||||
|
displayName := u.Username
|
||||||
|
|
||||||
|
return u.getDisplayName(displayName, nameFormat)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *User) GetDisplayNameWithPrefix(nameFormat, prefix string) string {
|
||||||
|
displayName := prefix + u.Username
|
||||||
|
|
||||||
|
return u.getDisplayName(displayName, nameFormat)
|
||||||
|
}
|
||||||
|
|
||||||
func (u *User) GetRoles() []string {
|
func (u *User) GetRoles() []string {
|
||||||
return strings.Fields(u.Roles)
|
return strings.Fields(u.Roles)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -252,6 +252,42 @@ func TestUserGetDisplayName(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUserGetDisplayNameWithPrefix(t *testing.T) {
|
||||||
|
user := User{Username: "username"}
|
||||||
|
|
||||||
|
if displayName := user.GetDisplayNameWithPrefix(SHOW_FULLNAME, "@"); displayName != "@username" {
|
||||||
|
t.Fatal("Display name should be username")
|
||||||
|
}
|
||||||
|
|
||||||
|
if displayName := user.GetDisplayNameWithPrefix(SHOW_NICKNAME_FULLNAME, "@"); displayName != "@username" {
|
||||||
|
t.Fatal("Display name should be username")
|
||||||
|
}
|
||||||
|
|
||||||
|
if displayName := user.GetDisplayNameWithPrefix(SHOW_USERNAME, "@"); displayName != "@username" {
|
||||||
|
t.Fatal("Display name should be username")
|
||||||
|
}
|
||||||
|
|
||||||
|
user.FirstName = "first"
|
||||||
|
user.LastName = "last"
|
||||||
|
|
||||||
|
if displayName := user.GetDisplayNameWithPrefix(SHOW_FULLNAME, "@"); displayName != "first last" {
|
||||||
|
t.Fatal("Display name should be full name")
|
||||||
|
}
|
||||||
|
|
||||||
|
if displayName := user.GetDisplayNameWithPrefix(SHOW_NICKNAME_FULLNAME, "@"); displayName != "first last" {
|
||||||
|
t.Fatal("Display name should be full name since there is no nickname")
|
||||||
|
}
|
||||||
|
|
||||||
|
if displayName := user.GetDisplayNameWithPrefix(SHOW_USERNAME, "@"); displayName != "@username" {
|
||||||
|
t.Fatal("Display name should be username")
|
||||||
|
}
|
||||||
|
|
||||||
|
user.Nickname = "nickname"
|
||||||
|
if displayName := user.GetDisplayNameWithPrefix(SHOW_NICKNAME_FULLNAME, "@"); displayName != "nickname" {
|
||||||
|
t.Fatal("Display name should be nickname")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var usernames = []struct {
|
var usernames = []struct {
|
||||||
value string
|
value string
|
||||||
expected bool
|
expected bool
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user