From bca7339e4ccb410a1aa048842074af9518198b9e Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Tue, 5 Jun 2018 13:34:21 -0400 Subject: MM-10597 Improved handling of punctuation around notifications (#8901) --- app/notification.go | 21 +++++++++---------- app/notification_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/app/notification.go b/app/notification.go index 401675f4e..741dadd9a 100644 --- a/app/notification.go +++ b/app/notification.go @@ -1004,23 +1004,24 @@ func GetExplicitMentions(message string, keywords map[string][]string) *Explicit continue } + word = strings.TrimLeft(word, ":.-_") + if checkForMention(word) { continue } - // remove trailing '.', as that is the end of a sentence - foundWithSuffix := false - for _, suffixPunctuation := range []string{".", ":"} { - for strings.HasSuffix(word, suffixPunctuation) { - word = strings.TrimSuffix(word, suffixPunctuation) - if checkForMention(word) { - foundWithSuffix = true - break - } + foundWithoutSuffix := false + wordWithoutSuffix := word + for strings.LastIndexAny(wordWithoutSuffix, ".-:_") != -1 { + wordWithoutSuffix = wordWithoutSuffix[0 : len(wordWithoutSuffix)-1] + + if checkForMention(wordWithoutSuffix) { + foundWithoutSuffix = true + break } } - if foundWithSuffix { + if foundWithoutSuffix { continue } diff --git a/app/notification_test.go b/app/notification_test.go index 1a4fb2859..fb8eef4d7 100644 --- a/app/notification_test.go +++ b/app/notification_test.go @@ -148,14 +148,58 @@ func TestGetExplicitMentions(t *testing.T) { OtherPotentialMentions: []string{"user"}, }, }, - "OnePersonWithColonAtEnd": { + "OnePersonWithPeriodAfter": { + Message: "this is a message for @user.", + Keywords: map[string][]string{"@user": {id1}}, + Expected: &ExplicitMentions{ + MentionedUserIds: map[string]bool{ + id1: true, + }, + }, + }, + "OnePersonWithPeriodBefore": { + Message: "this is a message for .@user", + Keywords: map[string][]string{"@user": {id1}}, + Expected: &ExplicitMentions{ + MentionedUserIds: map[string]bool{ + id1: true, + }, + }, + }, + "OnePersonWithColonAfter": { Message: "this is a message for @user:", - Keywords: map[string][]string{"this": {id1}}, + Keywords: map[string][]string{"@user": {id1}}, + Expected: &ExplicitMentions{ + MentionedUserIds: map[string]bool{ + id1: true, + }, + }, + }, + "OnePersonWithColonBefore": { + Message: "this is a message for :@user", + Keywords: map[string][]string{"@user": {id1}}, + Expected: &ExplicitMentions{ + MentionedUserIds: map[string]bool{ + id1: true, + }, + }, + }, + "OnePersonWithHyphenAfter": { + Message: "this is a message for @user.", + Keywords: map[string][]string{"@user": {id1}}, + Expected: &ExplicitMentions{ + MentionedUserIds: map[string]bool{ + id1: true, + }, + }, + }, + "OnePersonWithHyphenBefore": { + Message: "this is a message for -@user", + Keywords: map[string][]string{"@user": {id1}}, Expected: &ExplicitMentions{ MentionedUserIds: map[string]bool{ id1: true, }, - OtherPotentialMentions: []string{"user"}, }, }, "MultiplePeopleWithOneWord": { @@ -493,7 +537,7 @@ func TestGetExplicitMentionsAtHere(t *testing.T) { "(@here(": true, ")@here)": true, "-@here-": true, - "_@here_": false, // This case shouldn't mention since it would be mentioning "@here_" + "_@here_": true, "=@here=": true, "+@here+": true, "[@here[": true, -- cgit v1.2.3-1-g7c22