From e479d5266e21bea0fc8ec9d5b7ff989014581ca5 Mon Sep 17 00:00:00 2001 From: undefined Date: Wed, 7 Dec 2022 17:49:04 +0800 Subject: [PATCH] core: problem.getPrefixList: handle pid --- packages/hydrooj/src/handler/problem.ts | 11 ++++++++--- packages/hydrooj/src/typeutils.ts | 2 +- packages/hydrooj/src/utils.ts | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/hydrooj/src/handler/problem.ts b/packages/hydrooj/src/handler/problem.ts index ec4389e7..82a2f075 100644 --- a/packages/hydrooj/src/handler/problem.ts +++ b/packages/hydrooj/src/handler/problem.ts @@ -35,6 +35,7 @@ import * as bus from '../service/bus'; import { Handler, param, post, query, route, Types, } from '../service/server'; +import { buildProjection } from '../utils'; import { registerResolver, registerValue } from './api'; import { ContestDetailBaseHandler } from './contest'; @@ -992,15 +993,19 @@ export class ProblemCreateHandler extends Handler { export class ProblemPrefixListHandler extends Handler { @param('prefix', Types.Name) async get(domainId: string, prefix: string) { - const [pdocs, pdoc] = await Promise.all([ + const projection = ['domainId', 'docId', 'pid', 'title'] as const; + const [pdocs, pdoc, apdoc] = await Promise.all([ problem.getPrefixList(domainId, prefix), - problem.get(domainId, Number.isSafeInteger(+prefix) ? +prefix : prefix, ['domainId', 'docId', 'pid', 'title']), + problem.get(domainId, Number.isSafeInteger(+prefix) ? +prefix : prefix, projection), + /^P\d+$/.test(prefix) ? problem.get(domainId, +prefix.substring(1), projection) : Promise.resolve(null), ]); + if (apdoc) pdocs.unshift(apdoc); if (pdoc) pdocs.unshift(pdoc); if (pdocs.length < 20) { const search = global.Hydro.lib.problemSearch || defaultSearch; const result = await search(domainId, prefix, { limit: 20 - pdocs.length }); - const docs = await problem.getMulti(domainId, { docId: { $in: result.hits.map((i) => +i.split('/')[1]) } }).toArray(); + const docs = await problem.getMulti(domainId, { docId: { $in: result.hits.map((i) => +i.split('/')[1]) } }) + .project(buildProjection(projection)).toArray(); pdocs.push(...docs); } this.response.body = uniqBy(pdocs, 'docId'); diff --git a/packages/hydrooj/src/typeutils.ts b/packages/hydrooj/src/typeutils.ts index c1948905..9c44841a 100644 --- a/packages/hydrooj/src/typeutils.ts +++ b/packages/hydrooj/src/typeutils.ts @@ -10,7 +10,7 @@ export type NestKeys = O extends object ? O extends S ? n export type Value = { [K in keyof O]: V }; -export type Projection = (string & keyof O)[]; +export type Projection = readonly (string & keyof O)[]; export type Omit = Pick>; export type Filter = T extends U ? T : never; export type MaybeArray = T | T[]; diff --git a/packages/hydrooj/src/utils.ts b/packages/hydrooj/src/utils.ts index c427317e..063dfe1c 100644 --- a/packages/hydrooj/src/utils.ts +++ b/packages/hydrooj/src/utils.ts @@ -1,4 +1,4 @@ -export function buildProjection(fields: Array): Record { +export function buildProjection(fields: readonly (string | number)[]): Record { const o = {}; for (const k of fields) o[k] = 1; return o;