From 24e0d6f00d88587159142348deb17c956c7d40bd Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Fri, 23 Aug 2019 09:46:43 -0400 Subject: [PATCH] Add @ prefix for sender username in push notifications (#11934) * Add @ prefix for sender username in push notifications * de-duplicating code --- app/notification.go | 4 ++-- app/notification_test.go | 12 ++++++------ model/user.go | 16 ++++++++++++++-- model/user_test.go | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/app/notification.go b/app/notification.go index 8efa292156..1d4a2ccb49 100644 --- a/app/notification.go +++ b/app/notification.go @@ -640,7 +640,7 @@ type postNotification struct { func (n *postNotification) GetChannelName(userNameFormat string, excludeId string) string { switch n.channel.Type { case model.CHANNEL_DIRECT: - return n.sender.GetDisplayName(userNameFormat) + return n.sender.GetDisplayNameWithPrefix(userNameFormat, "@") case model.CHANNEL_GROUP: names := []string{} 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 diff --git a/app/notification_test.go b/app/notification_test.go index c07778896e..744b9738d4 100644 --- a/app/notification_test.go +++ b/app/notification_test.go @@ -1207,12 +1207,12 @@ func TestPostNotificationGetChannelName(t *testing.T) { }, "direct channel, unspecified": { channel: &model.Channel{Type: model.CHANNEL_DIRECT}, - expected: "sender", + expected: "@sender", }, "direct channel, username": { channel: &model.Channel{Type: model.CHANNEL_DIRECT}, nameFormat: model.SHOW_USERNAME, - expected: "sender", + expected: "@sender", }, "direct channel, full name": { channel: &model.Channel{Type: model.CHANNEL_DIRECT}, @@ -1290,11 +1290,11 @@ func TestPostNotificationGetSenderName(t *testing.T) { expected string }{ "name format unspecified": { - expected: sender.Username, + expected: "@" + sender.Username, }, "name format username": { nameFormat: model.SHOW_USERNAME, - expected: sender.Username, + expected: "@" + sender.Username, }, "name format full name": { nameFormat: model.SHOW_FULLNAME, @@ -1317,12 +1317,12 @@ func TestPostNotificationGetSenderName(t *testing.T) { channel: &model.Channel{Type: model.CHANNEL_DIRECT}, post: overriddenPost, allowOverrides: true, - expected: sender.Username, + expected: "@" + sender.Username, }, "overridden username, overrides disabled": { post: overriddenPost, allowOverrides: false, - expected: sender.Username, + expected: "@" + sender.Username, }, } { t.Run(name, func(t *testing.T) { diff --git a/model/user.go b/model/user.go index 4d71c13587..db0621e04e 100644 --- a/model/user.go +++ b/model/user.go @@ -539,8 +539,8 @@ func (u *User) GetFullName() string { } } -func (u *User) GetDisplayName(nameFormat string) string { - displayName := u.Username +func (u *User) getDisplayName(baseName, nameFormat string) string { + displayName := baseName if nameFormat == SHOW_NICKNAME_FULLNAME { if len(u.Nickname) > 0 { @@ -557,6 +557,18 @@ func (u *User) GetDisplayName(nameFormat string) string { 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 { return strings.Fields(u.Roles) } diff --git a/model/user_test.go b/model/user_test.go index 0211283dcb..95cb03a493 100644 --- a/model/user_test.go +++ b/model/user_test.go @@ -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 { value string expected bool