From 0cbba46018d3879234d3940e8ee6f23dd99a1c5a Mon Sep 17 00:00:00 2001 From: Stephen Kiers Date: Mon, 12 Feb 2018 16:30:03 -0700 Subject: Fixes ICU-764 --- app/notification.go | 3 +++ 1 file changed, 3 insertions(+) (limited to 'app/notification.go') diff --git a/app/notification.go b/app/notification.go index 1318308f8..6531f72f7 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 } -- cgit v1.2.3-1-g7c22 From b8a4316b13bc0664648abda6b8d3de585d79173d Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Mon, 12 Feb 2018 11:09:51 -0500 Subject: ICU-753 Remove special behaviour for group messages in generic push notifications --- app/notification.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/notification.go') diff --git a/app/notification.go b/app/notification.go index 1318308f8..2e99ca03c 100644 --- a/app/notification.go +++ b/app/notification.go @@ -607,7 +607,7 @@ func (a *App) sendPushNotification(post *model.Post, user *model.User, channel * if channel.Type == model.CHANNEL_DIRECT { msg.Category = model.CATEGORY_CAN_REPLY msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_message") - } else if wasMentioned || channel.Type == model.CHANNEL_GROUP { + } else if wasMentioned { msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_mention_no_channel") } else { msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_non_mention_no_channel") @@ -616,7 +616,7 @@ func (a *App) sendPushNotification(post *model.Post, user *model.User, channel * if channel.Type == model.CHANNEL_DIRECT { msg.Category = model.CATEGORY_CAN_REPLY msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_message") - } else if wasMentioned || channel.Type == model.CHANNEL_GROUP { + } else if wasMentioned { msg.Category = model.CATEGORY_CAN_REPLY msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_mention") + channelName } else { -- cgit v1.2.3-1-g7c22 From c1b49f8b77e0e75afcc6cf4dc0f1c36569824151 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Mon, 12 Feb 2018 11:50:41 -0500 Subject: ICU-753 Added unit tests for push notification contents --- app/notification.go | 90 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 39 deletions(-) (limited to 'app/notification.go') diff --git a/app/notification.go b/app/notification.go index 2e99ca03c..83debe584 100644 --- a/app/notification.go +++ b/app/notification.go @@ -566,8 +566,6 @@ func (a *App) sendPushNotification(post *model.Post, user *model.User, channel * channelName = senderName } - userLocale := utils.GetUserTranslations(user.Locale) - msg := model.PushNotification{} if badge := <-a.Srv.Store.User().GetUnreadCount(user.Id); badge.Err != nil { msg.Badge = 1 @@ -596,44 +594,10 @@ func (a *App) sendPushNotification(post *model.Post, user *model.User, channel * msg.FromWebhook = fw } - if *a.Config().EmailSettings.PushNotificationContents == model.FULL_NOTIFICATION { - msg.Category = model.CATEGORY_CAN_REPLY - if channel.Type == model.CHANNEL_DIRECT { - msg.Message = senderName + ": " + model.ClearMentionTags(post.Message) - } else { - msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_in") + channelName + ": " + model.ClearMentionTags(post.Message) - } - } else if *a.Config().EmailSettings.PushNotificationContents == model.GENERIC_NO_CHANNEL_NOTIFICATION { - if channel.Type == model.CHANNEL_DIRECT { - msg.Category = model.CATEGORY_CAN_REPLY - msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_message") - } else if wasMentioned { - msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_mention_no_channel") - } else { - msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_non_mention_no_channel") - } - } else { - if channel.Type == model.CHANNEL_DIRECT { - msg.Category = model.CATEGORY_CAN_REPLY - msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_message") - } else if wasMentioned { - msg.Category = model.CATEGORY_CAN_REPLY - msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_mention") + channelName - } else { - msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_non_mention") + channelName - } - } - - // If the post only has images then push an appropriate message - if len(post.Message) == 0 && post.FileIds != nil && len(post.FileIds) > 0 { - if channel.Type == model.CHANNEL_DIRECT { - msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_image_only_dm") - } else { - msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_image_only") + channelName - } - } + userLocale := utils.GetUserTranslations(user.Locale) + hasFiles := post.FileIds != nil && len(post.FileIds) > 0 - //l4g.Debug("Sending push notification for user %v with msg of '%v'", user.Id, msg.Message) + msg.Message, msg.Category = a.getPushNotificationMessage(post.Message, wasMentioned, hasFiles, senderName, channelName, channel.Type, userLocale) for _, session := range sessions { tmpMessage := *model.PushNotificationFromJson(strings.NewReader(msg.ToJson())) @@ -655,6 +619,54 @@ func (a *App) sendPushNotification(post *model.Post, user *model.User, channel * return nil } +func (a *App) getPushNotificationMessage(postMessage string, wasMentioned bool, hasFiles bool, senderName string, channelName string, channelType string, userLocale i18n.TranslateFunc) (string, string) { + message := "" + category := "" + + if *a.Config().EmailSettings.PushNotificationContents == model.FULL_NOTIFICATION { + category = model.CATEGORY_CAN_REPLY + + if channelType == model.CHANNEL_DIRECT { + message = senderName + ": " + model.ClearMentionTags(postMessage) + } else { + message = senderName + userLocale("api.post.send_notifications_and_forget.push_in") + channelName + ": " + model.ClearMentionTags(postMessage) + } + } else if *a.Config().EmailSettings.PushNotificationContents == model.GENERIC_NO_CHANNEL_NOTIFICATION { + if channelType == model.CHANNEL_DIRECT { + category = model.CATEGORY_CAN_REPLY + + message = senderName + userLocale("api.post.send_notifications_and_forget.push_message") + } else if wasMentioned { + message = senderName + userLocale("api.post.send_notifications_and_forget.push_mention_no_channel") + } else { + message = senderName + userLocale("api.post.send_notifications_and_forget.push_non_mention_no_channel") + } + } else { + if channelType == model.CHANNEL_DIRECT { + category = model.CATEGORY_CAN_REPLY + + message = senderName + userLocale("api.post.send_notifications_and_forget.push_message") + } else if wasMentioned { + category = model.CATEGORY_CAN_REPLY + + message = senderName + userLocale("api.post.send_notifications_and_forget.push_mention") + channelName + } else { + message = senderName + userLocale("api.post.send_notifications_and_forget.push_non_mention") + channelName + } + } + + // If the post only has images then push an appropriate message + if len(message) == 0 && hasFiles { + if channelType == model.CHANNEL_DIRECT { + message = senderName + userLocale("api.post.send_notifications_and_forget.push_image_only_dm") + } else { + message = senderName + userLocale("api.post.send_notifications_and_forget.push_image_only") + channelName + } + } + + return message, category +} + func (a *App) ClearPushNotification(userId string, channelId string) { a.Go(func() { // Sleep is to allow the read replicas a chance to fully sync -- cgit v1.2.3-1-g7c22 From 3fef21e350737c235e6dfc2d9f35311d65290c3e Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Mon, 12 Feb 2018 12:56:46 -0500 Subject: ICU-753 Added unit tests for messages with only push notifications --- app/notification.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/notification.go') diff --git a/app/notification.go b/app/notification.go index 83debe584..8d8e72cf9 100644 --- a/app/notification.go +++ b/app/notification.go @@ -656,7 +656,7 @@ func (a *App) getPushNotificationMessage(postMessage string, wasMentioned bool, } // If the post only has images then push an appropriate message - if len(message) == 0 && hasFiles { + if len(postMessage) == 0 && hasFiles { if channelType == model.CHANNEL_DIRECT { message = senderName + userLocale("api.post.send_notifications_and_forget.push_image_only_dm") } else { -- cgit v1.2.3-1-g7c22 From 08c21f75199f959bbe63396be246e2b7d36a9a39 Mon Sep 17 00:00:00 2001 From: Stephen Kiers Date: Tue, 13 Feb 2018 10:49:48 -0700 Subject: Added more tests and simplified code --- app/notification.go | 87 ++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 48 deletions(-) (limited to 'app/notification.go') diff --git a/app/notification.go b/app/notification.go index 6531f72f7..90303fb8f 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 } - // 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 checkForMention(word) { + continue } - if isMention { + // remove trailing '.', as that is the end of a sentence + word = strings.TrimSuffix(word, ".") + 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 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) + if checkForMention(splitWord) { + continue } - - // 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) } -- cgit v1.2.3-1-g7c22 From 1fe522659b0ecaa61d9c3d652ae2d89dd895248c Mon Sep 17 00:00:00 2001 From: Stephen Kiers Date: Tue, 13 Feb 2018 11:51:38 -0700 Subject: remove debug statement --- app/notification.go | 2 -- 1 file changed, 2 deletions(-) (limited to 'app/notification.go') diff --git a/app/notification.go b/app/notification.go index 90303fb8f..7725445e4 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 } -- cgit v1.2.3-1-g7c22 From 2544ad70c392db2d859c98d582efad28ce59987d Mon Sep 17 00:00:00 2001 From: Stephen Kiers Date: Tue, 13 Feb 2018 13:01:31 -0700 Subject: rerun jenkins? --- app/notification.go | 1 + 1 file changed, 1 insertion(+) (limited to 'app/notification.go') diff --git a/app/notification.go b/app/notification.go index 7725445e4..30ce83360 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 -- cgit v1.2.3-1-g7c22 From 69e56fc04237ae5a821a9d97c48bc830111d6c78 Mon Sep 17 00:00:00 2001 From: Stephen Kiers Date: Tue, 13 Feb 2018 13:20:46 -0700 Subject: gofmt --- app/notification.go | 2 -- 1 file changed, 2 deletions(-) (limited to 'app/notification.go') diff --git a/app/notification.go b/app/notification.go index 30ce83360..e158e08d5 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 -- cgit v1.2.3-1-g7c22