From 6905b9953e217874f6f31fdfc84f1f9aecbb9c34 Mon Sep 17 00:00:00 2001 From: undefined Date: Thu, 26 Aug 2021 01:11:50 +0800 Subject: [PATCH] core&ui: fix remote_judge for cors problem --- packages/hydrooj/package.json | 2 +- packages/hydrooj/src/handler/contest.ts | 34 ++++++++++++++----- packages/hydrooj/src/handler/homework.ts | 31 ++++++++++++----- packages/ui-default/package.json | 2 +- .../ui-default/pages/problem_submit.page.js | 9 ++--- 5 files changed, 54 insertions(+), 24 deletions(-) diff --git a/packages/hydrooj/package.json b/packages/hydrooj/package.json index 2e46d977c..adef8cb18 100644 --- a/packages/hydrooj/package.json +++ b/packages/hydrooj/package.json @@ -1,6 +1,6 @@ { "name": "hydrooj", - "version": "2.33.8", + "version": "2.33.9", "bin": "bin/hydrooj.js", "main": "src/loader", "module": "src/loader", diff --git a/packages/hydrooj/src/handler/contest.ts b/packages/hydrooj/src/handler/contest.ts index f5ec44b25..f5304e1ef 100644 --- a/packages/hydrooj/src/handler/contest.ts +++ b/packages/hydrooj/src/handler/contest.ts @@ -3,12 +3,15 @@ import { ObjectID } from 'mongodb'; import AdmZip from 'adm-zip'; import { Time } from '@hydrooj/utils/lib/utils'; import { lookup } from 'mime-types'; +import { intersection } from 'lodash'; import { ContestNotLiveError, ValidationError, ProblemNotFoundError, ContestNotAttendedError, PermissionError, BadRequestError, ContestNotFoundError, InvalidTokenError, RecordNotFoundError, } from '../error'; -import { ProblemDoc, Tdoc, User } from '../interface'; +import { + ProblemDoc, Tdoc, User, DomainDoc, +} from '../interface'; import paginate from '../lib/paginate'; import { PERM, PRIV } from '../model/builtin'; import * as contest from '../model/contest'; @@ -341,8 +344,25 @@ export class ContestProblemFileDownloadHandler extends ContestProblemHandler { } export class ContestDetailProblemSubmitHandler extends ContestProblemHandler { - async prepare() { + pdomainId: string; + ppid: number; + pdomain: DomainDoc; + + @param('pid', Types.Name) + async prepare(domainId: string, _pid: string) { + const pid = this.tdoc.pids[parseInt(_pid, 36) - 10]; + if (!this.attended) throw new ContestNotAttendedError(this.tdoc.docId); if (!contest.isOngoing(this.tdoc)) throw new ContestNotLiveError(this.tdoc.docId); + this.pdomainId = typeof pid === 'string' ? pid.split(':')[0] : domainId; + this.ppid = typeof pid === 'number' ? pid : +pid.split(':')[1]; + this.pdomain = await domain.get(this.pdomainId); + if (this.pdomain.langs) { + this.response.body.pdoc.config.langs = intersection( + this.response.body.pdoc.config.langs || this.pdomain.langs.split(','), + this.pdomain.langs.split(','), + ); + } + this.response.body.pdoc.config.domainId = this.pdomainId; } @param('tid', Types.ObjectID) @@ -367,24 +387,20 @@ export class ContestDetailProblemSubmitHandler extends ContestProblemHandler { @param('input', Types.String, true) async post(domainId: string, tid: ObjectID, lang: string, code: string, _pid: string, pretest = false, input = '') { const pid = this.tdoc.pids[parseInt(_pid, 36) - 10]; - if (this.response.body.pdoc.config?.langs && !this.response.body.pdoc.config.langs.includes(lang)) { + if (!(this.response.body.pdoc.config.langs || [lang]).includes(lang)) { throw new BadRequestError('Language not allowed.'); } - if (this.domain.langs && !this.domain.langs.includes(lang)) { - throw new BadRequestError('Language not allowed'); - } await this.limitRate('add_record', 60, system.get('limit.submission')); const rid = await record.add(domainId, pid, this.user._id, lang, code, true, pretest ? input : { type: document.TYPE_CONTEST, tid, }); - const pdomainId = typeof pid === 'string' ? pid.split(':')[0] : domainId; - const ppid = typeof pid === 'number' ? pid : +pid.split(':')[1]; const rdoc = await record.get(domainId, rid); if (!rdoc) throw new RecordNotFoundError(domainId, rid); if (!pretest) { await Promise.all([ - (this.tsdoc.journal || []).filter((i) => i.pid === pid).length ? Promise.resolve() : problem.inc(pdomainId, ppid, 'nSubmit', 1), + (this.tsdoc.journal || []).filter((i) => i.pid === pid).length + && problem.inc(this.pdomainId, this.ppid, 'nSubmit', 1), domain.incUserInDomain(domainId, this.user._id, 'nSubmit'), contest.updateStatus(domainId, tid, this.user._id, rid, pid), ]); diff --git a/packages/hydrooj/src/handler/homework.ts b/packages/hydrooj/src/handler/homework.ts index b8a314c41..79029be7d 100644 --- a/packages/hydrooj/src/handler/homework.ts +++ b/packages/hydrooj/src/handler/homework.ts @@ -4,12 +4,13 @@ import { ObjectID } from 'mongodb'; import AdmZip from 'adm-zip'; import { Time } from '@hydrooj/utils/lib/utils'; import { lookup } from 'mime-types'; +import { intersection } from 'lodash'; import { ValidationError, HomeworkNotLiveError, ProblemNotFoundError, HomeworkNotAttendedError, BadRequestError, } from '../error'; import { - PenaltyRules, Tdoc, ProblemDoc, User, + PenaltyRules, Tdoc, ProblemDoc, User, DomainDoc, } from '../interface'; import { Route, Handler, Types, param, @@ -206,10 +207,26 @@ export class HomeworkProblemFileDownloadHandler extends HomeworkDetailProblemHan } class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler { + pdomainId: string; + ppid: number; + pdomain: DomainDoc; + @param('tid', Types.ObjectID) - async prepare(domainId: string, tid: ObjectID) { + @param('pid', Types.Name) + async prepare(domainId: string, tid: ObjectID, _pid: string) { + const pid = this.tdoc.pids[parseInt(_pid, 36) - 10]; if (!this.attended) throw new HomeworkNotAttendedError(tid); if (!contest.isOngoing(this.tdoc)) throw new HomeworkNotLiveError(tid); + this.pdomainId = typeof pid === 'string' ? pid.split(':')[0] : domainId; + this.ppid = typeof pid === 'number' ? pid : +pid.split(':')[1]; + this.pdomain = await domain.get(this.pdomainId); + if (this.pdomain.langs) { + this.response.body.pdoc.config.langs = intersection( + this.response.body.pdoc.config.langs || this.pdomain.langs.split(','), + this.pdomain.langs.split(','), + ); + } + this.response.body.pdoc.config.domainId = this.pdomainId; } @param('tid', Types.ObjectID) @@ -236,20 +253,16 @@ class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler { if (this.response.body.pdoc.config?.langs && !this.response.body.pdoc.config.langs.includes(lang)) { throw new BadRequestError('Language not allowed.'); } - if (this.domain.langs && !this.domain.langs.includes(lang)) { - throw new BadRequestError('Language not allowed'); - } await this.limitRate('add_record', 60, system.get('limit.submission')); const rid = await record.add(domainId, pid, this.user._id, lang, code, true, pretest ? input : { type: document.TYPE_HOMEWORK, tid, }); - const pdomainId = typeof pid === 'string' ? pid.split(':')[0] : domainId; - const ppid = typeof pid === 'number' ? pid : +pid.split(':')[1]; const rdoc = await record.get(domainId, rid); - if (pretest) { + if (!pretest) { await Promise.all([ - (this.tsdoc.journal || []).filter((i) => i.pid === pid).length ? Promise.resolve() : problem.inc(pdomainId, ppid, 'nSubmit', 1), + (this.tsdoc.journal || []).filter((i) => i.pid === pid).length + && problem.inc(this.pdomainId, this.ppid, 'nSubmit', 1), domain.incUserInDomain(domainId, this.user._id, 'nSubmit'), contest.updateStatus(domainId, tid, this.user._id, rid, pid, false, 0, document.TYPE_HOMEWORK), diff --git a/packages/ui-default/package.json b/packages/ui-default/package.json index a910d32e5..07db897f7 100644 --- a/packages/ui-default/package.json +++ b/packages/ui-default/package.json @@ -1,6 +1,6 @@ { "name": "@hydrooj/ui-default", - "version": "4.18.5", + "version": "4.18.6", "author": "undefined ", "license": "AGPL-3.0", "main": "hydro.js", diff --git a/packages/ui-default/pages/problem_submit.page.js b/packages/ui-default/pages/problem_submit.page.js index 2315525d1..a119a3e2c 100644 --- a/packages/ui-default/pages/problem_submit.page.js +++ b/packages/ui-default/pages/problem_submit.page.js @@ -12,12 +12,14 @@ const page = new NamedPage(['problem_submit', 'contest_detail_problem_submit', ' $(ev.currentTarget).hide(); $('[name="problem-sidebar__categories"]').show(); }); + + const { domainId } = UiContext.pdocConfig; + function onChangeMain(update = true) { const options = {}; for (const key in window.LANGS) { if (UiContext.pdocConfig.langs && !UiContext.pdocConfig.langs.includes(key)) continue; - if (window.LANGS[key].domain && !window.LANGS[key].domain.includes(UiContext.domainId)) continue; - if (UiContext.domain.langs && !`,${UiContext.domain.langs},`.includes(`,${key},`)) continue; + if (window.LANGS[key].domain && !window.LANGS[key].domain.includes(domainId)) continue; if (key.startsWith(`${this.value}.`) && key !== this.value) options[key] = window.LANGS[key].display; } setOptions($('#codelang-sub-select'), options); @@ -33,8 +35,7 @@ const page = new NamedPage(['problem_submit', 'contest_detail_problem_submit', ' const main = {}; for (const key in window.LANGS) { if (UiContext.pdocConfig.langs && !UiContext.pdocConfig.langs.filter((i) => i === key || i.startsWith(`${key}.`)).length) continue; - if (window.LANGS[key].domain && !window.LANGS[key].domain.includes(UiContext.domainId)) continue; - if (UiContext.domain.langs && !`,${UiContext.domain.langs},`.includes(`,${key},`)) continue; + if (window.LANGS[key].domain && !window.LANGS[key].domain.includes(domainId)) continue; if (!key.includes('.')) main[key] = window.LANGS[key].display; } setOptions($('#codelang-main-select'), main);