Ongoing changes to notification.go (#11047)
* Change the functions that are used only by `notification.go` from public to private * Split getExplicitMentions() into smaller functions. - addMentionedUsers - checkForMention - isKeywordMultibyte - processText along with their tests. * Merge with master Fix wording on isKeywordMultibyte() function comment
Этот коммит содержится в:
коммит произвёл
Jesús Espino
родитель
544055bdb3
Коммит
bbdd6927de
@@ -89,9 +89,9 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
keywords := a.GetMentionKeywordsInChannel(profileMap, post.Type != model.POST_HEADER_CHANGE && post.Type != model.POST_PURPOSE_CHANGE, channelMemberNotifyPropsMap)
|
keywords := a.getMentionKeywordsInChannel(profileMap, post.Type != model.POST_HEADER_CHANGE && post.Type != model.POST_PURPOSE_CHANGE, channelMemberNotifyPropsMap)
|
||||||
|
|
||||||
m := GetExplicitMentions(post, keywords)
|
m := getExplicitMentions(post, keywords)
|
||||||
|
|
||||||
// Add an implicit mention when a user is added to a channel
|
// 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.
|
// even if the user has set 'username mentions' to false in account settings.
|
||||||
@@ -504,123 +504,18 @@ type ExplicitMentions struct {
|
|||||||
|
|
||||||
// Given a message and a map mapping mention keywords to the users who use them, returns a map of mentioned
|
// 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.
|
// users and a slice of potential mention users not in the channel and whether or not @here was mentioned.
|
||||||
func GetExplicitMentions(post *model.Post, keywords map[string][]string) *ExplicitMentions {
|
func getExplicitMentions(post *model.Post, keywords map[string][]string) *ExplicitMentions {
|
||||||
ret := &ExplicitMentions{
|
ret := &ExplicitMentions{
|
||||||
MentionedUserIds: make(map[string]bool),
|
MentionedUserIds: make(map[string]bool),
|
||||||
}
|
}
|
||||||
systemMentions := map[string]bool{"@here": true, "@channel": true, "@all": true}
|
|
||||||
|
|
||||||
addMentionedUsers := func(ids []string) {
|
|
||||||
for _, id := range ids {
|
|
||||||
ret.MentionedUserIds[id] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
checkForMention := func(word string) bool {
|
|
||||||
isMention := false
|
|
||||||
|
|
||||||
if strings.ToLower(word) == "@here" {
|
|
||||||
ret.HereMentioned = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.ToLower(word) == "@channel" {
|
|
||||||
ret.ChannelMentioned = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.ToLower(word) == "@all" {
|
|
||||||
ret.AllMentioned = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Non-case-sensitive check for regular keys
|
|
||||||
if ids, match := keywords[strings.ToLower(word)]; match {
|
|
||||||
addMentionedUsers(ids)
|
|
||||||
isMention = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Case-sensitive check for first name
|
|
||||||
if ids, match := keywords[word]; match {
|
|
||||||
addMentionedUsers(ids)
|
|
||||||
isMention = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return isMention
|
|
||||||
}
|
|
||||||
|
|
||||||
var multibyteKeywords []string
|
|
||||||
for keyword := range keywords {
|
|
||||||
if len(keyword) != utf8.RuneCountInString(keyword) {
|
|
||||||
multibyteKeywords = append(multibyteKeywords, keyword)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
processText := func(text string) {
|
|
||||||
for _, word := range strings.FieldsFunc(text, func(c rune) bool {
|
|
||||||
// Split on any whitespace or punctuation that can't be part of an at mention or emoji pattern
|
|
||||||
return !(c == ':' || c == '.' || c == '-' || c == '_' || c == '@' || unicode.IsLetter(c) || unicode.IsNumber(c))
|
|
||||||
}) {
|
|
||||||
// skip word with format ':word:' with an assumption that it is an emoji format only
|
|
||||||
if word[0] == ':' && word[len(word)-1] == ':' {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
word = strings.TrimLeft(word, ":.-_")
|
|
||||||
|
|
||||||
if checkForMention(word) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
foundWithoutSuffix := false
|
|
||||||
wordWithoutSuffix := word
|
|
||||||
for len(wordWithoutSuffix) > 0 && strings.LastIndexAny(wordWithoutSuffix, ".-:_") == (len(wordWithoutSuffix)-1) {
|
|
||||||
wordWithoutSuffix = wordWithoutSuffix[0 : len(wordWithoutSuffix)-1]
|
|
||||||
|
|
||||||
if checkForMention(wordWithoutSuffix) {
|
|
||||||
foundWithoutSuffix = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if foundWithoutSuffix {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := systemMentions[word]; !ok && strings.HasPrefix(word, "@") {
|
|
||||||
ret.OtherPotentialMentions = append(ret.OtherPotentialMentions, word[1:])
|
|
||||||
} else if strings.ContainsAny(word, ".-:") {
|
|
||||||
// This word contains a character that may be the end of a sentence, so split further
|
|
||||||
splitWords := strings.FieldsFunc(word, func(c rune) bool {
|
|
||||||
return c == '.' || c == '-' || c == ':'
|
|
||||||
})
|
|
||||||
|
|
||||||
for _, splitWord := range splitWords {
|
|
||||||
if checkForMention(splitWord) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, ok := systemMentions[splitWord]; !ok && strings.HasPrefix(splitWord, "@") {
|
|
||||||
ret.OtherPotentialMentions = append(ret.OtherPotentialMentions, splitWord[1:])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If word contains a multibyte character, check if it contains a multibyte keyword
|
|
||||||
if len(word) != utf8.RuneCountInString(word) {
|
|
||||||
for _, key := range multibyteKeywords {
|
|
||||||
if strings.Contains(word, key) {
|
|
||||||
if ids, match := keywords[key]; match {
|
|
||||||
addMentionedUsers(ids)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := ""
|
buf := ""
|
||||||
mentionsEnabledFields := GetMentionsEnabledFields(post)
|
mentionsEnabledFields := getMentionsEnabledFields(post)
|
||||||
for _, message := range mentionsEnabledFields {
|
for _, message := range mentionsEnabledFields {
|
||||||
markdown.Inspect(message, func(node interface{}) bool {
|
markdown.Inspect(message, func(node interface{}) bool {
|
||||||
text, ok := node.(*markdown.Text)
|
text, ok := node.(*markdown.Text)
|
||||||
if !ok {
|
if !ok {
|
||||||
processText(buf)
|
ret.processText(buf, keywords)
|
||||||
buf = ""
|
buf = ""
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -628,14 +523,14 @@ func GetExplicitMentions(post *model.Post, keywords map[string][]string) *Explic
|
|||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
processText(buf)
|
ret.processText(buf, keywords)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given a post returns the values of the fields in which mentions are possible.
|
// Given a post returns the values of the fields in which mentions are possible.
|
||||||
// post.message, preText and text in the attachment are enabled.
|
// post.message, preText and text in the attachment are enabled.
|
||||||
func GetMentionsEnabledFields(post *model.Post) model.StringArray {
|
func getMentionsEnabledFields(post *model.Post) model.StringArray {
|
||||||
ret := []string{}
|
ret := []string{}
|
||||||
|
|
||||||
ret = append(ret, post.Message)
|
ret = append(ret, post.Message)
|
||||||
@@ -653,7 +548,7 @@ func GetMentionsEnabledFields(post *model.Post) model.StringArray {
|
|||||||
|
|
||||||
// Given a map of user IDs to profiles, returns a list of mention
|
// Given a map of user IDs to profiles, returns a list of mention
|
||||||
// keywords for all users in the channel.
|
// keywords for all users in the channel.
|
||||||
func (a *App) GetMentionKeywordsInChannel(profiles map[string]*model.User, lookForSpecialMentions bool, channelMemberNotifyPropsMap map[string]model.StringMap) map[string][]string {
|
func (a *App) getMentionKeywordsInChannel(profiles map[string]*model.User, lookForSpecialMentions bool, channelMemberNotifyPropsMap map[string]model.StringMap) map[string][]string {
|
||||||
keywords := make(map[string][]string)
|
keywords := make(map[string][]string)
|
||||||
|
|
||||||
for id, profile := range profiles {
|
for id, profile := range profiles {
|
||||||
@@ -745,3 +640,115 @@ func (n *postNotification) GetSenderName(userNameFormat string, overridesAllowed
|
|||||||
|
|
||||||
return n.sender.GetDisplayName(userNameFormat)
|
return n.sender.GetDisplayName(userNameFormat)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// addMentionedUsers will add the mentioned user id in the struct's list for mentioned users
|
||||||
|
func (e *ExplicitMentions) addMentionedUsers(ids []string) {
|
||||||
|
for _, id := range ids {
|
||||||
|
e.MentionedUserIds[id] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkForMention checks if there is a mention to a specific user or to the keywords here / channel / all
|
||||||
|
func (e *ExplicitMentions) checkForMention(word string, keywords map[string][]string) bool {
|
||||||
|
isMention := false
|
||||||
|
|
||||||
|
switch strings.ToLower(word) {
|
||||||
|
case "@here":
|
||||||
|
e.HereMentioned = true
|
||||||
|
case "@channel":
|
||||||
|
e.ChannelMentioned = true
|
||||||
|
case "@all":
|
||||||
|
e.AllMentioned = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if ids, match := keywords[strings.ToLower(word)]; match {
|
||||||
|
e.addMentionedUsers(ids)
|
||||||
|
isMention = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case-sensitive check for first name
|
||||||
|
if ids, match := keywords[word]; match {
|
||||||
|
e.addMentionedUsers(ids)
|
||||||
|
isMention = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return isMention
|
||||||
|
}
|
||||||
|
|
||||||
|
// isKeywordMultibyte checks if a word containing a multibyte character contains a multibyte keyword
|
||||||
|
func isKeywordMultibyte(keywords map[string][]string, word string) ([]string, bool) {
|
||||||
|
ids := []string{}
|
||||||
|
match := false
|
||||||
|
var multibyteKeywords []string
|
||||||
|
for keyword := range keywords {
|
||||||
|
if len(keyword) != utf8.RuneCountInString(keyword) {
|
||||||
|
multibyteKeywords = append(multibyteKeywords, keyword)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(word) != utf8.RuneCountInString(word) {
|
||||||
|
for _, key := range multibyteKeywords {
|
||||||
|
if strings.Contains(word, key) {
|
||||||
|
ids, match = keywords[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ids, match
|
||||||
|
}
|
||||||
|
|
||||||
|
// Processes text to filter mentioned users and other potential mentions
|
||||||
|
func (e *ExplicitMentions) processText(text string, keywords map[string][]string) {
|
||||||
|
systemMentions := map[string]bool{"@here": true, "@channel": true, "@all": true}
|
||||||
|
|
||||||
|
for _, word := range strings.FieldsFunc(text, func(c rune) bool {
|
||||||
|
// Split on any whitespace or punctuation that can't be part of an at mention or emoji pattern
|
||||||
|
return !(c == ':' || c == '.' || c == '-' || c == '_' || c == '@' || unicode.IsLetter(c) || unicode.IsNumber(c))
|
||||||
|
}) {
|
||||||
|
// skip word with format ':word:' with an assumption that it is an emoji format only
|
||||||
|
if word[0] == ':' && word[len(word)-1] == ':' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
word = strings.TrimLeft(word, ":.-_")
|
||||||
|
|
||||||
|
if e.checkForMention(word, keywords) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
foundWithoutSuffix := false
|
||||||
|
wordWithoutSuffix := word
|
||||||
|
for len(wordWithoutSuffix) > 0 && strings.LastIndexAny(wordWithoutSuffix, ".-:_") == (len(wordWithoutSuffix)-1) {
|
||||||
|
wordWithoutSuffix = wordWithoutSuffix[0 : len(wordWithoutSuffix)-1]
|
||||||
|
|
||||||
|
if e.checkForMention(wordWithoutSuffix, keywords) {
|
||||||
|
foundWithoutSuffix = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if foundWithoutSuffix {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := systemMentions[word]; !ok && strings.HasPrefix(word, "@") {
|
||||||
|
e.OtherPotentialMentions = append(e.OtherPotentialMentions, word[1:])
|
||||||
|
} else if strings.ContainsAny(word, ".-:") {
|
||||||
|
// This word contains a character that may be the end of a sentence, so split further
|
||||||
|
splitWords := strings.FieldsFunc(word, func(c rune) bool {
|
||||||
|
return c == '.' || c == '-' || c == ':'
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, splitWord := range splitWords {
|
||||||
|
if e.checkForMention(splitWord, keywords) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, ok := systemMentions[splitWord]; !ok && strings.HasPrefix(splitWord, "@") {
|
||||||
|
e.OtherPotentialMentions = append(e.OtherPotentialMentions, splitWord[1:])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ids, match := isKeywordMultibyte(keywords, word); match {
|
||||||
|
e.addMentionedUsers(ids)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -597,7 +597,7 @@ func TestGetExplicitMentions(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
m := GetExplicitMentions(post, tc.Keywords)
|
m := getExplicitMentions(post, tc.Keywords)
|
||||||
if tc.Expected.MentionedUserIds == nil {
|
if tc.Expected.MentionedUserIds == nil {
|
||||||
tc.Expected.MentionedUserIds = make(map[string]bool)
|
tc.Expected.MentionedUserIds = make(map[string]bool)
|
||||||
}
|
}
|
||||||
@@ -652,7 +652,7 @@ func TestGetExplicitMentionsAtHere(t *testing.T) {
|
|||||||
|
|
||||||
for message, shouldMention := range cases {
|
for message, shouldMention := range cases {
|
||||||
post := &model.Post{Message: message}
|
post := &model.Post{Message: message}
|
||||||
if m := GetExplicitMentions(post, nil); m.HereMentioned && !shouldMention {
|
if m := getExplicitMentions(post, nil); m.HereMentioned && !shouldMention {
|
||||||
t.Fatalf("shouldn't have mentioned @here with \"%v\"", message)
|
t.Fatalf("shouldn't have mentioned @here with \"%v\"", message)
|
||||||
} else if !m.HereMentioned && shouldMention {
|
} else if !m.HereMentioned && shouldMention {
|
||||||
t.Fatalf("should've mentioned @here with \"%v\"", message)
|
t.Fatalf("should've mentioned @here with \"%v\"", message)
|
||||||
@@ -661,7 +661,7 @@ func TestGetExplicitMentionsAtHere(t *testing.T) {
|
|||||||
|
|
||||||
// mentioning @here and someone
|
// mentioning @here and someone
|
||||||
id := model.NewId()
|
id := model.NewId()
|
||||||
if m := GetExplicitMentions(&model.Post{Message: "@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\"")
|
t.Fatal("should've mentioned @here with \"@here @user\"")
|
||||||
} else if len(m.MentionedUserIds) != 1 || !m.MentionedUserIds[id] {
|
} else if len(m.MentionedUserIds) != 1 || !m.MentionedUserIds[id] {
|
||||||
t.Fatal("should've mentioned @user with \"@here @user\"")
|
t.Fatal("should've mentioned @user with \"@here @user\"")
|
||||||
@@ -691,7 +691,7 @@ func TestGetMentionKeywords(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
profiles := map[string]*model.User{user1.Id: user1}
|
profiles := map[string]*model.User{user1.Id: user1}
|
||||||
mentions := th.App.GetMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap1Off)
|
mentions := th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap1Off)
|
||||||
if len(mentions) != 3 {
|
if len(mentions) != 3 {
|
||||||
t.Fatal("should've returned three mention keywords")
|
t.Fatal("should've returned three mention keywords")
|
||||||
} else if ids, ok := mentions["user"]; !ok || ids[0] != user1.Id {
|
} else if ids, ok := mentions["user"]; !ok || ids[0] != user1.Id {
|
||||||
@@ -719,7 +719,7 @@ func TestGetMentionKeywords(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
profiles = map[string]*model.User{user2.Id: user2}
|
profiles = map[string]*model.User{user2.Id: user2}
|
||||||
mentions = th.App.GetMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap2Off)
|
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap2Off)
|
||||||
if len(mentions) != 2 {
|
if len(mentions) != 2 {
|
||||||
t.Fatal("should've returned two mention keyword")
|
t.Fatal("should've returned two mention keyword")
|
||||||
} else if ids, ok := mentions["First"]; !ok || ids[0] != user2.Id {
|
} else if ids, ok := mentions["First"]; !ok || ids[0] != user2.Id {
|
||||||
@@ -743,7 +743,7 @@ func TestGetMentionKeywords(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
profiles = map[string]*model.User{user3.Id: user3}
|
profiles = map[string]*model.User{user3.Id: user3}
|
||||||
mentions = th.App.GetMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap3Off)
|
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap3Off)
|
||||||
if len(mentions) != 3 {
|
if len(mentions) != 3 {
|
||||||
t.Fatal("should've returned three mention keywords")
|
t.Fatal("should've returned three mention keywords")
|
||||||
} else if ids, ok := mentions["@channel"]; !ok || ids[0] != user3.Id {
|
} else if ids, ok := mentions["@channel"]; !ok || ids[0] != user3.Id {
|
||||||
@@ -759,7 +759,7 @@ func TestGetMentionKeywords(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
profiles = map[string]*model.User{user3.Id: user3}
|
profiles = map[string]*model.User{user3.Id: user3}
|
||||||
mentions = th.App.GetMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMapDefault)
|
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMapDefault)
|
||||||
if len(mentions) != 3 {
|
if len(mentions) != 3 {
|
||||||
t.Fatal("should've returned three mention keywords")
|
t.Fatal("should've returned three mention keywords")
|
||||||
} else if ids, ok := mentions["@channel"]; !ok || ids[0] != user3.Id {
|
} else if ids, ok := mentions["@channel"]; !ok || ids[0] != user3.Id {
|
||||||
@@ -771,7 +771,7 @@ func TestGetMentionKeywords(t *testing.T) {
|
|||||||
// Channel member notify props is empty
|
// Channel member notify props is empty
|
||||||
channelMemberNotifyPropsMapEmpty := map[string]model.StringMap{}
|
channelMemberNotifyPropsMapEmpty := map[string]model.StringMap{}
|
||||||
profiles = map[string]*model.User{user3.Id: user3}
|
profiles = map[string]*model.User{user3.Id: user3}
|
||||||
mentions = th.App.GetMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMapEmpty)
|
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMapEmpty)
|
||||||
if len(mentions) != 3 {
|
if len(mentions) != 3 {
|
||||||
t.Fatal("should've returned three mention keywords")
|
t.Fatal("should've returned three mention keywords")
|
||||||
} else if ids, ok := mentions["@channel"]; !ok || ids[0] != user3.Id {
|
} else if ids, ok := mentions["@channel"]; !ok || ids[0] != user3.Id {
|
||||||
@@ -786,7 +786,7 @@ func TestGetMentionKeywords(t *testing.T) {
|
|||||||
"ignore_channel_mentions": model.IGNORE_CHANNEL_MENTIONS_ON,
|
"ignore_channel_mentions": model.IGNORE_CHANNEL_MENTIONS_ON,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
mentions = th.App.GetMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap3On)
|
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap3On)
|
||||||
if len(mentions) == 0 {
|
if len(mentions) == 0 {
|
||||||
t.Fatal("should've not returned any keywords")
|
t.Fatal("should've not returned any keywords")
|
||||||
}
|
}
|
||||||
@@ -811,7 +811,7 @@ func TestGetMentionKeywords(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
profiles = map[string]*model.User{user4.Id: user4}
|
profiles = map[string]*model.User{user4.Id: user4}
|
||||||
mentions = th.App.GetMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap4Off)
|
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap4Off)
|
||||||
if len(mentions) != 6 {
|
if len(mentions) != 6 {
|
||||||
t.Fatal("should've returned six mention keywords")
|
t.Fatal("should've returned six mention keywords")
|
||||||
} else if ids, ok := mentions["user"]; !ok || ids[0] != user4.Id {
|
} else if ids, ok := mentions["user"]; !ok || ids[0] != user4.Id {
|
||||||
@@ -834,7 +834,7 @@ func TestGetMentionKeywords(t *testing.T) {
|
|||||||
"ignore_channel_mentions": model.IGNORE_CHANNEL_MENTIONS_ON,
|
"ignore_channel_mentions": model.IGNORE_CHANNEL_MENTIONS_ON,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
mentions = th.App.GetMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap4On)
|
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap4On)
|
||||||
if len(mentions) != 4 {
|
if len(mentions) != 4 {
|
||||||
t.Fatal("should've returned four mention keywords")
|
t.Fatal("should've returned four mention keywords")
|
||||||
} else if ids, ok := mentions["user"]; !ok || ids[0] != user4.Id {
|
} else if ids, ok := mentions["user"]; !ok || ids[0] != user4.Id {
|
||||||
@@ -888,7 +888,7 @@ func TestGetMentionKeywords(t *testing.T) {
|
|||||||
"ignore_channel_mentions": model.IGNORE_CHANNEL_MENTIONS_OFF,
|
"ignore_channel_mentions": model.IGNORE_CHANNEL_MENTIONS_OFF,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
mentions = th.App.GetMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap5Off)
|
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap5Off)
|
||||||
if len(mentions) != 6 {
|
if len(mentions) != 6 {
|
||||||
t.Fatal("should've returned six mention keywords")
|
t.Fatal("should've returned six mention keywords")
|
||||||
} else if ids, ok := mentions["user"]; !ok || len(ids) != 2 || (ids[0] != user1.Id && ids[1] != user1.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) {
|
} else if ids, ok := mentions["user"]; !ok || len(ids) != 2 || (ids[0] != user1.Id && ids[1] != user1.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) {
|
||||||
@@ -907,7 +907,7 @@ func TestGetMentionKeywords(t *testing.T) {
|
|||||||
|
|
||||||
// multiple users and more than MaxNotificationsPerChannel
|
// multiple users and more than MaxNotificationsPerChannel
|
||||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.MaxNotificationsPerChannel = 3 })
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.MaxNotificationsPerChannel = 3 })
|
||||||
mentions = th.App.GetMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap4Off)
|
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap4Off)
|
||||||
if len(mentions) != 4 {
|
if len(mentions) != 4 {
|
||||||
t.Fatal("should've returned four mention keywords")
|
t.Fatal("should've returned four mention keywords")
|
||||||
} else if _, ok := mentions["@channel"]; ok {
|
} else if _, ok := mentions["@channel"]; ok {
|
||||||
@@ -922,7 +922,7 @@ func TestGetMentionKeywords(t *testing.T) {
|
|||||||
profiles = map[string]*model.User{
|
profiles = map[string]*model.User{
|
||||||
user1.Id: user1,
|
user1.Id: user1,
|
||||||
}
|
}
|
||||||
mentions = th.App.GetMentionKeywordsInChannel(profiles, false, channelMemberNotifyPropsMap4Off)
|
mentions = th.App.getMentionKeywordsInChannel(profiles, false, channelMemberNotifyPropsMap4Off)
|
||||||
if len(mentions) != 3 {
|
if len(mentions) != 3 {
|
||||||
t.Fatal("should've returned three mention keywords")
|
t.Fatal("should've returned three mention keywords")
|
||||||
} else if ids, ok := mentions["user"]; !ok || len(ids) != 1 || ids[0] != user1.Id {
|
} else if ids, ok := mentions["user"]; !ok || len(ids) != 1 || ids[0] != user1.Id {
|
||||||
@@ -969,7 +969,7 @@ func TestGetMentionsEnabledFields(t *testing.T) {
|
|||||||
"@here with mentions",
|
"@here with mentions",
|
||||||
"some text"}
|
"some text"}
|
||||||
|
|
||||||
mentionEnabledFields := GetMentionsEnabledFields(post)
|
mentionEnabledFields := getMentionsEnabledFields(post)
|
||||||
|
|
||||||
assert.EqualValues(t, 4, len(mentionEnabledFields))
|
assert.EqualValues(t, 4, len(mentionEnabledFields))
|
||||||
assert.EqualValues(t, expectedFields, mentionEnabledFields)
|
assert.EqualValues(t, expectedFields, mentionEnabledFields)
|
||||||
@@ -1136,3 +1136,400 @@ func TestPostNotificationGetSenderName(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsKeywordMultibyte(t *testing.T) {
|
||||||
|
id1 := model.NewId()
|
||||||
|
|
||||||
|
for name, tc := range map[string]struct {
|
||||||
|
Message string
|
||||||
|
Attachments []*model.SlackAttachment
|
||||||
|
Keywords map[string][]string
|
||||||
|
Expected *ExplicitMentions
|
||||||
|
}{
|
||||||
|
"MultibyteCharacter": {
|
||||||
|
Message: "My name is 萌",
|
||||||
|
Keywords: map[string][]string{"萌": {id1}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"MultibyteCharacterWithNoUser": {
|
||||||
|
Message: "My name is 萌",
|
||||||
|
Keywords: map[string][]string{"萌": {}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"MultibyteCharacterAtBeginningOfSentence": {
|
||||||
|
Message: "이메일을 보내다.",
|
||||||
|
Keywords: map[string][]string{"이메일": {id1}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"MultibyteCharacterAtBeginningOfSentenceWithNoUser": {
|
||||||
|
Message: "이메일을 보내다.",
|
||||||
|
Keywords: map[string][]string{"이메일": {}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"MultibyteCharacterInPartOfSentence": {
|
||||||
|
Message: "我爱吃番茄炒饭",
|
||||||
|
Keywords: map[string][]string{"番茄": {id1}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"MultibyteCharacterInPartOfSentenceWithNoUser": {
|
||||||
|
Message: "我爱吃番茄炒饭",
|
||||||
|
Keywords: map[string][]string{"番茄": {}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"MultibyteCharacterAtEndOfSentence": {
|
||||||
|
Message: "こんにちは、世界",
|
||||||
|
Keywords: map[string][]string{"世界": {id1}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"MultibyteCharacterAtEndOfSentenceWithNoUser": {
|
||||||
|
Message: "こんにちは、世界",
|
||||||
|
Keywords: map[string][]string{"世界": {}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"MultibyteCharacterTwiceInSentence": {
|
||||||
|
Message: "石橋さんが石橋を渡る",
|
||||||
|
Keywords: map[string][]string{"石橋": {id1}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"MultibyteCharacterTwiceInSentenceWithNoUser": {
|
||||||
|
Message: "石橋さんが石橋を渡る",
|
||||||
|
Keywords: map[string][]string{"石橋": {}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
assert.EqualValues(t, tc.Expected, m)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddMentionedUsers(t *testing.T) {
|
||||||
|
id1 := model.NewId()
|
||||||
|
id2 := model.NewId()
|
||||||
|
id3 := model.NewId()
|
||||||
|
id4 := model.NewId()
|
||||||
|
id5 := model.NewId()
|
||||||
|
id6 := model.NewId()
|
||||||
|
id7 := model.NewId()
|
||||||
|
id8 := model.NewId()
|
||||||
|
id9 := model.NewId()
|
||||||
|
|
||||||
|
for name, tc := range map[string]struct {
|
||||||
|
Mentions []string
|
||||||
|
ExplicitMentions *ExplicitMentions
|
||||||
|
Expected *ExplicitMentions
|
||||||
|
}{
|
||||||
|
"test": {
|
||||||
|
Mentions: []string{id1},
|
||||||
|
ExplicitMentions: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{},
|
||||||
|
},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"two users": {
|
||||||
|
Mentions: []string{id1, id2},
|
||||||
|
ExplicitMentions: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{},
|
||||||
|
},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
id2: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"no users": {
|
||||||
|
Mentions: []string{},
|
||||||
|
ExplicitMentions: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{},
|
||||||
|
},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"five users": {
|
||||||
|
Mentions: []string{id1, id5, id4, id8, id9},
|
||||||
|
ExplicitMentions: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{},
|
||||||
|
},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
id4: true,
|
||||||
|
id5: true,
|
||||||
|
id8: true,
|
||||||
|
id9: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"nine users": {
|
||||||
|
Mentions: []string{id1, id2, id3, id4, id5, id6, id7, id8, id9},
|
||||||
|
ExplicitMentions: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{},
|
||||||
|
},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
id2: true,
|
||||||
|
id3: true,
|
||||||
|
id4: true,
|
||||||
|
id5: true,
|
||||||
|
id6: true,
|
||||||
|
id7: true,
|
||||||
|
id8: true,
|
||||||
|
id9: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
tc.ExplicitMentions.addMentionedUsers(tc.Mentions)
|
||||||
|
if tc.ExplicitMentions.MentionedUserIds == nil {
|
||||||
|
tc.ExplicitMentions.MentionedUserIds = make(map[string]bool)
|
||||||
|
}
|
||||||
|
assert.EqualValues(t, tc.Expected.MentionedUserIds, tc.ExplicitMentions.MentionedUserIds)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCheckForMentionUsers(t *testing.T) {
|
||||||
|
id1 := model.NewId()
|
||||||
|
id2 := model.NewId()
|
||||||
|
|
||||||
|
for name, tc := range map[string]struct {
|
||||||
|
Word string
|
||||||
|
Attachments []*model.SlackAttachment
|
||||||
|
Keywords map[string][]string
|
||||||
|
Expected *ExplicitMentions
|
||||||
|
}{
|
||||||
|
"Nobody": {
|
||||||
|
Word: "nothing",
|
||||||
|
Keywords: map[string][]string{},
|
||||||
|
Expected: &ExplicitMentions{},
|
||||||
|
},
|
||||||
|
"UppercaseUser1": {
|
||||||
|
Word: "@User",
|
||||||
|
Keywords: map[string][]string{"@user": {id1}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"LowercaseUser1": {
|
||||||
|
Word: "@user",
|
||||||
|
Keywords: map[string][]string{"@user": {id1}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"LowercaseUser2": {
|
||||||
|
Word: "@user2",
|
||||||
|
Keywords: map[string][]string{"@user2": {id2}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id2: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"UppercaseUser2": {
|
||||||
|
Word: "@UsEr2",
|
||||||
|
Keywords: map[string][]string{"@user2": {id2}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id2: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"HereMention": {
|
||||||
|
Word: "@here",
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
HereMentioned: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"ChannelMention": {
|
||||||
|
Word: "@channel",
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
ChannelMentioned: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"AllMention": {
|
||||||
|
Word: "@all",
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
AllMentioned: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"UppercaseHere": {
|
||||||
|
Word: "@HeRe",
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
HereMentioned: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"UppercaseChannel": {
|
||||||
|
Word: "@ChaNNel",
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
ChannelMentioned: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"UppercaseAll": {
|
||||||
|
Word: "@ALL",
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
AllMentioned: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
|
||||||
|
e := &ExplicitMentions{
|
||||||
|
MentionedUserIds: make(map[string]bool),
|
||||||
|
}
|
||||||
|
e.checkForMention(tc.Word, tc.Keywords)
|
||||||
|
if tc.Expected.MentionedUserIds == nil {
|
||||||
|
tc.Expected.MentionedUserIds = make(map[string]bool)
|
||||||
|
}
|
||||||
|
assert.EqualValues(t, tc.Expected, e)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestProcessText(t *testing.T) {
|
||||||
|
id1 := model.NewId()
|
||||||
|
|
||||||
|
for name, tc := range map[string]struct {
|
||||||
|
Text string
|
||||||
|
Keywords map[string][]string
|
||||||
|
Expected *ExplicitMentions
|
||||||
|
}{
|
||||||
|
"Mention user in text": {
|
||||||
|
Text: "hello user @user1",
|
||||||
|
Keywords: map[string][]string{"@user1": {id1}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"Mention user after ending a sentence with full stop": {
|
||||||
|
Text: "hello user.@user1",
|
||||||
|
Keywords: map[string][]string{"@user1": {id1}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"Mention user after hyphen": {
|
||||||
|
Text: "hello user-@user1",
|
||||||
|
Keywords: map[string][]string{"@user1": {id1}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"Mention user after colon": {
|
||||||
|
Text: "hello user:@user1",
|
||||||
|
Keywords: map[string][]string{"@user1": {id1}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"Mention here after colon": {
|
||||||
|
Text: "hello all:@here",
|
||||||
|
Keywords: map[string][]string{},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
HereMentioned: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"Mention all after hyphen": {
|
||||||
|
Text: "hello all-@all",
|
||||||
|
Keywords: map[string][]string{},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
AllMentioned: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"Mention channel after full stop": {
|
||||||
|
Text: "hello channel.@channel",
|
||||||
|
Keywords: map[string][]string{},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
ChannelMentioned: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"Mention other pontential users or system calls": {
|
||||||
|
Text: "hello @potentialuser and @otherpotentialuser",
|
||||||
|
Keywords: map[string][]string{},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
OtherPotentialMentions: []string{"potentialuser", "otherpotentialuser"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"Mention a user and another pontential users or system calls": {
|
||||||
|
Text: "@user1, you can use @systembot to get help",
|
||||||
|
Keywords: map[string][]string{"@user1": {id1}},
|
||||||
|
Expected: &ExplicitMentions{
|
||||||
|
MentionedUserIds: map[string]bool{
|
||||||
|
id1: true,
|
||||||
|
},
|
||||||
|
OtherPotentialMentions: []string{"systembot"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
|
||||||
|
e := &ExplicitMentions{
|
||||||
|
MentionedUserIds: make(map[string]bool),
|
||||||
|
}
|
||||||
|
if tc.Expected.MentionedUserIds == nil {
|
||||||
|
tc.Expected.MentionedUserIds = make(map[string]bool)
|
||||||
|
}
|
||||||
|
e.processText(tc.Text, tc.Keywords)
|
||||||
|
assert.EqualValues(t, tc.Expected, e)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user