From 0cbba46018d3879234d3940e8ee6f23dd99a1c5a Mon Sep 17 00:00:00 2001 From: Stephen Kiers Date: Mon, 12 Feb 2018 16:30:03 -0700 Subject: [PATCH 1/6] Fixes ICU-764 --- app/notification.go | 3 +++ app/notification_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/app/notification.go b/app/notification.go index 1318308f8e..6531f72f73 100644 --- a/app/notification.go +++ b/app/notification.go @@ -832,6 +832,9 @@ func GetExplicitMentions(message string, keywords map[string][]string) *Explicit continue } + // remove trailing '.', as that is the end of a sentence + word = strings.TrimSuffix(word, ".") + if word == "@here" { ret.HereMentioned = true } diff --git a/app/notification_test.go b/app/notification_test.go index 11f4df6857..7f2a63ab99 100644 --- a/app/notification_test.go +++ b/app/notification_test.go @@ -109,6 +109,15 @@ func TestGetExplicitMentions(t *testing.T) { }, }, }, + "OnePersonAtEndOfSentence": { + Message: "this is a message for @user.", + Keywords: map[string][]string{"@user": {id1}}, + Expected: &ExplicitMentions{ + MentionedUserIds: map[string]bool{ + id1: true, + }, + }, + }, "OnePersonWithoutAtMention": { Message: "this is a message for @user", Keywords: map[string][]string{"this": {id1}}, @@ -179,6 +188,15 @@ func TestGetExplicitMentions(t *testing.T) { }, }, }, + "UserWithPeriodAtEndOfSentence": { + Message: "this is a message for user.period.", + Keywords: map[string][]string{"user.period": {id1}}, + Expected: &ExplicitMentions{ + MentionedUserIds: map[string]bool{ + id1: true, + }, + }, + }, "PotentialOutOfChannelUser": { Message: "this is an message for @potential and @user", Keywords: map[string][]string{"@user": {id1}}, From bdf478c75bb41c00cdfd47bd7ae68c70a06886af Mon Sep 17 00:00:00 2001 From: Stephen Kiers Date: Mon, 12 Feb 2018 20:50:20 -0700 Subject: [PATCH 2/6] Added another test --- app/notification_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/notification_test.go b/app/notification_test.go index 7f2a63ab99..61442c0489 100644 --- a/app/notification_test.go +++ b/app/notification_test.go @@ -188,6 +188,15 @@ func TestGetExplicitMentions(t *testing.T) { }, }, }, + "AtUserWithPeriodAtEndOfSentence": { + Message: "this is a message for @user.period.", + Keywords: map[string][]string{"@user.period": {id1}}, + Expected: &ExplicitMentions{ + MentionedUserIds: map[string]bool{ + id1: true, + }, + }, + }, "UserWithPeriodAtEndOfSentence": { Message: "this is a message for user.period.", Keywords: map[string][]string{"user.period": {id1}}, From 08c21f75199f959bbe63396be246e2b7d36a9a39 Mon Sep 17 00:00:00 2001 From: Stephen Kiers Date: Tue, 13 Feb 2018 10:49:48 -0700 Subject: [PATCH 3/6] Added more tests and simplified code --- app/notification.go | 87 ++++++++++++++++++---------------------- app/notification_test.go | 18 +++++++++ 2 files changed, 57 insertions(+), 48 deletions(-) diff --git a/app/notification.go b/app/notification.go index 6531f72f73..90303fb8fe 100644 --- a/app/notification.go +++ b/app/notification.go @@ -820,46 +820,54 @@ func GetExplicitMentions(message string, keywords map[string][]string) *Explicit } } + checkForMention := func(word string) bool { + isMention := false + + fmt.Printf("New Word: %v\n", word) + + if word == "@here" { + ret.HereMentioned = true + } + + if word == "@channel" { + ret.ChannelMentioned = true + } + + if 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 + } 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)) }) { - isMention := false - // skip word with format ':word:' with an assumption that it is an emoji format only if word[0] == ':' && word[len(word)-1] == ':' { continue } + if checkForMention(word) { + continue + } + // remove trailing '.', as that is the end of a sentence word = strings.TrimSuffix(word, ".") - - if word == "@here" { - ret.HereMentioned = true - } - - if word == "@channel" { - ret.ChannelMentioned = true - } - - if 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 - } - - if isMention { + if checkForMention(word) { continue } @@ -870,27 +878,10 @@ func GetExplicitMentions(message string, keywords map[string][]string) *Explicit }) for _, splitWord := range splitWords { - if splitWord == "@here" { - ret.HereMentioned = true + if checkForMention(splitWord) { + continue } - - if splitWord == "@all" { - ret.AllMentioned = true - } - - if splitWord == "@channel" { - ret.ChannelMentioned = true - } - - // Non-case-sensitive check for regular keys - if ids, match := keywords[strings.ToLower(splitWord)]; match { - addMentionedUsers(ids) - } - - // Case-sensitive check for first name - if ids, match := keywords[splitWord]; match { - addMentionedUsers(ids) - } else if _, ok := systemMentions[splitWord]; !ok && strings.HasPrefix(splitWord, "@") { + if _, ok := systemMentions[splitWord]; !ok && strings.HasPrefix(splitWord, "@") { username := splitWord[1:] ret.OtherPotentialMentions = append(ret.OtherPotentialMentions, username) } diff --git a/app/notification_test.go b/app/notification_test.go index 61442c0489..bd7da3db72 100644 --- a/app/notification_test.go +++ b/app/notification_test.go @@ -109,6 +109,24 @@ func TestGetExplicitMentions(t *testing.T) { }, }, }, + "OnePersonWithPeriodAtEndOfUsername": { + Message: "this is a message for @user.name.", + Keywords: map[string][]string{"@user.name.": {id1}}, + Expected: &ExplicitMentions{ + MentionedUserIds: map[string]bool{ + id1: true, + }, + }, + }, + "OnePersonWithPeriodAtEndOfUsernameButNotSimilarName": { + Message: "this is a message for @user.name.", + Keywords: map[string][]string{"@user.name.": {id1}, "@user.name": {id2}}, + Expected: &ExplicitMentions{ + MentionedUserIds: map[string]bool{ + id1: true, + }, + }, + }, "OnePersonAtEndOfSentence": { Message: "this is a message for @user.", Keywords: map[string][]string{"@user": {id1}}, From 1fe522659b0ecaa61d9c3d652ae2d89dd895248c Mon Sep 17 00:00:00 2001 From: Stephen Kiers Date: Tue, 13 Feb 2018 11:51:38 -0700 Subject: [PATCH 4/6] remove debug statement --- app/notification.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/notification.go b/app/notification.go index 90303fb8fe..7725445e42 100644 --- a/app/notification.go +++ b/app/notification.go @@ -823,8 +823,6 @@ func GetExplicitMentions(message string, keywords map[string][]string) *Explicit checkForMention := func(word string) bool { isMention := false - fmt.Printf("New Word: %v\n", word) - if word == "@here" { ret.HereMentioned = true } From 2544ad70c392db2d859c98d582efad28ce59987d Mon Sep 17 00:00:00 2001 From: Stephen Kiers Date: Tue, 13 Feb 2018 13:01:31 -0700 Subject: [PATCH 5/6] rerun jenkins? --- app/notification.go | 1 + 1 file changed, 1 insertion(+) diff --git a/app/notification.go b/app/notification.go index 7725445e42..30ce833605 100644 --- a/app/notification.go +++ b/app/notification.go @@ -849,6 +849,7 @@ func GetExplicitMentions(message string, keywords map[string][]string) *Explicit return isMention } + 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 From 69e56fc04237ae5a821a9d97c48bc830111d6c78 Mon Sep 17 00:00:00 2001 From: Stephen Kiers Date: Tue, 13 Feb 2018 13:20:46 -0700 Subject: [PATCH 6/6] gofmt --- app/notification.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/notification.go b/app/notification.go index 30ce833605..e158e08d59 100644 --- a/app/notification.go +++ b/app/notification.go @@ -819,7 +819,6 @@ func GetExplicitMentions(message string, keywords map[string][]string) *Explicit ret.MentionedUserIds[id] = true } } - checkForMention := func(word string) bool { isMention := false @@ -849,7 +848,6 @@ func GetExplicitMentions(message string, keywords map[string][]string) *Explicit return isMention } - 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