From 6c2a5555b85bd15106df5f4f631bb6e945a187b8 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Mon, 17 Sep 2018 14:50:12 -0400 Subject: MM-11700 Clean up handling of user display names for notifications (#9343) * MM-11700 Clean up handling of user display names for notifications --- app/notification.go | 96 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 39 deletions(-) (limited to 'app/notification.go') diff --git a/app/notification.go b/app/notification.go index 92b69039b..8092ba436 100644 --- a/app/notification.go +++ b/app/notification.go @@ -157,32 +157,11 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod updateMentionChans = append(updateMentionChans, a.Srv.Store.Channel().IncrementMentionCount(post.ChannelId, id)) } - var senderUsername string - senderName := "" - channelName := "" - if post.IsSystemMessage() { - senderName = utils.T("system.message.name") - } else { - if value, ok := post.Props["override_username"]; ok && post.Props["from_webhook"] == "true" && channel.Type != model.CHANNEL_DIRECT { - senderName = value.(string) - senderUsername = value.(string) - } else { - senderName = sender.Username - senderUsername = sender.Username - } - } - - if channel.Type == model.CHANNEL_GROUP { - userList := []*model.User{} - for _, u := range profileMap { - if u.Id != sender.Id { - userList = append(userList, u) - } - } - userList = append(userList, sender) - channelName = model.GetGroupDisplayNameFromUsers(userList, false) - } else { - channelName = channel.DisplayName + notification := &postNotification{ + post: post, + channel: channel, + profileMap: profileMap, + sender: sender, } if a.Config().EmailSettings.SendEmailNotifications { @@ -227,7 +206,7 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod autoResponderRelated := status.Status == model.STATUS_OUT_OF_OFFICE || post.Type == model.POST_AUTO_RESPONDER if userAllowsEmails && status.Status != model.STATUS_ONLINE && profileMap[id].DeleteAt == 0 && !autoResponderRelated { - a.sendNotificationEmail(post, profileMap[id], channel, team, channelName, senderName, sender) + a.sendNotificationEmail(notification, profileMap[id], team) } } } @@ -310,12 +289,8 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod } a.sendPushNotification( - post, + notification, profileMap[id], - channel, - channelName, - sender, - senderName, mentionedUserIds[id], (channelNotification || hereNotification || allNotification), replyToThreadType, @@ -337,12 +312,8 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod if ShouldSendPushNotification(profileMap[id], channelMemberNotifyPropsMap[id], false, status, post) { a.sendPushNotification( - post, + notification, profileMap[id], - channel, - channelName, - sender, - senderName, false, false, "", @@ -355,9 +326,9 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POSTED, "", post.ChannelId, "", nil) message.Add("post", a.PostWithProxyAddedToImageURLs(post).ToJson()) message.Add("channel_type", channel.Type) - message.Add("channel_display_name", channelName) + message.Add("channel_display_name", notification.GetChannelName(model.SHOW_USERNAME, "")) message.Add("channel_name", channel.Name) - message.Add("sender_name", senderUsername) + message.Add("sender_name", notification.GetSenderName(model.SHOW_USERNAME, a.Config().ServiceSettings.EnablePostUsernameOverride)) message.Add("team_id", team.Id) if len(post.FileIds) != 0 && fchan != nil { @@ -628,3 +599,50 @@ func (a *App) GetMentionKeywordsInChannel(profiles map[string]*model.User, lookF return keywords } + +// Represents either an email or push notification and contains the fields required to send it to any user. +type postNotification struct { + channel *model.Channel + post *model.Post + profileMap map[string]*model.User + sender *model.User +} + +// Returns the name of the channel for this notification. For direct messages, this is the sender's name +// preceeded by an at sign. For group messages, this is a comma-separated list of the members of the +// channel, with an option to exclude the recipient of the message from that list. +func (n *postNotification) GetChannelName(userNameFormat string, excludeId string) string { + switch n.channel.Type { + case model.CHANNEL_DIRECT: + return fmt.Sprintf("@%s", n.sender.GetDisplayName(userNameFormat)) + case model.CHANNEL_GROUP: + names := []string{} + for _, user := range n.profileMap { + if user.Id != excludeId { + names = append(names, user.GetDisplayName(userNameFormat)) + } + } + + sort.Strings(names) + + return strings.Join(names, ", ") + default: + return n.channel.DisplayName + } +} + +// Returns the name of the sender of this notification, accounting for things like system messages +// and whether or not the username has been overridden by an integration. +func (n *postNotification) GetSenderName(userNameFormat string, overridesAllowed bool) string { + if n.post.IsSystemMessage() { + return utils.T("system.message.name") + } + + if overridesAllowed && n.channel.Type != model.CHANNEL_DIRECT { + if value, ok := n.post.Props["override_username"]; ok && n.post.Props["from_webhook"] == "true" { + return value.(string) + } + } + + return n.sender.GetDisplayName(userNameFormat) +} -- cgit v1.2.3-1-g7c22