Skip to content

Commit

Permalink
[NEW][APPS] getUserUnreadMessageCount Bridge (#23972)
Browse files Browse the repository at this point in the history
Co-authored-by: Douglas Gubert <douglas.gubert@gmail.com>
  • Loading branch information
ggazzo and d-gubert authored Dec 20, 2021
1 parent fd63efa commit 2d6aa92
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
6 changes: 5 additions & 1 deletion app/apps/server/bridges/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IUserCreationOptions, IUser } from '@rocket.chat/apps-engine/definition

import { setUserAvatar, checkUsernameAvailability, deleteUser, _setStatusTextPromise } from '../../../lib/server/functions';
import { Users } from '../../../models/server';
import { Users as UsersRaw } from '../../../models/server/raw';
import { Subscriptions, Users as UsersRaw } from '../../../models/server/raw';
import { AppServerOrchestrator } from '../orchestrator';

export class AppUserBridge extends UserBridge {
Expand Down Expand Up @@ -107,4 +107,8 @@ export class AppUserBridge extends UserBridge {
protected async getActiveUserCount(): Promise<number> {
return Users.getActiveLocalUserCount();
}

protected async getUserUnreadMessageCount(uid: string): Promise<number> {
return Subscriptions.getBadgeCount(uid);
}
}
25 changes: 2 additions & 23 deletions app/lib/server/functions/notifications/mobile.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
import { Meteor } from 'meteor/meteor';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';

import { settings } from '../../../../settings';
import { Subscriptions } from '../../../../models';
import { Subscriptions } from '../../../../models/server/raw';
import { roomTypes } from '../../../../utils';

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

let SubscriptionRaw;
Meteor.startup(() => {
SubscriptionRaw = Subscriptions.model.rawCollection();
});

async function getBadgeCount(userId) {
const [result = {}] = await SubscriptionRaw.aggregate([
{ $match: { 'u._id': userId, archived: { $ne: true } } },
{
$group: {
_id: 'total',
total: { $sum: '$unread' },
},
},
]).toArray();

const { total } = result;
return total;
}

function enableNotificationReplyButton(room, username) {
// Some users may have permission to send messages even on readonly rooms, but we're ok with false negatives here in exchange of better perfomance
if (room.ro === true) {
Expand Down Expand Up @@ -68,7 +47,7 @@ export async function getPushData({ room, message, userId, senderUsername, sende
roomName: settings.get('Push_show_username_room') && roomTypes.getConfig(room.t).isGroupChat(room) ? `#${ roomTypes.getRoomName(room.t, room) }` : '',
username,
message: messageText,
badge: await getBadgeCount(userId),
badge: await Subscriptions.getBadgeCount(userId),
category: enableNotificationReplyButton(room, receiver.username) ? CATEGORY_MESSAGE : CATEGORY_MESSAGE_NOREPLY,
};
}
Expand Down
14 changes: 14 additions & 0 deletions app/models/server/raw/Subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ export class SubscriptionsRaw extends BaseRaw<T> {
super(col, trash);
}

async getBadgeCount(uid: string): Promise<number> {
const [result] = await this.col.aggregate<{ total: number } | undefined>([
{ $match: { 'u._id': uid, archived: { $ne: true } } },
{
$group: {
_id: 'total',
total: { $sum: '$unread' },
},
},
]).toArray();

return result?.total || 0;
}

findOneByRoomIdAndUserId(rid: string, uid: string, options: FindOneOptions<T> = {}): Promise<T | null> {
const query = {
rid,
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
"@nivo/heatmap": "0.73.0",
"@nivo/line": "0.62.0",
"@nivo/pie": "0.73.0",
"@rocket.chat/apps-engine": "^1.29.0-alpha.0.5607",
"@rocket.chat/apps-engine": "^1.29.0-alpha.0.5664",
"@rocket.chat/css-in-js": "^0.30.1",
"@rocket.chat/emitter": "^0.30.1",
"@rocket.chat/fuselage": "^0.6.3-dev.368",
Expand Down

0 comments on commit 2d6aa92

Please sign in to comment.