[MM-18058] Use GetAnyUnreadPostCountForChannel for NotifyProps.push == all (#11979)
* Use GetAnyUnreadPostCountForChannel for NotifyProps.push == all * Extract out msg building and add unit test
Этот коммит содержится в:
коммит произвёл
Jesús Espino
родитель
e1eb839636
Коммит
614ca90ca2
@@ -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()
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user