From 851e1672e74eec39a05c13219fa3c026cf98c668 Mon Sep 17 00:00:00 2001 From: Antares Date: Wed, 14 Oct 2026 17:09:29 +0800 Subject: [PATCH] core: fix: upsert each user when setting roles in bulk (#1224) * fix(domain): upsert each user when setting roles in bulk Use per-UID bulk upserts for batch role assignment and skip empty batches while preserving cache invalidation and autojoin behavior. Fixes hydro-dev/Hydro#1223 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * perf(domain): deduplicate UIDs before bulk role updates Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- packages/hydrooj/src/model/domain.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/hydrooj/src/model/domain.ts b/packages/hydrooj/src/model/domain.ts index db060092b..0a2d56f23 100644 --- a/packages/hydrooj/src/model/domain.ts +++ b/packages/hydrooj/src/model/domain.ts @@ -160,11 +160,18 @@ class DomainModel { deleteUserCache(udoc); return res; } + if (!uid.length) return undefined; const affected = await UserModel.getMulti({ _id: { $in: uid } }) .project<{ _id: number, mail: string, uname: string }>({ mail: 1, uname: 1 }) .toArray(); for (const udoc of affected) deleteUserCache(udoc); - return await collUser.updateMany({ domainId, uid: { $in: uid } }, update, { upsert: true }); + return await collUser.bulkWrite([...new Set(uid)].map((id) => ({ + updateOne: { + filter: { domainId, uid: id }, + update, + upsert: true, + }, + }))); } static async setJoin(domainId: string, uid: MaybeArray, join: boolean) {