Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
146 lines
3.5 KiB
TypeScript
146 lines
3.5 KiB
TypeScript
import { type IUser, ROOM_ROLE_PRIORITY_MAP, type ISubscription, type UserStatus } from '@rocket.chat/core-typings';
|
|
import { Subscriptions, Users } from '@rocket.chat/models';
|
|
import { escapeRegExp } from '@rocket.chat/tools';
|
|
import type { Document } from 'mongodb';
|
|
|
|
import { settings } from '../settings';
|
|
import { effectiveStatusExpression, effectiveStatusFilter } from './statusVisibility/effectiveStatus';
|
|
|
|
type FindUsersParam = {
|
|
rid: string;
|
|
status?: UserStatus[];
|
|
hidden?: Set<IUser['_id']>;
|
|
skip?: number;
|
|
limit?: number;
|
|
filter?: string;
|
|
sort?: Record<string, any>;
|
|
exceptions?: string[];
|
|
extraQuery?: Document[];
|
|
};
|
|
|
|
type UserWithRoleAndSubscriptionData = IUser & {
|
|
subscription: Pick<ISubscription, '_id' | 'status' | 'ts' | 'roles'>;
|
|
};
|
|
|
|
export async function findUsersOfRoomOrderedByRole({
|
|
rid,
|
|
status,
|
|
hidden,
|
|
skip = 0,
|
|
limit = 0,
|
|
filter = '',
|
|
sort = {},
|
|
exceptions = [],
|
|
extraQuery = [],
|
|
}: FindUsersParam): Promise<{ members: UserWithRoleAndSubscriptionData[]; total: number }> {
|
|
const searchFields = settings.get<string>('Accounts_SearchFields').trim().split(',');
|
|
const termRegex = new RegExp(escapeRegExp(filter), 'i');
|
|
const orStmt = filter && searchFields.length ? searchFields.map((field) => ({ [field.trim()]: termRegex })) : [];
|
|
|
|
const { rolePriority: rolePrioritySort, username: usernameSort } = sort;
|
|
|
|
const sortCriteria = {
|
|
rolePriority: rolePrioritySort ?? 1,
|
|
statusSortKey: -1,
|
|
...(usernameSort
|
|
? { username: usernameSort }
|
|
: {
|
|
...(settings.get('UI_Use_Real_Name') ? { name: 1 } : { username: 1 }),
|
|
}),
|
|
};
|
|
|
|
const matchUserFilter = {
|
|
$and: [
|
|
{
|
|
__rooms: rid,
|
|
active: true,
|
|
username: {
|
|
$exists: true,
|
|
...(exceptions.length > 0 && { $nin: exceptions }),
|
|
},
|
|
...(filter && orStmt.length > 0 && { $or: orStmt }),
|
|
},
|
|
...(status ? [effectiveStatusFilter(status, hidden)] : []),
|
|
...extraQuery,
|
|
],
|
|
};
|
|
|
|
const visibleStatus = hidden?.size ? effectiveStatusExpression(hidden) : '$status';
|
|
|
|
const membersResult = Users.col.aggregate<UserWithRoleAndSubscriptionData>(
|
|
[
|
|
{
|
|
$match: matchUserFilter,
|
|
},
|
|
{
|
|
$project: {
|
|
_id: 1,
|
|
name: 1,
|
|
username: 1,
|
|
nickname: 1,
|
|
status: visibleStatus,
|
|
avatarETag: 1,
|
|
_updatedAt: 1,
|
|
federated: 1,
|
|
statusSortKey: {
|
|
// Adding this because offline users should come last
|
|
$cond: [{ $eq: [visibleStatus, 'offline'] }, null, visibleStatus],
|
|
},
|
|
rolePriority: {
|
|
$ifNull: [`$roomRolePriorities.${rid}`, ROOM_ROLE_PRIORITY_MAP.default],
|
|
},
|
|
},
|
|
},
|
|
{ $sort: sortCriteria },
|
|
...(skip > 0 ? [{ $skip: skip }] : []),
|
|
...(limit > 0 ? [{ $limit: limit }] : []),
|
|
{
|
|
$lookup: {
|
|
from: Subscriptions.getCollectionName(),
|
|
as: 'subscription',
|
|
let: { userId: '$_id', roomId: rid },
|
|
pipeline: [
|
|
{
|
|
$match: {
|
|
$expr: {
|
|
$and: [{ $eq: ['$rid', '$$roomId'] }, { $eq: ['$u._id', '$$userId'] }],
|
|
},
|
|
},
|
|
},
|
|
{ $project: { roles: 1, status: 1, ts: 1 } },
|
|
],
|
|
},
|
|
},
|
|
{
|
|
$addFields: {
|
|
roles: { $arrayElemAt: ['$subscription.roles', 0] },
|
|
},
|
|
},
|
|
{
|
|
$unwind: {
|
|
path: '$subscription',
|
|
preserveNullAndEmptyArrays: true,
|
|
},
|
|
},
|
|
{
|
|
$project: {
|
|
statusSortKey: 0,
|
|
},
|
|
},
|
|
],
|
|
{
|
|
allowDiskUse: true,
|
|
},
|
|
);
|
|
|
|
const [members, totalCount] = await Promise.all([membersResult.toArray(), Users.countDocuments(matchUserFilter)]);
|
|
|
|
return {
|
|
members: members.map((member: any) => {
|
|
delete member.rolePriority;
|
|
return member;
|
|
}),
|
|
total: totalCount,
|
|
};
|
|
}
|