From e8d7701b60e8285568ed01c2fe155ed02c388136 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Fri, 20 Jan 2017 09:43:14 -0500 Subject: PLT-5284 Fix webhook notifications for channel creator is not in (#5119) * Fix webhook notifications for channel creator is not in * Fix unit test --- app/notification.go | 22 ++++++++++------------ app/notification_test.go | 2 +- app/post.go | 8 ++++---- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app/notification.go b/app/notification.go index 060810017..ec78c416b 100644 --- a/app/notification.go +++ b/app/notification.go @@ -24,7 +24,7 @@ import ( "github.com/nicksnyder/go-i18n/i18n" ) -func SendNotifications(post *model.Post, team *model.Team, channel *model.Channel) ([]string, *model.AppError) { +func SendNotifications(post *model.Post, team *model.Team, channel *model.Channel, sender *model.User) ([]string, *model.AppError) { pchan := Srv.Store.User().GetProfilesInChannel(channel.Id, -1, -1, true) fchan := Srv.Store.FileInfo().GetForPost(post.Id) @@ -36,7 +36,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe } // If the user who made the post isn't in the channel, don't send a notification - if _, ok := profileMap[post.UserId]; !ok { + if _, ok := profileMap[post.UserId]; !ok && post.Props["from_webhook"] != "true" { l4g.Debug(utils.T("api.post.send_notifications.user_id.debug"), post.Id, channel.Id, post.UserId) return []string{}, nil } @@ -90,7 +90,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe if len(potentialOtherMentions) > 0 { if result := <-Srv.Store.User().GetProfilesByUsernames(potentialOtherMentions, team.Id); result.Err == nil { outOfChannelMentions := result.Data.(map[string]*model.User) - go sendOutOfChannelMentions(post, team.Id, outOfChannelMentions) + go sendOutOfChannelMentions(sender, post, team.Id, outOfChannelMentions) } } @@ -110,25 +110,23 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe updateMentionChans = append(updateMentionChans, Srv.Store.Channel().IncrementMentionCount(post.ChannelId, id)) } - var sender *model.User senderName := make(map[string]string) for _, id := range mentionedUsersList { senderName[id] = "" if post.IsSystemMessage() { senderName[id] = utils.T("system.message.name") - } else if profile, ok := profileMap[post.UserId]; ok { + } else { if value, ok := post.Props["override_username"]; ok && post.Props["from_webhook"] == "true" { senderName[id] = value.(string) } else { // Get the Display name preference from the receiver if result := <-Srv.Store.Preference().Get(id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, "name_format"); result.Err != nil { // Show default sender's name if user doesn't set display settings. - senderName[id] = profile.Username + senderName[id] = sender.Username } else { - senderName[id] = profile.GetDisplayNameForPreference(result.Data.(model.Preference).Value) + senderName[id] = sender.GetDisplayNameForPreference(result.Data.(model.Preference).Value) } } - sender = profile } } @@ -136,7 +134,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe if value, ok := post.Props["override_username"]; ok && post.Props["from_webhook"] == "true" { senderUsername = value.(string) } else { - senderUsername = profileMap[post.UserId].Username + senderUsername = sender.Username } if utils.Cfg.EmailSettings.SendEmailNotifications { @@ -161,7 +159,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe } } - T := utils.GetUserTranslations(profileMap[post.UserId].Locale) + T := utils.GetUserTranslations(sender.Locale) // If the channel has more than 1K users then @here is disabled if hereNotification && int64(len(profileMap)) > *utils.Cfg.TeamSettings.MaxNotificationsPerChannel { @@ -568,7 +566,7 @@ func getMobileAppSessions(userId string) ([]*model.Session, *model.AppError) { } } -func sendOutOfChannelMentions(post *model.Post, teamId string, profiles map[string]*model.User) *model.AppError { +func sendOutOfChannelMentions(sender *model.User, post *model.Post, teamId string, profiles map[string]*model.User) *model.AppError { if len(profiles) == 0 { return nil } @@ -579,7 +577,7 @@ func sendOutOfChannelMentions(post *model.Post, teamId string, profiles map[stri } sort.Strings(usernames) - T := utils.GetUserTranslations(profiles[post.UserId].Locale) + T := utils.GetUserTranslations(sender.Locale) var message string if len(usernames) == 1 { diff --git a/app/notification_test.go b/app/notification_test.go index 8554d0af6..10eb09247 100644 --- a/app/notification_test.go +++ b/app/notification_test.go @@ -24,7 +24,7 @@ func TestSendNotifications(t *testing.T) { t.Fatal(postErr) } - mentions, err := SendNotifications(post1, th.BasicTeam, th.BasicChannel) + mentions, err := SendNotifications(post1, th.BasicTeam, th.BasicChannel, th.BasicUser) if err != nil { t.Fatal(err) } else if mentions == nil { diff --git a/app/post.go b/app/post.go index 7eebe905f..f37ce8ad3 100644 --- a/app/post.go +++ b/app/post.go @@ -99,10 +99,6 @@ func handlePostEvents(post *model.Post, teamId string, triggerWebhooks bool) *mo channel = result.Data.(*model.Channel) } - if _, err := SendNotifications(post, team, channel); err != nil { - return err - } - var user *model.User if result := <-uchan; result.Err != nil { return result.Err @@ -110,6 +106,10 @@ func handlePostEvents(post *model.Post, teamId string, triggerWebhooks bool) *mo user = result.Data.(*model.User) } + if _, err := SendNotifications(post, team, channel, user); err != nil { + return err + } + if triggerWebhooks { go func() { if err := handleWebhookEvents(post, team, channel, user); err != nil { -- cgit v1.2.3-1-g7c22