diff --git a/packages/hydrojudge/src/hosts/hydro.ts b/packages/hydrojudge/src/hosts/hydro.ts index a09cb5692..f0d24450e 100644 --- a/packages/hydrojudge/src/hosts/hydro.ts +++ b/packages/hydrojudge/src/hosts/hydro.ts @@ -58,7 +58,7 @@ class JudgeTask { this.stat.handle = new Date(); this.tag = this.request.tag; this.type = this.request.type; - this.domainId = this.request.domainId; + this.domainId = this.request.pdomain; this.pid = this.request.pid.toString(); this.rid = this.request.rid; this.lang = this.request.lang; @@ -120,7 +120,6 @@ class JudgeTask { next(data, id?: number) { log.debug('Next: %d %o', id, data); data.key = 'next'; - data.domainId = this.request.domainId; data.rid = new ObjectID(this.rid); if (data.time_ms) data.time = data.time_ms; if (data.memory_kb) data.memory = data.memory_kb; @@ -162,7 +161,6 @@ class JudgeTask { log.info('End: %o', data); data.key = 'end'; data.rid = new ObjectID(this.request.rid); - data.domainId = this.request.domainId; data.time = data.time_ms ?? data.time; data.memory = data.memory_kb ?? data.memory; this.ws.send(JSON.stringify(data)); diff --git a/packages/hydrojudge/src/service.ts b/packages/hydrojudge/src/service.ts index 75e095878..18ea36f89 100644 --- a/packages/hydrojudge/src/service.ts +++ b/packages/hydrojudge/src/service.ts @@ -102,7 +102,6 @@ async function postInit() { that.nextWaiting = []; return (data, id = 0) => { logger.debug('Next: %d %o', id, data); - data.domainId = that.domainId; data.rid = new ObjectID(that.rid); if (data.time_ms) data.time = data.time_ms; if (data.memory_kb) data.memory = data.memory_kb; @@ -139,11 +138,10 @@ async function postInit() { }; } - function getEnd(domainId: string, rid: string) { + function getEnd(rid: string) { return (data) => { data.key = 'end'; data.rid = new ObjectID(rid); - data.domainId = domainId; data.time = data.time_ms ?? data.time; data.memory = data.memory_kb ?? data.memory; logger.info('End: status=%d score=%d time=%dms memory=%dkb', data.status, data.score, data.time, data.memory); @@ -188,14 +186,14 @@ async function postInit() { this.stat.handle = new Date(); this.pid = (this.request.pid || 'unknown').toString(); this.rid = this.request.rid.toString(); - this.domainId = this.request.domainId; + this.domainId = this.request.pdomain; this.lang = this.request.lang; this.code = this.request.code; this.data = this.request.data; this.config = this.request.config; this.input = this.request.input; this.next = getNext(this); - this.end = getEnd(this.domainId, this.rid); + this.end = getEnd(this.rid); this.tmpdir = path.resolve(getConfig('tmp_dir'), this.rid); this.clean = []; fs.ensureDirSync(this.tmpdir); diff --git a/packages/hydrooj/package.json b/packages/hydrooj/package.json index bc0462b86..d40cd3ab1 100644 --- a/packages/hydrooj/package.json +++ b/packages/hydrooj/package.json @@ -1,6 +1,6 @@ { "name": "hydrooj", - "version": "2.27.7", + "version": "2.28.0", "bin": "bin/hydrooj.js", "main": "dist/loader.js", "typings": "dist/loader.d.ts", diff --git a/packages/hydrooj/src/handler/contest.ts b/packages/hydrooj/src/handler/contest.ts index a2e448926..94b700950 100644 --- a/packages/hydrooj/src/handler/contest.ts +++ b/packages/hydrooj/src/handler/contest.ts @@ -1,11 +1,14 @@ import moment from 'moment-timezone'; import { ObjectID } from 'mongodb'; import AdmZip from 'adm-zip'; +import { Time } from '@hydrooj/utils/lib/utils'; import { ContestNotLiveError, ValidationError, ProblemNotFoundError, ContestNotAttendedError, PermissionError, BadRequestError, } from '../error'; -import { ProblemDoc, Tdoc, User } from '../interface'; +import { + ProblemId, ProblemDoc, Tdoc, User, +} from '../interface'; import paginate from '../lib/paginate'; import { PERM, PRIV } from '../model/builtin'; import * as contest from '../model/contest'; @@ -22,23 +25,13 @@ import { import * as bus from '../service/bus'; import storage from '../service/storage'; -class ContestListHandler extends Handler { +export class ContestListHandler extends Handler { @param('rule', Types.Range(contest.RULES), true) @param('page', Types.PositiveInt, true) async get(domainId: string, rule = '', page = 1) { - this.response.template = 'contest_main.html'; - let tdocs; - let qs; - let tpcount; - if (!rule) { - tdocs = contest.getMulti(domainId).sort({ beginAt: -1 }); - qs = ''; - } else { - tdocs = contest.getMulti(domainId, { rule }).sort({ beginAt: -1 }); - qs = `rule=${rule}`; - } - // eslint-disable-next-line prefer-const - [tdocs, tpcount] = await paginate(tdocs, page, system.get('pagination.contest')); + const cursor = contest.getMulti(domainId, rule ? { rule } : undefined).sort({ beginAt: -1 }); + const qs = rule ? `rule=${rule}` : ''; + const [tdocs, tpcount] = await paginate(cursor, page, system.get('pagination.contest')); const tids = []; for (const tdoc of tdocs) tids.push(tdoc.docId); const tsdict = await contest.getListStatus(domainId, this.user._id, tids); @@ -46,13 +39,14 @@ class ContestListHandler extends Handler { ['Hydro', 'homepage'], ['contest_main', null], ]; + this.response.template = 'contest_main.html'; this.response.body = { page, tpcount, qs, rule, tdocs, tsdict, path, }; } } -class ContestDetailHandler extends Handler { +export class ContestDetailHandler extends Handler { @param('tid', Types.ObjectID) @param('page', Types.PositiveInt, true) async get(domainId: string, tid: ObjectID, page = 1) { @@ -165,42 +159,49 @@ class ContestScoreboardDownloadHandler extends Handler { } } -class ContestEditHandler extends Handler { +export class ContestEditHandler extends Handler { tdoc: Tdoc; - @param('tid', Types.ObjectID) + @param('tid', Types.ObjectID, true) async prepare(domainId: string, tid: ObjectID) { - this.tdoc = await contest.get(domainId, tid); - if (!this.user.own(this.tdoc)) this.checkPerm(PERM.PERM_EDIT_CONTEST); - else this.checkPerm(PERM.PERM_EDIT_CONTEST_SELF); + if (tid) { + this.tdoc = await contest.get(domainId, tid); + if (!this.user.own(this.tdoc)) this.checkPerm(PERM.PERM_EDIT_CONTEST); + else this.checkPerm(PERM.PERM_EDIT_CONTEST_SELF); + } } - async get() { + @param('tid', Types.ObjectID, true) + async get(domainId: string, tid: ObjectID) { this.response.template = 'contest_edit.html'; const rules = {}; - for (const i in contest.RULES) { - rules[i] = contest.RULES[i].TEXT; - } - const duration = (this.tdoc.endAt.getTime() - this.tdoc.beginAt.getTime()) / 3600 / 1000; + for (const i in contest.RULES) rules[i] = contest.RULES[i].TEXT; const path = [ ['Hydro', 'homepage'], ['contest_main', 'contest_main'], - [this.tdoc.title, 'contest_detail', { tid: this.tdoc.docId }, true], - ['contest_edit', null], + ...tid + ? [ + [this.tdoc.title, 'contest_detail', { tid: this.tdoc.docId }, true], + ['contest_edit', null], + ] + : [['contest_create', null]], ]; - const dt = this.tdoc.beginAt; + let ts = new Date().getTime(); + ts = ts - (ts % (15 * Time.minute)) + 15 * Time.minute; + const dt = this.tdoc?.beginAt || new Date(ts); this.response.body = { rules, tdoc: this.tdoc, - duration, + duration: tid ? (this.tdoc.endAt.getTime() - this.tdoc.beginAt.getTime()) / Time.hour : 2, path, - pids: this.tdoc.pids.join(','), + pids: tid ? this.tdoc.pids.join(',') : '', date_text: dt.format('%Y-%m-%d'), time_text: dt.format('%H:%M'), - page_name: 'contest_edit', + page_name: tid ? 'contest_edit' : 'contest_create', }; } + @param('tid', Types.ObjectID, true) @param('beginAtDate', Types.Date) @param('beginAtTime', Types.Time) @param('duration', Types.Float) @@ -210,31 +211,36 @@ class ContestEditHandler extends Handler { @param('pids', Types.Content) @param('rated', Types.Boolean) async post( - domainId: string, beginAtDate: string, beginAtTime: string, duration: number, + domainId: string, tid: ObjectID, beginAtDate: string, beginAtTime: string, duration: number, title: string, content: string, rule: string, _pids: string, rated = false, ) { - const pids = _pids.replace(/,/g, ',').split(',').map((i) => +i).filter((i) => i); - await problem.getList(domainId, pids, this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN), true); + const pids = _pids.replace(/,/g, ',').split(',').map((i) => { + if ((+i).toString() === i) return +i; + return i; + }).filter((i) => i); const beginAtMoment = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone); - if (!beginAtMoment.isValid()) { - throw new ValidationError('beginAtDate', 'beginAtTime'); - } + if (!beginAtMoment.isValid()) throw new ValidationError('beginAtDate', 'beginAtTime'); const endAt = beginAtMoment.clone().add(duration, 'hours').toDate(); if (beginAtMoment.isSameOrAfter(endAt)) throw new ValidationError('duration'); const beginAt = beginAtMoment.toDate(); - await contest.edit(domainId, this.tdoc.docId, { - title, content, rule, beginAt, endAt, pids, rated, - }); - if (this.tdoc.beginAt !== beginAt || this.tdoc.endAt !== endAt - || Array.isDiff(this.tdoc.pids, pids) || this.tdoc.rule !== rule) { - await contest.recalcStatus(domainId, this.tdoc.docId); + await problem.getList(domainId, pids, this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN), true); + if (tid) { + await contest.edit(domainId, tid, { + title, content, rule, beginAt, endAt, pids, rated, + }); + if (this.tdoc.beginAt !== beginAt || this.tdoc.endAt !== endAt + || Array.isDiff(this.tdoc.pids, pids) || this.tdoc.rule !== rule) { + await contest.recalcStatus(domainId, this.tdoc.docId); + } + } else { + tid = await contest.add(domainId, title, content, this.user._id, rule, beginAt, endAt, pids, rated); } - this.response.body = { tid: this.tdoc.docId }; - this.response.redirect = this.url('contest_detail', { tid: this.tdoc.docId }); + this.response.body = { tid }; + this.response.redirect = this.url('contest_detail', { tid }); } } -class ContestProblemHandler extends Handler { +export class ContestProblemHandler extends Handler { tdoc: Tdoc<30 | 60>; pdoc: ProblemDoc; tsdoc: any; @@ -242,25 +248,22 @@ class ContestProblemHandler extends Handler { attended: boolean; @param('tid', Types.ObjectID) - @param('pid', Types.UnsignedInt) - async _prepare(domainId: string, tid: ObjectID, pid: number) { - [this.tdoc, this.pdoc] = await Promise.all([ - contest.get(domainId, tid), + @param('pid', Types.Name) + async _prepare(domainId: string, tid: ObjectID, pid: ProblemId) { + this.tdoc = await contest.get(domainId, tid); + if (!this.tdoc.pids.includes(pid)) throw new ProblemNotFoundError(domainId, pid); + [this.pdoc, this.tsdoc, this.udoc] = await Promise.all([ problem.get(domainId, pid), - ]); - [this.tsdoc, this.udoc] = await Promise.all([ - contest.getStatus(domainId, this.tdoc.docId, this.user._id), + contest.getStatus(domainId, tid, this.user._id), user.getById(domainId, this.tdoc.owner), ]); + if (!this.pdoc) throw new ProblemNotFoundError(domainId, pid); + // @ts-ignore + if (this.pdoc.domainId !== domainId) this.pdoc.docId = `${this.pdoc.domainId}:${this.pdoc.docId}`; this.attended = this.tsdoc && this.tsdoc.attend === 1; + if (contest.isNotStarted(this.tdoc)) throw new ContestNotLiveError(tid); + if (!contest.isDone(this.tdoc) && !this.attended) throw new ContestNotAttendedError(tid); this.response.template = 'problem_detail.html'; - if (!contest.isDone(this.tdoc)) { - if (!this.attended) throw new ContestNotAttendedError(this.tdoc.docId); - if (contest.isNotStarted(this.tdoc)) throw new ContestNotLiveError(this.tdoc.docId); - } - if (!this.tdoc.pids.map((s) => s.toString()).includes(this.pdoc.docId.toString())) { - throw new ProblemNotFoundError(domainId, this.tdoc.docId); - } this.response.body = { tdoc: this.tdoc, tsdoc: this.tsdoc, @@ -271,18 +274,19 @@ class ContestProblemHandler extends Handler { }; } - // eslint-disable-next-line + @param('tid', Types.ObjectID) + @param('pid', Types.Name) async get(...args: any[]) { this.response.body.path = [ ['Hydro', 'homepage'], ['contest_main', 'contest_main'], - [this.tdoc.title, 'contest_detail', { tid: this.tdoc.docId }, true], + [this.tdoc.title, 'contest_detail', { tid: args[1] }, true], [this.pdoc.title, null, null, true], ]; // Navigate to current additional file download // e.g. ![img](a.jpg) will navigate to ![img](./pid/file/a.jpg) this.response.body.pdoc.content = this.response.body.pdoc.content - .replace(/\(file:\/\//g, `(./${this.pdoc.docId}/file/`); + .replace(/\(file:\/\//g, `(./${args[2]}/file/`); } } @@ -301,19 +305,19 @@ export class ContestProblemFileDownloadHandler extends ContestProblemHandler { } } -class ContestDetailProblemSubmitHandler extends ContestProblemHandler { +export class ContestDetailProblemSubmitHandler extends ContestProblemHandler { async prepare() { if (!contest.isOngoing(this.tdoc)) throw new ContestNotLiveError(this.tdoc.docId); } @param('tid', Types.ObjectID) - @param('pid', Types.UnsignedInt) - async get(domainId: string, tid: ObjectID, pid: number) { + @param('pid', Types.Name) + async get(domainId: string, tid: ObjectID, pid: ProblemId) { this.response.body.rdocs = []; if (contest.canShowRecord.call(this, this.tdoc)) { this.response.body.rdocs = await record.getUserInProblemMulti( domainId, this.user._id, - this.pdoc.docId, true, + pid, { type: document.TYPE_CONTEST, tid }, ).sort({ _id: -1 }).limit(10).toArray(); } this.response.body.path = [ @@ -330,20 +334,21 @@ class ContestDetailProblemSubmitHandler extends ContestProblemHandler { @param('tid', Types.ObjectID) @param('lang', Types.Name) @param('code', Types.Content) - async post(domainId: string, tid: ObjectID, lang: string, code: string) { + @param('pid', Types.Name) + async post(domainId: string, tid: ObjectID, lang: string, code: string, pid: ProblemId) { if (this.response.body.pdoc.config?.langs && !this.response.body.pdoc.config.langs.includes(lang)) { throw new BadRequestError('Language not allowed.'); } await this.limitRate('add_record', 60, 10); - const rid = await record.add(domainId, this.pdoc.docId, this.user._id, lang, code, true, { + const rid = await record.add(domainId, pid, this.user._id, lang, code, true, { type: document.TYPE_CONTEST, tid, }); const [rdoc] = await Promise.all([ record.get(domainId, rid), - problem.inc(domainId, this.pdoc.docId, 'nSubmit', 1), + problem.inc(domainId, pid, 'nSubmit', 1), domain.incUserInDomain(domainId, this.user._id, 'nSubmit'), - contest.updateStatus(domainId, this.tdoc.docId, this.user._id, rid, this.pdoc.docId), + contest.updateStatus(domainId, tid, this.user._id, rid, pid), ]); bus.boardcast('record/change', rdoc); if (!contest.canShowRecord.call(this, this.tdoc)) { @@ -356,7 +361,7 @@ class ContestDetailProblemSubmitHandler extends ContestProblemHandler { } } -class ContestCodeHandler extends Handler { +export class ContestCodeHandler extends Handler { @param('tid', Types.ObjectID) async get(domainId: string, tid: ObjectID) { if (!this.user.hasPriv(PRIV.PRIV_READ_RECORD_CODE)) { @@ -381,58 +386,8 @@ class ContestCodeHandler extends Handler { } } -class ContestCreateHandler extends Handler { - async get() { - this.response.template = 'contest_edit.html'; - const rules = {}; - for (const i in contest.RULES) rules[i] = contest.RULES[i].TEXT; - const now = new Date(); - let ts = now.getTime(); - ts = ts - (ts % (15 * 60 * 1000)) + 15 * 60 * 1000; - const dt = new Date(ts); - const path = [ - ['Hydro', 'homepage'], - ['contest_main', 'contest_main'], - ['contest_create', null], - ]; - this.response.body = { - rules, - path, - page_name: 'contest_create', - date_text: dt.format('%Y-%m-%d'), - time_text: dt.format('%H:%M'), - pids: '1000, 1001', - }; - } - - @param('beginAtDate', Types.Date) - @param('beginAtTime', Types.Time) - @param('duration', Types.Float) - @param('title', Types.Title) - @param('content', Types.Content) - @param('rule', Types.Name) - @param('pids', Types.Content) - @param('rated', Types.Boolean) - async post( - domainId: string, beginAtDate: string, beginAtTime: string, duration: number, - title: string, content: string, rule: string, _pids: string, rated = false, - ) { - const pids = _pids.replace(/,/g, ',').split(',').map((i) => +i).filter((i) => i); - const beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone); - if (!beginAt.isValid()) throw new ValidationError('beginAtDate', 'beginAtTime'); - const endAt = beginAt.clone().add(duration, 'hours').toDate(); - await problem.getList(domainId, pids, this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN), true); - const tid = await contest.add( - domainId, title, content, - this.user._id, rule, beginAt.toDate(), endAt, pids, rated, - ); - this.response.body = { tid }; - this.response.redirect = this.url('contest_detail', { tid }); - } -} - export async function apply() { - Route('contest_create', '/contest/create', ContestCreateHandler, PERM.PERM_CREATE_CONTEST); + Route('contest_create', '/contest/create', ContestEditHandler, PERM.PERM_CREATE_CONTEST); Route('contest_main', '/contest', ContestListHandler, PERM.PERM_VIEW_CONTEST); Route('contest_detail', '/contest/:tid', ContestDetailHandler, PERM.PERM_VIEW_CONTEST); Route('contest_boardcast', '/contest/:tid/boardcast', ContestBoardcastHandler); diff --git a/packages/hydrooj/src/handler/home.ts b/packages/hydrooj/src/handler/home.ts index c80c329ba..64f6dc0db 100644 --- a/packages/hydrooj/src/handler/home.ts +++ b/packages/hydrooj/src/handler/home.ts @@ -11,7 +11,7 @@ import { Route, Connection, Handler, ConnectionHandler, param, Types, } from '../service/server'; import { md5 } from '../lib/crypto'; -import { isPassword, isEmail } from '../lib/validator'; +import { isPassword, isEmail, isDomainId } from '../lib/validator'; import avatar from '../lib/avatar'; import * as mail from '../lib/mail'; import * as contest from '../model/contest'; @@ -321,7 +321,7 @@ class HomeDomainCreateHandler extends Handler { this.response.template = 'domain_create.html'; } - @param('id', Types.Name) + @param('id', Types.Name, isDomainId) @param('name', Types.Title) @param('bulletin', Types.Content) @param('avatar', Types.Content, true) diff --git a/packages/hydrooj/src/handler/homework.ts b/packages/hydrooj/src/handler/homework.ts index 01be9b319..d640a2710 100644 --- a/packages/hydrooj/src/handler/homework.ts +++ b/packages/hydrooj/src/handler/homework.ts @@ -2,13 +2,13 @@ import yaml from 'js-yaml'; import moment from 'moment-timezone'; import { ObjectID } from 'mongodb'; import AdmZip from 'adm-zip'; +import { Time } from '@hydrooj/utils/lib/utils'; import { ValidationError, HomeworkNotLiveError, ProblemNotFoundError, - HomeworkNotAttendedError, - BadRequestError, + HomeworkNotAttendedError, BadRequestError, } from '../error'; import { - PenaltyRules, Tdoc, ProblemDoc, User, + PenaltyRules, Tdoc, ProblemDoc, User, ProblemId, } from '../interface'; import { Route, Handler, Types, param, @@ -24,11 +24,9 @@ import problem from '../model/problem'; import record from '../model/record'; import * as document from '../model/document'; import paginate from '../lib/paginate'; +import { isExternalPid } from '../lib/validator'; -const validatePenaltyRules = (input: string) => { - const s = yaml.load(input); - return s; -}; +const validatePenaltyRules = (input: string) => yaml.load(input); const convertPenaltyRules = validatePenaltyRules; class HomeworkMainHandler extends Handler { @@ -40,9 +38,7 @@ class HomeworkMainHandler extends Handler { if (contest.isExtended(tdoc) || contest.isDone(tdoc)) { cal.endAt = tdoc.endAt; cal.penaltySince = tdoc.penaltySince; - } else { - cal.endAt = tdoc.penaltySince; - } + } else cal.endAt = tdoc.penaltySince; calendar.push(cal); } const path = [ @@ -126,12 +122,14 @@ class HomeworkDetailProblemHandler extends Handler { attended: boolean; @param('tid', Types.ObjectID) - @param('pid', Types.UnsignedInt) - async prepare(domainId: string, tid: ObjectID, pid: number) { - this.checkPerm(PERM.PERM_VIEW_PROBLEM); + @param('pid', Types.String, isExternalPid) + async _prepare(domainId: string, tid: ObjectID, pid: ProblemId) { + if (!this.tdoc.pids.includes(pid)) throw new ProblemNotFoundError(domainId, pid, tid); + const pdomainId = typeof pid === 'string' ? pid.split(':')[0] : domainId; + const ppid = typeof pid === 'number' ? pid : +pid.split(':')[1]; [this.tdoc, this.pdoc, this.tsdoc] = await Promise.all([ contest.get(domainId, tid, document.TYPE_HOMEWORK), - problem.get(domainId, pid), + problem.get(pdomainId, ppid), contest.getStatus(domainId, tid, this.user._id, document.TYPE_HOMEWORK), ]); this.udoc = await user.getById(domainId, this.tdoc.owner); @@ -146,13 +144,13 @@ class HomeworkDetailProblemHandler extends Handler { } @param('tid', Types.ObjectID) - @param('pid', Types.UnsignedInt) - async get(domainId: string, tid: ObjectID, pid: number) { + @param('pid', Types.Name) + // eslint-disable-next-line @typescript-eslint/no-unused-vars + async get(domainId: string, tid: ObjectID, pid: ProblemId) { if (!contest.isDone(this.tdoc)) { if (!this.attended) throw new HomeworkNotAttendedError(tid); if (contest.isNotStarted(this.tdoc)) throw new HomeworkNotLiveError(tid); } - if (!this.tdoc.pids.includes(pid)) throw new ProblemNotFoundError(domainId, pid, tid); this.response.template = 'problem_detail.html'; this.response.body.page_name = 'homework_detail_problem'; this.response.body.path = [ @@ -166,13 +164,16 @@ class HomeworkDetailProblemHandler extends Handler { class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler { @param('tid', Types.ObjectID) - @param('pid', Types.UnsignedInt) - async get(domainId: string, tid: ObjectID, pid: number) { + async prepare(domainId: string, tid: ObjectID) { if (!this.attended) throw new HomeworkNotAttendedError(tid); if (!contest.isOngoing(this.tdoc)) throw new HomeworkNotLiveError(tid); - if (!this.tdoc.pids.includes(pid)) throw new ProblemNotFoundError(domainId, pid, tid); + } + + @param('tid', Types.ObjectID) + @param('pid', Types.String, isExternalPid) + async get(domainId: string, tid: ObjectID, pid: ProblemId) { this.response.body.rdocs = contest.canShowRecord.call(this, this.tdoc) - ? await record.getUserInProblemMulti(domainId, this.user._id, pid, true) + ? await record.getUserInProblemMulti(domainId, this.user._id, pid, { type: document.TYPE_HOMEWORK, tid }) .sort('_id', -1).limit(10).toArray() : []; this.response.body.path = [ @@ -186,24 +187,23 @@ class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler { } @param('tid', Types.ObjectID) - @param('pid', Types.UnsignedInt) + @param('pid', Types.String, isExternalPid) @param('code', Types.Content) @param('lang', Types.Name) - async post(domainId: string, tid: ObjectID, pid: number, code: string, lang: string) { + async post(domainId: string, tid: ObjectID, pid: ProblemId, code: string, lang: string) { if (this.response.body.pdoc.config?.langs && !this.response.body.pdoc.config.langs.includes(lang)) { throw new BadRequestError('Language not allowed.'); } await this.limitRate('add_record', 60, 5); - if (!this.attended) throw new HomeworkNotAttendedError(tid); - if (!contest.isOngoing(this.tdoc)) throw new HomeworkNotLiveError(tid); - if (!this.tdoc.pids.includes(pid)) throw new ProblemNotFoundError(domainId, pid); const rid = await record.add(domainId, pid, this.user._id, lang, code, true, { 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 Promise.all([ record.get(domainId, rid), - problem.inc(domainId, pid, 'nSubmit', 1), + problem.inc(pdomainId, ppid, 'nSubmit', 1), domain.incUserInDomain(domainId, this.user._id, 'nSubmit'), contest.updateStatus(domainId, tid, this.user._id, rid, pid, false, 0, document.TYPE_HOMEWORK), @@ -216,77 +216,35 @@ class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler { } } -class HomeworkCreateHandler extends Handler { - async get() { - const beginAt = moment().add(1, 'day'); - const penaltySince = beginAt.clone().add(7, 'days'); - const path = [ - ['Hydro', 'homepage'], - ['homework_main', 'homework_main'], - ['homework_create', null], - ]; - this.response.template = 'homework_edit.html'; - this.response.body = { - path, - dateBeginText: beginAt.tz(this.user.timeZone).format('YYYY-M-D'), - timeBeginText: '00:00', - datePenaltyText: penaltySince.tz(this.user.timeZone).format('YYYY-M-D'), - timePenaltyText: '23:59', - pids: '1000, 1001', - extensionDays: 1, - page_name: 'homework_create', - }; - } - - @param('beginAtDate', Types.Date) - @param('beginAtTime', Types.Time) - @param('penaltySinceDate', Types.Date) - @param('penaltySinceTime', Types.Time) - @param('extensionDays', Types.Float) - @param('penaltyRules', Types.Content, validatePenaltyRules, convertPenaltyRules) - @param('title', Types.Title) - @param('content', Types.Content) - @param('pids', Types.Content) - @param('rated', Types.Boolean) - async post( - domainId: string, beginAtDate: string, beginAtTime: string, - penaltySinceDate: string, penaltySinceTime: string, extensionDays: number, - penaltyRules: PenaltyRules, title: string, content: string, _pids: string, rated = false, - ) { - const pids = _pids.replace(/,/g, ',').split(',').map((i) => +i).filter((i) => i); - const beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone); - if (!beginAt.isValid()) throw new ValidationError('beginAtDate', 'beginAtTime'); - const penaltySince = moment.tz(`${penaltySinceDate} ${penaltySinceTime}`, this.user.timeZone); - if (!penaltySince.isValid()) throw new ValidationError('endAtDate', 'endAtTime'); - const endAt = penaltySince.clone().add(extensionDays, 'days'); - if (beginAt.isSameOrAfter(penaltySince)) throw new ValidationError('endAtDate', 'endAtTime'); - if (penaltySince.isAfter(endAt)) throw new ValidationError('extensionDays'); - await problem.getList(domainId, pids, this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN), true); - const tid = await contest.add(domainId, title, content, this.user._id, - 'homework', beginAt.toDate(), endAt.toDate(), pids, rated, - { penaltySince: penaltySince.toDate(), penaltyRules }, document.TYPE_HOMEWORK); - this.response.body = { tid }; - this.response.redirect = this.url('homework_detail', { tid }); - } -} - class HomeworkEditHandler extends Handler { - @param('tid', Types.ObjectID) + @param('tid', Types.ObjectID, true) async get(domainId: string, tid: ObjectID) { - const tdoc = await contest.get(domainId, tid, document.TYPE_HOMEWORK); + const tdoc = tid + ? await contest.get(domainId, tid, document.TYPE_HOMEWORK) + : null; if (!this.user.own(tdoc)) this.checkPerm(PERM.PERM_EDIT_HOMEWORK); else this.checkPerm(PERM.PERM_EDIT_HOMEWORK_SELF); - const extensionDays = Math.round( - (tdoc.endAt.getTime() - tdoc.penaltySince.getTime()) / 36000 / 24, - ) / 100; + const extensionDays = tid + ? Math.round( + (tdoc.endAt.getTime() - tdoc.penaltySince.getTime()) / (Time.day / 100), + ) / 100 + : 1; const path = [ ['Hydro', 'homepage'], ['homework_main', 'homework_main'], - [tdoc.title, 'homework_detail', { tid }, true], - ['homework_edit', null], + ...tid + ? [ + [tdoc.title, 'homework_detail', { tid }, true], + ['homework_edit', null], + ] + : [['homework_create', null]], ]; - const beginAt = moment(tdoc.beginAt).tz(this.user.timeZone); - const penaltySince = moment(tdoc.penaltySince).tz(this.user.timeZone); + const beginAt = tid + ? moment(tdoc.beginAt).tz(this.user.timeZone) + : moment().add(1, 'day').tz(this.user.timeZone).hour(0).minute(0).millisecond(0); + const penaltySince = tid + ? moment(tdoc.penaltySince).tz(this.user.timeZone) + : beginAt.clone().add(7, 'days').tz(this.user.timeZone).hour(23).minute(59).millisecond(0); this.response.template = 'homework_edit.html'; this.response.body = { tdoc, @@ -298,10 +256,11 @@ class HomeworkEditHandler extends Handler { penaltyRules: yaml.dump(tdoc.penaltyRules), pids: tdoc.pids.join(','), path, + page_name: tid ? 'homework_edit' : 'homework_create', }; } - @param('tid', Types.ObjectID) + @param('tid', Types.ObjectID, true) @param('beginAtDate', Types.Date) @param('beginAtTime', Types.Time) @param('penaltySinceDate', Types.Date) @@ -315,38 +274,37 @@ class HomeworkEditHandler extends Handler { async post( domainId: string, tid: ObjectID, beginAtDate: string, beginAtTime: string, penaltySinceDate: string, penaltySinceTime: string, extensionDays: number, - penaltyRules: string, title: string, content: string, _pids: string, rated = false, + penaltyRules: PenaltyRules, title: string, content: string, _pids: string, rated = false, ) { - const pids = _pids.replace(/,/g, ',').split(',').map((i) => +i).filter((i) => i); + const pids = _pids.replace(/,/g, ',').split(',').map((i) => { + if ((+i).toString() === i) return +i; + return i; + }).filter((i) => i); const tdoc = await contest.get(domainId, tid, document.TYPE_HOMEWORK); if (!this.user.own(tdoc)) this.checkPerm(PERM.PERM_EDIT_HOMEWORK); else this.checkPerm(PERM.PERM_EDIT_HOMEWORK_SELF); - let beginAt; - let penaltySince; - try { - beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone); - } catch (e) { - throw new ValidationError('beginAtDate', 'beginAtTime'); - } - try { - penaltySince = moment.tz(`${penaltySinceDate} ${penaltySinceTime}`, this.user.timeZone); - } catch (e) { - throw new ValidationError('endAtDate', 'endAtTime'); - } - let endAt = penaltySince.clone().add(extensionDays, 'days'); + const beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone); + if (!beginAt.isValid()) throw new ValidationError('beginAtDate', 'beginAtTime'); + const penaltySince = moment.tz(`${penaltySinceDate} ${penaltySinceTime}`, this.user.timeZone); + if (!penaltySince.isValid()) throw new ValidationError('endAtDate', 'endAtTime'); + const endAt = penaltySince.clone().add(extensionDays, 'days'); if (beginAt.isSameOrAfter(penaltySince)) throw new ValidationError('endAtDate', 'endAtTime'); + if (penaltySince.isAfter(endAt)) throw new ValidationError('extensionDays'); await problem.getList(domainId, pids, this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN), true); - beginAt = beginAt.toDate(); - endAt = endAt.toDate(); - penaltySince = penaltySince.toDate(); - await contest.edit(domainId, tid, { - title, content, beginAt, endAt, pids, penaltySince, penaltyRules, rated, - }, document.TYPE_HOMEWORK); - if (tdoc.beginAt !== beginAt - || tdoc.endAt !== endAt - || tdoc.penaltySince !== penaltySince - || tdoc.pids.sort().join(' ') !== pids.sort().join(' ')) { - await contest.recalcStatus(domainId, tdoc.docId, document.TYPE_HOMEWORK); + if (!tid) { + tid = await contest.add(domainId, title, content, this.user._id, + 'homework', beginAt.toDate(), endAt.toDate(), pids, rated, + { penaltySince: penaltySince.toDate(), penaltyRules }, document.TYPE_HOMEWORK); + } else { + await contest.edit(domainId, tid, { + title, content, beginAt: beginAt.toDate(), endAt: endAt.toDate(), pids, penaltySince: penaltySince.toDate(), penaltyRules, rated, + }, document.TYPE_HOMEWORK); + if (tdoc.beginAt !== beginAt.toDate() + || tdoc.endAt !== endAt.toDate() + || tdoc.penaltySince !== penaltySince.toDate() + || tdoc.pids.sort().join(' ') !== pids.sort().join(' ')) { + await contest.recalcStatus(domainId, tdoc.docId, document.TYPE_HOMEWORK); + } } this.response.body = { tid }; this.response.redirect = this.url('homework_detail', { tid }); @@ -405,9 +363,7 @@ class HomeworkCodeHandler extends Handler { } const zip = new AdmZip(); const rdocs = await record.getMulti(domainId, { - _id: { - $in: Array.from(Object.keys(rnames)).map((id) => new ObjectID(id)), - }, + _id: { $in: Array.from(Object.keys(rnames)).map((id) => new ObjectID(id)) }, }).toArray(); for (const rdoc of rdocs) { zip.addFile(`${rnames[rdoc._id.toHexString()]}.${rdoc.lang}`, Buffer.from(rdoc.code)); @@ -418,7 +374,7 @@ class HomeworkCodeHandler extends Handler { export async function apply() { Route('homework_main', '/homework', HomeworkMainHandler, PERM.PERM_VIEW_HOMEWORK); - Route('homework_create', '/homework/create', HomeworkCreateHandler, PERM.PERM_CREATE_HOMEWORK); + Route('homework_create', '/homework/create', HomeworkEditHandler, PERM.PERM_CREATE_HOMEWORK); Route('homework_detail', '/homework/:tid', HomeworkDetailHandler, PERM.PERM_VIEW_HOMEWORK); Route('homework_scoreboard', '/homework/:tid/scoreboard', HomeworkScoreboardHandler, PERM.PERM_VIEW_HOMEWORK_SCOREBOARD); Route( diff --git a/packages/hydrooj/src/handler/judge.ts b/packages/hydrooj/src/handler/judge.ts index acf128704..a5d12b101 100644 --- a/packages/hydrooj/src/handler/judge.ts +++ b/packages/hydrooj/src/handler/judge.ts @@ -23,16 +23,20 @@ const logger = new Logger('judge'); async function _postJudge(rdoc: RecordDoc) { if (typeof rdoc.input === 'string') return; const accept = rdoc.status === builtin.STATUS.STATUS_ACCEPTED; - const updated = await problem.updateStatus(rdoc.domainId, rdoc.pid, rdoc.uid, rdoc._id, rdoc.status, rdoc.score); + const updated = await problem.updateStatus(rdoc.pdomain, rdoc.pid, rdoc.uid, rdoc._id, rdoc.status, rdoc.score); if (rdoc.contest) { await contest.updateStatus( rdoc.domainId, rdoc.contest.tid, rdoc.uid, rdoc._id, rdoc.pid, accept, rdoc.score, rdoc.contest.type, ); + await contest.updateStatus( + rdoc.domainId, rdoc.contest.tid, rdoc.uid, + rdoc._id, `${rdoc.pdomain}:${rdoc.pid}`, accept, rdoc.score, rdoc.contest.type, + ); } else if (accept && updated) await domain.incUserInDomain(rdoc.domainId, rdoc.uid, 'nAccept', 1); const pdoc = (accept && updated) - ? await problem.inc(rdoc.domainId, rdoc.pid, 'nAccept', 1) - : await problem.get(rdoc.domainId, rdoc.pid); + ? await problem.inc(rdoc.pdomain, rdoc.pid, 'nAccept', 1) + : await problem.get(rdoc.pdomain, rdoc.pid); const difficulty = difficultyAlgorithm(pdoc.nSubmit, pdoc.nAccept); if (!updated) await record.updateMulti(rdoc.domainId, { uid: rdoc.uid, status: builtin.STATUS.STATUS_ACCEPTED }, { effective: false }); await Promise.all([ @@ -45,8 +49,8 @@ async function _postJudge(rdoc: RecordDoc) { } export async function next(body: JudgeResultBody) { - if (body.rid) body.rid = new ObjectID(body.rid); - let rdoc = await record.get(body.domainId, body.rid); + body.rid = new ObjectID(body.rid); + let rdoc = await record.get(body.rid); const $set: Partial = {}; const $push: any = {}; if (body.case) { @@ -72,13 +76,13 @@ export async function next(body: JudgeResultBody) { if (body.time !== undefined) $set.time = body.time; if (body.memory !== undefined) $set.memory = body.memory; if (body.progress !== undefined) $set.progress = body.progress; - rdoc = await record.update(body.domainId, body.rid, $set, $push); + rdoc = await record.update(rdoc.domainId, body.rid, $set, $push); bus.boardcast('record/change', rdoc, $set, $push); } export async function end(body: JudgeResultBody) { if (body.rid) body.rid = new ObjectID(body.rid); - let rdoc = await record.get(body.domainId, body.rid); + let rdoc = await record.get(body.rid); const $set: Partial = {}; const $push: any = {}; const $unset: any = { progress: '' }; @@ -97,7 +101,7 @@ export async function end(body: JudgeResultBody) { $set.judgeAt = new Date(); $set.judger = body.judger ?? 1; await sleep(100); // Make sure that all 'next' event already triggered - rdoc = await record.update(body.domainId, body.rid, $set, $push, $unset); + rdoc = await record.update(rdoc.domainId, body.rid, $set, $push, $unset); await _postJudge(rdoc); bus.boardcast('record/change', rdoc); // trigger a full update } diff --git a/packages/hydrooj/src/handler/problem.ts b/packages/hydrooj/src/handler/problem.ts index fce9340ba..0e257270a 100644 --- a/packages/hydrooj/src/handler/problem.ts +++ b/packages/hydrooj/src/handler/problem.ts @@ -247,7 +247,7 @@ export class ProblemSubmitHandler extends ProblemDetailHandler { async get(domainId: string, pid: string | number) { this.response.template = 'problem_submit.html'; const rdocs = await record - .getUserInProblemMulti(domainId, this.user._id, this.pdoc.docId) + .getUserInProblemMulti(domainId, this.user._id, pid) .sort({ _id: -1 }) .limit(10) .toArray(); diff --git a/packages/hydrooj/src/handler/record.ts b/packages/hydrooj/src/handler/record.ts index e94d8f0ea..8705e2a43 100644 --- a/packages/hydrooj/src/handler/record.ts +++ b/packages/hydrooj/src/handler/record.ts @@ -1,4 +1,5 @@ import { FilterQuery, ObjectID } from 'mongodb'; +import { pick } from 'lodash'; import { ContestNotFoundError, PermissionError, ProblemNotFoundError, RecordNotFoundError, UserNotFoundError, @@ -58,8 +59,10 @@ class RecordListHandler extends Handler { const [udict, pdict] = await Promise.all([ user.getList(domainId, rdocs.map((rdoc) => rdoc.uid)), canViewProblem - ? problem.getList(domainId, rdocs.map((rdoc) => rdoc.pid), canViewProblemHidden, false) - : Object.fromEntries([rdocs.map((rdoc) => [rdoc.pid, problem.create(rdoc.pid, rdoc.pid.toString())])]), + ? problem.getList(domainId, rdocs.map( + (rdoc) => (rdoc.domainId === domainId ? rdoc.pid : `${rdoc.pdomain}:${rdoc.pid}`) + ), canViewProblemHidden, false) + : Object.fromEntries([rdocs.map((rdoc) => [rdoc.pid, { ...problem.default, pid: rdoc.pid }])]), ]); const path = [ ['Hydro', 'homepage'], @@ -100,16 +103,15 @@ class RecordDetailHandler extends Handler { } // eslint-disable-next-line prefer-const let [pdoc, udoc] = await Promise.all([ - problem.get(domainId, rdoc.pid), + problem.get(rdoc.pdomain, rdoc.pid), user.getById(domainId, rdoc.uid), ]); - if (!(pdoc && this.user.hasPerm(PERM.PERM_VIEW_PROBLEM))) { - pdoc = problem.create(pdoc?.docId || 0, pdoc?.pid || '*'); - } - if (!rdoc.contest && pdoc.hidden && !this.user.own(pdoc)) { - if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) { - pdoc = problem.create(pdoc.docId, pdoc.pid); - } + if ( + !pdoc + || !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM) + || (!rdoc.contest && pdoc.hidden && !this.user.own(pdoc) && !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) + ) { + pdoc = { ...problem.default, docId: pdoc?.docId || 0, pid: '*' }; } this.response.body = { path: [ @@ -162,6 +164,7 @@ class RecordMainConnectionHandler extends ConnectionHandler { dispose: bus.Disposable; tid: string; uid: number; + pdomain: string; pid: number; status: number; pretest: boolean; @@ -172,16 +175,11 @@ class RecordMainConnectionHandler extends ConnectionHandler { @param('status', Types.Int, true) @param('pretest', Types.Boolean) async prepare(domainId: string, tid?: ObjectID, pid?: string, uidOrName?: string, status?: number, pretest = false) { - if (tid) { - const tdoc = await contest.get(domainId, tid, -1); - if (contest.canShowRecord.call(this, tdoc)) this.tid = tid.toHexString(); - else throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD); - } if (tid) { const tdoc = await contest.get(domainId, tid, -1); if (!tdoc) throw new ContestNotFoundError(domainId, tid); - if (!contest.canShowScoreboard.call(this, tdoc, true)) throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD); - this.tid = tid.toHexString(); + if (contest.canShowScoreboard.call(this, tdoc, true)) this.tid = tid.toHexString(); + else throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD); } if (uidOrName) { let udoc = await user.getById(domainId, +uidOrName); @@ -193,9 +191,13 @@ class RecordMainConnectionHandler extends ConnectionHandler { } } if (pid) { - const pdoc = await problem.get(domainId, pid); - if (pdoc) this.pid = pdoc.docId; - else throw new ProblemNotFoundError(domainId, pid); + const pdomain = pid.includes(':') ? pid.split(':')[0] : domainId; + const ppid = pid.includes(':') ? +pid.split(':')[1] : pid; + const pdoc = await problem.get(pdomain, ppid); + if (pdoc) { + this.pdomain = pdoc.domainId; + this.pid = pdoc.docId; + } else throw new ProblemNotFoundError(domainId, pid); } if (status) this.status = status; if (pretest) this.pretest = true; @@ -219,7 +221,7 @@ class RecordMainConnectionHandler extends ConnectionHandler { if (!this.pretest && rdoc.input) return; if (rdoc.contest && rdoc.contest.tid.toString() !== this.tid) return; if (this.uid && rdoc.uid !== this.uid) return; - if (this.pid && rdoc.pid !== this.pid) return; + if (this.pid && (rdoc.pid !== this.pid || rdoc.pdomain !== this.pdomain)) return; // eslint-disable-next-line prefer-const let [udoc, pdoc] = await Promise.all([ user.getById(this.domainId, rdoc.uid), diff --git a/packages/hydrooj/src/handler/user.ts b/packages/hydrooj/src/handler/user.ts index 28d02cb89..7970e056d 100644 --- a/packages/hydrooj/src/handler/user.ts +++ b/packages/hydrooj/src/handler/user.ts @@ -206,7 +206,7 @@ class UserDetailHandler extends Handler { this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN), false, ) : Object.fromEntries(rdocs.map( - (rdoc) => [rdoc.pid, problem.create(undefined, rdoc.pid.toString())], + (rdoc) => [rdoc.pid, { ...problem.default, pid: rdoc.pid }], )); // Remove sensitive data if (!isSelfProfile && sdoc) { diff --git a/packages/hydrooj/src/interface.ts b/packages/hydrooj/src/interface.ts index c7ee2d403..2500a859d 100644 --- a/packages/hydrooj/src/interface.ts +++ b/packages/hydrooj/src/interface.ts @@ -256,27 +256,31 @@ export interface ContestInfo { } export interface RecordDoc { - _id: ObjectID, - domainId: string, - pid: number - uid: number, - lang: string, - code: string, - score: number, - memory: number, - time: number, - judgeTexts: string[], - compilerTexts: string[], - testCases: TestCase[], - rejudged: boolean, - judger: number, - judgeAt: Date, - status: number, - hidden: boolean, - progress?: number, - input?: string, - contest?: ContestInfo, - effective?: boolean, + _id: ObjectID; + domainId: string; + pdomain: string; + pid: number; + uid: number; + lang: string; + code: string; + score: number; + memory: number; + time: number; + judgeTexts: string[]; + compilerTexts: string[]; + testCases: TestCase[]; + rejudged: boolean; + /** judge uid */ + judger: number; + judgeAt: Date; + status: number; + progress?: number; + /** pretest/script */ + hidden?: boolean; + /** pretest */ + input?: string; + contest?: ContestInfo; + effective?: boolean; } export interface ScoreboardNode { @@ -296,6 +300,9 @@ export interface TrainingNode { pids: number[], } +export type ExternalProblemId = string; // ${string}:${number} +export type ProblemId = ExternalProblemId | number; + export interface Tdoc extends Document { docId: ObjectID, docType: docType & number, @@ -305,7 +312,7 @@ export interface Tdoc s && s.length < 64; export const checkTitle = (s) => { if (!(s && s.length < 64)) throw new ValidationError('title'); else return s; }; +export const isDomainId = (s) => RE_DOMAINID.test(s); +export const checkDomainId = (s) => { if (!isDomainId(s)) throw new ValidationError('domainId'); else return s; }; +export const isExternalPid = (s) => RE_EXTERNAL_PID.test(s); +export const checkExternalPid = (s) => { if (!isExternalPid(s)) throw new ValidationError('pid'); else return s; }; export const isUid = (s) => RE_UID.test(s); export const checkUid = (s) => { if (!isUid(s)) throw new ValidationError('uid'); else return s; }; export const isUname = (s) => RE_UNAME.test(s); @@ -87,6 +93,10 @@ global.Hydro.lib.validator = { validate, isTitle, checkTitle, + isDomainId, + checkDomainId, + isExternalPid, + checkExternalPid, isUid, checkUid, isUname, diff --git a/packages/hydrooj/src/model/contest.ts b/packages/hydrooj/src/model/contest.ts index 94c8584ef..d78522b3d 100644 --- a/packages/hydrooj/src/model/contest.ts +++ b/packages/hydrooj/src/model/contest.ts @@ -8,7 +8,7 @@ import { ContestNotAttendedError, ContestScoreboardHiddenError, } from '../error'; import { - ContestRule, ContestRules, ProblemDict, + ProblemId, ContestRule, ContestRules, ProblemDict, ScoreboardNode, ScoreboardRow, Tdoc, Udict, } from '../interface'; @@ -178,14 +178,20 @@ const oi: ContestRule = { const psdict = {}; // eslint-disable-next-line @typescript-eslint/no-use-before-define if (isDone(tdoc)) { - const psdocs = await problem.getMultiStatus(tdoc.domainId, { - $or: rankedTsdocs.map(([, i]) => ({ - uid: i.uid, - docId: { $in: tdoc.pids }, - })), - }).toArray(); - for (const psdoc of psdocs) { - psdict[`${psdoc.uid}/${psdoc.docId}`] = psdoc; + const psdocs = await Promise.all( + tdoc.pids.map((pid) => { + let pdomain = tdoc.domainId; + if (typeof pid === 'string') { + pdomain = pid.split(':')[0]; + pid = +pid.split(':')[1]; + } + return problem.getMultiStatus(pdomain, { uid: { $in: uids } }).toArray(); + }), + ); + for (const tpsdoc of psdocs) { + for (const psdoc of tpsdoc) { + psdict[`${psdoc.uid}/${psdoc.docId}`] = psdoc; + } } } const rows = [columns]; @@ -397,7 +403,7 @@ function _getStatusJournal(tsdoc) { export function add( domainId: string, title: string, content: string, owner: number, - rule: string, beginAt = new Date(), endAt = new Date(), pids = [], + rule: string, beginAt = new Date(), endAt = new Date(), pids: ProblemId[] = [], rated = false, data: Partial = {}, type: Type = document.TYPE_CONTEST, ) { if (!this.RULES[rule]) throw new ValidationError('rule'); @@ -444,12 +450,12 @@ export async function get(domainId: string, tid: ObjectID, type: Type | -1 = doc return tdoc; } -export async function getRelated(domainId: string, pid: number, type: Type = document.TYPE_CONTEST) { +export async function getRelated(domainId: string, pid: ProblemId, type: Type = document.TYPE_CONTEST) { return await document.getMulti(domainId, type, { pids: pid }).toArray(); } export async function updateStatus( - domainId: string, tid: ObjectID, uid: number, rid: ObjectID, pid: number, + domainId: string, tid: ObjectID, uid: number, rid: ObjectID, pid: ProblemId, accept = false, score = 0, type: 30 | 60 = document.TYPE_CONTEST, ) { const tdoc = await get(domainId, tid, type); diff --git a/packages/hydrooj/src/model/problem.ts b/packages/hydrooj/src/model/problem.ts index 53233df99..9ba71a33c 100644 --- a/packages/hydrooj/src/model/problem.ts +++ b/packages/hydrooj/src/model/problem.ts @@ -1,12 +1,17 @@ import { ObjectID, FilterQuery } from 'mongodb'; -import { Dictionary, escapeRegExp, pick } from 'lodash'; +import { + Dictionary, escapeRegExp, flatten, groupBy, pick, +} from 'lodash'; import { streamToBuffer } from '@hydrooj/utils/lib/utils'; import type { Readable } from 'stream'; import * as document from './document'; import { STATUS } from './builtin'; +import domain from './domain'; import storage from './storage'; import { buildProjection } from '../utils'; -import type { ProblemStatusDoc, ProblemDict, Document } from '../interface'; +import type { + ProblemStatusDoc, ProblemDict, Document, ProblemId, +} from '../interface'; import { ArrayKeys, MaybeArray, NumberKeys, Projection, } from '../typeutils'; @@ -16,9 +21,6 @@ import { parseConfig } from '../lib/testdataConfig'; export interface ProblemDoc extends Document { } export type Field = keyof ProblemDoc; -const fields: Field[] = []; -export type Getter = (docId?: number, pid?: string) => Partial; -const getters: Getter[] = []; export class ProblemModel { static PROJECTION_LIST: Field[] = [ @@ -32,18 +34,47 @@ export class ProblemModel { 'content', 'html', 'data', 'config', 'additional_file', ]; - static extend(getter: Getter) { - getters.push(getter); - fields.push(...Object.keys(getter(0, '0')) as any); - } + static default = { + _id: new ObjectID(), + domainId: 'system', + docType: document.TYPE_PROBLEM, + docId: 0, + pid: null, + owner: 1, + title: '*', + content: '', + html: false, + nSubmit: 0, + nAccept: 0, + tag: [], + data: [], + additional_file: [], + stats: {}, + hidden: true, + config: '', + difficulty: 0, + }; - static create(docId?: number, pid?: string) { - const result = {} as ProblemDoc; - for (const getter of getters) { - Object.assign(result, getter(docId, pid)); - } - return result; - } + static deleted = { + _id: new ObjectID(), + domainId: 'system', + docType: document.TYPE_PROBLEM, + docId: -1, + pid: null, + owner: 1, + title: '*', + content: 'Deleted Problem', + html: false, + nSubmit: 0, + nAccept: 0, + tag: [], + data: [], + additional_file: [], + stats: {}, + hidden: true, + config: '', + difficulty: 0, + }; static async add( domainId: string, pid: string = null, title: string, content: string, owner: number, @@ -80,6 +111,12 @@ export class ProblemModel { if (typeof pid !== 'number') { if (Number.isSafeInteger(parseInt(pid, 10))) pid = parseInt(pid, 10); } + if (typeof pid === 'string') { + if (pid.includes(':')) { + domainId = pid.split(':')[0]; + pid = +pid.split(':')[1]; + } + } const res = typeof pid === 'number' ? await document.get(domainId, document.TYPE_PROBLEM, pid, projection) : (await document.getMulti(domainId, document.TYPE_PROBLEM, { pid }).toArray())[0]; @@ -121,7 +158,12 @@ export class ProblemModel { return document.deleteSub(domainId, document.TYPE_PROBLEM, pid, key, values); } - static inc(domainId: string, _id: number, field: NumberKeys | string, n: number): Promise { + static inc(domainId: string, _id: ProblemId, field: NumberKeys | string, n: number): Promise { + if (typeof _id === 'string') { + if (!_id.includes(':')) throw new Error(`model.problem.inc: invalid _id <${_id}>`); + domainId = _id.split(':')[0]; + _id = +_id.split(':')[1]; + } return document.inc(domainId, document.TYPE_PROBLEM, _id, field as any, n); } @@ -191,16 +233,43 @@ export class ProblemModel { } static async getList( - domainId: string, pids: Array, + domainId: string, pids: ProblemId[], getHidden = false, doThrow = true, projection = ProblemModel.PROJECTION_PUBLIC, ): Promise { - pids = Array.from(new Set(pids)).map((i) => (+i ? +i : i)); + const parsed = groupBy( + Array.from(new Set(pids)).map((i) => ({ + domainId: (typeof i === 'string' && i.includes(':')) ? i.split(':')[0] : domainId, + pid: (typeof i === 'string' && i.includes(':')) ? +i.split(':')[1] : i, + })), + 'domainId', + ); const r = {}; const l = {}; - const q: any = { $or: [{ docId: { $in: pids } }, { pid: { $in: pids } }] }; - if (!getHidden) q.hidden = false; - const pdocs = await document.getMulti(domainId, document.TYPE_PROBLEM, q) - .project(buildProjection(projection)).toArray(); + const ddocs = await Promise.all(Object.keys(parsed).map((i) => domain.get(i))); + const f = ddocs.filter((i) => !( + i.share === '*' + || (`,${i.share.replace(/,/g, ',').split(',').map((i) => i.trim()).join(',')},`).includes(`,${domainId},`) + )); + if (f.length) { + if (doThrow) throw new ProblemNotFoundError(f[0]._id, parsed[f[0]._id][0].pid); + else { + for (const sf of f) { + for (const pinfo of parsed[sf._id]) { + r[pinfo.pid] = { ...ProblemModel.default, domainId: sf._id, pid: pinfo.pid }; + } + delete parsed[sf._id]; + } + } + } + const tasks = []; + for (const task in parsed) { + const range = { $in: parsed[task].map((i) => i.pid) }; + const q: any = { $or: [{ docId: range }, { pid: range }] }; + if (!getHidden) q.hidden = false; + console.log(task, parsed[task]); + tasks.push(document.getMulti(task, document.TYPE_PROBLEM, q).project(buildProjection(projection)).toArray()); + } + const pdocs = flatten(await Promise.all(tasks)); for (const pdoc of pdocs) { try { // eslint-disable-next-line no-await-in-loop @@ -208,15 +277,20 @@ export class ProblemModel { } catch (e) { pdoc.config = `Cannot parse: ${e.message}`; } - r[pdoc.docId] = pdoc; - l[pdoc.pid] = pdoc; + if (pdoc.domainId === domainId) { + r[pdoc.docId] = pdoc; + if (pdoc.pid) l[pdoc.pid] = pdoc; + } else { + r[`${pdoc.domainId}:${pdoc.docId}`] = pdoc; + if (pdoc.pid) l[`${pdoc.domainId}:${pdoc.pid}`] = pdoc; + } } // TODO enhance if (pdocs.length !== pids.length) { for (const pid of pids) { if (!(r[pid] || l[pid])) { if (doThrow) throw new ProblemNotFoundError(domainId, pid); - else r[pid] = ProblemModel.create(undefined, pid.toString()); + else r[pid] = { ...ProblemModel.default, domainId, pid }; } } } @@ -273,26 +347,5 @@ bus.on('problem/delTestdata', async (domainId, docId, names) => { await ProblemModel.edit(domainId, docId, { config: '' }); }); -ProblemModel.extend((docId, pid) => ({ - _id: new ObjectID(), - domainId: 'system', - docType: document.TYPE_PROBLEM, - docId: docId || -1, - pid: pid || docId.toString(), - owner: 1, - title: '*', - content: '', - html: false, - nSubmit: 0, - nAccept: 0, - tag: [], - data: [], - additional_file: [], - stats: {}, - hidden: true, - config: '', - difficulty: 0, -})); - global.Hydro.model.problem = ProblemModel; export default ProblemModel; diff --git a/packages/hydrooj/src/model/record.ts b/packages/hydrooj/src/model/record.ts index 6a7a7023c..22f8517f7 100644 --- a/packages/hydrooj/src/model/record.ts +++ b/packages/hydrooj/src/model/record.ts @@ -8,7 +8,9 @@ import moment from 'moment'; import { STATUS } from './builtin'; import task from './task'; import problem from './problem'; -import { RecordDoc, ContestInfo, ProblemConfigFile } from '../interface'; +import { + RecordDoc, ContestInfo, ProblemConfigFile, ExternalProblemId, +} from '../interface'; import { ArgMethod, buildProjection, Time } from '../utils'; import { MaybeArray } from '../typeutils'; import * as bus from '../service/bus'; @@ -29,9 +31,13 @@ class RecordModel { return base - ((pending + 0.5) * (sum(timeRecent.map((i) => i.time || 0)) / 10000)); } - static async get(domainId: string, _id: ObjectID): Promise { + static async get(_id: ObjectID): Promise + static async get(domainId: string, _id: ObjectID): Promise + static async get(arg0, arg1?) { + const _id = arg1 || arg0; + const domainId = arg1 ? arg0 : null; const res = await RecordModel.coll.findOne({ _id }); - if (res && res.domainId === domainId) return res; + if (res.domainId === (domainId || res.domainId)) return res; return null; } @@ -55,7 +61,7 @@ class RecordModel { const rdoc = await RecordModel.get(domainId, rid); let data = []; if (rdoc.pid) { - const pdoc = await problem.get(domainId, rdoc.pid); + const pdoc = await problem.get(rdoc.pdomain, rdoc.pid); data = pdoc.data; } delete rdoc._id; @@ -71,15 +77,21 @@ class RecordModel { } static async add( - domainId: string, pid: number, uid: number, + domainId: string, pid: ExternalProblemId | number, uid: number, lang: string, code: string, addTask: boolean, contestOrConfig?: ContestInfo | string, ) { + let pdomain = domainId; + if (typeof pid === 'string') { + pdomain = pid.split(':')[0]; + pid = +pid.split(':')[1]; + } const data: RecordDoc = { status: STATUS.STATUS_WAITING, _id: new ObjectID(), uid, code, lang, + pdomain, pid, domainId, score: 0, @@ -189,16 +201,20 @@ class RecordModel { } @ArgMethod - static getUserInProblemMulti( - domainId: string, uid: number, pid: number, - getHidden: boolean = false, - ) { - if (!getHidden) { + static getUserInProblemMulti(domainId: string, uid: number, pid: ExternalProblemId | number, contestInfo?: ContestInfo) { + let pdomain = domainId; + if (typeof pid === 'string') { + pdomain = pid.split(':')[0]; + pid = +pid.split(':')[1]; + } + if (!contestInfo) { return RecordModel.coll.find({ - domainId, uid, pid, hidden: false, + domainId, uid, pdomain, pid, contest: { $exists: false }, }); } - return RecordModel.coll.find({ domainId, uid, pid }); + return RecordModel.coll.find({ + domainId, uid, pdomain, pid, contest: contestInfo, + }); } @ArgMethod @@ -207,8 +223,12 @@ class RecordModel { } } -bus.on('problem/delete', (domainId, docId) => RecordModel.coll.deleteMany({ domainId, pid: docId })); -bus.on('domain/delete', (domainId) => RecordModel.coll.deleteMany({ domainId })); +// Mark problem as deleted +bus.on('problem/delete', (domainId, docId) => RecordModel.coll.updateMany({ pdomain: domainId, pid: docId }, { $set: { pid: -1 } })); +bus.on('domain/delete', (domainId) => Promise.all([ + RecordModel.coll.deleteMany({ domainId }), + RecordModel.coll.updateMany({ pdomain: domainId }, { $set: { docId: -1 } }), +])); export default RecordModel; global.Hydro.model.record = RecordModel; diff --git a/packages/hydrooj/src/model/setting.ts b/packages/hydrooj/src/model/setting.ts index e09bc1031..0f757a152 100644 --- a/packages/hydrooj/src/model/setting.ts +++ b/packages/hydrooj/src/model/setting.ts @@ -138,6 +138,7 @@ AccountSetting( DomainSetting( Setting('setting_domain', 'name', 'New domain', 'text', 'name'), Setting('setting_domain', 'avatar', '', 'text', 'avatar', 'Will be used as the domain icon.'), + Setting('setting_domain', 'share', '', 'text', 'Share problem with domain (* for any)'), Setting('setting_domain', 'bulletin', '', 'markdown', 'Bulletin'), Setting('setting_storage', 'host', '', 'text', 'Custom host', null, FLAG_HIDDEN | FLAG_DISABLED), ); diff --git a/packages/hydrooj/src/pipelineUtils.ts b/packages/hydrooj/src/pipelineUtils.ts index d14ea05b9..5163f6423 100644 --- a/packages/hydrooj/src/pipelineUtils.ts +++ b/packages/hydrooj/src/pipelineUtils.ts @@ -1,10 +1,13 @@ /* eslint-disable no-await-in-loop */ import { FilterQuery } from 'mongodb'; -import type { DomainDoc, Udoc, ProblemStatusDoc } from './interface'; +import type { + DomainDoc, Udoc, ProblemStatusDoc, RecordDoc, +} from './interface'; import domain from './model/domain'; import * as document from './model/document'; import user from './model/user'; import problem, { Field, ProblemDoc } from './model/problem'; +import RecordModel from './model/record'; export async function iterateAllDomain(cb: (ddoc: DomainDoc, current?: number, total?: number) => Promise) { const ddocs = await domain.getMulti().toArray(); @@ -56,3 +59,16 @@ export async function iterateAllProblem( await iterateAllProblemInDomain(d._id, fields, cb); }); } + +export async function iterateAllRecord( + cb: (rdoc: RecordDoc, current: number, total: number) => any, +) { + const total = await RecordModel.coll.count(); + let i = 0; + const cursor = RecordModel.coll.find().sort('_id', 1); + while (await cursor.hasNext()) { + const rdoc = await cursor.next(); + i++; + await cb(rdoc, i, total); + } +} diff --git a/packages/hydrooj/src/service/server.ts b/packages/hydrooj/src/service/server.ts index e155306e5..15032bbd0 100644 --- a/packages/hydrooj/src/service/server.ts +++ b/packages/hydrooj/src/service/server.ts @@ -349,11 +349,11 @@ export class HandlerCommon { const query: any = {}; for (const key in kwargs) { if (kwargs[key] instanceof ObjectID) args[key] = kwargs[key].toHexString(); - else args[key] = encodeURIComponent(kwargs[key].toString()); + else args[key] = kwargs[key].toString().replace(/\//g, '%2F'); } for (const key in kwargs.query || {}) { if (query[key] instanceof ObjectID) query[key] = kwargs.query[key].toHexString(); - else query[key] = encodeURIComponent(kwargs.query[key].toString()); + else query[key] = kwargs.query[key].toString().replace(/\//g, '%2F'); } try { const { anchor } = args; diff --git a/packages/hydrooj/src/service/storage.ts b/packages/hydrooj/src/service/storage.ts index 36d19f20a..57bb61b94 100644 --- a/packages/hydrooj/src/service/storage.ts +++ b/packages/hydrooj/src/service/storage.ts @@ -107,6 +107,7 @@ class StorageService { this.error = null; } catch (e) { logger.warn('Storage init fail. will retry later.'); + if (process.env.DEV) logger.warn(e); this.error = e.toString(); setTimeout(async () => { await this.start(); diff --git a/packages/hydrooj/src/upgrade.ts b/packages/hydrooj/src/upgrade.ts index 89b83c9f4..1b75811eb 100644 --- a/packages/hydrooj/src/upgrade.ts +++ b/packages/hydrooj/src/upgrade.ts @@ -393,6 +393,11 @@ const scripts: UpgradeScript[] = [ }); return true; }, + async function _29_30() { + const _FRESH_INSTALL_IGNORE = 1; + await iterateAllDomain((ddoc) => RecordModel.coll.updateMany({ domainId: ddoc._id }, { $set: { pdomain: ddoc._id } })); + return true; + }, ]; export default scripts; diff --git a/packages/migrate-vijos/package.json b/packages/migrate-vijos/package.json index 7f6acec40..07474fb60 100644 --- a/packages/migrate-vijos/package.json +++ b/packages/migrate-vijos/package.json @@ -1,6 +1,6 @@ { "name": "@hydrooj/migrate-vijos", - "version": "1.4.2", + "version": "1.4.3", "main": "package.json", "repository": "git@github.com:hydro-dev/Hydro.git", "author": "undefined ", diff --git a/packages/migrate-vijos/script.ts b/packages/migrate-vijos/script.ts index 0b635628c..e167a6c9e 100644 --- a/packages/migrate-vijos/script.ts +++ b/packages/migrate-vijos/script.ts @@ -220,6 +220,7 @@ const tasks = { code: doc.code, lang: doc.lang, uid: doc.uid, + pdomain: doc.domain_id, pid: pid(doc.pid), domainId: doc.domain_id, judger: doc.judge_uid || 1, diff --git a/packages/ui-default/package.json b/packages/ui-default/package.json index 14b4ffd9d..b0d500dab 100644 --- a/packages/ui-default/package.json +++ b/packages/ui-default/package.json @@ -1,6 +1,6 @@ { "name": "@hydrooj/ui-default", - "version": "4.10.8", + "version": "4.11.0", "author": "undefined ", "license": "AGPL-3.0", "main": "hydro.js", diff --git a/packages/ui-default/templates/problem_detail.html b/packages/ui-default/templates/problem_detail.html index 3bafcfb65..2829451cb 100644 --- a/packages/ui-default/templates/problem_detail.html +++ b/packages/ui-default/templates/problem_detail.html @@ -15,13 +15,13 @@ {{ set(UiContext, 'postSubmitUrl', url('contest_detail_problem_submit' if tdoc.docType == model.document.TYPE_CONTEST else 'homework_detail_problem_submit', pid=pdoc.docId, tid=tdoc.docId)) }} {{ set(UiContext, 'getSubmissionsUrl', url('contest_detail_problem_submit' if tdoc.docType == model.document.TYPE_CONTEST else 'homework_detail_problem_submit', pid=pdoc.docId, tid=tdoc.docId)) }} {{ set(UiContext, 'getRecordDetailUrl', url('record_detail', rid='{rid}', query={tid:tdoc.docId})) }} - {{ set(UiContext, 'pretestConnUrl', "/record-conn?pretest=1&uid=" + handler.user._id + "&pid=" + pdoc.pid|default(pdoc.docId) + "&domainId=" + handler.domainId + "&tid=" + tdoc.docId) }} + {{ set(UiContext, 'pretestConnUrl', "/record-conn?pretest=1&uid=" + handler.user._id + "&pid=" + pdoc.docId + "&domainId=" + handler.domainId + "&tid=" + tdoc.docId) }} {% else %} {{ set(UiContext, 'canViewRecord', true) }} {{ set(UiContext, 'postSubmitUrl', url('problem_submit', pid=pdoc.docId)) }} {{ set(UiContext, 'getSubmissionsUrl', url('problem_submit', pid=pdoc.docId)) }} {{ set(UiContext, 'getRecordDetailUrl', url('record_detail', rid='{rid}')) }} - {{ set(UiContext, 'pretestConnUrl', "/record-conn?pretest=1&uid=" + handler.user._id + "&pid=" + pdoc.pid|default(pdoc.docId) + "&domainId=" + handler.domainId) }} + {{ set(UiContext, 'pretestConnUrl', "/record-conn?pretest=1&uid=" + handler.user._id + "&pid=" + pdoc.docId + "&domainId=" + handler.domainId) }} {% endif %}
diff --git a/packages/utils/lib/utils.ts b/packages/utils/lib/utils.ts index 3de316e93..95d2a3895 100644 --- a/packages/utils/lib/utils.ts +++ b/packages/utils/lib/utils.ts @@ -1,7 +1,7 @@ import path from 'path'; import fs from 'fs-extra'; import os from 'os'; -import { Duplex } from 'stream'; +import { Duplex, Readable } from 'stream'; import { ObjectID } from 'mongodb'; import { isMoment } from 'moment'; import type { Moment } from 'moment-timezone'; @@ -192,11 +192,20 @@ export function isClass(obj: any, strict = false) { return false; } -export function streamToBuffer(stream): Promise { +export function streamToBuffer(stream: any, maxSize = 0): Promise { return new Promise((resolve, reject) => { const buffers = []; + let length = 0; + function onData(data) { + buffers.push(data); + length += data.length; + if (maxSize && length > maxSize) { + stream.removeListener('data', onData); + reject(new Error('buffer length exceeded')); + } + } stream.on('error', reject); - stream.on('data', (data) => buffers.push(data)); + stream.on('data', onData); stream.on('end', () => resolve(Buffer.concat(buffers))); }); }