MM-18463 Added update_badge notification when marking a channel as unread (#12499)

* MM-18463 Added update_badge notification when marking a channel as unread

* Address feedback
Этот коммит содержится в:
Harrison Healey
2019-10-07 09:58:39 -04:00
коммит произвёл GitHub
родитель 3fa26bd0ee
Коммит 66da2bab2b
9 изменённых файлов: 92 добавлений и 78 удалений

Просмотреть файл

@@ -20,6 +20,7 @@ type NotificationType string
const NOTIFICATION_TYPE_CLEAR NotificationType = "clear"
const NOTIFICATION_TYPE_MESSAGE NotificationType = "message"
const NOTIFICATION_TYPE_UPDATE_BADGE NotificationType = "update_badge"
const PUSH_NOTIFICATION_HUB_WORKERS = 1000
const PUSH_NOTIFICATIONS_HUB_BUFFER_PER_WORKER = 50
@@ -53,16 +54,23 @@ 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 bool, channelWideMention bool, replyToThreadType string) *model.AppError {
sessions, err := a.getMobileAppSessions(user.Id)
msg, err := a.BuildPushNotificationMessage(post, user, channel, channelName, senderName, explicitMention, channelWideMention, replyToThreadType)
if err != nil {
return err
}
msg := a.BuildPushNotificationMessage(post, user, channel, channelName, senderName, explicitMention, channelWideMention, replyToThreadType)
return a.sendPushNotificationToAllSessions(msg, user.Id, "")
}
func (a *App) sendPushNotificationToAllSessions(msg *model.PushNotification, userId string, skipSessionId string) *model.AppError {
sessions, err := a.getMobileAppSessions(userId)
if err != nil {
return err
}
for _, session := range sessions {
if session.IsExpired() {
// Don't send notifications to this session if it's expired or we want to skip it
if session.IsExpired() || (skipSessionId != "" && skipSessionId == session.Id) {
continue
}
@@ -170,63 +178,22 @@ func (a *App) getPushNotificationMessage(postMessage string, explicitMention, ch
return senderName + userLocale("api.post.send_notifications_and_forget.push_general_message")
}
func (a *App) ClearPushNotificationSync(currentSessionId, userId, channelId string) {
sessions, err := a.getMobileAppSessions(userId)
if err != nil {
mlog.Error("error getting mobile app sessions", mlog.Err(err))
return
}
msg := model.PushNotification{
func (a *App) ClearPushNotificationSync(currentSessionId, userId, channelId string) *model.AppError {
msg := &model.PushNotification{
Type: model.PUSH_TYPE_CLEAR,
Version: model.PUSH_MESSAGE_V2,
ChannelId: channelId,
ContentAvailable: 1,
}
if unreadCount, err := a.Srv.Store.User().GetUnreadCount(userId); err != nil {
msg.Badge = 0
mlog.Error("We could not get the unread message count for", mlog.String("user_id", userId), mlog.Err(err))
} else {
msg.Badge = int(unreadCount)
unreadCount, err := a.Srv.Store.User().GetUnreadCount(userId)
if err != nil {
return err
}
for _, session := range sessions {
if currentSessionId != session.Id {
tmpMessage := model.PushNotificationFromJson(strings.NewReader(msg.ToJson()))
tmpMessage.SetDeviceIdAndPlatform(session.DeviceId)
tmpMessage.AckId = model.NewId()
msg.Badge = int(unreadCount)
err := a.sendToPushProxy(*tmpMessage, session)
if err != nil {
a.NotificationsLog.Error("Notification error",
mlog.String("ackId", tmpMessage.AckId),
mlog.String("type", tmpMessage.Type),
mlog.String("userId", session.UserId),
mlog.String("postId", tmpMessage.PostId),
mlog.String("channelId", tmpMessage.ChannelId),
mlog.String("deviceId", tmpMessage.DeviceId),
mlog.String("status", err.Error()),
)
continue
}
a.NotificationsLog.Info("Notification sent",
mlog.String("ackId", tmpMessage.AckId),
mlog.String("type", tmpMessage.Type),
mlog.String("userId", session.UserId),
mlog.String("postId", tmpMessage.PostId),
mlog.String("channelId", tmpMessage.ChannelId),
mlog.String("deviceId", tmpMessage.DeviceId),
mlog.String("status", model.PUSH_SEND_SUCCESS),
)
if a.Metrics != nil {
a.Metrics.IncrementPostSentPush()
}
}
}
return a.sendPushNotificationToAllSessions(msg, userId, currentSessionId)
}
func (a *App) ClearPushNotification(currentSessionId, userId, channelId string) {
@@ -239,6 +206,32 @@ func (a *App) ClearPushNotification(currentSessionId, userId, channelId string)
}
}
func (a *App) UpdateMobileAppBadgeSync(userId string) *model.AppError {
msg := &model.PushNotification{
Type: model.PUSH_TYPE_UPDATE_BADGE,
Version: model.PUSH_MESSAGE_V2,
Sound: "none",
ContentAvailable: 1,
}
unreadCount, err := a.Srv.Store.User().GetUnreadCount(userId)
if err != nil {
return err
}
msg.Badge = int(unreadCount)
return a.sendPushNotificationToAllSessions(msg, userId, "")
}
func (a *App) UpdateMobileAppBadge(userId string) {
channel := a.Srv.PushNotificationsHub.GetGoChannelFromUserId(userId)
channel <- PushNotification{
notificationType: NOTIFICATION_TYPE_UPDATE_BADGE,
userId: userId,
}
}
func (a *App) CreatePushNotificationsHub() {
hub := PushNotificationsHub{
Channels: []chan PushNotification{},
@@ -251,11 +244,13 @@ func (a *App) CreatePushNotificationsHub() {
func (a *App) pushNotificationWorker(notifications chan PushNotification) {
for notification := range notifications {
var err *model.AppError
switch notification.notificationType {
case NOTIFICATION_TYPE_CLEAR:
a.ClearPushNotificationSync(notification.currentSessionId, notification.userId, notification.channelId)
err = a.ClearPushNotificationSync(notification.currentSessionId, notification.userId, notification.channelId)
case NOTIFICATION_TYPE_MESSAGE:
a.sendPushNotificationSync(
err = a.sendPushNotificationSync(
notification.post,
notification.user,
notification.channel,
@@ -265,9 +260,15 @@ func (a *App) pushNotificationWorker(notifications chan PushNotification) {
notification.channelWideMention,
notification.replyToThreadType,
)
case NOTIFICATION_TYPE_UPDATE_BADGE:
err = a.UpdateMobileAppBadgeSync(notification.userId)
default:
mlog.Error("Invalid notification type", mlog.String("notification_type", string(notification.notificationType)))
}
if err != nil {
mlog.Error("Unable to send push notification", mlog.String("notification_type", string(notification.notificationType)), mlog.Err(err))
}
}
}
@@ -429,9 +430,9 @@ func DoesStatusAllowPushNotification(userNotifyProps model.StringMap, status *mo
}
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 {
explicitMention bool, channelWideMention bool, replyToThreadType string) (*model.PushNotification, *model.AppError) {
msg := model.PushNotification{
msg := &model.PushNotification{
Category: model.CATEGORY_CAN_REPLY,
Version: model.PUSH_MESSAGE_V2,
Type: model.PUSH_TYPE_MESSAGE,
@@ -443,19 +444,19 @@ func (a *App) BuildPushNotificationMessage(post *model.Post, user *model.User, c
}
if user.NotifyProps["push"] == "all" {
if unreadCount, err := a.Srv.Store.User().GetAnyUnreadPostCountForChannel(user.Id, channel.Id); err != nil {
msg.Badge = 1
mlog.Error("We could not get the unread message count for the user", mlog.String("user_id", user.Id), mlog.Err(err))
} else {
msg.Badge = int(unreadCount)
unreadCount, err := a.Srv.Store.User().GetAnyUnreadPostCountForChannel(user.Id, channel.Id)
if err != nil {
return nil, err
}
msg.Badge = int(unreadCount)
} else {
if unreadCount, err := a.Srv.Store.User().GetUnreadCount(user.Id); err != nil {
msg.Badge = 1
mlog.Error("We could not get the unread message count for the user", mlog.String("user_id", user.Id), mlog.Err(err))
} else {
msg.Badge = int(unreadCount)
unreadCount, err := a.Srv.Store.User().GetUnreadCount(user.Id)
if err != nil {
return nil, err
}
msg.Badge = int(unreadCount)
}
cfg := a.Config()
@@ -483,5 +484,5 @@ func (a *App) BuildPushNotificationMessage(post *model.Post, user *model.User, c
msg.Message = a.getPushNotificationMessage(post.Message, explicitMention, channelWideMention, hasFiles, msg.SenderName, channelName, channel.Type, replyToThreadType, userLocale)
return msg
return msg, nil
}