From ac722011bdd0aaaa4b3758aea9751d4c64d2fe5a Mon Sep 17 00:00:00 2001 From: carlosrodrigues94 Date: Tue, 19 Jul 2022 18:10:21 -0300 Subject: [PATCH 1/4] fix: error in list teams --- apps/meteor/server/methods/browseChannels.js | 36 +++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/apps/meteor/server/methods/browseChannels.js b/apps/meteor/server/methods/browseChannels.js index 7f2e1a3c7837..eee84502938b 100644 --- a/apps/meteor/server/methods/browseChannels.js +++ b/apps/meteor/server/methods/browseChannels.js @@ -104,9 +104,14 @@ async function getChannelsAndGroups(user, canViewAnon, searchTerm, sort, paginat }; } -const getChannelsCountForTeam = mem((teamId) => Promise.await(Rooms.findByTeamId(teamId, { projection: { _id: 1 } }).count()), { - maxAge: 2000, -}); +const getChannelsCountForTeam = mem( + (teamId) => { + return Rooms.findByTeamId(teamId, { projection: { _id: 1 } }); + }, + { + maxAge: 2000, + }, +); async function getTeams(user, searchTerm, sort, pagination) { if (!user) { @@ -114,6 +119,7 @@ async function getTeams(user, searchTerm, sort, pagination) { } const userSubs = Subscriptions.cachedFindByUserId(user._id).fetch(); + const ids = userSubs.map((sub) => sub.rid); const { cursor, totalCount } = Rooms.findPaginatedContainingNameOrFNameInIdsAsTeamMain( searchTerm ? new RegExp(searchTerm, 'i') : null, @@ -143,19 +149,23 @@ async function getTeams(user, searchTerm, sort, pagination) { }, ); - const [rooms, total] = await Promise.all([ - cursor - .map((room) => ({ - ...room, - roomsCount: getChannelsCountForTeam(room.teamId), - })) - .toArray(), - totalCount, - ]); + const roomsPromises = cursor.map((room) => room).toArray(); + + const [rooms] = await Promise.all([roomsPromises]); + + const teams = []; + + for await (const room of rooms) { + const result = await getChannelsCountForTeam(room.teamId).toArray(); + + teams.push({ ...room, roomsCount: result.length }); + } + + const [total] = await Promise.all([totalCount]); return { total, - results: rooms, + results: teams, }; } From 2f566b0a7d41a7b31281b26d3dd85793a16768f1 Mon Sep 17 00:00:00 2001 From: carlosrodrigues94 Date: Wed, 20 Jul 2022 14:41:28 -0300 Subject: [PATCH 2/4] chore: improve code --- apps/meteor/server/methods/browseChannels.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/apps/meteor/server/methods/browseChannels.js b/apps/meteor/server/methods/browseChannels.js index eee84502938b..b1e2f4c2c7f4 100644 --- a/apps/meteor/server/methods/browseChannels.js +++ b/apps/meteor/server/methods/browseChannels.js @@ -149,20 +149,16 @@ async function getTeams(user, searchTerm, sort, pagination) { }, ); - const roomsPromises = cursor.map((room) => room).toArray(); - - const [rooms] = await Promise.all([roomsPromises]); + const [rooms, total] = await Promise.all([cursor.toArray(), totalCount]); const teams = []; for await (const room of rooms) { - const result = await getChannelsCountForTeam(room.teamId).toArray(); + const result = await getChannelsCountForTeam(room.teamId).count(); teams.push({ ...room, roomsCount: result.length }); } - const [total] = await Promise.all([totalCount]); - return { total, results: teams, From 0a25901980e6042cf0b814152509da9ab4609306 Mon Sep 17 00:00:00 2001 From: Matheus Barbosa Silva <36537004+matheusbsilva137@users.noreply.github.com> Date: Wed, 20 Jul 2022 16:35:52 -0300 Subject: [PATCH 3/4] Update apps/meteor/server/methods/browseChannels.js --- apps/meteor/server/methods/browseChannels.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/apps/meteor/server/methods/browseChannels.js b/apps/meteor/server/methods/browseChannels.js index b1e2f4c2c7f4..88f31873f5b4 100644 --- a/apps/meteor/server/methods/browseChannels.js +++ b/apps/meteor/server/methods/browseChannels.js @@ -154,9 +154,8 @@ async function getTeams(user, searchTerm, sort, pagination) { const teams = []; for await (const room of rooms) { - const result = await getChannelsCountForTeam(room.teamId).count(); - - teams.push({ ...room, roomsCount: result.length }); + const roomsCount = await getChannelsCountForTeam(room.teamId).count(); + teams.push({ ...room, roomsCount }); } return { From 13e8b06f67d37fc80d9eb55d7f06817ed5ff8e9c Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Wed, 20 Jul 2022 18:59:37 -0300 Subject: [PATCH 4/4] review --- apps/meteor/server/methods/browseChannels.js | 33 ++++++++------------ 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/apps/meteor/server/methods/browseChannels.js b/apps/meteor/server/methods/browseChannels.js index 88f31873f5b4..747ce3df9a64 100644 --- a/apps/meteor/server/methods/browseChannels.js +++ b/apps/meteor/server/methods/browseChannels.js @@ -104,14 +104,9 @@ async function getChannelsAndGroups(user, canViewAnon, searchTerm, sort, paginat }; } -const getChannelsCountForTeam = mem( - (teamId) => { - return Rooms.findByTeamId(teamId, { projection: { _id: 1 } }); - }, - { - maxAge: 2000, - }, -); +const getChannelsCountForTeam = mem((teamId) => Rooms.findByTeamId(teamId, { projection: { _id: 1 } }).count(), { + maxAge: 2000, +}); async function getTeams(user, searchTerm, sort, pagination) { if (!user) { @@ -119,7 +114,6 @@ async function getTeams(user, searchTerm, sort, pagination) { } const userSubs = Subscriptions.cachedFindByUserId(user._id).fetch(); - const ids = userSubs.map((sub) => sub.rid); const { cursor, totalCount } = Rooms.findPaginatedContainingNameOrFNameInIdsAsTeamMain( searchTerm ? new RegExp(searchTerm, 'i') : null, @@ -148,19 +142,18 @@ async function getTeams(user, searchTerm, sort, pagination) { }, }, ); - - const [rooms, total] = await Promise.all([cursor.toArray(), totalCount]); - - const teams = []; - - for await (const room of rooms) { - const roomsCount = await getChannelsCountForTeam(room.teamId).count(); - teams.push({ ...room, roomsCount }); - } + const results = await Promise.all( + ( + await cursor.toArray() + ).map(async (room) => ({ + ...room, + roomsCount: await getChannelsCountForTeam(room.teamId), + })), + ); return { - total, - results: teams, + total: await totalCount, + results, }; }