Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: fix directory endpoint not listing teams #26310

Merged
merged 5 commits into from
Jul 20, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions apps/meteor/server/methods/browseChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,22 @@ 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 } });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as i understand this returns a cursor, cursors are VERY FAST, they only generates a object structure, no cache needed

},
{
maxAge: 2000,
},
);

async function getTeams(user, searchTerm, sort, pagination) {
if (!user) {
return;
}

const userSubs = Subscriptions.cachedFindByUserId(user._id).fetch();

matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
const ids = userSubs.map((sub) => sub.rid);
const { cursor, totalCount } = Rooms.findPaginatedContainingNameOrFNameInIdsAsTeamMain(
searchTerm ? new RegExp(searchTerm, 'i') : null,
Expand Down Expand Up @@ -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) {
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
const result = await getChannelsCountForTeam(room.teamId).toArray();

teams.push({ ...room, roomsCount: result.length });
}

const [total] = await Promise.all([totalCount]);
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved

return {
total,
results: rooms,
results: teams,
};
}

Expand Down