mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
PLT-5284 Fix webhook notifications for channel creator is not in (#5119)
* Fix webhook notifications for channel creator is not in * Fix unit test
This commit is contained in:
committed by
Christopher Speller
parent
6aec2b6c55
commit
e8d7701b60
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user