Skip to content

Commit c3ad3cc

Browse files
committed
aggregation changes
Signed-off-by: Abhinav Kumar <abhinav@avitechlab.com>
1 parent 141a551 commit c3ad3cc

File tree

1 file changed

+64
-88
lines changed

1 file changed

+64
-88
lines changed

apps/meteor/server/lib/findUsersOfRoomOrderedByRole.ts

+64-88
Original file line numberDiff line numberDiff line change
@@ -45,42 +45,25 @@ export async function findUsersOfRoomOrderedByRole({
4545
...(sort || defaultSort),
4646
};
4747

48-
const userLookupPipeline: Document[] = [{ $match: { $expr: { $eq: ['$_id', '$$userId'] } } }];
49-
50-
if (status) {
51-
userLookupPipeline.push({ $match: { status } });
52-
}
53-
54-
userLookupPipeline.push(
48+
const userLookupPipeline: Document[] = [
5549
{
5650
$match: {
5751
$and: [
5852
{
53+
$expr: { $eq: ['$_id', '$$userId'] },
5954
active: true,
6055
username: {
6156
$exists: true,
6257
...(exceptions.length > 0 && { $nin: exceptions }),
6358
},
59+
...(status && { status }),
6460
...(filter && orStmt.length > 0 && { $or: orStmt }),
6561
},
6662
...extraQuery,
6763
],
6864
},
6965
},
70-
{
71-
$project: {
72-
_id: 1,
73-
username: 1,
74-
name: 1,
75-
nickname: 1,
76-
status: 1,
77-
avatarETag: 1,
78-
_updatedAt: 1,
79-
federated: 1,
80-
statusConnection: 1,
81-
},
82-
},
83-
);
66+
];
8467

8568
const defaultPriority = rolesInOrder.length + 1;
8669

@@ -99,91 +82,84 @@ export async function findUsersOfRoomOrderedByRole({
9982
},
10083
},
10184
{ $unwind: '$userDetails' },
102-
{
103-
$addFields: {
104-
primaryRole: {
105-
$reduce: {
106-
input: '$roles',
107-
initialValue: { role: null, priority: defaultPriority },
108-
in: {
109-
$let: {
110-
vars: {
111-
currentPriority: {
112-
$switch: {
113-
branches,
114-
default: defaultPriority,
85+
];
86+
87+
const membersResult = Subscriptions.col.aggregate(
88+
[
89+
{ $match: { rid } },
90+
...filteredPipeline,
91+
{
92+
$addFields: {
93+
primaryRole: {
94+
$reduce: {
95+
input: '$roles',
96+
initialValue: { role: null, priority: defaultPriority },
97+
in: {
98+
$let: {
99+
vars: {
100+
currentPriority: {
101+
$switch: {
102+
branches,
103+
default: defaultPriority,
104+
},
115105
},
116106
},
117-
},
118-
in: {
119-
$cond: [
120-
{
121-
$and: [{ $in: ['$$this', rolesInOrder] }, { $lt: ['$$currentPriority', '$$value.priority'] }],
122-
},
123-
{ role: '$$this', priority: '$$currentPriority' },
124-
'$$value',
125-
],
107+
in: {
108+
$cond: [
109+
{
110+
$and: [{ $in: ['$$this', rolesInOrder] }, { $lt: ['$$currentPriority', '$$value.priority'] }],
111+
},
112+
{ role: '$$this', priority: '$$currentPriority' },
113+
'$$value',
114+
],
115+
},
126116
},
127117
},
128118
},
129119
},
130120
},
131121
},
132-
},
133-
{
134-
$addFields: {
135-
rolePriority: { $ifNull: ['$primaryRole.priority', defaultPriority] },
136-
},
137-
},
138-
{
139-
$project: {
140-
_id: '$userDetails._id',
141-
rid: 1,
142-
roles: 1,
143-
primaryRole: '$primaryRole.role',
144-
rolePriority: 1,
145-
username: '$userDetails.username',
146-
name: '$userDetails.name',
147-
nickname: '$userDetails.nickname',
148-
status: '$userDetails.status',
149-
avatarETag: '$userDetails.avatarETag',
150-
_updatedAt: '$userDetails._updatedAt',
151-
federated: '$userDetails.federated',
152-
statusConnection: '$userDetails.statusConnection',
122+
{
123+
$addFields: {
124+
rolePriority: { $ifNull: ['$primaryRole.priority', defaultPriority] },
125+
},
153126
},
154-
},
155-
];
156-
157-
const facetPipeline: Document[] = [
158-
{ $match: { rid } },
159-
{
160-
$facet: {
161-
totalCount: [{ $match: { rid } }, ...filteredPipeline, { $count: 'total' }],
162-
members: [
163-
{ $match: { rid } },
164-
...filteredPipeline,
165-
{ $sort: sortCriteria },
166-
...(skip > 0 ? [{ $skip: skip }] : []),
167-
...(limit > 0 ? [{ $limit: limit }] : []),
168-
],
127+
{
128+
$project: {
129+
_id: '$userDetails._id',
130+
rid: 1,
131+
roles: 1,
132+
primaryRole: '$primaryRole.role',
133+
rolePriority: 1,
134+
username: '$userDetails.username',
135+
name: '$userDetails.name',
136+
nickname: '$userDetails.nickname',
137+
status: '$userDetails.status',
138+
avatarETag: '$userDetails.avatarETag',
139+
_updatedAt: '$userDetails._updatedAt',
140+
federated: '$userDetails.federated',
141+
statusConnection: '$userDetails.statusConnection',
142+
},
169143
},
170-
},
144+
{ $sort: sortCriteria },
145+
...(skip > 0 ? [{ $skip: skip }] : []),
146+
...(limit > 0 ? [{ $limit: limit }] : []),
147+
],
171148
{
172-
$project: {
173-
members: 1,
174-
totalCount: { $arrayElemAt: ['$totalCount.total', 0] },
175-
},
149+
allowDiskUse: true,
176150
},
177-
];
151+
);
152+
153+
const totalResult = Subscriptions.col.aggregate([{ $match: { rid } }, ...filteredPipeline, { $count: 'total' }], { allowDiskUse: true });
178154

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

181157
return {
182-
members: result.members.map((member: any) => {
158+
members: members.map((member: any) => {
183159
delete member.primaryRole;
184160
delete member.rolePriority;
185161
return member;
186162
}),
187-
total: result.totalCount,
163+
total: totalCount,
188164
};
189165
}

0 commit comments

Comments
 (0)