From f75c8c7c9f15d3985263653371365fd8a94754d8 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Wed, 1 Jan 2020 13:46:51 +0530 Subject: [PATCH] MM-20225: Fix notification badge count for All Activity (#13353) When push notification setting was selected to "All Activity", we were just sending the unread count for the channel from where the message was sent and not for all channels. After discussion with @enahum and @migbot, we decided to keep the badge count to only refer to mentions and not unread posts. Therefore, we only take the unread count for the user irrespective of the notify_props settings. Updated the tests to reflect that. Co-authored-by: mattermod --- app/notification_push.go | 17 ++--------------- app/notification_push_test.go | 31 ++++++++++++++++++------------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/app/notification_push.go b/app/notification_push.go index d6bd1bbdf1..7c51ec96b3 100644 --- a/app/notification_push.go +++ b/app/notification_push.go @@ -453,11 +453,11 @@ func (a *App) BuildPushNotificationMessage(contentsConfig string, post *model.Po msg = a.buildFullPushNotificationMessage(contentsConfig, post, user, channel, channelName, senderName, explicitMention, channelWideMention, replyToThreadType) } - badge, err := a.getPushNotificationBadge(user, channel) + unreadCount, err := a.Srv.Store.User().GetUnreadCount(user.Id) if err != nil { return nil, err } - msg.Badge = badge + msg.Badge = int(unreadCount) return msg, nil } @@ -519,16 +519,3 @@ func (a *App) buildFullPushNotificationMessage(contentsConfig string, post *mode return msg } - -func (a *App) getPushNotificationBadge(user *model.User, channel *model.Channel) (int, *model.AppError) { - var unreadCount int64 - var err *model.AppError - - if user.NotifyProps["push"] == "all" { - unreadCount, err = a.Srv.Store.User().GetAnyUnreadPostCountForChannel(user.Id, channel.Id) - } else { - unreadCount, err = a.Srv.Store.User().GetUnreadCount(user.Id) - } - - return int(unreadCount), err -} diff --git a/app/notification_push_test.go b/app/notification_push_test.go index a7db86e395..0830ae3634 100644 --- a/app/notification_push_test.go +++ b/app/notification_push_test.go @@ -909,16 +909,21 @@ func TestBuildPushNotificationMessageMentions(t *testing.T) { receiver := th.CreateUser() th.LinkUserToTeam(sender, team) th.LinkUserToTeam(receiver, team) - channel := th.CreateChannel(team) - th.AddUserToChannel(sender, channel) - th.AddUserToChannel(receiver, channel) + channel1 := th.CreateChannel(team) + th.AddUserToChannel(sender, channel1) + th.AddUserToChannel(receiver, channel1) + + channel2 := th.CreateChannel(team) + th.AddUserToChannel(sender, channel2) + th.AddUserToChannel(receiver, channel2) // 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) + th.CreateMessagePost(channel1, "@channel Hello") + th.CreateMessagePost(channel1, "@all Hello") + th.CreateMessagePost(channel1, fmt.Sprintf("@%s Hello in channel 1", receiver.Username)) + th.CreateMessagePost(channel2, fmt.Sprintf("@%s Hello in channel 2", receiver.Username)) + th.CreatePost(channel1) + post := th.CreatePost(channel1) for name, tc := range map[string]struct { explicitMention bool @@ -927,24 +932,24 @@ func TestBuildPushNotificationMessageMentions(t *testing.T) { pushNotifyProps string expectedBadge int }{ - "only mentions included in badge count": { + "only mentions included for notify_props=mention": { explicitMention: false, channelWideMention: true, replyToThreadType: "", pushNotifyProps: "mention", - expectedBadge: 3, + expectedBadge: 4, }, - "mentions and non-mentions included in badge count": { + "only mentions included for notify_props=all": { explicitMention: false, channelWideMention: true, replyToThreadType: "", pushNotifyProps: "all", - expectedBadge: 5, + expectedBadge: 4, }, } { t.Run(name, func(t *testing.T) { receiver.NotifyProps["push"] = tc.pushNotifyProps - msg, err := th.App.BuildPushNotificationMessage(model.FULL_NOTIFICATION, post, receiver, channel, channel.Name, sender.Username, tc.explicitMention, tc.channelWideMention, tc.replyToThreadType) + msg, err := th.App.BuildPushNotificationMessage(model.FULL_NOTIFICATION, post, receiver, channel1, channel1.Name, sender.Username, tc.explicitMention, tc.channelWideMention, tc.replyToThreadType) require.Nil(t, err) assert.Equal(t, tc.expectedBadge, msg.Badge) })