From e701807cbcdd48bad9ad59d39ffc0f15c36561cf Mon Sep 17 00:00:00 2001 From: undefined Date: Tue, 6 Apr 2021 12:01:26 +0800 Subject: [PATCH] core: update contentType for import --- packages/hydrooj/package.json | 2 +- packages/hydrooj/src/entry/master.ts | 6 +++--- packages/hydrooj/src/handler/import.syzoj.ts | 4 ++-- packages/hydrooj/src/model/problem.ts | 13 ++++++++++--- packages/hydrooj/src/upgrade.ts | 13 +++++-------- packages/import-qduoj/handler.ts | 2 +- packages/import-qduoj/package.json | 2 +- 7 files changed, 23 insertions(+), 19 deletions(-) diff --git a/packages/hydrooj/package.json b/packages/hydrooj/package.json index ff29d6cd9..ea6669703 100644 --- a/packages/hydrooj/package.json +++ b/packages/hydrooj/package.json @@ -1,6 +1,6 @@ { "name": "hydrooj", - "version": "2.21.7", + "version": "2.21.8", "bin": "bin/hydrooj.js", "main": "dist/loader.js", "typings": "dist/loader.d.ts", diff --git a/packages/hydrooj/src/entry/master.ts b/packages/hydrooj/src/entry/master.ts index dd03b4ea7..05e2b387a 100644 --- a/packages/hydrooj/src/entry/master.ts +++ b/packages/hydrooj/src/entry/master.ts @@ -74,14 +74,14 @@ export async function load(call: Entry) { const expected = scripts.length; while (dbVer < expected) { logger.info('Upgrading database: from %d to %d', dbVer, expected); + const func = scripts[dbVer]; if (isFresh) { - const func = scripts[dbVer].toString(); - if (func.includes('_FRESH_INSTALL_IGNORE')) { + if (typeof func !== 'function' || func.toString().includes('_FRESH_INSTALL_IGNORE')) { dbVer++; continue; } } - const result = await scripts[dbVer](); + const result = await func(); if (!result) break; dbVer++; await modelSystem.set('db.ver', dbVer); diff --git a/packages/hydrooj/src/handler/import.syzoj.ts b/packages/hydrooj/src/handler/import.syzoj.ts index 99742b0a0..eb43f6b1e 100644 --- a/packages/hydrooj/src/handler/import.syzoj.ts +++ b/packages/hydrooj/src/handler/import.syzoj.ts @@ -96,7 +96,7 @@ class ProblemImportSYZOJHandler extends Handler { }); } const docId = await problem.add( - domainId, target, p.title, content, this.user._id, p.tags || [], hidden, + domainId, target, p.title, JSON.stringify(content), this.user._id, p.tags || [], hidden, ); const r = download(`${url}testdata/download`); const file = path.resolve(os.tmpdir(), 'hydro', `import_${domainId}_${docId}.zip`); @@ -172,7 +172,7 @@ class ProblemImportSYZOJHandler extends Handler { ...result.body.localizedContentsOfAllLocales, ][0].title; const docId = await problem.add( - domainId, target, title, content, this.user._id, tags || [], hidden, + domainId, target, title, JSON.stringify(content), this.user._id, tags || [], hidden, ); const syncFiles = async () => { const judge = result.body.judgeInfo; diff --git a/packages/hydrooj/src/model/problem.ts b/packages/hydrooj/src/model/problem.ts index 651024478..693024dde 100644 --- a/packages/hydrooj/src/model/problem.ts +++ b/packages/hydrooj/src/model/problem.ts @@ -6,7 +6,6 @@ import * as document from './document'; import domain from './domain'; import { buildProjection } from '../utils'; import type { ProblemStatusDoc, Pdict, Document } from '../interface'; -import { Content } from '../loader'; import { ArrayKeys, NumberKeys, Projection } from '../typeutils'; import { ProblemNotFoundError, ValidationError } from '../error'; import storage from '../service/storage'; @@ -45,16 +44,24 @@ export class ProblemModel { } static async add( - domainId: string, pid: string = null, title: string, content: Content, owner: number, + domainId: string, pid: string = null, title: string, content: string, owner: number, tag: string[] = [], hidden = false, ) { const pidCounter = await domain.inc(domainId, 'pidCounter', 1); + const result = await ProblemModel.addWithId(domainId, pidCounter, pid, title, content, owner, tag, hidden); + return result; + } + + static async addWithId( + domainId: string, docId: number, pid: string = null, title: string, + content: string, owner: number, tag: string[] = [], hidden = false, + ) { const args: Partial = { title, tag, hidden, nSubmit: 0, nAccept: 0, }; if (pid) args.pid = pid; await bus.serial('problem/before-add', args); - const result = await document.add(domainId, content, owner, document.TYPE_PROBLEM, pidCounter, null, null, args); + const result = await document.add(domainId, content, owner, document.TYPE_PROBLEM, docId, null, null, args); await bus.emit('problem/add', args, result); return result; } diff --git a/packages/hydrooj/src/upgrade.ts b/packages/hydrooj/src/upgrade.ts index 6651bec3d..f738743a6 100644 --- a/packages/hydrooj/src/upgrade.ts +++ b/packages/hydrooj/src/upgrade.ts @@ -18,7 +18,7 @@ import * as system from './model/system'; import difficultyAlgorithm from './lib/difficulty'; const logger = new Logger('upgrade'); -type UpgradeScript = () => Promise; +type UpgradeScript = void | (() => Promise); export async function iterateAllDomain(cb: (ddoc: DomainDoc, current?: number, total?: number) => Promise) { const ddocs = await domain.getMulti().project({ _id: 1, owner: 1 }).toArray(); @@ -58,9 +58,7 @@ export async function iterateAllProblem( const scripts: UpgradeScript[] = [ // Mark as used - async function _0_1() { - return true; - }, + null, // Init async function _1_2() { const ddoc = await domain.get('system'); @@ -253,10 +251,9 @@ const scripts: UpgradeScript[] = [ }); return true; }, - async function _16_17() { - return true; - }, - async function _17_18() { + null, + null, + async function _18_19() { const _FRESH_INSTALL_IGNORE = 1; await iterateAllProblem(['content'], async (pdoc) => { if (typeof pdoc.content !== 'string') { diff --git a/packages/import-qduoj/handler.ts b/packages/import-qduoj/handler.ts index 2c440e530..4049cedc8 100644 --- a/packages/import-qduoj/handler.ts +++ b/packages/import-qduoj/handler.ts @@ -76,7 +76,7 @@ class ImportQduojHandler extends Handler { if (+pdoc.display_id) pdoc.display_id = `P${pdoc.display_id}`; const n = await problem.get(domainId, pdoc.display_id); if (n) pdoc.display_id = null; - const pid = await problem.add(domainId, pdoc.display_id, pdoc.title, content, this.user._id, pdoc.tags); + const pid = await problem.add(domainId, pdoc.display_id, pdoc.title, JSON.stringify(content), this.user._id, pdoc.tags); const config: ProblemConfig = { time: `${pdoc.time_limit}ms`, memory: `${pdoc.memory_limit}m`, diff --git a/packages/import-qduoj/package.json b/packages/import-qduoj/package.json index 9f493dd7e..ba3622670 100644 --- a/packages/import-qduoj/package.json +++ b/packages/import-qduoj/package.json @@ -1,6 +1,6 @@ { "name": "@hydrooj/import-qduoj", - "version": "1.1.5", + "version": "1.1.6", "description": "Import QDUOJ problem export", "main": "package.json", "repository": "https://github.com/hydro-dev/Hydro.git",