diff --git a/packages/rocketchat-lib/server/functions/notifications/mobile.js b/packages/rocketchat-lib/server/functions/notifications/mobile.js index 017b9235d661..68bc6fdc82b0 100644 --- a/packages/rocketchat-lib/server/functions/notifications/mobile.js +++ b/packages/rocketchat-lib/server/functions/notifications/mobile.js @@ -6,19 +6,31 @@ RocketChat.settings.get('Notifications_Always_Notify_Mobile', (key, value) => { alwaysNotifyMobileBoolean = value; }); -// function getBadgeCount(userId) { -// const subscriptions = RocketChat.models.Subscriptions.findUnreadByUserId(userId).fetch(); +let SubscriptionRaw; +Meteor.startup(() => { + SubscriptionRaw = RocketChat.models.Subscriptions.model.rawCollection(); +}); + +async function getBadgeCount(userId) { + const [ result ] = await SubscriptionRaw.aggregate([ + { $match: { 'u._id': userId } }, + { + $group: { + _id: 'total', + total: { $sum: '$unread' } + } + } + ]).toArray(); -// return subscriptions.reduce((unread, sub) => { -// return sub.unread + unread; -// }, 0); -// } + const { total } = result; + return total; +} function canSendMessageToRoom(room, username) { return !((room.muted || []).includes(username)); } -export function sendSinglePush({ room, message, userId, receiverUsername, senderUsername, notificationMessage }) { +export async function sendSinglePush({ room, message, userId, receiverUsername, senderUsername, notificationMessage }) { RocketChat.PushNotification.send({ roomId: message.rid, payload: { @@ -31,7 +43,7 @@ export function sendSinglePush({ room, message, userId, receiverUsername, sender roomName: RocketChat.settings.get('Push_show_username_room') ? `${ room.t === 'd' ? '' : '#' }${ RocketChat.roomTypes.getRoomName(room.t, room) }` : '', username: RocketChat.settings.get('Push_show_username_room') ? senderUsername : '', message: RocketChat.settings.get('Push_show_message') ? notificationMessage : ' ', - // badge: getBadgeCount(userIdToNotify), + badge: await getBadgeCount(userId), usersTo: { userId },