diff --git a/packages/hydrooj/src/handler/contest.ts b/packages/hydrooj/src/handler/contest.ts index d3840bc8c..fce6e3d0c 100644 --- a/packages/hydrooj/src/handler/contest.ts +++ b/packages/hydrooj/src/handler/contest.ts @@ -562,7 +562,10 @@ export class ContestManagementHandler extends ContestManagementBaseHandler { owner_udoc: await user.getById(domainId, this.tdoc.owner), pdict: await problem.getList(domainId, this.tdoc.pids, true, true, [...problem.PROJECTION_CONTEST_LIST, 'tag']), files: sortFiles(this.tdoc.files || []), - udict: await user.getListForRender(domainId, tcdocs.map((i) => i.owner), this.user.hasPerm(PERM.PERM_VIEW_DISPLAYNAME)), + udict: await user.getListForRender( + domainId, tcdocs.map((i) => i.owner), + this.user.hasPerm(PERM.PERM_VIEW_DISPLAYNAME) ? ['displayName'] : [] + ), tcdocs, urlForFile: (filename: string) => this.url('contest_file_download', { tid, filename }), }; @@ -668,8 +671,10 @@ export class ContestUserHandler extends ContestManagementBaseHandler { for (const tsdoc of tsdocs) { tsdoc.endAt = (this.tdoc.duration && tsdoc.startAt) ? moment(tsdoc.startAt).add(this.tdoc.duration, 'hours').toDate() : null; } - const udict = await user.getListForRender(domainId, - [this.tdoc.owner, ...tsdocs.map((i) => i.uid)], this.user.hasPerm(PERM.PERM_VIEW_DISPLAYNAME)); + const udict = await user.getListForRender( + domainId, [this.tdoc.owner, ...tsdocs.map((i) => i.uid)], + this.user.hasPerm(PERM.PERM_VIEW_DISPLAYNAME) ? ['displayName'] : [] + ); this.response.body = { tdoc: this.tdoc, tsdocs, udict }; this.response.pjax = 'partials/contest_user.html'; this.response.template = 'contest_user.html'; @@ -709,7 +714,7 @@ export class ContestBalloonHandler extends ContestManagementBaseHandler { owner_udoc: await user.getById(domainId, this.tdoc.owner), pdict: await problem.getList(domainId, this.tdoc.pids, true, true, problem.PROJECTION_CONTEST_LIST), bdocs, - udict: await user.getListForRender(domainId, uids, this.user.hasPerm(PERM.PERM_VIEW_DISPLAYNAME)), + udict: await user.getListForRender(domainId, uids, this.user.hasPerm(PERM.PERM_VIEW_DISPLAYNAME) ? ['displayName'] : []), }; this.response.pjax = 'partials/contest_balloon.html'; this.response.template = 'contest_balloon.html'; diff --git a/packages/hydrooj/src/handler/problem.ts b/packages/hydrooj/src/handler/problem.ts index ca6bf2a0a..929521933 100644 --- a/packages/hydrooj/src/handler/problem.ts +++ b/packages/hydrooj/src/handler/problem.ts @@ -960,7 +960,7 @@ export class ProblemStatisticsHandler extends ProblemDetailHandler { 'record', ); const [udict, udoc] = await Promise.all([ - user.getListForRender(domainId, rsdocs.map((i) => i.uid), this.user.hasPerm(PERM.PERM_VIEW_DISPLAYNAME)), + user.getListForRender(domainId, rsdocs.map((i) => i.uid), this.user.hasPerm(PERM.PERM_VIEW_DISPLAYNAME) ? ['displayName'] : []), user.getById(domainId, this.pdoc.owner), ]); this.response.template = 'problem_statistics.html'; diff --git a/packages/hydrooj/src/handler/training.ts b/packages/hydrooj/src/handler/training.ts index 9442b5c5f..c616ca978 100644 --- a/packages/hydrooj/src/handler/training.ts +++ b/packages/hydrooj/src/handler/training.ts @@ -112,7 +112,7 @@ class TrainingDetailHandler extends Handler { const canViewHidden = this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN) || this.user._id; const [udoc, udict, pdict, psdict, selfPsdict] = await Promise.all([ user.getById(domainId, tdoc.owner), - user.getListForRender(domainId, enrollUsers, this.user.hasPerm(PERM.PERM_VIEW_DISPLAYNAME)), + user.getListForRender(domainId, enrollUsers, this.user.hasPerm(PERM.PERM_VIEW_DISPLAYNAME) ? ['displayName'] : []), problem.getList(domainId, pids, canViewHidden, true), problem.getListStatus(domainId, uid, pids), shouldCompare ? problem.getListStatus(domainId, this.user._id, pids) : {}, diff --git a/packages/hydrooj/src/model/contest.ts b/packages/hydrooj/src/model/contest.ts index c4abe746e..aaaa016b9 100644 --- a/packages/hydrooj/src/model/contest.ts +++ b/packages/hydrooj/src/model/contest.ts @@ -225,7 +225,7 @@ const acm = buildContestRule({ async scoreboard(config, _, tdoc, pdict, cursor) { const rankedTsdocs = await db.ranked(cursor, (a, b) => a.score === b.score && a.time === b.time); const uids = rankedTsdocs.map(([, tsdoc]) => tsdoc.uid); - const udict = await user.getListForRender(tdoc.domainId, uids, config.showDisplayName); + const udict = await user.getListForRender(tdoc.domainId, uids, config.showDisplayName ? ['displayName'] : []); // Find first accept const first = {}; const data = await document.collStatus.aggregate([ @@ -396,7 +396,7 @@ const oi = buildContestRule({ async scoreboard(config, _, tdoc, pdict, cursor) { const rankedTsdocs = await db.ranked(cursor, (a, b) => a.score === b.score); const uids = rankedTsdocs.map(([, tsdoc]) => tsdoc.uid); - const udict = await user.getListForRender(tdoc.domainId, uids, config.showDisplayName); + const udict = await user.getListForRender(tdoc.domainId, uids, config.showDisplayName ? ['displayName'] : []); const psdict = {}; const first = {}; await Promise.all(tdoc.pids.map(async (pid) => { @@ -746,7 +746,7 @@ const homework = buildContestRule({ async scoreboard(config, _, tdoc, pdict, cursor) { const rankedTsdocs = await db.ranked(cursor, (a, b) => a.score === b.score); const uids = rankedTsdocs.map(([, tsdoc]) => tsdoc.uid); - const udict = await user.getListForRender(tdoc.domainId, uids, config.showDisplayName); + const udict = await user.getListForRender(tdoc.domainId, uids, config.showDisplayName ? ['displayName'] : []); const columns = await this.scoreboardHeader(config, _, tdoc, pdict); const rows: ScoreboardRow[] = [ columns, diff --git a/packages/hydrooj/src/model/user.ts b/packages/hydrooj/src/model/user.ts index 8a4389bd1..de315081c 100644 --- a/packages/hydrooj/src/model/user.ts +++ b/packages/hydrooj/src/model/user.ts @@ -384,9 +384,12 @@ class UserModel { return projection ? coll.find(params).project(buildProjection(projection)) : coll.find(params); } - static async getListForRender(domainId: string, uids: number[], showDisplayName = true) { + static async getListForRender(domainId: string, uids: number[], extraFields?: string[]): Promise; + static async getListForRender(domainId: string, uids: number[], arg: string[] | boolean) { + const fields = ['_id', 'uname', 'mail', 'avatar', 'school', 'studentId'].concat(arg instanceof Array ? arg : []); + const showDisplayName = arg instanceof Array ? fields.includes('displayName') : arg; const [udocs, vudocs, dudocs] = await Promise.all([ - UserModel.getMulti({ _id: { $in: uids } }, ['_id', 'uname', 'mail', 'avatar', 'school', 'studentId']).toArray(), + UserModel.getMulti({ _id: { $in: uids } }, fields).toArray(), collV.find({ _id: { $in: uids } }).toArray(), domain.getDomainUserMulti(domainId, uids).project({ uid: 1, ...(showDisplayName ? { displayName: 1 } : {}) }).toArray(), ]);