const CATEGORY_MESSAGE = 'MESSAGE';
const CATEGORY_MESSAGE_NOREPLY = 'MESSAGE_NOREPLY';

let alwaysNotifyMobileBoolean;
RocketChat.settings.get('Notifications_Always_Notify_Mobile', (key, value) => {
	alwaysNotifyMobileBoolean = value;
});

// function getBadgeCount(userId) {
// 	const subscriptions = RocketChat.models.Subscriptions.findUnreadByUserId(userId).fetch();

// 	return subscriptions.reduce((unread, sub) => {
// 		return sub.unread + unread;
// 	}, 0);
// }

function canSendMessageToRoom(room, username) {
	return !((room.muted || []).includes(username));
}

export function sendSinglePush({ room, message, userId, receiverUsername, senderUsername, notificationMessage }) {
	RocketChat.PushNotification.send({
		roomId: message.rid,
		payload: {
			host: Meteor.absoluteUrl(),
			rid: message.rid,
			sender: message.u,
			type: room.t,
			name: room.name
		},
		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),
		usersTo: {
			userId
		},
		category: canSendMessageToRoom(room, receiverUsername) ? CATEGORY_MESSAGE : CATEGORY_MESSAGE_NOREPLY
	});
}

export function shouldNotifyMobile({
	disableAllMessageNotifications,
	mobilePushNotifications,
	hasMentionToAll,
	isHighlighted,
	hasMentionToUser,
	statusConnection,
	roomType
}) {
	if (disableAllMessageNotifications && mobilePushNotifications == null) {
		return false;
	}

	if (mobilePushNotifications === 'nothing') {
		return false;
	}

	if (!alwaysNotifyMobileBoolean && statusConnection === 'online') {
		return false;
	}

	if (!mobilePushNotifications) {
		if (RocketChat.settings.get('Accounts_Default_User_Preferences_mobileNotifications') === 'all') {
			return true;
		}
		if (RocketChat.settings.get('Accounts_Default_User_Preferences_mobileNotifications') === 'nothing') {
			return false;
		}
	}

	return roomType === 'd' || (!disableAllMessageNotifications && hasMentionToAll) || isHighlighted || mobilePushNotifications === 'all' || hasMentionToUser;
}

@@ -238,55 +238,54 @@
 );%0A%0A
-// function getBadgeCount(userId) %7B%0A// %09const s
+let SubscriptionRaw;%0AMeteor.startup(() =%3E %7B%0A%09S
 ubsc
@@ -291,17 +291,19 @@
 cription
-s
+Raw
  = Rocke
@@ -333,143 +333,297 @@
 ons.
-findUnreadByUserId(userId).fetch();%0A%0A// %09return subscriptions.reduce((unread, sub) =%3E %7B%0A// %09%09return sub.unread + unread;%0A// %09%7D, 0);%0A// 
+model.rawCollection();%0A%7D);%0A%0Aasync function getBadgeCount(userId) %7B%0A%09const %5B result %5D = await SubscriptionRaw.aggregate(%5B%0A%09%09%7B $match: %7B 'u._id': userId %7D %7D,%0A%09%09%7B%0A%09%09%09$group: %7B%0A%09%09%09%09_id: 'total',%0A%09%09%09%09total: %7B $sum: '$unread' %7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%5D).toArray();%0A%0A%09const %7B total %7D = result;%0A%09return total;%0A
 %7D%0A%0Af
@@ -721,32 +721,38 @@
 me));%0A%7D%0A%0Aexport 
+async 
 function sendSin
@@ -1362,17 +1362,20 @@
 ,%0A%09%09
-// 
 badge:
+ await
  get
@@ -1395,16 +1395,8 @@
 erId
-ToNotify
 ),%0A%09

Add badge back to push notifications (#10779)