Skip to content

Commit

Permalink
aggregation changes
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinav Kumar <[email protected]>
  • Loading branch information
abhinavkrin committed Dec 20, 2024
1 parent 74f786c commit 6ceb8e3
Showing 1 changed file with 64 additions and 88 deletions.
152 changes: 64 additions & 88 deletions apps/meteor/server/lib/findUsersOfRoomOrderedByRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,25 @@ export async function findUsersOfRoomOrderedByRole({
...(sort || defaultSort),
};

const userLookupPipeline: Document[] = [{ $match: { $expr: { $eq: ['$_id', '$$userId'] } } }];

if (status) {
userLookupPipeline.push({ $match: { status } });
}

userLookupPipeline.push(
const userLookupPipeline: Document[] = [
{
$match: {
$and: [
{
$expr: { $eq: ['$_id', '$$userId'] },
active: true,
username: {
$exists: true,
...(exceptions.length > 0 && { $nin: exceptions }),
},
...(status && { status }),
...(filter && orStmt.length > 0 && { $or: orStmt }),
},
...extraQuery,
],
},
},
{
$project: {
_id: 1,
username: 1,
name: 1,
nickname: 1,
status: 1,
avatarETag: 1,
_updatedAt: 1,
federated: 1,
statusConnection: 1,
},
},
);
];

const defaultPriority = rolesInOrder.length + 1;

Expand All @@ -99,91 +82,84 @@ export async function findUsersOfRoomOrderedByRole({
},
},
{ $unwind: '$userDetails' },
{
$addFields: {
primaryRole: {
$reduce: {
input: '$roles',
initialValue: { role: null, priority: defaultPriority },
in: {
$let: {
vars: {
currentPriority: {
$switch: {
branches,
default: defaultPriority,
];

const membersResult = Subscriptions.col.aggregate(
[
{ $match: { rid } },
...filteredPipeline,
{
$addFields: {
primaryRole: {
$reduce: {
input: '$roles',
initialValue: { role: null, priority: defaultPriority },
in: {
$let: {
vars: {
currentPriority: {
$switch: {
branches,
default: defaultPriority,
},
},
},
},
in: {
$cond: [
{
$and: [{ $in: ['$$this', rolesInOrder] }, { $lt: ['$$currentPriority', '$$value.priority'] }],
},
{ role: '$$this', priority: '$$currentPriority' },
'$$value',
],
in: {
$cond: [
{
$and: [{ $in: ['$$this', rolesInOrder] }, { $lt: ['$$currentPriority', '$$value.priority'] }],
},
{ role: '$$this', priority: '$$currentPriority' },
'$$value',
],
},
},
},
},
},
},
},
},
{
$addFields: {
rolePriority: { $ifNull: ['$primaryRole.priority', defaultPriority] },
},
},
{
$project: {
_id: '$userDetails._id',
rid: 1,
roles: 1,
primaryRole: '$primaryRole.role',
rolePriority: 1,
username: '$userDetails.username',
name: '$userDetails.name',
nickname: '$userDetails.nickname',
status: '$userDetails.status',
avatarETag: '$userDetails.avatarETag',
_updatedAt: '$userDetails._updatedAt',
federated: '$userDetails.federated',
statusConnection: '$userDetails.statusConnection',
{
$addFields: {
rolePriority: { $ifNull: ['$primaryRole.priority', defaultPriority] },
},
},
},
];

const facetPipeline: Document[] = [
{ $match: { rid } },
{
$facet: {
totalCount: [{ $match: { rid } }, ...filteredPipeline, { $count: 'total' }],
members: [
{ $match: { rid } },
...filteredPipeline,
{ $sort: sortCriteria },
...(skip > 0 ? [{ $skip: skip }] : []),
...(limit > 0 ? [{ $limit: limit }] : []),
],
{
$project: {
_id: '$userDetails._id',
rid: 1,
roles: 1,
primaryRole: '$primaryRole.role',
rolePriority: 1,
username: '$userDetails.username',
name: '$userDetails.name',
nickname: '$userDetails.nickname',
status: '$userDetails.status',
avatarETag: '$userDetails.avatarETag',
_updatedAt: '$userDetails._updatedAt',
federated: '$userDetails.federated',
statusConnection: '$userDetails.statusConnection',
},
},
},
{ $sort: sortCriteria },
...(skip > 0 ? [{ $skip: skip }] : []),
...(limit > 0 ? [{ $limit: limit }] : []),
],
{
$project: {
members: 1,
totalCount: { $arrayElemAt: ['$totalCount.total', 0] },
},
allowDiskUse: true,
},
];
);

const totalResult = Subscriptions.col.aggregate([{ $match: { rid } }, ...filteredPipeline, { $count: 'total' }], { allowDiskUse: true });

const [result] = await Subscriptions.col.aggregate(facetPipeline, { allowDiskUse: true }).toArray();
const [members, [{ totalCount }]] = await Promise.all([membersResult.toArray(), totalResult.toArray()]);

return {
members: result.members.map((member: any) => {
members: members.map((member: any) => {
delete member.primaryRole;
delete member.rolePriority;
return member;
}),
total: result.totalCount,
total: totalCount,
};
}

0 comments on commit 6ceb8e3

Please sign in to comment.