From fb3c17567215a3aebab4c218f8fbd2998e13a430 Mon Sep 17 00:00:00 2001 From: Himanshu Date: Sat, 28 Mar 2026 21:07:45 +0000 Subject: [PATCH] perf(models): add partial index for deactivated account lookups Implemented a partial compound index on { active: 1, lastLogin: 1 } for accounts where active is false. This ensures O(1) time complexity for security telemetry detection logic, preventing collection scans during brute-force attempts on deactivated accounts. --- .changeset/gold-badgers-worry.md | 5 ++ packages/models/src/models/Users.ts | 132 ++++++++++++++-------------- 2 files changed, 73 insertions(+), 64 deletions(-) create mode 100644 .changeset/gold-badgers-worry.md diff --git a/.changeset/gold-badgers-worry.md b/.changeset/gold-badgers-worry.md new file mode 100644 index 0000000000000..caabf2330fdbf --- /dev/null +++ b/.changeset/gold-badgers-worry.md @@ -0,0 +1,5 @@ +--- +'@rocket.chat/models': patch +--- + +perf(models): add partial index for deactivated account lookups diff --git a/packages/models/src/models/Users.ts b/packages/models/src/models/Users.ts index 95cc31246380a..6b707219b7fee 100644 --- a/packages/models/src/models/Users.ts +++ b/packages/models/src/models/Users.ts @@ -100,6 +100,10 @@ export class UsersRaw extends BaseRaw> implements IU key: { active: 1, lastLogin: 1 }, partialFilterExpression: { active: true, lastLogin: { $exists: true } }, }, + { + key: { active: 1, lastLogin: 1 }, + partialFilterExpression: { active: false }, + } ]; } @@ -564,26 +568,26 @@ export class UsersRaw extends BaseRaw> implements IU const departmentFilter = department ? [ - { - $lookup: { - from: 'rocketchat_livechat_department_agents', - let: { userId: '$_id' }, - pipeline: [ - { - $match: { - $expr: { - $and: [{ $eq: ['$$userId', '$agentId'] }, { $eq: ['$departmentId', department] }], - }, + { + $lookup: { + from: 'rocketchat_livechat_department_agents', + let: { userId: '$_id' }, + pipeline: [ + { + $match: { + $expr: { + $and: [{ $eq: ['$$userId', '$agentId'] }, { $eq: ['$departmentId', department] }], }, }, - ], - as: 'department', - }, - }, - { - $match: { department: { $size: 1 } }, + }, + ], + as: 'department', }, - ] + }, + { + $match: { department: { $size: 1 } }, + }, + ] : []; const aggregate: Document[] = [ @@ -644,26 +648,26 @@ export class UsersRaw extends BaseRaw> implements IU ); const departmentFilter = department ? [ - { - $lookup: { - from: 'rocketchat_livechat_department_agents', - let: { userId: '$_id' }, - pipeline: [ - { - $match: { - $expr: { - $and: [{ $eq: ['$$userId', '$agentId'] }, { $eq: ['$departmentId', department] }], - }, + { + $lookup: { + from: 'rocketchat_livechat_department_agents', + let: { userId: '$_id' }, + pipeline: [ + { + $match: { + $expr: { + $and: [{ $eq: ['$$userId', '$agentId'] }, { $eq: ['$departmentId', department] }], }, }, - ], - as: 'department', - }, - }, - { - $match: { department: { $size: 1 } }, + }, + ], + as: 'department', }, - ] + }, + { + $match: { department: { $size: 1 } }, + }, + ] : []; const aggregate: Document[] = [ @@ -763,23 +767,23 @@ export class UsersRaw extends BaseRaw> implements IU }, ...(departmentId ? { - 'queueInfo.chatsForDepartment': { - $size: { - $filter: { - input: '$subs', - as: 'sub', - cond: { - $and: [ - { $eq: ['$$sub.t', 'l'] }, - { $eq: ['$$sub.open', true] }, - { $ne: ['$$sub.onHold', true] }, - { $eq: ['$$sub.department', departmentId] }, - ], - }, + 'queueInfo.chatsForDepartment': { + $size: { + $filter: { + input: '$subs', + as: 'sub', + cond: { + $and: [ + { $eq: ['$$sub.t', 'l'] }, + { $eq: ['$$sub.open', true] }, + { $ne: ['$$sub.onHold', true] }, + { $eq: ['$$sub.department', departmentId] }, + ], }, }, }, - } + }, + } : {}), }, }, @@ -2980,15 +2984,15 @@ export class UsersRaw extends BaseRaw> implements IU const update: UpdateFilter = { ...(bio.trim() ? { - $set: { - bio, - }, - } + $set: { + bio, + }, + } : { - $unset: { - bio: 1, - }, - }), + $unset: { + bio: 1, + }, + }), }; return this.updateOne({ _id }, update); } @@ -2997,15 +3001,15 @@ export class UsersRaw extends BaseRaw> implements IU const update: UpdateFilter = { ...(nickname.trim() ? { - $set: { - nickname, - }, - } + $set: { + nickname, + }, + } : { - $unset: { - nickname: 1, - }, - }), + $unset: { + nickname: 1, + }, + }), }; return this.updateOne({ _id }, update); }