From ab344a98211a9e81ac920efea93503a5c4c90ecc Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Thu, 8 Nov 2018 13:09:52 -0200 Subject: [PATCH 1/7] [IMPROVE] method getUsersOfRoom --- server/methods/getUsersOfRoom.js | 35 +++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/server/methods/getUsersOfRoom.js b/server/methods/getUsersOfRoom.js index ce9239fd86e4..68985b70fcbd 100644 --- a/server/methods/getUsersOfRoom.js +++ b/server/methods/getUsersOfRoom.js @@ -1,7 +1,7 @@ import { Meteor } from 'meteor/meteor'; Meteor.methods({ - getUsersOfRoom(rid, showAll) { + async getUsersOfRoom(rid, showAll) { const userId = Meteor.userId(); if (!userId) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'getUsersOfRoom' }); @@ -16,17 +16,32 @@ Meteor.methods({ throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'getUsersOfRoom' }); } - const subscriptions = RocketChat.models.Subscriptions.findByRoomIdWhenUsernameExists(rid, { fields: { 'u._id': 1 } }).fetch(); - const userIds = subscriptions.map((s) => s.u._id); // TODO: CACHE: expensive - const options = { fields: { username: 1, name: 1 } }; - - const users = showAll === true - ? RocketChat.models.Users.findUsersWithUsernameByIds(userIds, options).fetch() - : RocketChat.models.Users.findUsersWithUsernameByIdsNotOffline(userIds, options).fetch(); + const subscriptions = RocketChat.models.Subscriptions.findByRoomIdWhenUsernameExists(rid, { fields: { 'u._id': 1 } }); return { - total: userIds.length, - records: users, + total: subscriptions.count(), + records: await RocketChat.models.Subscriptions.model.rawCollection().aggregate([ + { $match: { rid: 'GENERAL' } }, + + { + $lookup: + { + from: 'users', + localField: 'u._id', + foreignField: '_id', + as: 'u', + }, + }, + ...(showAll ? [{ $match: { 'u.status': 'online' } }] : []), + { + $project: { + _id: { $arrayElemAt: ['$u._id', 0] }, + name: { $arrayElemAt: ['$u.name', 0] }, + username: { $arrayElemAt: ['$u.username', 0] }, + }, + }, + + ]).toArray(), }; }, }); From 741ed477305060af2c246bdb71911685000d0333 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Thu, 8 Nov 2018 13:11:00 -0200 Subject: [PATCH 2/7] Update getUsersOfRoom.js --- server/methods/getUsersOfRoom.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/methods/getUsersOfRoom.js b/server/methods/getUsersOfRoom.js index 68985b70fcbd..e315b2b7b9cf 100644 --- a/server/methods/getUsersOfRoom.js +++ b/server/methods/getUsersOfRoom.js @@ -16,7 +16,7 @@ Meteor.methods({ throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'getUsersOfRoom' }); } - const subscriptions = RocketChat.models.Subscriptions.findByRoomIdWhenUsernameExists(rid, { fields: { 'u._id': 1 } }); + const subscriptions = RocketChat.models.Subscriptions.findByRoomIdWhenUsernameExists(rid); return { total: subscriptions.count(), From 5c07b1db8abdcde6e0510c4e0305024b9de30dc2 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Thu, 8 Nov 2018 13:12:55 -0200 Subject: [PATCH 3/7] Update getUsersOfRoom.js --- server/methods/getUsersOfRoom.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/methods/getUsersOfRoom.js b/server/methods/getUsersOfRoom.js index e315b2b7b9cf..b716bfd93e2b 100644 --- a/server/methods/getUsersOfRoom.js +++ b/server/methods/getUsersOfRoom.js @@ -21,7 +21,7 @@ Meteor.methods({ return { total: subscriptions.count(), records: await RocketChat.models.Subscriptions.model.rawCollection().aggregate([ - { $match: { rid: 'GENERAL' } }, + { $match: { rid } }, { $lookup: From c170863866c1d7cc1704801e5ae2c36635f3a5a0 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Mon, 3 Dec 2018 17:30:21 -0200 Subject: [PATCH 4/7] Update getUsersOfRoom.js --- server/methods/getUsersOfRoom.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/server/methods/getUsersOfRoom.js b/server/methods/getUsersOfRoom.js index b716bfd93e2b..038f861dc0aa 100644 --- a/server/methods/getUsersOfRoom.js +++ b/server/methods/getUsersOfRoom.js @@ -35,9 +35,7 @@ Meteor.methods({ ...(showAll ? [{ $match: { 'u.status': 'online' } }] : []), { $project: { - _id: { $arrayElemAt: ['$u._id', 0] }, - name: { $arrayElemAt: ['$u.name', 0] }, - username: { $arrayElemAt: ['$u.username', 0] }, + $replaceRoot: { newRoot: { $arrayElemAt: [ "$u", 0 ] } } }, }, From e1d2a302bfcccc5786c5b40a7dd3b79bb8c90391 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Mon, 3 Dec 2018 17:34:17 -0200 Subject: [PATCH 5/7] Use $replaceRoot --- server/methods/getUsersOfRoom.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/server/methods/getUsersOfRoom.js b/server/methods/getUsersOfRoom.js index 038f861dc0aa..754073ef6e7e 100644 --- a/server/methods/getUsersOfRoom.js +++ b/server/methods/getUsersOfRoom.js @@ -22,7 +22,6 @@ Meteor.methods({ total: subscriptions.count(), records: await RocketChat.models.Subscriptions.model.rawCollection().aggregate([ { $match: { rid } }, - { $lookup: { @@ -32,12 +31,16 @@ Meteor.methods({ as: 'u', }, }, - ...(showAll ? [{ $match: { 'u.status': 'online' } }] : []), { $project: { - $replaceRoot: { newRoot: { $arrayElemAt: [ "$u", 0 ] } } + 'u._id': 1, + 'u.name': 1, + 'u.username': 1, + 'u.status': 1, }, }, + ...(showAll ? [{ $match: { 'u.status': 'online' } }] : []), + { $replaceRoot: { newRoot: { $arrayElemAt: [ "$u", 0 ] } } }, ]).toArray(), }; From f380e6a324c2c871cb538a0da684cc6821e3b215 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Mon, 3 Dec 2018 17:45:34 -0200 Subject: [PATCH 6/7] Fix lint --- server/methods/getUsersOfRoom.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server/methods/getUsersOfRoom.js b/server/methods/getUsersOfRoom.js index 754073ef6e7e..36412655f14a 100644 --- a/server/methods/getUsersOfRoom.js +++ b/server/methods/getUsersOfRoom.js @@ -40,8 +40,7 @@ Meteor.methods({ }, }, ...(showAll ? [{ $match: { 'u.status': 'online' } }] : []), - { $replaceRoot: { newRoot: { $arrayElemAt: [ "$u", 0 ] } } }, - + { $replaceRoot: { newRoot: { $arrayElemAt: ['$u', 0] } } }, ]).toArray(), }; }, From 79c99921a9ee3147ac105c2a706f612496fbf6cf Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Mon, 3 Dec 2018 22:15:25 -0200 Subject: [PATCH 7/7] Update getUsersOfRoom.js --- server/methods/getUsersOfRoom.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/methods/getUsersOfRoom.js b/server/methods/getUsersOfRoom.js index 36412655f14a..a939fa914570 100644 --- a/server/methods/getUsersOfRoom.js +++ b/server/methods/getUsersOfRoom.js @@ -39,7 +39,7 @@ Meteor.methods({ 'u.status': 1, }, }, - ...(showAll ? [{ $match: { 'u.status': 'online' } }] : []), + ...(showAll ? [] : [{ $match: { 'u.status': 'online' } }]), { $replaceRoot: { newRoot: { $arrayElemAt: ['$u', 0] } } }, ]).toArray(), };