diff --git a/packages/hydrooj/src/handler/problem.ts b/packages/hydrooj/src/handler/problem.ts index 93d905922..9d2194ec6 100644 --- a/packages/hydrooj/src/handler/problem.ts +++ b/packages/hydrooj/src/handler/problem.ts @@ -145,7 +145,6 @@ export class ProblemMainHandler extends Handler { : await problem.list( domainId, query, sort.length ? 1 : page, limit, quick ? ['title', 'pid', 'domainId', 'docId'] : undefined, - this.user._id, ); if (total) { pcount = total; diff --git a/packages/hydrooj/src/model/problem.ts b/packages/hydrooj/src/model/problem.ts index 2f7dc4a1c..42754ef3e 100644 --- a/packages/hydrooj/src/model/problem.ts +++ b/packages/hydrooj/src/model/problem.ts @@ -23,6 +23,7 @@ import { import { buildProjection } from '../utils'; import { PERM, STATUS } from './builtin'; import * as document from './document'; +import DomainModel from './domain'; import RecordModel from './record'; import SolutionModel from './solution'; import storage from './storage'; @@ -32,9 +33,10 @@ export interface ProblemDoc extends Document { } export type Field = keyof ProblemDoc; const logger = new Logger('problem'); -function sortable(source: string) { - const [namespace, pid] = source.includes('-') ? source.split('-') : ['', source]; - return (namespace ? namespace.padEnd(6, ' ') : '') + pid.replace(/(\d+)/g, (str) => (str.length >= 6 ? str : ('0'.repeat(6 - str.length) + str))); +function sortable(source: string, namespaces: Record = {}) { + const [namespace, pid] = source.includes('-') ? source.split('-') : ['default', source]; + return ((namespaces ? `${namespaces[namespace]}-` : '') + pid) + .replace(/(\d+)/g, (str) => (str.length >= 6 ? str : ('0'.repeat(6 - str.length) + str))); } function findOverrideContent(dir: string, base: string) { @@ -149,8 +151,9 @@ export class ProblemModel { content: string, owner: number, tag: string[] = [], meta: ProblemCreateOptions = {}, ) { + const ddoc = await DomainModel.get(domainId); const args: Partial = { - title, tag, hidden: meta.hidden || false, nSubmit: 0, nAccept: 0, sort: sortable(pid || `P${docId}`), + title, tag, hidden: meta.hidden || false, nSubmit: 0, nAccept: 0, sort: sortable(pid || `P${docId}`, ddoc?.namespaces), }; if (pid) args.pid = pid; if (meta.difficulty) args.difficulty = meta.difficulty; @@ -171,9 +174,10 @@ export class ProblemModel { rawConfig = false, ): Promise { if (Number.isSafeInteger(+pid)) pid = +pid; + const ddoc = await DomainModel.get(domainId); const res = typeof pid === 'number' ? await document.get(domainId, document.TYPE_PROBLEM, pid, projection) - : (await document.getMulti(domainId, document.TYPE_PROBLEM, { sort: sortable(pid), pid }) + : (await document.getMulti(domainId, document.TYPE_PROBLEM, { sort: sortable(pid, ddoc?.namespaces), pid }) .project(buildProjection(projection)).limit(1).toArray())[0]; if (!res) return null; try { @@ -209,11 +213,12 @@ export class ProblemModel { static async edit(domainId: string, _id: number, $set: Partial): Promise { const delpid = $set.pid === ''; + const ddoc = await DomainModel.get(domainId); if (delpid) { delete $set.pid; - $set.sort = sortable(`P${_id}`); + $set.sort = sortable(`P${_id}`, ddoc.namespaces); } else if ($set.pid) { - $set.sort = sortable($set.pid); + $set.sort = sortable($set.pid, ddoc.namespaces); } await bus.parallel('problem/before-edit', $set); const result = await document.set(domainId, document.TYPE_PROBLEM, _id, $set, delpid ? { pid: '' } : undefined);