Commit 06abe98b authored by Pradeep Murugesan's avatar Pradeep Murugesan Committed by Harrison Healey

GH-8964 enable notification for text and preText fields in Attachments (#9069)

parent 5b63e87a
......@@ -94,7 +94,7 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod
} else {
keywords := a.GetMentionKeywordsInChannel(profileMap, post.Type != model.POST_HEADER_CHANGE && post.Type != model.POST_PURPOSE_CHANGE)
m := GetExplicitMentions(post.Message, keywords)
m := GetExplicitMentions(post, keywords)
// Add an implicit mention when a user is added to a channel
// even if the user has set 'username mentions' to false in account settings.
......@@ -961,7 +961,7 @@ type ExplicitMentions struct {
// Given a message and a map mapping mention keywords to the users who use them, returns a map of mentioned
// users and a slice of potential mention users not in the channel and whether or not @here was mentioned.
func GetExplicitMentions(message string, keywords map[string][]string) *ExplicitMentions {
func GetExplicitMentions(post *model.Post, keywords map[string][]string) *ExplicitMentions {
ret := &ExplicitMentions{
MentionedUserIds: make(map[string]bool),
}
......@@ -1053,21 +1053,42 @@ func GetExplicitMentions(message string, keywords map[string][]string) *Explicit
}
buf := ""
markdown.Inspect(message, func(node interface{}) bool {
text, ok := node.(*markdown.Text)
if !ok {
processText(buf)
buf = ""
return true
}
buf += text.Text
return false
})
mentionsEnabledFields := GetMentionsEnabledFields(post)
for _, message := range mentionsEnabledFields {
markdown.Inspect(message, func(node interface{}) bool {
text, ok := node.(*markdown.Text)
if !ok {
processText(buf)
buf = ""
return true
}
buf += text.Text
return false
})
}
processText(buf)
return ret
}
// Given a post returns the values of the fields in which mentions are possible.
// post.message, preText and text in the attachment are enabled.
func GetMentionsEnabledFields(post *model.Post) model.StringArray {
ret := []string{}
ret = append(ret, post.Message)
for _, attachment := range post.Attachments() {
if len(attachment.Pretext) != 0 {
ret = append(ret, attachment.Pretext)
}
if len(attachment.Text) != 0 {
ret = append(ret, attachment.Text)
}
}
return ret
}
// Given a map of user IDs to profiles, returns a list of mention
// keywords for all users in the channel.
func (a *App) GetMentionKeywordsInChannel(profiles map[string]*model.User, lookForSpecialMentions bool) map[string][]string {
......
......@@ -88,6 +88,7 @@ func TestGetExplicitMentions(t *testing.T) {
for name, tc := range map[string]struct {
Message string
Attachments []*model.SlackAttachment
Keywords map[string][]string
Expected *ExplicitMentions
}{
......@@ -508,9 +509,32 @@ func TestGetExplicitMentions(t *testing.T) {
},
},
},
"should include the mentions from attachment text and preText": {
Message: "this is an message for @user1",
Attachments: []*model.SlackAttachment{
{
Text: "this is a message For @user2",
Pretext: "this is a message for @here",
},
},
Keywords: map[string][]string{"@user1": {id1}, "@user2": {id2}},
Expected: &ExplicitMentions{
MentionedUserIds: map[string]bool{
id1: true,
id2: true,
},
HereMentioned: true,
},
},
} {
t.Run(name, func(t *testing.T) {
m := GetExplicitMentions(tc.Message, tc.Keywords)
post := &model.Post{Message: tc.Message, Props: model.StringInterface{
"attachments": tc.Attachments,
},
}
m := GetExplicitMentions(post, tc.Keywords)
if tc.Expected.MentionedUserIds == nil {
tc.Expected.MentionedUserIds = make(map[string]bool)
}
......@@ -564,7 +588,8 @@ func TestGetExplicitMentionsAtHere(t *testing.T) {
}
for message, shouldMention := range cases {
if m := GetExplicitMentions(message, nil); m.HereMentioned && !shouldMention {
post := &model.Post{Message: message}
if m := GetExplicitMentions(post, nil); m.HereMentioned && !shouldMention {
t.Fatalf("shouldn't have mentioned @here with \"%v\"", message)
} else if !m.HereMentioned && shouldMention {
t.Fatalf("should've mentioned @here with \"%v\"", message)
......@@ -573,7 +598,7 @@ func TestGetExplicitMentionsAtHere(t *testing.T) {
// mentioning @here and someone
id := model.NewId()
if m := GetExplicitMentions("@here @user @potential", map[string][]string{"@user": {id}}); !m.HereMentioned {
if m := GetExplicitMentions(&model.Post{Message: "@here @user @potential"}, map[string][]string{"@user": {id}}); !m.HereMentioned {
t.Fatal("should've mentioned @here with \"@here @user\"")
} else if len(m.MentionedUserIds) != 1 || !m.MentionedUserIds[id] {
t.Fatal("should've mentioned @user with \"@here @user\"")
......@@ -1787,3 +1812,37 @@ func TestGetPushNotificationMessage(t *testing.T) {
})
}
}
func TestGetMentionsEnabledFields(t *testing.T) {
attachmentWithTextAndPreText := model.SlackAttachment{
Text: "@here with mentions",
Pretext: "@Channel some comment for the channel",
}
attachmentWithOutPreText := model.SlackAttachment{
Text: "some text",
}
attachments := []*model.SlackAttachment{
&attachmentWithTextAndPreText,
&attachmentWithOutPreText,
}
post := &model.Post{
Message: "This is the message",
Props: model.StringInterface{
"attachments": attachments,
},
}
expectedFields := []string{
"This is the message",
"@Channel some comment for the channel",
"@here with mentions",
"some text"}
mentionEnabledFields := GetMentionsEnabledFields(post)
assert.EqualValues(t, 4, len(mentionEnabledFields))
assert.EqualValues(t, expectedFields, mentionEnabledFields)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment