From 614ca90ca2d42a30477943b855c1a919362be666 Mon Sep 17 00:00:00 2001 From: Miguel Alatzar Date: Fri, 30 Aug 2019 22:00:28 +0900 Subject: [PATCH] [MM-18058] Use GetAnyUnreadPostCountForChannel for NotifyProps.push == all (#11979) * Use GetAnyUnreadPostCountForChannel for NotifyProps.push == all * Extract out msg building and add unit test --- app/helper_test.go | 20 +++++++ app/notification_push.go | 104 ++++++++++++++++++++-------------- app/notification_push_test.go | 51 +++++++++++++++++ 3 files changed, 131 insertions(+), 44 deletions(-) diff --git a/app/helper_test.go b/app/helper_test.go index adfc0fbfe9..947f07888c 100644 --- a/app/helper_test.go +++ b/app/helper_test.go @@ -296,6 +296,26 @@ func (me *TestHelper) CreatePost(channel *model.Channel) *model.Post { return post } +func (me *TestHelper) CreateMessagePost(channel *model.Channel, message string) *model.Post { + post := &model.Post{ + UserId: me.BasicUser.Id, + ChannelId: channel.Id, + Message: message, + CreateAt: model.GetMillis() - 10000, + } + + utils.DisableDebugLogForTest() + var err *model.AppError + if post, err = me.App.CreatePost(post, channel, false); err != nil { + mlog.Error(err.Error()) + + time.Sleep(time.Second) + panic(err) + } + utils.EnableDebugLogForTest() + return post +} + func (me *TestHelper) LinkUserToTeam(user *model.User, team *model.Team) { utils.DisableDebugLogForTest() diff --git a/app/notification_push.go b/app/notification_push.go index edcd7310c3..aea201ce6b 100644 --- a/app/notification_push.go +++ b/app/notification_push.go @@ -53,58 +53,16 @@ func (hub *PushNotificationsHub) GetGoChannelFromUserId(userId string) chan Push } func (a *App) sendPushNotificationSync(post *model.Post, user *model.User, channel *model.Channel, channelName string, senderName string, - explicitMention, channelWideMention bool, replyToThreadType string) *model.AppError { - cfg := a.Config() + explicitMention bool, channelWideMention bool, replyToThreadType string) *model.AppError { sessions, err := a.getMobileAppSessions(user.Id) if err != nil { return err } - msg := model.PushNotification{ - Category: model.CATEGORY_CAN_REPLY, - Version: model.PUSH_MESSAGE_V2, - Type: model.PUSH_TYPE_MESSAGE, - TeamId: channel.TeamId, - ChannelId: channel.Id, - PostId: post.Id, - RootId: post.RootId, - SenderId: post.UserId, - } - - if unreadCount, err := a.Srv.Store.User().GetUnreadCount(user.Id); err != nil { - msg.Badge = 1 - mlog.Error(fmt.Sprint("We could not get the unread message count for the user", user.Id, err), mlog.String("user_id", user.Id)) - } else { - msg.Badge = int(unreadCount) - } - - contentsConfig := *cfg.EmailSettings.PushNotificationContents - if contentsConfig != model.GENERIC_NO_CHANNEL_NOTIFICATION || channel.Type == model.CHANNEL_DIRECT { - msg.ChannelName = channelName - } - - msg.SenderName = senderName - if ou, ok := post.Props["override_username"].(string); ok && *cfg.ServiceSettings.EnablePostUsernameOverride { - msg.OverrideUsername = ou - msg.SenderName = ou - } - - if oi, ok := post.Props["override_icon_url"].(string); ok && *cfg.ServiceSettings.EnablePostIconOverride { - msg.OverrideIconUrl = oi - } - - if fw, ok := post.Props["from_webhook"].(string); ok { - msg.FromWebhook = fw - } - - userLocale := utils.GetUserTranslations(user.Locale) - hasFiles := post.FileIds != nil && len(post.FileIds) > 0 - - msg.Message = a.getPushNotificationMessage(post.Message, explicitMention, channelWideMention, hasFiles, msg.SenderName, channelName, channel.Type, replyToThreadType, userLocale) + msg := a.BuildPushNotificationMessage(post, user, channel, channelName, senderName, explicitMention, channelWideMention, replyToThreadType) for _, session := range sessions { - if session.IsExpired() { continue } @@ -475,3 +433,61 @@ func DoesStatusAllowPushNotification(userNotifyProps model.StringMap, status *mo return false } + +func (a *App) BuildPushNotificationMessage(post *model.Post, user *model.User, channel *model.Channel, channelName string, senderName string, + explicitMention bool, channelWideMention bool, replyToThreadType string) model.PushNotification { + + msg := model.PushNotification{ + Category: model.CATEGORY_CAN_REPLY, + Version: model.PUSH_MESSAGE_V2, + Type: model.PUSH_TYPE_MESSAGE, + TeamId: channel.TeamId, + ChannelId: channel.Id, + PostId: post.Id, + RootId: post.RootId, + SenderId: post.UserId, + } + + if user.NotifyProps["push"] == "all" { + if unreadCount, err := a.Srv.Store.User().GetAnyUnreadPostCountForChannel(user.Id, channel.Id); err != nil { + msg.Badge = 1 + mlog.Error(fmt.Sprint("We could not get the unread message count for the user", user.Id, err), mlog.String("user_id", user.Id)) + } else { + msg.Badge = int(unreadCount) + } + } else { + if unreadCount, err := a.Srv.Store.User().GetUnreadCount(user.Id); err != nil { + msg.Badge = 1 + mlog.Error(fmt.Sprint("We could not get the unread message count for the user", user.Id, err), mlog.String("user_id", user.Id)) + } else { + msg.Badge = int(unreadCount) + } + } + + cfg := a.Config() + contentsConfig := *cfg.EmailSettings.PushNotificationContents + if contentsConfig != model.GENERIC_NO_CHANNEL_NOTIFICATION || channel.Type == model.CHANNEL_DIRECT { + msg.ChannelName = channelName + } + + msg.SenderName = senderName + if ou, ok := post.Props["override_username"].(string); ok && *cfg.ServiceSettings.EnablePostUsernameOverride { + msg.OverrideUsername = ou + msg.SenderName = ou + } + + if oi, ok := post.Props["override_icon_url"].(string); ok && *cfg.ServiceSettings.EnablePostIconOverride { + msg.OverrideIconUrl = oi + } + + if fw, ok := post.Props["from_webhook"].(string); ok { + msg.FromWebhook = fw + } + + userLocale := utils.GetUserTranslations(user.Locale) + hasFiles := post.FileIds != nil && len(post.FileIds) > 0 + + msg.Message = a.getPushNotificationMessage(post.Message, explicitMention, channelWideMention, hasFiles, msg.SenderName, channelName, channel.Type, replyToThreadType, userLocale) + + return msg +} diff --git a/app/notification_push_test.go b/app/notification_push_test.go index a7a7cf36d0..69fbb227bc 100644 --- a/app/notification_push_test.go +++ b/app/notification_push_test.go @@ -4,6 +4,7 @@ package app import ( + "fmt" "testing" "github.com/mattermost/mattermost-server/model" @@ -896,3 +897,53 @@ func TestGetPushNotificationMessage(t *testing.T) { }) } } + +func TestBuildPushNotificationMessage(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + team := th.CreateTeam() + sender := th.CreateUser() + receiver := th.CreateUser() + th.LinkUserToTeam(sender, team) + th.LinkUserToTeam(receiver, team) + channel := th.CreateChannel(team) + th.AddUserToChannel(sender, channel) + th.AddUserToChannel(receiver, channel) + + // Create three mention posts and two non-mention posts + th.CreateMessagePost(channel, "@channel Hello") + th.CreateMessagePost(channel, "@all Hello") + th.CreateMessagePost(channel, fmt.Sprintf("@%s Hello", receiver.Username)) + th.CreatePost(channel) + post := th.CreatePost(channel) + + for name, tc := range map[string]struct { + explicitMention bool + channelWideMention bool + replyToThreadType string + pushNotifyProps string + expectedBadge int + }{ + "only mentions included in badge count": { + explicitMention: false, + channelWideMention: true, + replyToThreadType: "", + pushNotifyProps: "mention", + expectedBadge: 3, + }, + "mentions and non-mentions included in badge count": { + explicitMention: false, + channelWideMention: true, + replyToThreadType: "", + pushNotifyProps: "all", + expectedBadge: 5, + }, + } { + t.Run(name, func(t *testing.T) { + receiver.NotifyProps["push"] = tc.pushNotifyProps + msg := th.App.BuildPushNotificationMessage(post, receiver, channel, channel.Name, sender.Username, tc.explicitMention, tc.channelWideMention, tc.replyToThreadType) + assert.Equal(t, tc.expectedBadge, msg.Badge) + }) + } +}