From 71f86cf79b0796a08d25cc43a08457f56df79420 Mon Sep 17 00:00:00 2001 From: undefined Date: Tue, 25 May 2021 23:06:20 +0800 Subject: [PATCH] core: remove pdoc.psdoc hack --- packages/hydrooj/src/handler/contest.ts | 2 +- packages/hydrooj/src/handler/homework.ts | 2 +- packages/hydrooj/src/handler/problem.ts | 22 +++++++++---------- packages/hydrooj/src/model/document.ts | 2 +- packages/hydrooj/src/model/problem.ts | 13 +++++------ packages/hydrooj/src/script/rating.ts | 2 +- .../partials/problem-sidebar-information.html | 15 ------------- 7 files changed, 21 insertions(+), 37 deletions(-) diff --git a/packages/hydrooj/src/handler/contest.ts b/packages/hydrooj/src/handler/contest.ts index 1a10a6e80..8b3fb2e17 100644 --- a/packages/hydrooj/src/handler/contest.ts +++ b/packages/hydrooj/src/handler/contest.ts @@ -247,7 +247,7 @@ class ContestProblemHandler extends Handler { async _prepare(domainId: string, tid: ObjectID, pid: number) { [this.tdoc, this.pdoc] = await Promise.all([ contest.get(domainId, tid), - problem.get(domainId, pid, this.user._id), + problem.get(domainId, pid), ]); [this.tsdoc, this.udoc] = await Promise.all([ contest.getStatus(domainId, this.tdoc.docId, this.user._id), diff --git a/packages/hydrooj/src/handler/homework.ts b/packages/hydrooj/src/handler/homework.ts index 2ffb0923b..eec3087a5 100644 --- a/packages/hydrooj/src/handler/homework.ts +++ b/packages/hydrooj/src/handler/homework.ts @@ -132,7 +132,7 @@ class HomeworkDetailProblemHandler extends Handler { this.checkPerm(PERM.PERM_VIEW_PROBLEM); [this.tdoc, this.pdoc, this.tsdoc] = await Promise.all([ contest.get(domainId, tid, document.TYPE_HOMEWORK), - problem.get(domainId, pid, this.user._id), + problem.get(domainId, pid), contest.getStatus(domainId, tid, this.user._id, document.TYPE_HOMEWORK), ]); this.udoc = await user.getById(domainId, this.tdoc.owner); diff --git a/packages/hydrooj/src/handler/problem.ts b/packages/hydrooj/src/handler/problem.ts index 8a29f5aa4..dc5956d18 100644 --- a/packages/hydrooj/src/handler/problem.ts +++ b/packages/hydrooj/src/handler/problem.ts @@ -98,7 +98,7 @@ export class ProblemMainHandler extends ProblemHandler { ); if (sort) pdocs = pdocs.sort((a, b) => sort.indexOf(a.docId) - sort.indexOf(b.docId)); if (q && +q) { - const pdoc = await problem.get(domainId, +q, this.user._id, problem.PROJECTION_LIST); + const pdoc = await problem.get(domainId, +q, problem.PROJECTION_LIST); if (pdoc) pdocs.unshift(pdoc); } if (this.user.hasPriv(PRIV.PRIV_USER_PROFILE)) { @@ -174,24 +174,24 @@ export class ProblemRandomHandler extends ProblemHandler { } } -export interface PdocWithPsdoc extends ProblemDoc { - psdoc?: ProblemStatusDoc -} - export class ProblemDetailHandler extends ProblemHandler { - pdoc: PdocWithPsdoc; + pdoc: ProblemDoc; udoc: User; + psdoc: ProblemStatusDoc; @route('pid', Types.Name, true, null, parsePid) async _prepare(domainId: string, pid: number | string) { this.response.template = 'problem_detail.html'; - this.pdoc = await problem.get(domainId, pid, this.user._id); + this.pdoc = await problem.get(domainId, pid); if (!this.pdoc) throw new ProblemNotFoundError(domainId, pid); if (this.pdoc.hidden && !this.user.own(this.pdoc)) { this.checkPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN); } await bus.serial('problem/get', this.pdoc, this); - this.udoc = await user.getById(domainId, this.pdoc.owner); + [this.psdoc, this.udoc] = await Promise.all([ + problem.getStatus(domainId, this.pdoc.docId, this.user._id), + user.getById(domainId, this.pdoc.owner), + ]); this.response.body = { pdoc: this.pdoc, udoc: this.udoc, @@ -219,8 +219,8 @@ export class ProblemDetailHandler extends ProblemHandler { this.response.body.pdoc.content = this.response.body.pdoc.content .replace(/\(file:\/\//g, `(./${this.pdoc.docId}/file/`); } - if (this.pdoc.psdoc) { - this.response.body.rdoc = await record.get(this.domainId, this.pdoc.psdoc.rid); + if (this.psdoc) { + this.response.body.rdoc = await record.get(this.domainId, this.psdoc.rid); } this.response.body.ctdocs = await contest.getRelated(this.domainId, this.pdoc.docId); } @@ -599,7 +599,7 @@ export class ProblemPrefixListHandler extends Handler { async get(domainId: string, prefix: string) { this.response.body = await problem.getPrefixList(domainId, prefix); if (!Number.isNaN(+prefix)) { - const pdoc = await problem.get(domainId, +prefix, undefined, ['domainId', 'docId', 'pid', 'title']); + const pdoc = await problem.get(domainId, +prefix, ['domainId', 'docId', 'pid', 'title']); if (pdoc) this.response.body.unshift(pdoc); } } diff --git a/packages/hydrooj/src/model/document.ts b/packages/hydrooj/src/model/document.ts index 2a106ffa9..c315dd14a 100644 --- a/packages/hydrooj/src/model/document.ts +++ b/packages/hydrooj/src/model/document.ts @@ -129,7 +129,7 @@ export function deleteMultiStatus( export function getMulti( domainId: string, docType: K, query?: FilterQuery, projection?: Projection, ): Cursor { - let cursor = coll.find({ ...query, docType, domainId }); + let cursor = coll.find({ docType, domainId, ...query }); if (projection) cursor = cursor.project(buildProjection(projection)); return cursor; } diff --git a/packages/hydrooj/src/model/problem.ts b/packages/hydrooj/src/model/problem.ts index 52f18c451..d7fb9f7e2 100644 --- a/packages/hydrooj/src/model/problem.ts +++ b/packages/hydrooj/src/model/problem.ts @@ -74,25 +74,24 @@ export class ProblemModel { static async get( domainId: string, pid: string | number, - uid: number = null, projection: Projection = ProblemModel.PROJECTION_PUBLIC, + projection: Projection = ProblemModel.PROJECTION_PUBLIC, ): Promise { if (typeof pid !== 'number') { if (Number.isSafeInteger(parseInt(pid, 10))) pid = parseInt(pid, 10); } - const pdoc = typeof pid === 'number' + return typeof pid === 'number' ? await document.get(domainId, document.TYPE_PROBLEM, pid, projection) : (await document.getMulti(domainId, document.TYPE_PROBLEM, { pid }).toArray())[0]; - if (!pdoc) return null; - if (uid) { - pdoc.psdoc = await document.getStatus(domainId, document.TYPE_PROBLEM, pdoc.docId, uid); - } - return pdoc; } static getMulti(domainId: string, query: FilterQuery, projection = ProblemModel.PROJECTION_LIST) { return document.getMulti(domainId, document.TYPE_PROBLEM, query, projection); } + static getStatus(domainId: string, docId: number, uid: number) { + return document.getStatus(domainId, document.TYPE_PROBLEM, docId, uid); + } + static getMultiStatus(domainId: string, query: FilterQuery) { return document.getMultiStatus(domainId, document.TYPE_PROBLEM, query); } diff --git a/packages/hydrooj/src/script/rating.ts b/packages/hydrooj/src/script/rating.ts index ab8ada849..a169257ea 100644 --- a/packages/hydrooj/src/script/rating.ts +++ b/packages/hydrooj/src/script/rating.ts @@ -18,7 +18,7 @@ async function runProblem(pdoc: ProblemDoc, udict: ND): Promise; async function runProblem(domainId: string, pid: number, udict: ND): Promise; async function runProblem(...arg: any[]) { const pdoc: ProblemDoc = (typeof arg[0] === 'string') - ? await problem.get(arg[0], arg[1], -1) + ? await problem.get(arg[0], arg[1]) : arg[0]; const udict: ND = (typeof arg[0] === 'string') ? arg[2] : arg[1]; const [, nPages] = await paginate( diff --git a/packages/ui-default/templates/partials/problem-sidebar-information.html b/packages/ui-default/templates/partials/problem-sidebar-information.html index ddaff647b..d750cfd72 100644 --- a/packages/ui-default/templates/partials/problem-sidebar-information.html +++ b/packages/ui-default/templates/partials/problem-sidebar-information.html @@ -9,21 +9,6 @@
{{ pdoc.config.timeMax if pdoc.config.timeMax == pdoc.config.timeMin else pdoc.config.timeMin+'~'+pdoc.config.timeMax }}ms
{{ _('Memory') }}
{{ pdoc.config.memoryMax if pdoc.config.memoryMax == pdoc.config.memoryMin else pdoc.config.memoryMin+'~'+pdoc.config.memoryMax }}MiB
- {% if page_name != 'discussion_node' and page_name != 'discussion_detail' %} - {% if handler.user.hasPriv(PRIV.PRIV_USER_PROFILE) %} -
{{ _('Submission') }}
-
- {% if pdoc.psdoc.status %} - - - {{ model.builtin.STATUS_TEXTS[pdoc['psdoc']['status']] }} - - {% else %} - {{ _('(None)') }} - {% endif %} -
- {% endif %} - {% endif %}
{{ _('Difficulty') }}
{{ pdoc['difficulty'] or _('(None)') }}
{{ _('Category') }}