core: !EXPERIMENTAL! 支持跨域引用题目

This commit is contained in:
undefined committed 2021-05-28 22:10:16 +08:00
1 parent 33d7c70ef6
commit d897391e9a
25 files changed
+434 -392

No files matched your search

+1 -3
View File
@@ -58,7 +58,7 @@ class JudgeTask {
this.stat.handle = new Date(); this.stat.handle = new Date();
this.tag = this.request.tag; this.tag = this.request.tag;
this.type = this.request.type; this.type = this.request.type;
this.domainId = this.request.domainId; this.domainId = this.request.pdomain;
this.pid = this.request.pid.toString(); this.pid = this.request.pid.toString();
this.rid = this.request.rid; this.rid = this.request.rid;
this.lang = this.request.lang; this.lang = this.request.lang;
@@ -120,7 +120,6 @@ class JudgeTask {
next(data, id?: number) { next(data, id?: number) {
log.debug('Next: %d %o', id, data); log.debug('Next: %d %o', id, data);
data.key = 'next'; data.key = 'next';
data.domainId = this.request.domainId;
data.rid = new ObjectID(this.rid); data.rid = new ObjectID(this.rid);
if (data.time_ms) data.time = data.time_ms; if (data.time_ms) data.time = data.time_ms;
if (data.memory_kb) data.memory = data.memory_kb; if (data.memory_kb) data.memory = data.memory_kb;
@@ -162,7 +161,6 @@ class JudgeTask {
log.info('End: %o', data); log.info('End: %o', data);
data.key = 'end'; data.key = 'end';
data.rid = new ObjectID(this.request.rid); data.rid = new ObjectID(this.request.rid);
data.domainId = this.request.domainId;
data.time = data.time_ms ?? data.time; data.time = data.time_ms ?? data.time;
data.memory = data.memory_kb ?? data.memory; data.memory = data.memory_kb ?? data.memory;
this.ws.send(JSON.stringify(data)); this.ws.send(JSON.stringify(data));
+3 -5
View File
@@ -102,7 +102,6 @@ async function postInit() {
that.nextWaiting = []; that.nextWaiting = [];
return (data, id = 0) => { return (data, id = 0) => {
logger.debug('Next: %d %o', id, data); logger.debug('Next: %d %o', id, data);
data.domainId = that.domainId;
data.rid = new ObjectID(that.rid); data.rid = new ObjectID(that.rid);
if (data.time_ms) data.time = data.time_ms; if (data.time_ms) data.time = data.time_ms;
if (data.memory_kb) data.memory = data.memory_kb; 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) => { return (data) => {
data.key = 'end'; data.key = 'end';
data.rid = new ObjectID(rid); data.rid = new ObjectID(rid);
data.domainId = domainId;
data.time = data.time_ms ?? data.time; data.time = data.time_ms ?? data.time;
data.memory = data.memory_kb ?? data.memory; 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); 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.stat.handle = new Date();
this.pid = (this.request.pid || 'unknown').toString(); this.pid = (this.request.pid || 'unknown').toString();
this.rid = this.request.rid.toString(); this.rid = this.request.rid.toString();
this.domainId = this.request.domainId; this.domainId = this.request.pdomain;
this.lang = this.request.lang; this.lang = this.request.lang;
this.code = this.request.code; this.code = this.request.code;
this.data = this.request.data; this.data = this.request.data;
this.config = this.request.config; this.config = this.request.config;
this.input = this.request.input; this.input = this.request.input;
this.next = getNext(this); 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.tmpdir = path.resolve(getConfig('tmp_dir'), this.rid);
this.clean = []; this.clean = [];
fs.ensureDirSync(this.tmpdir); fs.ensureDirSync(this.tmpdir);
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.27.7", "version": "2.28.0",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "dist/loader.js", "main": "dist/loader.js",
"typings": "dist/loader.d.ts", "typings": "dist/loader.d.ts",
+79 -124
View File
@@ -1,11 +1,14 @@
import moment from 'moment-timezone'; import moment from 'moment-timezone';
import { ObjectID } from 'mongodb'; import { ObjectID } from 'mongodb';
import AdmZip from 'adm-zip'; import AdmZip from 'adm-zip';
import { Time } from '@hydrooj/utils/lib/utils';
import { import {
ContestNotLiveError, ValidationError, ProblemNotFoundError, ContestNotLiveError, ValidationError, ProblemNotFoundError,
ContestNotAttendedError, PermissionError, BadRequestError, ContestNotAttendedError, PermissionError, BadRequestError,
} from '../error'; } from '../error';
import { ProblemDoc, Tdoc, User } from '../interface'; import {
ProblemId, ProblemDoc, Tdoc, User,
} from '../interface';
import paginate from '../lib/paginate'; import paginate from '../lib/paginate';
import { PERM, PRIV } from '../model/builtin'; import { PERM, PRIV } from '../model/builtin';
import * as contest from '../model/contest'; import * as contest from '../model/contest';
@@ -22,23 +25,13 @@ import {
import * as bus from '../service/bus'; import * as bus from '../service/bus';
import storage from '../service/storage'; import storage from '../service/storage';
class ContestListHandler extends Handler { export class ContestListHandler extends Handler {
@param('rule', Types.Range(contest.RULES), true) @param('rule', Types.Range(contest.RULES), true)
@param('page', Types.PositiveInt, true) @param('page', Types.PositiveInt, true)
async get(domainId: string, rule = '', page = 1) { async get(domainId: string, rule = '', page = 1) {
this.response.template = 'contest_main.html'; const cursor = contest.getMulti(domainId, rule ? { rule } : undefined).sort({ beginAt: -1 });
let tdocs; const qs = rule ? `rule=${rule}` : '';
let qs; const [tdocs, tpcount] = await paginate(cursor, page, system.get('pagination.contest'));
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 tids = []; const tids = [];
for (const tdoc of tdocs) tids.push(tdoc.docId); for (const tdoc of tdocs) tids.push(tdoc.docId);
const tsdict = await contest.getListStatus(domainId, this.user._id, tids); const tsdict = await contest.getListStatus(domainId, this.user._id, tids);
@@ -46,13 +39,14 @@ class ContestListHandler extends Handler {
['Hydro', 'homepage'], ['Hydro', 'homepage'],
['contest_main', null], ['contest_main', null],
]; ];
this.response.template = 'contest_main.html';
this.response.body = { this.response.body = {
page, tpcount, qs, rule, tdocs, tsdict, path, page, tpcount, qs, rule, tdocs, tsdict, path,
}; };
} }
} }
class ContestDetailHandler extends Handler { export class ContestDetailHandler extends Handler {
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID)
@param('page', Types.PositiveInt, true) @param('page', Types.PositiveInt, true)
async get(domainId: string, tid: ObjectID, page = 1) { 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; tdoc: Tdoc;
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID, true)
async prepare(domainId: string, tid: ObjectID) { async prepare(domainId: string, tid: ObjectID) {
this.tdoc = await contest.get(domainId, tid); if (tid) {
if (!this.user.own(this.tdoc)) this.checkPerm(PERM.PERM_EDIT_CONTEST); this.tdoc = await contest.get(domainId, tid);
else this.checkPerm(PERM.PERM_EDIT_CONTEST_SELF); 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'; this.response.template = 'contest_edit.html';
const rules = {}; const rules = {};
for (const i in contest.RULES) { for (const i in contest.RULES) rules[i] = contest.RULES[i].TEXT;
rules[i] = contest.RULES[i].TEXT;
}
const duration = (this.tdoc.endAt.getTime() - this.tdoc.beginAt.getTime()) / 3600 / 1000;
const path = [ const path = [
['Hydro', 'homepage'], ['Hydro', 'homepage'],
['contest_main', 'contest_main'], ['contest_main', 'contest_main'],
[this.tdoc.title, 'contest_detail', { tid: this.tdoc.docId }, true], ...tid
['contest_edit', null], ? [
[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 = { this.response.body = {
rules, rules,
tdoc: this.tdoc, tdoc: this.tdoc,
duration, duration: tid ? (this.tdoc.endAt.getTime() - this.tdoc.beginAt.getTime()) / Time.hour : 2,
path, path,
pids: this.tdoc.pids.join(','), pids: tid ? this.tdoc.pids.join(',') : '',
date_text: dt.format('%Y-%m-%d'), date_text: dt.format('%Y-%m-%d'),
time_text: dt.format('%H:%M'), 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('beginAtDate', Types.Date)
@param('beginAtTime', Types.Time) @param('beginAtTime', Types.Time)
@param('duration', Types.Float) @param('duration', Types.Float)
@@ -210,31 +211,36 @@ class ContestEditHandler extends Handler {
@param('pids', Types.Content) @param('pids', Types.Content)
@param('rated', Types.Boolean) @param('rated', Types.Boolean)
async post( 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, title: string, content: string, rule: 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) => {
await problem.getList(domainId, pids, this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN), true); if ((+i).toString() === i) return +i;
return i;
}).filter((i) => i);
const beginAtMoment = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone); const beginAtMoment = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
if (!beginAtMoment.isValid()) { if (!beginAtMoment.isValid()) throw new ValidationError('beginAtDate', 'beginAtTime');
throw new ValidationError('beginAtDate', 'beginAtTime');
}
const endAt = beginAtMoment.clone().add(duration, 'hours').toDate(); const endAt = beginAtMoment.clone().add(duration, 'hours').toDate();
if (beginAtMoment.isSameOrAfter(endAt)) throw new ValidationError('duration'); if (beginAtMoment.isSameOrAfter(endAt)) throw new ValidationError('duration');
const beginAt = beginAtMoment.toDate(); const beginAt = beginAtMoment.toDate();
await contest.edit(domainId, this.tdoc.docId, { await problem.getList(domainId, pids, this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN), true);
title, content, rule, beginAt, endAt, pids, rated, if (tid) {
}); await contest.edit(domainId, tid, {
if (this.tdoc.beginAt !== beginAt || this.tdoc.endAt !== endAt title, content, rule, beginAt, endAt, pids, rated,
|| Array.isDiff(this.tdoc.pids, pids) || this.tdoc.rule !== rule) { });
await contest.recalcStatus(domainId, this.tdoc.docId); 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.body = { tid };
this.response.redirect = this.url('contest_detail', { tid: this.tdoc.docId }); this.response.redirect = this.url('contest_detail', { tid });
} }
} }
class ContestProblemHandler extends Handler { export class ContestProblemHandler extends Handler {
tdoc: Tdoc<30 | 60>; tdoc: Tdoc<30 | 60>;
pdoc: ProblemDoc; pdoc: ProblemDoc;
tsdoc: any; tsdoc: any;
@@ -242,25 +248,22 @@ class ContestProblemHandler extends Handler {
attended: boolean; attended: boolean;
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID)
@param('pid', Types.UnsignedInt) @param('pid', Types.Name)
async _prepare(domainId: string, tid: ObjectID, pid: number) { async _prepare(domainId: string, tid: ObjectID, pid: ProblemId) {
[this.tdoc, this.pdoc] = await Promise.all([ this.tdoc = await contest.get(domainId, tid);
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), problem.get(domainId, pid),
]); contest.getStatus(domainId, tid, this.user._id),
[this.tsdoc, this.udoc] = await Promise.all([
contest.getStatus(domainId, this.tdoc.docId, this.user._id),
user.getById(domainId, this.tdoc.owner), 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; 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'; 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 = { this.response.body = {
tdoc: this.tdoc, tdoc: this.tdoc,
tsdoc: this.tsdoc, 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[]) { async get(...args: any[]) {
this.response.body.path = [ this.response.body.path = [
['Hydro', 'homepage'], ['Hydro', 'homepage'],
['contest_main', 'contest_main'], ['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], [this.pdoc.title, null, null, true],
]; ];
// Navigate to current additional file download // Navigate to current additional file download
// e.g. ![img](a.jpg) will navigate to ![img](./pid/file/a.jpg) // e.g. ![img](a.jpg) will navigate to ![img](./pid/file/a.jpg)
this.response.body.pdoc.content = this.response.body.pdoc.content 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() { async prepare() {
if (!contest.isOngoing(this.tdoc)) throw new ContestNotLiveError(this.tdoc.docId); if (!contest.isOngoing(this.tdoc)) throw new ContestNotLiveError(this.tdoc.docId);
} }
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID)
@param('pid', Types.UnsignedInt) @param('pid', Types.Name)
async get(domainId: string, tid: ObjectID, pid: number) { async get(domainId: string, tid: ObjectID, pid: ProblemId) {
this.response.body.rdocs = []; this.response.body.rdocs = [];
if (contest.canShowRecord.call(this, this.tdoc)) { if (contest.canShowRecord.call(this, this.tdoc)) {
this.response.body.rdocs = await record.getUserInProblemMulti( this.response.body.rdocs = await record.getUserInProblemMulti(
domainId, this.user._id, domainId, this.user._id,
this.pdoc.docId, true, pid, { type: document.TYPE_CONTEST, tid },
).sort({ _id: -1 }).limit(10).toArray(); ).sort({ _id: -1 }).limit(10).toArray();
} }
this.response.body.path = [ this.response.body.path = [
@@ -330,20 +334,21 @@ class ContestDetailProblemSubmitHandler extends ContestProblemHandler {
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID)
@param('lang', Types.Name) @param('lang', Types.Name)
@param('code', Types.Content) @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)) { if (this.response.body.pdoc.config?.langs && !this.response.body.pdoc.config.langs.includes(lang)) {
throw new BadRequestError('Language not allowed.'); throw new BadRequestError('Language not allowed.');
} }
await this.limitRate('add_record', 60, 10); 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, type: document.TYPE_CONTEST,
tid, tid,
}); });
const [rdoc] = await Promise.all([ const [rdoc] = await Promise.all([
record.get(domainId, rid), 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'), 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); bus.boardcast('record/change', rdoc);
if (!contest.canShowRecord.call(this, this.tdoc)) { 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) @param('tid', Types.ObjectID)
async get(domainId: string, tid: ObjectID) { async get(domainId: string, tid: ObjectID) {
if (!this.user.hasPriv(PRIV.PRIV_READ_RECORD_CODE)) { 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() { 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_main', '/contest', ContestListHandler, PERM.PERM_VIEW_CONTEST);
Route('contest_detail', '/contest/:tid', ContestDetailHandler, PERM.PERM_VIEW_CONTEST); Route('contest_detail', '/contest/:tid', ContestDetailHandler, PERM.PERM_VIEW_CONTEST);
Route('contest_boardcast', '/contest/:tid/boardcast', ContestBoardcastHandler); Route('contest_boardcast', '/contest/:tid/boardcast', ContestBoardcastHandler);
+2 -2
View File
@@ -11,7 +11,7 @@ import {
Route, Connection, Handler, ConnectionHandler, param, Types, Route, Connection, Handler, ConnectionHandler, param, Types,
} from '../service/server'; } from '../service/server';
import { md5 } from '../lib/crypto'; import { md5 } from '../lib/crypto';
import { isPassword, isEmail } from '../lib/validator'; import { isPassword, isEmail, isDomainId } from '../lib/validator';
import avatar from '../lib/avatar'; import avatar from '../lib/avatar';
import * as mail from '../lib/mail'; import * as mail from '../lib/mail';
import * as contest from '../model/contest'; import * as contest from '../model/contest';
@@ -321,7 +321,7 @@ class HomeDomainCreateHandler extends Handler {
this.response.template = 'domain_create.html'; this.response.template = 'domain_create.html';
} }
@param('id', Types.Name) @param('id', Types.Name, isDomainId)
@param('name', Types.Title) @param('name', Types.Title)
@param('bulletin', Types.Content) @param('bulletin', Types.Content)
@param('avatar', Types.Content, true) @param('avatar', Types.Content, true)
+77 -121
View File
@@ -2,13 +2,13 @@ import yaml from 'js-yaml';
import moment from 'moment-timezone'; import moment from 'moment-timezone';
import { ObjectID } from 'mongodb'; import { ObjectID } from 'mongodb';
import AdmZip from 'adm-zip'; import AdmZip from 'adm-zip';
import { Time } from '@hydrooj/utils/lib/utils';
import { import {
ValidationError, HomeworkNotLiveError, ProblemNotFoundError, ValidationError, HomeworkNotLiveError, ProblemNotFoundError,
HomeworkNotAttendedError, HomeworkNotAttendedError, BadRequestError,
BadRequestError,
} from '../error'; } from '../error';
import { import {
PenaltyRules, Tdoc, ProblemDoc, User, PenaltyRules, Tdoc, ProblemDoc, User, ProblemId,
} from '../interface'; } from '../interface';
import { import {
Route, Handler, Types, param, Route, Handler, Types, param,
@@ -24,11 +24,9 @@ import problem from '../model/problem';
import record from '../model/record'; import record from '../model/record';
import * as document from '../model/document'; import * as document from '../model/document';
import paginate from '../lib/paginate'; import paginate from '../lib/paginate';
import { isExternalPid } from '../lib/validator';
const validatePenaltyRules = (input: string) => { const validatePenaltyRules = (input: string) => yaml.load(input);
const s = yaml.load(input);
return s;
};
const convertPenaltyRules = validatePenaltyRules; const convertPenaltyRules = validatePenaltyRules;
class HomeworkMainHandler extends Handler { class HomeworkMainHandler extends Handler {
@@ -40,9 +38,7 @@ class HomeworkMainHandler extends Handler {
if (contest.isExtended(tdoc) || contest.isDone(tdoc)) { if (contest.isExtended(tdoc) || contest.isDone(tdoc)) {
cal.endAt = tdoc.endAt; cal.endAt = tdoc.endAt;
cal.penaltySince = tdoc.penaltySince; cal.penaltySince = tdoc.penaltySince;
} else { } else cal.endAt = tdoc.penaltySince;
cal.endAt = tdoc.penaltySince;
}
calendar.push(cal); calendar.push(cal);
} }
const path = [ const path = [
@@ -126,12 +122,14 @@ class HomeworkDetailProblemHandler extends Handler {
attended: boolean; attended: boolean;
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID)
@param('pid', Types.UnsignedInt) @param('pid', Types.String, isExternalPid)
async prepare(domainId: string, tid: ObjectID, pid: number) { async _prepare(domainId: string, tid: ObjectID, pid: ProblemId) {
this.checkPerm(PERM.PERM_VIEW_PROBLEM); 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([ [this.tdoc, this.pdoc, this.tsdoc] = await Promise.all([
contest.get(domainId, tid, document.TYPE_HOMEWORK), 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), contest.getStatus(domainId, tid, this.user._id, document.TYPE_HOMEWORK),
]); ]);
this.udoc = await user.getById(domainId, this.tdoc.owner); this.udoc = await user.getById(domainId, this.tdoc.owner);
@@ -146,13 +144,13 @@ class HomeworkDetailProblemHandler extends Handler {
} }
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID)
@param('pid', Types.UnsignedInt) @param('pid', Types.Name)
async get(domainId: string, tid: ObjectID, pid: number) { // eslint-disable-next-line @typescript-eslint/no-unused-vars
async get(domainId: string, tid: ObjectID, pid: ProblemId) {
if (!contest.isDone(this.tdoc)) { if (!contest.isDone(this.tdoc)) {
if (!this.attended) throw new HomeworkNotAttendedError(tid); if (!this.attended) throw new HomeworkNotAttendedError(tid);
if (contest.isNotStarted(this.tdoc)) throw new HomeworkNotLiveError(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.template = 'problem_detail.html';
this.response.body.page_name = 'homework_detail_problem'; this.response.body.page_name = 'homework_detail_problem';
this.response.body.path = [ this.response.body.path = [
@@ -166,13 +164,16 @@ class HomeworkDetailProblemHandler extends Handler {
class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler { class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler {
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID)
@param('pid', Types.UnsignedInt) async prepare(domainId: string, tid: ObjectID) {
async get(domainId: string, tid: ObjectID, pid: number) {
if (!this.attended) throw new HomeworkNotAttendedError(tid); if (!this.attended) throw new HomeworkNotAttendedError(tid);
if (!contest.isOngoing(this.tdoc)) throw new HomeworkNotLiveError(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) 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() .sort('_id', -1).limit(10).toArray()
: []; : [];
this.response.body.path = [ this.response.body.path = [
@@ -186,24 +187,23 @@ class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler {
} }
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID)
@param('pid', Types.UnsignedInt) @param('pid', Types.String, isExternalPid)
@param('code', Types.Content) @param('code', Types.Content)
@param('lang', Types.Name) @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)) { if (this.response.body.pdoc.config?.langs && !this.response.body.pdoc.config.langs.includes(lang)) {
throw new BadRequestError('Language not allowed.'); throw new BadRequestError('Language not allowed.');
} }
await this.limitRate('add_record', 60, 5); 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, { const rid = await record.add(domainId, pid, this.user._id, lang, code, true, {
type: document.TYPE_HOMEWORK, type: document.TYPE_HOMEWORK,
tid, tid,
}); });
const pdomainId = typeof pid === 'string' ? pid.split(':')[0] : domainId;
const ppid = typeof pid === 'number' ? pid : +pid.split(':')[1];
const [rdoc] = await Promise.all([ const [rdoc] = await Promise.all([
record.get(domainId, rid), record.get(domainId, rid),
problem.inc(domainId, pid, 'nSubmit', 1), problem.inc(pdomainId, ppid, 'nSubmit', 1),
domain.incUserInDomain(domainId, this.user._id, 'nSubmit'), domain.incUserInDomain(domainId, this.user._id, 'nSubmit'),
contest.updateStatus(domainId, tid, this.user._id, contest.updateStatus(domainId, tid, this.user._id,
rid, pid, false, 0, document.TYPE_HOMEWORK), 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 { class HomeworkEditHandler extends Handler {
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID, true)
async get(domainId: string, tid: ObjectID) { 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); if (!this.user.own(tdoc)) this.checkPerm(PERM.PERM_EDIT_HOMEWORK);
else this.checkPerm(PERM.PERM_EDIT_HOMEWORK_SELF); else this.checkPerm(PERM.PERM_EDIT_HOMEWORK_SELF);
const extensionDays = Math.round( const extensionDays = tid
(tdoc.endAt.getTime() - tdoc.penaltySince.getTime()) / 36000 / 24, ? Math.round(
) / 100; (tdoc.endAt.getTime() - tdoc.penaltySince.getTime()) / (Time.day / 100),
) / 100
: 1;
const path = [ const path = [
['Hydro', 'homepage'], ['Hydro', 'homepage'],
['homework_main', 'homework_main'], ['homework_main', 'homework_main'],
[tdoc.title, 'homework_detail', { tid }, true], ...tid
['homework_edit', null], ? [
[tdoc.title, 'homework_detail', { tid }, true],
['homework_edit', null],
]
: [['homework_create', null]],
]; ];
const beginAt = moment(tdoc.beginAt).tz(this.user.timeZone); const beginAt = tid
const penaltySince = moment(tdoc.penaltySince).tz(this.user.timeZone); ? 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.template = 'homework_edit.html';
this.response.body = { this.response.body = {
tdoc, tdoc,
@@ -298,10 +256,11 @@ class HomeworkEditHandler extends Handler {
penaltyRules: yaml.dump(tdoc.penaltyRules), penaltyRules: yaml.dump(tdoc.penaltyRules),
pids: tdoc.pids.join(','), pids: tdoc.pids.join(','),
path, path,
page_name: tid ? 'homework_edit' : 'homework_create',
}; };
} }
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID, true)
@param('beginAtDate', Types.Date) @param('beginAtDate', Types.Date)
@param('beginAtTime', Types.Time) @param('beginAtTime', Types.Time)
@param('penaltySinceDate', Types.Date) @param('penaltySinceDate', Types.Date)
@@ -315,38 +274,37 @@ class HomeworkEditHandler extends Handler {
async post( async post(
domainId: string, tid: ObjectID, beginAtDate: string, beginAtTime: string, domainId: string, tid: ObjectID, beginAtDate: string, beginAtTime: string,
penaltySinceDate: string, penaltySinceTime: string, extensionDays: number, 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); const tdoc = await contest.get(domainId, tid, document.TYPE_HOMEWORK);
if (!this.user.own(tdoc)) this.checkPerm(PERM.PERM_EDIT_HOMEWORK); if (!this.user.own(tdoc)) this.checkPerm(PERM.PERM_EDIT_HOMEWORK);
else this.checkPerm(PERM.PERM_EDIT_HOMEWORK_SELF); else this.checkPerm(PERM.PERM_EDIT_HOMEWORK_SELF);
let beginAt; const beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
let penaltySince; if (!beginAt.isValid()) throw new ValidationError('beginAtDate', 'beginAtTime');
try { const penaltySince = moment.tz(`${penaltySinceDate} ${penaltySinceTime}`, this.user.timeZone);
beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone); if (!penaltySince.isValid()) throw new ValidationError('endAtDate', 'endAtTime');
} catch (e) { const endAt = penaltySince.clone().add(extensionDays, 'days');
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');
if (beginAt.isSameOrAfter(penaltySince)) throw new ValidationError('endAtDate', 'endAtTime'); 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); await problem.getList(domainId, pids, this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN), true);
beginAt = beginAt.toDate(); if (!tid) {
endAt = endAt.toDate(); tid = await contest.add(domainId, title, content, this.user._id,
penaltySince = penaltySince.toDate(); 'homework', beginAt.toDate(), endAt.toDate(), pids, rated,
await contest.edit(domainId, tid, { { penaltySince: penaltySince.toDate(), penaltyRules }, document.TYPE_HOMEWORK);
title, content, beginAt, endAt, pids, penaltySince, penaltyRules, rated, } else {
}, document.TYPE_HOMEWORK); await contest.edit(domainId, tid, {
if (tdoc.beginAt !== beginAt title, content, beginAt: beginAt.toDate(), endAt: endAt.toDate(), pids, penaltySince: penaltySince.toDate(), penaltyRules, rated,
|| tdoc.endAt !== endAt }, document.TYPE_HOMEWORK);
|| tdoc.penaltySince !== penaltySince if (tdoc.beginAt !== beginAt.toDate()
|| tdoc.pids.sort().join(' ') !== pids.sort().join(' ')) { || tdoc.endAt !== endAt.toDate()
await contest.recalcStatus(domainId, tdoc.docId, document.TYPE_HOMEWORK); || 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.body = { tid };
this.response.redirect = this.url('homework_detail', { tid }); this.response.redirect = this.url('homework_detail', { tid });
@@ -405,9 +363,7 @@ class HomeworkCodeHandler extends Handler {
} }
const zip = new AdmZip(); const zip = new AdmZip();
const rdocs = await record.getMulti(domainId, { const rdocs = await record.getMulti(domainId, {
_id: { _id: { $in: Array.from(Object.keys(rnames)).map((id) => new ObjectID(id)) },
$in: Array.from(Object.keys(rnames)).map((id) => new ObjectID(id)),
},
}).toArray(); }).toArray();
for (const rdoc of rdocs) { for (const rdoc of rdocs) {
zip.addFile(`${rnames[rdoc._id.toHexString()]}.${rdoc.lang}`, Buffer.from(rdoc.code)); zip.addFile(`${rnames[rdoc._id.toHexString()]}.${rdoc.lang}`, Buffer.from(rdoc.code));
@@ -418,7 +374,7 @@ class HomeworkCodeHandler extends Handler {
export async function apply() { export async function apply() {
Route('homework_main', '/homework', HomeworkMainHandler, PERM.PERM_VIEW_HOMEWORK); 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_detail', '/homework/:tid', HomeworkDetailHandler, PERM.PERM_VIEW_HOMEWORK);
Route('homework_scoreboard', '/homework/:tid/scoreboard', HomeworkScoreboardHandler, PERM.PERM_VIEW_HOMEWORK_SCOREBOARD); Route('homework_scoreboard', '/homework/:tid/scoreboard', HomeworkScoreboardHandler, PERM.PERM_VIEW_HOMEWORK_SCOREBOARD);
Route( Route(
+12 -8
View File
@@ -23,16 +23,20 @@ const logger = new Logger('judge');
async function _postJudge(rdoc: RecordDoc) { async function _postJudge(rdoc: RecordDoc) {
if (typeof rdoc.input === 'string') return; if (typeof rdoc.input === 'string') return;
const accept = rdoc.status === builtin.STATUS.STATUS_ACCEPTED; 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) { if (rdoc.contest) {
await contest.updateStatus( await contest.updateStatus(
rdoc.domainId, rdoc.contest.tid, rdoc.uid, rdoc.domainId, rdoc.contest.tid, rdoc.uid,
rdoc._id, rdoc.pid, accept, rdoc.score, rdoc.contest.type, 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); } else if (accept && updated) await domain.incUserInDomain(rdoc.domainId, rdoc.uid, 'nAccept', 1);
const pdoc = (accept && updated) const pdoc = (accept && updated)
? await problem.inc(rdoc.domainId, rdoc.pid, 'nAccept', 1) ? await problem.inc(rdoc.pdomain, rdoc.pid, 'nAccept', 1)
: await problem.get(rdoc.domainId, rdoc.pid); : await problem.get(rdoc.pdomain, rdoc.pid);
const difficulty = difficultyAlgorithm(pdoc.nSubmit, pdoc.nAccept); const difficulty = difficultyAlgorithm(pdoc.nSubmit, pdoc.nAccept);
if (!updated) await record.updateMulti(rdoc.domainId, { uid: rdoc.uid, status: builtin.STATUS.STATUS_ACCEPTED }, { effective: false }); if (!updated) await record.updateMulti(rdoc.domainId, { uid: rdoc.uid, status: builtin.STATUS.STATUS_ACCEPTED }, { effective: false });
await Promise.all([ await Promise.all([
@@ -45,8 +49,8 @@ async function _postJudge(rdoc: RecordDoc) {
} }
export async function next(body: JudgeResultBody) { export async function next(body: JudgeResultBody) {
if (body.rid) body.rid = new ObjectID(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<RecordDoc> = {}; const $set: Partial<RecordDoc> = {};
const $push: any = {}; const $push: any = {};
if (body.case) { if (body.case) {
@@ -72,13 +76,13 @@ export async function next(body: JudgeResultBody) {
if (body.time !== undefined) $set.time = body.time; if (body.time !== undefined) $set.time = body.time;
if (body.memory !== undefined) $set.memory = body.memory; if (body.memory !== undefined) $set.memory = body.memory;
if (body.progress !== undefined) $set.progress = body.progress; 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); bus.boardcast('record/change', rdoc, $set, $push);
} }
export async function end(body: JudgeResultBody) { export async function end(body: JudgeResultBody) {
if (body.rid) body.rid = new ObjectID(body.rid); 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<RecordDoc> = {}; const $set: Partial<RecordDoc> = {};
const $push: any = {}; const $push: any = {};
const $unset: any = { progress: '' }; const $unset: any = { progress: '' };
@@ -97,7 +101,7 @@ export async function end(body: JudgeResultBody) {
$set.judgeAt = new Date(); $set.judgeAt = new Date();
$set.judger = body.judger ?? 1; $set.judger = body.judger ?? 1;
await sleep(100); // Make sure that all 'next' event already triggered 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); await _postJudge(rdoc);
bus.boardcast('record/change', rdoc); // trigger a full update bus.boardcast('record/change', rdoc); // trigger a full update
} }
+1 -1
View File
@@ -247,7 +247,7 @@ export class ProblemSubmitHandler extends ProblemDetailHandler {
async get(domainId: string, pid: string | number) { async get(domainId: string, pid: string | number) {
this.response.template = 'problem_submit.html'; this.response.template = 'problem_submit.html';
const rdocs = await record const rdocs = await record
.getUserInProblemMulti(domainId, this.user._id, this.pdoc.docId) .getUserInProblemMulti(domainId, this.user._id, pid)
.sort({ _id: -1 }) .sort({ _id: -1 })
.limit(10) .limit(10)
.toArray(); .toArray();
+23 -21
View File
@@ -1,4 +1,5 @@
import { FilterQuery, ObjectID } from 'mongodb'; import { FilterQuery, ObjectID } from 'mongodb';
import { pick } from 'lodash';
import { import {
ContestNotFoundError, PermissionError, ProblemNotFoundError, ContestNotFoundError, PermissionError, ProblemNotFoundError,
RecordNotFoundError, UserNotFoundError, RecordNotFoundError, UserNotFoundError,
@@ -58,8 +59,10 @@ class RecordListHandler extends Handler {
const [udict, pdict] = await Promise.all([ const [udict, pdict] = await Promise.all([
user.getList(domainId, rdocs.map((rdoc) => rdoc.uid)), user.getList(domainId, rdocs.map((rdoc) => rdoc.uid)),
canViewProblem canViewProblem
? problem.getList(domainId, rdocs.map((rdoc) => rdoc.pid), canViewProblemHidden, false) ? problem.getList(domainId, rdocs.map(
: Object.fromEntries([rdocs.map((rdoc) => [rdoc.pid, problem.create(rdoc.pid, rdoc.pid.toString())])]), (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 = [ const path = [
['Hydro', 'homepage'], ['Hydro', 'homepage'],
@@ -100,16 +103,15 @@ class RecordDetailHandler extends Handler {
} }
// eslint-disable-next-line prefer-const // eslint-disable-next-line prefer-const
let [pdoc, udoc] = await Promise.all([ let [pdoc, udoc] = await Promise.all([
problem.get(domainId, rdoc.pid), problem.get(rdoc.pdomain, rdoc.pid),
user.getById(domainId, rdoc.uid), user.getById(domainId, rdoc.uid),
]); ]);
if (!(pdoc && this.user.hasPerm(PERM.PERM_VIEW_PROBLEM))) { if (
pdoc = problem.create(pdoc?.docId || 0, pdoc?.pid || '*'); !pdoc
} || !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM)
if (!rdoc.contest && pdoc.hidden && !this.user.own(pdoc)) { || (!rdoc.contest && pdoc.hidden && !this.user.own(pdoc) && !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN))
if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) { ) {
pdoc = problem.create(pdoc.docId, pdoc.pid); pdoc = { ...problem.default, docId: pdoc?.docId || 0, pid: '*' };
}
} }
this.response.body = { this.response.body = {
path: [ path: [
@@ -162,6 +164,7 @@ class RecordMainConnectionHandler extends ConnectionHandler {
dispose: bus.Disposable; dispose: bus.Disposable;
tid: string; tid: string;
uid: number; uid: number;
pdomain: string;
pid: number; pid: number;
status: number; status: number;
pretest: boolean; pretest: boolean;
@@ -172,16 +175,11 @@ class RecordMainConnectionHandler extends ConnectionHandler {
@param('status', Types.Int, true) @param('status', Types.Int, true)
@param('pretest', Types.Boolean) @param('pretest', Types.Boolean)
async prepare(domainId: string, tid?: ObjectID, pid?: string, uidOrName?: string, status?: number, pretest = false) { 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) { if (tid) {
const tdoc = await contest.get(domainId, tid, -1); const tdoc = await contest.get(domainId, tid, -1);
if (!tdoc) throw new ContestNotFoundError(domainId, tid); if (!tdoc) throw new ContestNotFoundError(domainId, tid);
if (!contest.canShowScoreboard.call(this, tdoc, true)) throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD); if (contest.canShowScoreboard.call(this, tdoc, true)) this.tid = tid.toHexString();
this.tid = tid.toHexString(); else throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
} }
if (uidOrName) { if (uidOrName) {
let udoc = await user.getById(domainId, +uidOrName); let udoc = await user.getById(domainId, +uidOrName);
@@ -193,9 +191,13 @@ class RecordMainConnectionHandler extends ConnectionHandler {
} }
} }
if (pid) { if (pid) {
const pdoc = await problem.get(domainId, pid); const pdomain = pid.includes(':') ? pid.split(':')[0] : domainId;
if (pdoc) this.pid = pdoc.docId; const ppid = pid.includes(':') ? +pid.split(':')[1] : pid;
else throw new ProblemNotFoundError(domainId, 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 (status) this.status = status;
if (pretest) this.pretest = true; if (pretest) this.pretest = true;
@@ -219,7 +221,7 @@ class RecordMainConnectionHandler extends ConnectionHandler {
if (!this.pretest && rdoc.input) return; if (!this.pretest && rdoc.input) return;
if (rdoc.contest && rdoc.contest.tid.toString() !== this.tid) return; if (rdoc.contest && rdoc.contest.tid.toString() !== this.tid) return;
if (this.uid && rdoc.uid !== this.uid) 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 // eslint-disable-next-line prefer-const
let [udoc, pdoc] = await Promise.all([ let [udoc, pdoc] = await Promise.all([
user.getById(this.domainId, rdoc.uid), user.getById(this.domainId, rdoc.uid),
+1 -1
View File
@@ -206,7 +206,7 @@ class UserDetailHandler extends Handler {
this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN), false, this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN), false,
) )
: Object.fromEntries(rdocs.map( : Object.fromEntries(rdocs.map(
(rdoc) => [rdoc.pid, problem.create(undefined, rdoc.pid.toString())], (rdoc) => [rdoc.pid, { ...problem.default, pid: rdoc.pid }],
)); ));
// Remove sensitive data // Remove sensitive data
if (!isSelfProfile && sdoc) { if (!isSelfProfile && sdoc) {
+29 -22
View File
@@ -256,27 +256,31 @@ export interface ContestInfo {
} }
export interface RecordDoc { export interface RecordDoc {
_id: ObjectID, _id: ObjectID;
domainId: string, domainId: string;
pid: number pdomain: string;
uid: number, pid: number;
lang: string, uid: number;
code: string, lang: string;
score: number, code: string;
memory: number, score: number;
time: number, memory: number;
judgeTexts: string[], time: number;
compilerTexts: string[], judgeTexts: string[];
testCases: TestCase[], compilerTexts: string[];
rejudged: boolean, testCases: TestCase[];
judger: number, rejudged: boolean;
judgeAt: Date, /** judge uid */
status: number, judger: number;
hidden: boolean, judgeAt: Date;
progress?: number, status: number;
input?: string, progress?: number;
contest?: ContestInfo, /** pretest/script */
effective?: boolean, hidden?: boolean;
/** pretest */
input?: string;
contest?: ContestInfo;
effective?: boolean;
} }
export interface ScoreboardNode { export interface ScoreboardNode {
@@ -296,6 +300,9 @@ export interface TrainingNode {
pids: number[], pids: number[],
} }
export type ExternalProblemId = string; // ${string}:${number}
export type ProblemId = ExternalProblemId | number;
export interface Tdoc<docType = document['TYPE_CONTEST'] | document['TYPE_HOMEWORK'] | document['TYPE_TRAINING']> extends Document { export interface Tdoc<docType = document['TYPE_CONTEST'] | document['TYPE_HOMEWORK'] | document['TYPE_TRAINING']> extends Document {
docId: ObjectID, docId: ObjectID,
docType: docType & number, docType: docType & number,
@@ -305,7 +312,7 @@ export interface Tdoc<docType = document['TYPE_CONTEST'] | document['TYPE_HOMEWO
title: string, title: string,
content: string, content: string,
rule: string, rule: string,
pids: number[], pids: ProblemId[],
rated?: boolean, rated?: boolean,
// For homework // For homework
+10
View File
@@ -1,6 +1,8 @@
import { ValidationError } from '../error'; import { ValidationError } from '../error';
const RE_UID = /^-?\d+$/i; const RE_UID = /^-?\d+$/i;
const RE_DOMAINID = /^[a-zA-Z][a-zA-Z0-9_]{3,31}$/i;
const RE_EXTERNAL_PID = /^[a-zA-Z][a-zA-Z0-9_]{3,31}:\d+$/i;
const RE_PID = /^[a-zA-Z]+[a-zA-Z0-9]*$/i; const RE_PID = /^[a-zA-Z]+[a-zA-Z0-9]*$/i;
const RE_UNAME = /^.{3,254}$/i; const RE_UNAME = /^.{3,254}$/i;
const RE_ROLE = /^[_0-9A-Za-z]{1,256}$/i; const RE_ROLE = /^[_0-9A-Za-z]{1,256}$/i;
@@ -60,6 +62,10 @@ export function validate(scheme: any, arg: any) {
export const isTitle = (s) => s && s.length < 64; export const isTitle = (s) => s && s.length < 64;
export const checkTitle = (s) => { if (!(s && s.length < 64)) throw new ValidationError('title'); else return s; }; 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 isUid = (s) => RE_UID.test(s);
export const checkUid = (s) => { if (!isUid(s)) throw new ValidationError('uid'); else return s; }; export const checkUid = (s) => { if (!isUid(s)) throw new ValidationError('uid'); else return s; };
export const isUname = (s) => RE_UNAME.test(s); export const isUname = (s) => RE_UNAME.test(s);
@@ -87,6 +93,10 @@ global.Hydro.lib.validator = {
validate, validate,
isTitle, isTitle,
checkTitle, checkTitle,
isDomainId,
checkDomainId,
isExternalPid,
checkExternalPid,
isUid, isUid,
checkUid, checkUid,
isUname, isUname,
+18 -12
View File
@@ -8,7 +8,7 @@ import {
ContestNotAttendedError, ContestScoreboardHiddenError, ContestNotAttendedError, ContestScoreboardHiddenError,
} from '../error'; } from '../error';
import { import {
ContestRule, ContestRules, ProblemDict, ProblemId, ContestRule, ContestRules, ProblemDict,
ScoreboardNode, ScoreboardRow, Tdoc, ScoreboardNode, ScoreboardRow, Tdoc,
Udict, Udict,
} from '../interface'; } from '../interface';
@@ -178,14 +178,20 @@ const oi: ContestRule = {
const psdict = {}; const psdict = {};
// eslint-disable-next-line @typescript-eslint/no-use-before-define // eslint-disable-next-line @typescript-eslint/no-use-before-define
if (isDone(tdoc)) { if (isDone(tdoc)) {
const psdocs = await problem.getMultiStatus(tdoc.domainId, { const psdocs = await Promise.all(
$or: rankedTsdocs.map(([, i]) => ({ tdoc.pids.map((pid) => {
uid: i.uid, let pdomain = tdoc.domainId;
docId: { $in: tdoc.pids }, if (typeof pid === 'string') {
})), pdomain = pid.split(':')[0];
}).toArray(); pid = +pid.split(':')[1];
for (const psdoc of psdocs) { }
psdict[`${psdoc.uid}/${psdoc.docId}`] = psdoc; 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]; const rows = [columns];
@@ -397,7 +403,7 @@ function _getStatusJournal(tsdoc) {
export function add( export function add(
domainId: string, title: string, content: string, owner: number, 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<Tdoc> = {}, type: Type = document.TYPE_CONTEST, rated = false, data: Partial<Tdoc> = {}, type: Type = document.TYPE_CONTEST,
) { ) {
if (!this.RULES[rule]) throw new ValidationError('rule'); 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; 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(); return await document.getMulti(domainId, type, { pids: pid }).toArray();
} }
export async function updateStatus( 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, accept = false, score = 0, type: 30 | 60 = document.TYPE_CONTEST,
) { ) {
const tdoc = await get(domainId, tid, type); const tdoc = await get(domainId, tid, type);
+100 -47
View File
@@ -1,12 +1,17 @@
import { ObjectID, FilterQuery } from 'mongodb'; 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 { streamToBuffer } from '@hydrooj/utils/lib/utils';
import type { Readable } from 'stream'; import type { Readable } from 'stream';
import * as document from './document'; import * as document from './document';
import { STATUS } from './builtin'; import { STATUS } from './builtin';
import domain from './domain';
import storage from './storage'; import storage from './storage';
import { buildProjection } from '../utils'; import { buildProjection } from '../utils';
import type { ProblemStatusDoc, ProblemDict, Document } from '../interface'; import type {
ProblemStatusDoc, ProblemDict, Document, ProblemId,
} from '../interface';
import { import {
ArrayKeys, MaybeArray, NumberKeys, Projection, ArrayKeys, MaybeArray, NumberKeys, Projection,
} from '../typeutils'; } from '../typeutils';
@@ -16,9 +21,6 @@ import { parseConfig } from '../lib/testdataConfig';
export interface ProblemDoc extends Document { } export interface ProblemDoc extends Document { }
export type Field = keyof ProblemDoc; export type Field = keyof ProblemDoc;
const fields: Field[] = [];
export type Getter = (docId?: number, pid?: string) => Partial<ProblemDoc>;
const getters: Getter[] = [];
export class ProblemModel { export class ProblemModel {
static PROJECTION_LIST: Field[] = [ static PROJECTION_LIST: Field[] = [
@@ -32,18 +34,47 @@ export class ProblemModel {
'content', 'html', 'data', 'config', 'additional_file', 'content', 'html', 'data', 'config', 'additional_file',
]; ];
static extend(getter: Getter) { static default = {
getters.push(getter); _id: new ObjectID(),
fields.push(...Object.keys(getter(0, '0')) as any); 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) { static deleted = {
const result = {} as ProblemDoc; _id: new ObjectID(),
for (const getter of getters) { domainId: 'system',
Object.assign(result, getter(docId, pid)); docType: document.TYPE_PROBLEM,
} docId: -1,
return result; 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( static async add(
domainId: string, pid: string = null, title: string, content: string, owner: number, domainId: string, pid: string = null, title: string, content: string, owner: number,
@@ -80,6 +111,12 @@ export class ProblemModel {
if (typeof pid !== 'number') { if (typeof pid !== 'number') {
if (Number.isSafeInteger(parseInt(pid, 10))) pid = parseInt(pid, 10); 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' const res = typeof pid === 'number'
? await document.get(domainId, document.TYPE_PROBLEM, pid, projection) ? await document.get(domainId, document.TYPE_PROBLEM, pid, projection)
: (await document.getMulti(domainId, document.TYPE_PROBLEM, { pid }).toArray())[0]; : (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); return document.deleteSub(domainId, document.TYPE_PROBLEM, pid, key, values);
} }
static inc(domainId: string, _id: number, field: NumberKeys<ProblemDoc> | string, n: number): Promise<ProblemDoc> { static inc(domainId: string, _id: ProblemId, field: NumberKeys<ProblemDoc> | string, n: number): Promise<ProblemDoc> {
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); return document.inc(domainId, document.TYPE_PROBLEM, _id, field as any, n);
} }
@@ -191,16 +233,43 @@ export class ProblemModel {
} }
static async getList( static async getList(
domainId: string, pids: Array<number | string>, domainId: string, pids: ProblemId[],
getHidden = false, doThrow = true, projection = ProblemModel.PROJECTION_PUBLIC, getHidden = false, doThrow = true, projection = ProblemModel.PROJECTION_PUBLIC,
): Promise<ProblemDict> { ): Promise<ProblemDict> {
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 r = {};
const l = {}; const l = {};
const q: any = { $or: [{ docId: { $in: pids } }, { pid: { $in: pids } }] }; const ddocs = await Promise.all(Object.keys(parsed).map((i) => domain.get(i)));
if (!getHidden) q.hidden = false; const f = ddocs.filter((i) => !(
const pdocs = await document.getMulti(domainId, document.TYPE_PROBLEM, q) i.share === '*'
.project(buildProjection(projection)).toArray(); || (`,${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) { for (const pdoc of pdocs) {
try { try {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
@@ -208,15 +277,20 @@ export class ProblemModel {
} catch (e) { } catch (e) {
pdoc.config = `Cannot parse: ${e.message}`; pdoc.config = `Cannot parse: ${e.message}`;
} }
r[pdoc.docId] = pdoc; if (pdoc.domainId === domainId) {
l[pdoc.pid] = pdoc; 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 // TODO enhance
if (pdocs.length !== pids.length) { if (pdocs.length !== pids.length) {
for (const pid of pids) { for (const pid of pids) {
if (!(r[pid] || l[pid])) { if (!(r[pid] || l[pid])) {
if (doThrow) throw new ProblemNotFoundError(domainId, 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: '' }); 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; global.Hydro.model.problem = ProblemModel;
export default ProblemModel; export default ProblemModel;
+34 -14
View File
@@ -8,7 +8,9 @@ import moment from 'moment';
import { STATUS } from './builtin'; import { STATUS } from './builtin';
import task from './task'; import task from './task';
import problem from './problem'; import problem from './problem';
import { RecordDoc, ContestInfo, ProblemConfigFile } from '../interface'; import {
RecordDoc, ContestInfo, ProblemConfigFile, ExternalProblemId,
} from '../interface';
import { ArgMethod, buildProjection, Time } from '../utils'; import { ArgMethod, buildProjection, Time } from '../utils';
import { MaybeArray } from '../typeutils'; import { MaybeArray } from '../typeutils';
import * as bus from '../service/bus'; 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)); return base - ((pending + 0.5) * (sum(timeRecent.map((i) => i.time || 0)) / 10000));
} }
static async get(domainId: string, _id: ObjectID): Promise<RecordDoc | null> { static async get(_id: ObjectID): Promise<RecordDoc | null>
static async get(domainId: string, _id: ObjectID): Promise<RecordDoc | null>
static async get(arg0, arg1?) {
const _id = arg1 || arg0;
const domainId = arg1 ? arg0 : null;
const res = await RecordModel.coll.findOne({ _id }); const res = await RecordModel.coll.findOne({ _id });
if (res && res.domainId === domainId) return res; if (res.domainId === (domainId || res.domainId)) return res;
return null; return null;
} }
@@ -55,7 +61,7 @@ class RecordModel {
const rdoc = await RecordModel.get(domainId, rid); const rdoc = await RecordModel.get(domainId, rid);
let data = []; let data = [];
if (rdoc.pid) { if (rdoc.pid) {
const pdoc = await problem.get(domainId, rdoc.pid); const pdoc = await problem.get(rdoc.pdomain, rdoc.pid);
data = pdoc.data; data = pdoc.data;
} }
delete rdoc._id; delete rdoc._id;
@@ -71,15 +77,21 @@ class RecordModel {
} }
static async add( 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, 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 = { const data: RecordDoc = {
status: STATUS.STATUS_WAITING, status: STATUS.STATUS_WAITING,
_id: new ObjectID(), _id: new ObjectID(),
uid, uid,
code, code,
lang, lang,
pdomain,
pid, pid,
domainId, domainId,
score: 0, score: 0,
@@ -189,16 +201,20 @@ class RecordModel {
} }
@ArgMethod @ArgMethod
static getUserInProblemMulti( static getUserInProblemMulti(domainId: string, uid: number, pid: ExternalProblemId | number, contestInfo?: ContestInfo) {
domainId: string, uid: number, pid: number, let pdomain = domainId;
getHidden: boolean = false, if (typeof pid === 'string') {
) { pdomain = pid.split(':')[0];
if (!getHidden) { pid = +pid.split(':')[1];
}
if (!contestInfo) {
return RecordModel.coll.find({ 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 @ArgMethod
@@ -207,8 +223,12 @@ class RecordModel {
} }
} }
bus.on('problem/delete', (domainId, docId) => RecordModel.coll.deleteMany({ domainId, pid: docId })); // Mark problem as deleted
bus.on('domain/delete', (domainId) => RecordModel.coll.deleteMany({ domainId })); 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; export default RecordModel;
global.Hydro.model.record = RecordModel; global.Hydro.model.record = RecordModel;
+1
View File
@@ -138,6 +138,7 @@ AccountSetting(
DomainSetting( DomainSetting(
Setting('setting_domain', 'name', 'New domain', 'text', 'name'), Setting('setting_domain', 'name', 'New domain', 'text', 'name'),
Setting('setting_domain', 'avatar', '', 'text', 'avatar', 'Will be used as the domain icon.'), 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_domain', 'bulletin', '', 'markdown', 'Bulletin'),
Setting('setting_storage', 'host', '', 'text', 'Custom host', null, FLAG_HIDDEN | FLAG_DISABLED), Setting('setting_storage', 'host', '', 'text', 'Custom host', null, FLAG_HIDDEN | FLAG_DISABLED),
); );
+17 -1
View File
@@ -1,10 +1,13 @@
/* eslint-disable no-await-in-loop */ /* eslint-disable no-await-in-loop */
import { FilterQuery } from 'mongodb'; 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 domain from './model/domain';
import * as document from './model/document'; import * as document from './model/document';
import user from './model/user'; import user from './model/user';
import problem, { Field, ProblemDoc } from './model/problem'; import problem, { Field, ProblemDoc } from './model/problem';
import RecordModel from './model/record';
export async function iterateAllDomain(cb: (ddoc: DomainDoc, current?: number, total?: number) => Promise<any>) { export async function iterateAllDomain(cb: (ddoc: DomainDoc, current?: number, total?: number) => Promise<any>) {
const ddocs = await domain.getMulti().toArray(); const ddocs = await domain.getMulti().toArray();
@@ -56,3 +59,16 @@ export async function iterateAllProblem(
await iterateAllProblemInDomain(d._id, fields, cb); 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);
}
}
+2 -2
View File
@@ -349,11 +349,11 @@ export class HandlerCommon {
const query: any = {}; const query: any = {};
for (const key in kwargs) { for (const key in kwargs) {
if (kwargs[key] instanceof ObjectID) args[key] = kwargs[key].toHexString(); 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 || {}) { for (const key in kwargs.query || {}) {
if (query[key] instanceof ObjectID) query[key] = kwargs.query[key].toHexString(); 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 { try {
const { anchor } = args; const { anchor } = args;
+1
View File
@@ -107,6 +107,7 @@ class StorageService {
this.error = null; this.error = null;
} catch (e) { } catch (e) {
logger.warn('Storage init fail. will retry later.'); logger.warn('Storage init fail. will retry later.');
if (process.env.DEV) logger.warn(e);
this.error = e.toString(); this.error = e.toString();
setTimeout(async () => { setTimeout(async () => {
await this.start(); await this.start();
+5
View File
@@ -393,6 +393,11 @@ const scripts: UpgradeScript[] = [
}); });
return true; 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; export default scripts;
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@hydrooj/migrate-vijos", "name": "@hydrooj/migrate-vijos",
"version": "1.4.2", "version": "1.4.3",
"main": "package.json", "main": "package.json",
"repository": "git@github.com:hydro-dev/Hydro.git", "repository": "git@github.com:hydro-dev/Hydro.git",
"author": "undefined <i@undefined.moe>", "author": "undefined <i@undefined.moe>",
+1
View File
@@ -220,6 +220,7 @@ const tasks = {
code: doc.code, code: doc.code,
lang: doc.lang, lang: doc.lang,
uid: doc.uid, uid: doc.uid,
pdomain: doc.domain_id,
pid: pid(doc.pid), pid: pid(doc.pid),
domainId: doc.domain_id, domainId: doc.domain_id,
judger: doc.judge_uid || 1, judger: doc.judge_uid || 1,
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@hydrooj/ui-default", "name": "@hydrooj/ui-default",
"version": "4.10.8", "version": "4.11.0",
"author": "undefined <i@undefined.moe>", "author": "undefined <i@undefined.moe>",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"main": "hydro.js", "main": "hydro.js",
@@ -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, '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, '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, '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 %} {% else %}
{{ set(UiContext, 'canViewRecord', true) }} {{ set(UiContext, 'canViewRecord', true) }}
{{ set(UiContext, 'postSubmitUrl', url('problem_submit', pid=pdoc.docId)) }} {{ set(UiContext, 'postSubmitUrl', url('problem_submit', pid=pdoc.docId)) }}
{{ set(UiContext, 'getSubmissionsUrl', 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, '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 %} {% endif %}
</script> </script>
<div class="row"> <div class="row">
+12 -3
View File
@@ -1,7 +1,7 @@
import path from 'path'; import path from 'path';
import fs from 'fs-extra'; import fs from 'fs-extra';
import os from 'os'; import os from 'os';
import { Duplex } from 'stream'; import { Duplex, Readable } from 'stream';
import { ObjectID } from 'mongodb'; import { ObjectID } from 'mongodb';
import { isMoment } from 'moment'; import { isMoment } from 'moment';
import type { Moment } from 'moment-timezone'; import type { Moment } from 'moment-timezone';
@@ -192,11 +192,20 @@ export function isClass(obj: any, strict = false) {
return false; return false;
} }
export function streamToBuffer(stream): Promise<Buffer> { export function streamToBuffer(stream: any, maxSize = 0): Promise<Buffer> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const buffers = []; 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('error', reject);
stream.on('data', (data) => buffers.push(data)); stream.on('data', onData);
stream.on('end', () => resolve(Buffer.concat(buffers))); stream.on('end', () => resolve(Buffer.concat(buffers)));
}); });
} }