mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 01:38:19 +08:00
core: update contest path
This commit is contained in:
1 parent
3b6b6e3c17
commit
42b73f36a4
22 files changed
+131
-448
No files matched your search
@@ -1,28 +1,20 @@
|
||||
import AdmZip from 'adm-zip';
|
||||
import { intersection } from 'lodash';
|
||||
import { lookup } from 'mime-types';
|
||||
import moment from 'moment-timezone';
|
||||
import { ObjectID } from 'mongodb';
|
||||
import { Time } from '@hydrooj/utils/lib/utils';
|
||||
import {
|
||||
BadRequestError, ContestNotAttendedError, ContestNotFoundError,
|
||||
ContestNotLiveError, InvalidTokenError, PermissionError,
|
||||
ProblemNotFoundError, RecordNotFoundError, ValidationError,
|
||||
ContestNotFoundError, ContestNotLiveError, InvalidTokenError,
|
||||
PermissionError, ValidationError,
|
||||
} from '../error';
|
||||
import {
|
||||
ProblemDoc, Tdoc, User,
|
||||
} from '../interface';
|
||||
import { Tdoc } from '../interface';
|
||||
import paginate from '../lib/paginate';
|
||||
import { PERM, PRIV } from '../model/builtin';
|
||||
import * as contest from '../model/contest';
|
||||
import domain from '../model/domain';
|
||||
import message from '../model/message';
|
||||
import problem from '../model/problem';
|
||||
import record from '../model/record';
|
||||
import storage from '../model/storage';
|
||||
import * as system from '../model/system';
|
||||
import user from '../model/user';
|
||||
import * as bus from '../service/bus';
|
||||
import {
|
||||
Handler, param, Route, Types,
|
||||
} from '../service/server';
|
||||
@@ -56,9 +48,7 @@ export class ContestDetailHandler extends Handler {
|
||||
]);
|
||||
const psdict = {};
|
||||
let rdict = {};
|
||||
let attended: boolean;
|
||||
if (tsdoc) {
|
||||
attended = tsdoc.attend === 1;
|
||||
for (const pdetail of tsdoc.journal || []) psdict[pdetail.pid] = pdetail;
|
||||
if (contest.canShowSelfRecord.call(this, tdoc)) {
|
||||
const q = [];
|
||||
@@ -67,16 +57,10 @@ export class ContestDetailHandler extends Handler {
|
||||
} else {
|
||||
for (const i in psdict) rdict[psdict[i].rid] = { _id: psdict[i].rid };
|
||||
}
|
||||
} else attended = false;
|
||||
const udict = await user.getList(domainId, [tdoc.owner]);
|
||||
const index = tdoc.pids.map((i) => i.toString());
|
||||
for (const key in pdict) {
|
||||
pdict[key].tag.length = 0;
|
||||
const i = (index.indexOf(key) + 10).toString(36).toUpperCase();
|
||||
if (i !== '9') pdict[key].pid = i;
|
||||
}
|
||||
const udict = await user.getList(domainId, [tdoc.owner]);
|
||||
this.response.body = {
|
||||
tdoc, tsdoc, attended, udict, pdict, psdict, rdict, page,
|
||||
tdoc, tsdoc, udict, pdict, psdict, rdict, page,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -118,7 +102,7 @@ export class ContestBroadcastHandler extends Handler {
|
||||
@param('content', Types.Content)
|
||||
async post(domainId: string, tid: ObjectID, content: string) {
|
||||
const tdoc = await contest.get(domainId, tid);
|
||||
if (!this.user.own(tdoc)) throw new PermissionError('Boardcast Message');
|
||||
if (!this.user.own(tdoc)) throw new PermissionError('Broadcast Message');
|
||||
const tsdocs = await contest.getMultiStatus(domainId, { docId: tid }).toArray();
|
||||
const uids: number[] = Array.from(new Set(tsdocs.map((tsdoc) => tsdoc.uid)));
|
||||
await Promise.all(
|
||||
@@ -241,157 +225,6 @@ export class ContestEditHandler extends Handler {
|
||||
}
|
||||
}
|
||||
|
||||
export class ContestProblemHandler extends Handler {
|
||||
tdoc: Tdoc<30>;
|
||||
pdoc: ProblemDoc;
|
||||
tsdoc: any;
|
||||
udoc: User;
|
||||
attended: boolean;
|
||||
|
||||
@param('tid', Types.ObjectID)
|
||||
@param('pid', Types.Name)
|
||||
async _prepare(domainId: string, tid: ObjectID, _pid: string) {
|
||||
this.tdoc = await contest.get(domainId, tid);
|
||||
const pid = this.tdoc.pids[parseInt(_pid, 36) - 10];
|
||||
if (!pid) throw new ProblemNotFoundError(domainId, tid, _pid);
|
||||
[this.pdoc, this.tsdoc, this.udoc] = await Promise.all([
|
||||
problem.get(domainId, pid),
|
||||
contest.getStatus(domainId, tid, this.user._id),
|
||||
user.getById(domainId, this.tdoc.owner),
|
||||
]);
|
||||
if (!this.pdoc) throw new ProblemNotFoundError(domainId, pid);
|
||||
this.pdoc.pid = _pid;
|
||||
this.pdoc.tag.length = 0;
|
||||
this.attended = this.tsdoc && this.tsdoc.attend === 1;
|
||||
if (this.pdoc.reference) {
|
||||
const pdoc = await problem.get(this.pdoc.reference.domainId, this.pdoc.reference.pid);
|
||||
this.pdoc.config = pdoc.config;
|
||||
}
|
||||
const showAccept = contest.canShowScoreboard.call(this, this.tdoc, true);
|
||||
if (!showAccept) this.pdoc.nAccept = 0;
|
||||
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.body = {
|
||||
showAccept,
|
||||
tdoc: this.tdoc,
|
||||
tsdoc: this.tsdoc,
|
||||
pdoc: this.pdoc,
|
||||
udoc: this.udoc,
|
||||
attended: this.attended,
|
||||
page_name: 'contest_detail_problem',
|
||||
};
|
||||
}
|
||||
|
||||
@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: args[1] }, true],
|
||||
[this.pdoc.title, null, null, true],
|
||||
];
|
||||
// Navigate to current additional file download
|
||||
// e.g.  will navigate to 
|
||||
this.response.body.pdoc.content = this.response.body.pdoc.content
|
||||
.replace(/\(file:\/\//g, `(./${args[2]}/file/`)
|
||||
.replace(/="file:\/\//g, `="./${args[2]}/file/`);
|
||||
}
|
||||
}
|
||||
|
||||
export class ContestProblemFileDownloadHandler extends ContestProblemHandler {
|
||||
@param('filename', Types.Name)
|
||||
@param('noDisposition', Types.Boolean)
|
||||
async get(domainId: string, filename: string, noDisposition = false) {
|
||||
if (this.pdoc.reference) {
|
||||
this.pdoc = await problem.get(this.pdoc.reference.domainId, this.pdoc.reference.pid);
|
||||
if (!this.pdoc) throw new ProblemNotFoundError();
|
||||
}
|
||||
const target = `problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${filename}`;
|
||||
const file = await storage.getMeta(target);
|
||||
if (!file) {
|
||||
this.response.redirect = await storage.signDownloadLink(
|
||||
target, noDisposition ? undefined : filename, false, 'user',
|
||||
);
|
||||
return;
|
||||
}
|
||||
const type = lookup(filename).toString();
|
||||
const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => type.includes(i)).length;
|
||||
if (shouldProxy && file.size! < 32 * 1024 * 1024) {
|
||||
this.response.etag = file.etag;
|
||||
this.response.body = await storage.get(target);
|
||||
this.response.type = file['Content-Type'] || type;
|
||||
this.response.disposition = `attachment; filename=${encodeURIComponent(filename)}`;
|
||||
} else {
|
||||
this.response.redirect = await storage.signDownloadLink(
|
||||
target, noDisposition ? undefined : filename, false, 'user',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class ContestDetailProblemSubmitHandler extends ContestProblemHandler {
|
||||
@param('pid', Types.Name)
|
||||
async prepare() {
|
||||
if (!this.attended) throw new ContestNotAttendedError(this.tdoc.docId);
|
||||
if (!contest.isOngoing(this.tdoc)) throw new ContestNotLiveError(this.tdoc.docId);
|
||||
if (this.domain.langs) {
|
||||
this.response.body.pdoc.config.langs = intersection(
|
||||
this.response.body.pdoc.config.langs || this.domain.langs.split(','),
|
||||
this.domain.langs.split(','),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@param('tid', Types.ObjectID)
|
||||
@param('pid', Types.Name)
|
||||
async get(domainId: string, tid: ObjectID, pid: string) {
|
||||
this.response.body.path = [
|
||||
['Hydro', 'homepage'],
|
||||
['contest_main', 'contest_main'],
|
||||
[this.tdoc.title, 'contest_detail', { tid }, true],
|
||||
[this.pdoc.title, 'contest_detail_problem', { tid, pid }, true],
|
||||
['contest_detail_problem_submit', null],
|
||||
];
|
||||
this.response.body.page_name = 'contest_detail_problem_submit';
|
||||
this.response.template = 'problem_submit.html';
|
||||
}
|
||||
|
||||
@param('tid', Types.ObjectID)
|
||||
@param('lang', Types.Name)
|
||||
@param('code', Types.Content)
|
||||
@param('pid', Types.Name)
|
||||
@param('pretest', Types.Boolean)
|
||||
@param('input', Types.String, true)
|
||||
async post(domainId: string, tid: ObjectID, lang: string, code: string, _pid: string, pretest = false, input = '') {
|
||||
const pid = this.tdoc.pids[parseInt(_pid, 36) - 10];
|
||||
if (!(this.response.body.pdoc.config.langs || [lang]).includes(lang)) {
|
||||
throw new BadRequestError('Language not allowed.');
|
||||
}
|
||||
await this.limitRate('add_record', 60, system.get('limit.submission'));
|
||||
const rid = await record.add(domainId, pid, this.user._id, lang, code, true, pretest ? input : tid, !pretest);
|
||||
const rdoc = await record.get(domainId, rid);
|
||||
if (!rdoc) throw new RecordNotFoundError(domainId, rid);
|
||||
if (!pretest) {
|
||||
await Promise.all([
|
||||
(this.tsdoc.journal || []).filter((i) => i.pid === pid).length
|
||||
&& problem.inc(this.domainId, this.pdoc.docId, 'nSubmit', 1),
|
||||
domain.incUserInDomain(domainId, this.user._id, 'nSubmit'),
|
||||
contest.updateStatus(domainId, tid, this.user._id, rid, pid),
|
||||
]);
|
||||
}
|
||||
bus.broadcast('record/change', rdoc);
|
||||
if (!pretest && !contest.canShowSelfRecord.call(this, this.tdoc)) {
|
||||
this.response.body = { tid };
|
||||
this.response.redirect = this.url('contest_detail', { tid });
|
||||
} else {
|
||||
this.response.body = { rid };
|
||||
this.response.redirect = this.url('record_detail', { rid });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class ContestCodeHandler extends Handler {
|
||||
@param('tid', Types.ObjectID)
|
||||
async get(domainId: string, tid: ObjectID) {
|
||||
@@ -428,9 +261,6 @@ export async function apply() {
|
||||
Route('contest_edit', '/contest/:tid/edit', ContestEditHandler, PERM.PERM_VIEW_CONTEST);
|
||||
Route('contest_scoreboard', '/contest/:tid/scoreboard', ContestScoreboardHandler, PERM.PERM_VIEW_CONTEST);
|
||||
Route('contest_scoreboard_download', '/contest/:tid/export/:ext', ContestScoreboardDownloadHandler, PERM.PERM_VIEW_CONTEST);
|
||||
Route('contest_detail_problem', '/contest/:tid/p/:pid', ContestProblemHandler, PERM.PERM_VIEW_CONTEST);
|
||||
Route('contest_detail_problem_file_download', '/contest/:tid/p/:pid/file/:filename', ContestProblemFileDownloadHandler, PERM.PERM_VIEW_PROBLEM);
|
||||
Route('contest_detail_problem_submit', '/contest/:tid/p/:pid/submit', ContestDetailProblemSubmitHandler, PERM.PERM_VIEW_CONTEST);
|
||||
Route('contest_code', '/contest/:tid/code', ContestCodeHandler, PERM.PERM_VIEW_CONTEST);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,28 +1,20 @@
|
||||
import AdmZip from 'adm-zip';
|
||||
import yaml from 'js-yaml';
|
||||
import { intersection } from 'lodash';
|
||||
import { lookup } from 'mime-types';
|
||||
import moment from 'moment-timezone';
|
||||
import { ObjectID } from 'mongodb';
|
||||
import { Time } from '@hydrooj/utils/lib/utils';
|
||||
import {
|
||||
BadRequestError, ContestNotFoundError, HomeworkNotAttendedError,
|
||||
HomeworkNotLiveError, ProblemNotFoundError, ValidationError,
|
||||
ContestNotFoundError, HomeworkNotLiveError, ValidationError,
|
||||
} from '../error';
|
||||
import {
|
||||
PenaltyRules, ProblemDoc, Tdoc, User,
|
||||
} from '../interface';
|
||||
import { PenaltyRules } from '../interface';
|
||||
import paginate from '../lib/paginate';
|
||||
import { PERM, PRIV, STATUS } from '../model/builtin';
|
||||
import { PERM, PRIV } from '../model/builtin';
|
||||
import * as contest from '../model/contest';
|
||||
import * as discussion from '../model/discussion';
|
||||
import domain from '../model/domain';
|
||||
import problem from '../model/problem';
|
||||
import record from '../model/record';
|
||||
import storage from '../model/storage';
|
||||
import * as system from '../model/system';
|
||||
import user from '../model/user';
|
||||
import * as bus from '../service/bus';
|
||||
import {
|
||||
Handler, param, Route, Types,
|
||||
} from '../service/server';
|
||||
@@ -59,9 +51,7 @@ class HomeworkDetailHandler extends Handler {
|
||||
]);
|
||||
const psdict = {};
|
||||
let rdict = {};
|
||||
let attended = false;
|
||||
if (tsdoc) {
|
||||
attended = tsdoc.attend === 1;
|
||||
for (const pdetail of tsdoc.journal || []) {
|
||||
psdict[pdetail.pid] = pdetail;
|
||||
rdict[pdetail.rid] = { _id: pdetail.rid };
|
||||
@@ -82,14 +72,9 @@ class HomeworkDetailHandler extends Handler {
|
||||
const uids = ddocs.map((ddoc) => ddoc.owner);
|
||||
uids.push(tdoc.owner);
|
||||
const udict = await user.getList(domainId, uids);
|
||||
const index = tdoc.pids.map((i) => i.toString());
|
||||
for (const key in pdict) {
|
||||
const i = (index.indexOf(key) + 10).toString(36).toUpperCase();
|
||||
if (i !== '9') pdict[key].pid = i;
|
||||
}
|
||||
this.response.template = 'homework_detail.html';
|
||||
this.response.body = {
|
||||
tdoc, tsdoc, attended, udict, pdict, psdict, rdict, ddocs, page, dpcount, dcount,
|
||||
tdoc, tsdoc, udict, pdict, psdict, rdict, ddocs, page, dpcount, dcount,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -111,151 +96,6 @@ class HomeworkDetailHandler extends Handler {
|
||||
}
|
||||
}
|
||||
|
||||
class HomeworkDetailProblemHandler extends Handler {
|
||||
tdoc: Tdoc<30>;
|
||||
pdoc: ProblemDoc;
|
||||
pid: number;
|
||||
tsdoc: any;
|
||||
udoc: User;
|
||||
attended: boolean;
|
||||
|
||||
@param('tid', Types.ObjectID)
|
||||
@param('pid', Types.String)
|
||||
async _prepare(domainId: string, tid: ObjectID, _pid: string) {
|
||||
this.tdoc = await contest.get(domainId, tid);
|
||||
if (this.tdoc.rule !== 'homework') throw new ContestNotFoundError(domainId, tid);
|
||||
this.pid = this.tdoc.pids[parseInt(_pid, 36) - 10];
|
||||
if (!this.pid) throw new ProblemNotFoundError(domainId, tid, _pid);
|
||||
[this.udoc, this.pdoc, this.tsdoc] = await Promise.all([
|
||||
user.getById(domainId, this.tdoc.owner),
|
||||
problem.get(domainId, this.pid),
|
||||
contest.getStatus(domainId, tid, this.user._id),
|
||||
]);
|
||||
if (!this.pdoc) throw new ProblemNotFoundError(domainId, this.pid);
|
||||
if (this.pdoc.reference) {
|
||||
const pdoc = await problem.get(this.pdoc.reference.domainId, this.pdoc.reference.pid);
|
||||
this.pdoc.config = pdoc.config;
|
||||
}
|
||||
this.pdoc.pid = _pid;
|
||||
this.attended = this.tsdoc?.attend === 1;
|
||||
this.response.body = {
|
||||
tdoc: this.tdoc,
|
||||
pdoc: this.pdoc,
|
||||
tsdoc: this.tsdoc,
|
||||
udoc: this.udoc,
|
||||
attended: this.attended,
|
||||
};
|
||||
}
|
||||
|
||||
@param('tid', Types.ObjectID)
|
||||
@param('pid', Types.Name)
|
||||
async get(domainId: string, tid: ObjectID, pid: string) {
|
||||
if (!contest.isDone(this.tdoc)) {
|
||||
if (!this.attended) throw new HomeworkNotAttendedError(tid);
|
||||
if (contest.isNotStarted(this.tdoc)) throw new HomeworkNotLiveError(tid);
|
||||
}
|
||||
this.response.template = 'problem_detail.html';
|
||||
this.response.body.page_name = 'homework_detail_problem';
|
||||
this.response.body.path = [
|
||||
['Hydro', 'homepage'],
|
||||
['homework_main', 'homework_main'],
|
||||
[this.tdoc.title, 'homework_detail', { tid }, true],
|
||||
[this.pdoc.title, null, null, true],
|
||||
];
|
||||
// Navigate to current additional file download
|
||||
// e.g.  will navigate to 
|
||||
this.response.body.pdoc.content = this.response.body.pdoc.content
|
||||
.replace(/\(file:\/\//g, `(./${pid}/file/`)
|
||||
.replace(/="file:\/\//g, `="./${pid}/file/`);
|
||||
}
|
||||
}
|
||||
|
||||
export class HomeworkProblemFileDownloadHandler extends HomeworkDetailProblemHandler {
|
||||
@param('filename', Types.Name)
|
||||
@param('noDisposition', Types.Boolean)
|
||||
// @ts-ignore
|
||||
async get(domainId: string, filename: string, noDisposition = false) {
|
||||
if (this.pdoc.reference) {
|
||||
this.pdoc = await problem.get(this.pdoc.reference.domainId, this.pdoc.reference.pid);
|
||||
if (!this.pdoc) throw new ProblemNotFoundError();
|
||||
}
|
||||
const target = `problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${filename}`;
|
||||
const file = await storage.getMeta(target);
|
||||
if (!file) {
|
||||
this.response.redirect = await storage.signDownloadLink(
|
||||
target, noDisposition ? undefined : filename, false, 'user',
|
||||
);
|
||||
return;
|
||||
}
|
||||
const type = lookup(filename).toString();
|
||||
const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => type.includes(i)).length;
|
||||
if (shouldProxy && file.size! < 32 * 1024 * 1024) {
|
||||
this.response.etag = file.etag;
|
||||
this.response.body = await storage.get(target);
|
||||
this.response.type = file['Content-Type'] || type;
|
||||
this.response.disposition = `attachment; filename=${encodeURIComponent(filename)}`;
|
||||
} else {
|
||||
this.response.redirect = await storage.signDownloadLink(
|
||||
target, noDisposition ? undefined : filename, false, 'user',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler {
|
||||
@param('tid', Types.ObjectID)
|
||||
async prepare(domainId: string, tid: ObjectID) {
|
||||
if (!this.attended) throw new HomeworkNotAttendedError(tid);
|
||||
if (!contest.isOngoing(this.tdoc)) throw new HomeworkNotLiveError(tid);
|
||||
if (this.domain.langs) {
|
||||
this.response.body.pdoc.config.langs = intersection(
|
||||
this.response.body.pdoc.config.langs || this.domain.langs.split(','),
|
||||
this.domain.langs.split(','),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@param('tid', Types.ObjectID)
|
||||
@param('pid', Types.String)
|
||||
async get(domainId: string, tid: ObjectID, pid: string) {
|
||||
this.response.body.path = [
|
||||
['Hydro', 'homepage'],
|
||||
['homework_main', 'homework_main'],
|
||||
[this.tdoc.title, 'homework_detail', { tid }, true],
|
||||
[this.pdoc.title, 'homework_detail_problem', { tid, pid }, true],
|
||||
['homework_detail_problem_submit', null],
|
||||
];
|
||||
this.response.template = 'problem_submit.html';
|
||||
}
|
||||
|
||||
@param('tid', Types.ObjectID)
|
||||
@param('code', Types.Content)
|
||||
@param('lang', Types.Name)
|
||||
@param('pretest', Types.Boolean)
|
||||
@param('input', Types.String, true)
|
||||
async post(domainId: string, tid: ObjectID, code: string, lang: string, pretest = false, input = '') {
|
||||
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, system.get('limit.submission'));
|
||||
const rid = await record.add(domainId, this.pid, this.user._id, lang, code, true, pretest ? input : tid);
|
||||
const rdoc = await record.get(domainId, rid);
|
||||
if (!pretest) {
|
||||
await Promise.all([
|
||||
(this.tsdoc.journal || []).filter((i) => i.pid === this.pid).length
|
||||
&& problem.inc(this.domainId, this.pdoc.docId, 'nSubmit', 1),
|
||||
domain.incUserInDomain(domainId, this.user._id, 'nSubmit'),
|
||||
contest.updateStatus(domainId, tid, this.user._id, rid, this.pid, STATUS.STATUS_WAITING),
|
||||
]);
|
||||
}
|
||||
bus.broadcast('record/change', rdoc);
|
||||
this.response.body.tid = tid;
|
||||
this.response.body.rid = rid;
|
||||
if (pretest || contest.canShowSelfRecord.call(this, this.tdoc)) this.response.redirect = this.url('record_detail', { rid });
|
||||
else this.response.redirect = this.url('homework_detail', { tid });
|
||||
}
|
||||
}
|
||||
|
||||
class HomeworkEditHandler extends Handler {
|
||||
@param('tid', Types.ObjectID, true)
|
||||
async get(domainId: string, tid: ObjectID) {
|
||||
@@ -406,12 +246,6 @@ export async function apply() {
|
||||
'homework_scoreboard_download', '/homework/:tid/scoreboard/download/:ext',
|
||||
HomeworkScoreboardDownloadHandler, PERM.PERM_VIEW_HOMEWORK_SCOREBOARD,
|
||||
);
|
||||
Route('homework_detail_problem', '/homework/:tid/p/:pid', HomeworkDetailProblemHandler, PERM.PERM_VIEW_HOMEWORK);
|
||||
Route(
|
||||
'homework_detail_problem_file_download', '/homework/:tid/p/:pid/file/:filename',
|
||||
HomeworkProblemFileDownloadHandler, PERM.PERM_VIEW_PROBLEM,
|
||||
);
|
||||
Route('homework_detail_problem_submit', '/homework/:tid/p/:pid/submit', HomeworkDetailProblemSubmitHandler, PERM.PERM_SUBMIT_PROBLEM);
|
||||
Route('homework_code', '/homework/:tid/code', HomeworkCodeHandler, PERM.PERM_VIEW_HOMEWORK);
|
||||
Route('homework_edit', '/homework/:tid/edit', HomeworkEditHandler);
|
||||
}
|
||||
|
||||
@@ -5,12 +5,12 @@ import { lookup } from 'mime-types';
|
||||
import { FilterQuery, ObjectID } from 'mongodb';
|
||||
import { sortFiles } from '@hydrooj/utils/lib/utils';
|
||||
import {
|
||||
BadRequestError, ForbiddenError, NoProblemError,
|
||||
BadRequestError, ContestNotAttendedError, ContestNotFoundError, ContestNotLiveError, ForbiddenError, NoProblemError,
|
||||
PermissionError, ProblemNotFoundError, SolutionNotFoundError,
|
||||
ValidationError,
|
||||
} from '../error';
|
||||
import {
|
||||
ProblemDoc, ProblemStatusDoc, User,
|
||||
ProblemDoc, ProblemStatusDoc, Tdoc, User,
|
||||
} from '../interface';
|
||||
import difficultyAlgorithm from '../lib/difficulty';
|
||||
import paginate from '../lib/paginate';
|
||||
@@ -208,26 +208,37 @@ export class ProblemRandomHandler extends ProblemHandler {
|
||||
|
||||
export class ProblemDetailHandler extends ProblemHandler {
|
||||
pdoc: ProblemDoc;
|
||||
tdoc?: Tdoc<30>;
|
||||
tsdoc?: any;
|
||||
udoc: User;
|
||||
psdoc: ProblemStatusDoc;
|
||||
|
||||
@route('pid', Types.Name, true, null, parsePid)
|
||||
async _prepare(domainId: string, pid: number | string) {
|
||||
this.response.template = 'problem_detail.html';
|
||||
@query('tid', Types.ObjectID, true)
|
||||
async _prepare(domainId: string, pid: number | string, tid?: ObjectID) {
|
||||
this.pdoc = await problem.get(domainId, pid);
|
||||
if (!this.pdoc) throw new ProblemNotFoundError(domainId, pid);
|
||||
if (this.pdoc.hidden && !this.user.own(this.pdoc)) {
|
||||
if (tid) {
|
||||
this.tdoc = await contest.get(domainId, tid);
|
||||
if (!this.tdoc) throw new ContestNotFoundError(domainId, tid);
|
||||
this.tsdoc = await contest.getStatus(domainId, tid, this.user._id);
|
||||
this.pdoc.tag.length = 0;
|
||||
const showAccept = contest.canShowScoreboard.call(this, this.tdoc, true);
|
||||
if (!showAccept) this.pdoc.nAccept = 0;
|
||||
if (contest.isNotStarted(this.tdoc)) throw new ContestNotLiveError(tid);
|
||||
if (!contest.isDone(this.tdoc) && !this.tsdoc.attend) throw new ContestNotAttendedError(tid);
|
||||
} else if (this.pdoc.hidden && !this.user.own(this.pdoc)) {
|
||||
this.checkPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
|
||||
}
|
||||
if (this.pdoc.reference) {
|
||||
const pdoc = await problem.get(this.pdoc.reference.domainId, this.pdoc.reference.pid);
|
||||
this.pdoc.config = pdoc.config;
|
||||
}
|
||||
await bus.serial('problem/get', this.pdoc, this);
|
||||
[this.psdoc, this.udoc] = await Promise.all([
|
||||
problem.getStatus(domainId, this.pdoc.docId, this.user._id),
|
||||
user.getById(domainId, this.pdoc.owner),
|
||||
]);
|
||||
if (this.pdoc.reference) {
|
||||
const pdoc = await problem.get(this.pdoc.reference.domainId, this.pdoc.reference.pid);
|
||||
this.pdoc.config = pdoc.config;
|
||||
}
|
||||
const [scnt, dcnt] = await Promise.all([
|
||||
solution.count(domainId, { parentId: this.pdoc.docId }),
|
||||
discussion.count(domainId, { parentId: this.pdoc.docId }),
|
||||
@@ -239,7 +250,15 @@ export class ProblemDetailHandler extends ProblemHandler {
|
||||
title: this.pdoc.title,
|
||||
solutionCount: scnt,
|
||||
discussionCount: dcnt,
|
||||
tdoc: this.tdoc,
|
||||
tsdoc: this.tsdoc,
|
||||
page_name: this.tdoc
|
||||
? this.tdoc.rule === 'homework'
|
||||
? 'homework_detail_problem'
|
||||
: 'contest_detail_problem'
|
||||
: 'problem_detail',
|
||||
};
|
||||
this.response.template = 'problem_detail.html';
|
||||
this.extraTitleContent = this.pdoc.title;
|
||||
}
|
||||
|
||||
@@ -252,10 +271,12 @@ export class ProblemDetailHandler extends ProblemHandler {
|
||||
.replace(/\(file:\/\//g, `(./${this.pdoc.docId}/file/`)
|
||||
.replace(/="file:\/\//g, `="./${this.pdoc.docId}/file/`);
|
||||
}
|
||||
if (this.psdoc) {
|
||||
this.response.body.rdoc = await record.get(this.domainId, this.psdoc.rid);
|
||||
if (!this.response.body.tdoc) {
|
||||
if (this.psdoc?.rid) {
|
||||
this.response.body.rdoc = await record.get(this.domainId, this.psdoc.rid);
|
||||
}
|
||||
this.response.body.ctdocs = await contest.getRelated(this.domainId, this.pdoc.docId);
|
||||
}
|
||||
this.response.body.ctdocs = await contest.getRelated(this.domainId, this.pdoc.docId);
|
||||
}
|
||||
|
||||
@param('pid', Types.UnsignedInt)
|
||||
@@ -288,60 +309,59 @@ export class ProblemDetailHandler extends ProblemHandler {
|
||||
}
|
||||
|
||||
export class ProblemSubmitHandler extends ProblemDetailHandler {
|
||||
async get() {
|
||||
async get(domainId: string, tid?: ObjectID) {
|
||||
this.response.template = 'problem_submit.html';
|
||||
this.response.body = {
|
||||
pdoc: this.pdoc,
|
||||
udoc: this.udoc,
|
||||
title: this.pdoc.title,
|
||||
page_name: this.tdoc
|
||||
? this.tdoc.rule === 'homework'
|
||||
? 'homework_detail_problem_submit'
|
||||
: 'contest_detail_problem_submit'
|
||||
: 'problem_submit',
|
||||
};
|
||||
if (tid && !contest.isOngoing(this.tdoc)) throw new ContestNotLiveError(this.tdoc.docId);
|
||||
if (this.domain.langs) {
|
||||
this.response.body.pdoc.config.langs = intersection(
|
||||
this.response.body.pdoc.config.langs || this.domain.langs.split(','),
|
||||
this.domain.langs.split(','),
|
||||
);
|
||||
}
|
||||
this.response.body.pdoc.config.domainId = this.domainId;
|
||||
}
|
||||
|
||||
@param('lang', Types.Name)
|
||||
@param('code', Types.Content)
|
||||
@param('pretest', Types.Boolean)
|
||||
@param('input', Types.String, true)
|
||||
async post(domainId: string, lang: string, code: string, pretest = false, input = '') {
|
||||
@param('tid', Types.ObjectID, true)
|
||||
async post(domainId: string, lang: string, code: string, pretest = false, input = '', tid?: ObjectID) {
|
||||
if (this.response.body.pdoc.config?.langs && !this.response.body.pdoc.config.langs.includes(lang)) {
|
||||
throw new BadRequestError('Language not allowed.');
|
||||
}
|
||||
if (pretest && this.response.body.pdoc.config?.type !== 'default') throw new BadRequestError('unable to run pretest');
|
||||
await this.limitRate('add_record', 60, system.get('limit.submission'));
|
||||
const rid = await record.add(domainId, this.pdoc.docId, this.user._id, lang, code, true, pretest ? input : undefined);
|
||||
const rid = await record.add(domainId, this.pdoc.docId, this.user._id, lang, code, true, pretest ? input : tid, !pretest);
|
||||
const rdoc = await record.get(domainId, rid);
|
||||
if (!pretest) {
|
||||
await Promise.all([
|
||||
this.psdoc?.rid ? Promise.resolve() : problem.inc(domainId, this.pdoc.docId, 'nSubmit', 1),
|
||||
(this.tdoc
|
||||
? (this.tsdoc.journal || []).filter((i) => i.pid === this.pdoc.docId).length
|
||||
: this.psdoc?.rid ? Promise.resolve() : problem.inc(domainId, this.pdoc.docId, 'nSubmit', 1)
|
||||
) && problem.inc(this.domainId, this.pdoc.docId, 'nSubmit', 1),
|
||||
problem.incStatus(domainId, this.pdoc.docId, this.user._id, 'nSubmit', 1),
|
||||
domain.incUserInDomain(domainId, this.user._id, 'nSubmit'),
|
||||
tid && contest.updateStatus(domainId, tid, this.user._id, rid, this.pdoc.docId),
|
||||
]);
|
||||
}
|
||||
bus.broadcast('record/change', rdoc);
|
||||
this.response.body = { rid };
|
||||
this.response.redirect = this.url('record_detail', { rid });
|
||||
}
|
||||
}
|
||||
|
||||
export class ProblemPretestHandler extends ProblemDetailHandler {
|
||||
@param('lang', Types.Name)
|
||||
@param('code', Types.Content)
|
||||
@param('input', Types.Content, true)
|
||||
async post(domainId: string, lang: string, code: string, input = '') {
|
||||
await this.limitRate('do_pretest', 60, system.get('limit.pretest'));
|
||||
const rid = await record.add(
|
||||
domainId, this.pdoc.docId, this.user._id,
|
||||
lang, code, true, input,
|
||||
);
|
||||
const rdoc = await record.get(domainId, rid);
|
||||
bus.broadcast('record/change', rdoc);
|
||||
this.response.body = { rid };
|
||||
if (tid && !pretest && !contest.canShowSelfRecord.call(this, this.tdoc)) {
|
||||
this.response.body = { tid };
|
||||
this.response.redirect = this.url(this.tdoc.rule === 'homework' ? 'homework_detail' : 'contest_detail', { tid });
|
||||
} else {
|
||||
this.response.body = { rid };
|
||||
this.response.redirect = this.url('record_detail', { rid });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -524,7 +544,9 @@ export class ProblemFileDownloadHandler extends ProblemDetailHandler {
|
||||
|
||||
export class ProblemSolutionHandler extends ProblemDetailHandler {
|
||||
@param('page', Types.PositiveInt, true)
|
||||
async get(domainId: string, page = 1) {
|
||||
@param('tid', Types.ObjectID, true)
|
||||
async get(domainId: string, page = 1, tid?: ObjectID) {
|
||||
if (tid) throw new PermissionError(PERM.PERM_VIEW_PROBLEM_SOLUTION);
|
||||
this.response.template = 'problem_solution.html';
|
||||
this.checkPerm(PERM.PERM_VIEW_PROBLEM_SOLUTION);
|
||||
const [psdocs, pcount, pscount] = await paginate(
|
||||
@@ -627,7 +649,9 @@ export class ProblemSolutionHandler extends ProblemDetailHandler {
|
||||
export class ProblemSolutionRawHandler extends ProblemDetailHandler {
|
||||
@param('psid', Types.ObjectID)
|
||||
@route('psrid', Types.ObjectID, true)
|
||||
async get(domainId: string, psid: ObjectID, psrid?: ObjectID) {
|
||||
@param('tid', Types.ObjectID, true)
|
||||
async get(domainId: string, psid: ObjectID, psrid?: ObjectID, tid?: ObjectID) {
|
||||
if (tid) throw new PermissionError(PERM.PERM_VIEW_PROBLEM_SOLUTION);
|
||||
this.checkPerm(PERM.PERM_VIEW_PROBLEM_SOLUTION);
|
||||
if (psrid) {
|
||||
const [psdoc, psrdoc] = await solution.getReply(domainId, psid, psrid);
|
||||
|
||||
@@ -238,6 +238,7 @@ class RecordMainConnectionHandler extends ConnectionHandler {
|
||||
if (rdoc.domainId !== this.domainId) return;
|
||||
if (rdoc.contest && ![this.tid, '000000000000000000000000'].includes(rdoc.contest.toString())) return;
|
||||
}
|
||||
if (this.tid && rdoc.contest?.toString() !== this.tid) return;
|
||||
if (this.pid && rdoc.pid !== this.pid) return;
|
||||
if (this.uid && rdoc.uid !== this.uid) return;
|
||||
|
||||
|
||||
Reference in new issue
Block a user