core: use common plugin-api interface

This commit is contained in:
undefined committed 2022-09-30 19:02:40 +08:00
1 parent c61c875bee
commit 70a96aecac
54 files changed
+387 -566

No files matched your search

@@ -1,15 +1,10 @@
/* eslint-disable no-await-in-loop */
import os from 'os';
import path from 'path';
import AdmZip from 'adm-zip';
import fs from 'fs-extra';
import yaml from 'js-yaml';
import type { ContentNode, PluginContext, ProblemConfigFile } from 'hydrooj';
import { ValidationError } from 'hydrooj/src/error';
import { buildContent } from 'hydrooj/src/lib/content';
import { PERM } from 'hydrooj/src/model/builtin';
import problem from 'hydrooj/src/model/problem';
import { Handler } from 'hydrooj/src/service/server';
import {
AdmZip, buildContent, ContentNode, Context, fs, Handler, PERM,
ProblemConfigFile, ProblemModel, ValidationError, yaml,
} from 'hydrooj';
fs.ensureDirSync('/tmp/hydro/import-qduoj');
@@ -79,9 +74,9 @@ class ImportQduojHandler extends Handler {
});
}
if (+pdoc.display_id) pdoc.display_id = `P${pdoc.display_id}`;
const n = await problem.get(domainId, pdoc.display_id);
const n = await ProblemModel.get(domainId, pdoc.display_id);
if (n) pdoc.display_id = null;
const pid = await problem.add(domainId, pdoc.display_id, pdoc.title, buildContent(content, 'html'), this.user._id, pdoc.tags);
const pid = await ProblemModel.add(domainId, pdoc.display_id, pdoc.title, buildContent(content, 'html'), this.user._id, pdoc.tags);
const config: ProblemConfigFile = {
time: `${pdoc.time_limit}ms`,
memory: `${pdoc.memory_limit}m`,
@@ -89,11 +84,11 @@ class ImportQduojHandler extends Handler {
};
for (const tc of pdoc.test_case_score) {
await Promise.all([
problem.addTestdata(
ProblemModel.addTestdata(
domainId, pid, tc.input_name,
path.join(tmp, folder, 'testcase', tc.input_name),
),
problem.addTestdata(
ProblemModel.addTestdata(
domainId, pid, tc.output_name,
path.join(tmp, folder, 'testcase', tc.output_name),
),
@@ -107,8 +102,8 @@ class ImportQduojHandler extends Handler {
});
}
await Promise.all([
problem.addTestdata(domainId, pid, 'config.yaml', Buffer.from(yaml.dump(config))),
problem.edit(domainId, pid, { html: true }),
ProblemModel.addTestdata(domainId, pid, 'config.yaml', Buffer.from(yaml.dump(config))),
ProblemModel.edit(domainId, pid, { html: true }),
]);
}
} finally {
@@ -130,7 +125,7 @@ class ImportQduojHandler extends Handler {
}
}
export async function apply(ctx: PluginContext) {
export async function apply(ctx: Context) {
ctx.Route('problem_import_qduoj', '/problem/import/qduoj', ImportQduojHandler, PERM.PERM_CREATE_PROBLEM);
ctx.inject('ProblemAdd', 'problem_import_qduoj', { icon: 'copy', text: 'From QDUOJ Export' });
}
+1 -3
View File
@@ -8,8 +8,6 @@
"license": "AGPL-3.0-or-later",
"preferUnplugged": true,
"dependencies": {
"adm-zip": "0.5.5",
"decode-html": "^2.0.0",
"fs-extra": "^10.1.0"
"decode-html": "^2.0.0"
}
}