[MM-22622] Add Ephemeral response when using mentions without permissions (#13902)

* MM-22282 Add Ephemeral response when using mentions without permission and add new prop to disable mention highlights on client

* MM-22622 Make test name test actually what it does and fix comment style

* MM-22622 Check ephemeral post created when post create with mentions on API

* MM-22622 More tests for App>CreatePost

* MM-22622 Make DisableMentionHighlights more concise and rename ephemeral post

* MM-22622 Dont send ephemeral message for system message created by user

* Trigger build
Этот коммит содержится в:
Farhan Munshi
2020-03-03 05:22:49 -05:00
коммит произвёл GitHub
родитель 4c7fee7ac8
Коммит aec606500f
7 изменённых файлов: 366 добавлений и 1 удалений

Просмотреть файл

@@ -7,6 +7,7 @@ import (
"encoding/json"
"io"
"net/http"
"regexp"
"sort"
"strings"
"unicode/utf8"
@@ -58,6 +59,8 @@ const (
POST_PROPS_DELETE_BY = "deleteBy"
POST_PROPS_OVERRIDE_ICON_URL = "override_icon_url"
POST_PROPS_OVERRIDE_ICON_EMOJI = "override_icon_emoji"
POST_PROPS_MENTION_HIGHLIGHT_DISABLED = "mentionHighlightDisabled"
)
type Post struct {
@@ -432,6 +435,34 @@ func (o *Post) ChannelMentions() []string {
return ChannelMentions(o.Message)
}
// DisableMentionHighlights disables a posts mention highlighting and returns the first channel mention that was present in the message.
func (o *Post) DisableMentionHighlights() string {
mention, hasMentions := findAtChannelMention(o.Message)
if hasMentions {
o.AddProp(POST_PROPS_MENTION_HIGHLIGHT_DISABLED, true)
}
return mention
}
// DisableMentionHighlights disables mention highlighting for a post patch if required.
func (o *PostPatch) DisableMentionHighlights() {
if _, hasMentions := findAtChannelMention(*o.Message); hasMentions {
if o.Props == nil {
o.Props = &StringInterface{}
}
(*o.Props)[POST_PROPS_MENTION_HIGHLIGHT_DISABLED] = true
}
}
func findAtChannelMention(message string) (mention string, found bool) {
re := regexp.MustCompile(`(?i)\B@(channel|all|here)\b`)
matched := re.FindStringSubmatch(message)
if found = (len(matched) > 0); found {
mention = strings.ToLower(matched[0])
}
return
}
func (o *Post) Attachments() []*SlackAttachment {
if attachments, ok := o.Props["attachments"].([]*SlackAttachment); ok {
return attachments

Просмотреть файл

@@ -502,3 +502,173 @@ func BenchmarkRewriteImageURLs(b *testing.B) {
})
}
}
func Test_findAtChannelMention(t *testing.T) {
testCases := []struct {
Name string
Message string
Mention string
Found bool
}{
{
"Returns mention for @here wrapped by spaces",
"hi guys @here wrapped by spaces",
"@here",
true,
},
{
"Returns mention for @all wrapped by spaces",
"hi guys @all wrapped by spaces",
"@all",
true,
},
{
"Returns mention for @channel wrapped by spaces",
"hi guys @channel wrapped by spaces",
"@channel",
true,
},
{
"Returns mention for @here wrapped by dash",
"-@here-",
"@here",
true,
},
{
"Returns mention for @all wrapped by back tick",
"`@all`",
"@all",
true,
},
{
"Returns mention for @channel wrapped by tags",
"<@channel>",
"@channel",
true,
},
{
"Returns mention for @channel wrapped by asterisks",
"*@channel*",
"@channel",
true,
},
{
"Does not return mention when prefixed by letters",
"hi@channel",
"",
false,
},
{
"Does not return mention when suffixed by letters",
"hi @channelanotherword",
"",
false,
},
{
"Returns mention when prefixed by word ending in special character",
"hi-@channel",
"@channel",
true,
},
{
"Returns mention when suffixed by word starting in special character",
"hi @channel-guys",
"@channel",
true,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
mention, found := findAtChannelMention(tc.Message)
assert.Equal(t, tc.Mention, mention)
assert.Equal(t, tc.Found, found)
})
}
}
func TestPostDisableMentionHighlights(t *testing.T) {
post := &Post{}
testCases := []struct {
Name string
Message string
ExpectedProps StringInterface
ExpectedMention string
}{
{
"Does nothing for post with no mentions",
"Sample message with no mentions",
StringInterface(nil),
"",
},
{
"Sets POST_PROPS_MENTION_HIGHLIGHT_DISABLED and returns mention",
"Sample message with @here",
StringInterface{POST_PROPS_MENTION_HIGHLIGHT_DISABLED: true},
"@here",
},
{
"Sets POST_PROPS_MENTION_HIGHLIGHT_DISABLED and returns mention",
"Sample message with @channel",
StringInterface{POST_PROPS_MENTION_HIGHLIGHT_DISABLED: true},
"@channel",
},
{
"Sets POST_PROPS_MENTION_HIGHLIGHT_DISABLED and returns mention",
"Sample message with @all",
StringInterface{POST_PROPS_MENTION_HIGHLIGHT_DISABLED: true},
"@all",
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
post.Message = tc.Message
mention := post.DisableMentionHighlights()
assert.Equal(t, tc.ExpectedMention, mention)
assert.Equal(t, tc.ExpectedProps, post.Props)
post.Props = StringInterface{}
})
}
}
func TestPostPatchDisableMentionHighlights(t *testing.T) {
patch := &PostPatch{}
testCases := []struct {
Name string
Message string
ExpectedProps *StringInterface
}{
{
"Does nothing for post with no mentions",
"Sample message with no mentions",
nil,
},
{
"Sets POST_PROPS_MENTION_HIGHLIGHT_DISABLED",
"Sample message with @here",
&StringInterface{POST_PROPS_MENTION_HIGHLIGHT_DISABLED: true},
},
{
"Sets POST_PROPS_MENTION_HIGHLIGHT_DISABLED",
"Sample message with @channel",
&StringInterface{POST_PROPS_MENTION_HIGHLIGHT_DISABLED: true},
},
{
"Sets POST_PROPS_MENTION_HIGHLIGHT_DISABLED",
"Sample message with @all",
&StringInterface{POST_PROPS_MENTION_HIGHLIGHT_DISABLED: true},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
patch.Message = &tc.Message
patch.DisableMentionHighlights()
if tc.ExpectedProps == nil {
assert.Nil(t, patch.Props)
} else {
assert.Equal(t, *tc.ExpectedProps, *patch.Props)
}
patch.Props = nil
})
}
}