core: use rdoc.source & pdoc.reference

This commit is contained in:
undefined committed 2021-10-24 18:28:35 +08:00
1 parent 465a80d617
commit 4aa689b508
24 files changed
+250 -250

No files matched your search

+13 -26
View File
@@ -10,7 +10,7 @@ import {
ProblemNotFoundError, RecordNotFoundError, ValidationError,
} from '../error';
import {
DomainDoc, ProblemDoc, Tdoc, User,
ProblemDoc, Tdoc, User,
} from '../interface';
import paginate from '../lib/paginate';
import { PERM, PRIV } from '../model/builtin';
@@ -153,10 +153,10 @@ export class ContestScoreboardDownloadHandler extends Handler {
await this.limitRate('scoreboard_download', 120, 3);
const getContent = {
csv: async (rows) => `\uFEFF${rows.map((c) => (c.map((i) => i.value).join(','))).join('\n')}`,
html: (rows) => this.renderHTML('contest_scoreboard_download_html.html', { rows }),
html: (rows, tdoc) => this.renderHTML('contest_scoreboard_download_html.html', { rows, tdoc }),
};
const [tdoc, rows] = await contest.getScoreboard.call(this, domainId, tid, true, 0);
this.binary(await getContent[ext](rows), `${tdoc.title}.${ext}`);
this.binary(await getContent[ext](rows, tdoc), `${tdoc.title}.${ext}`);
}
}
@@ -217,13 +217,7 @@ export class ContestEditHandler extends Handler {
domainId: string, tid: ObjectID, beginAtDate: string, beginAtTime: string, duration: number,
title: string, content: string, rule: string, _pids: string, rated = false, _code = '',
) {
const pids = _pids.replace(/,/g, ',').split(',').map((i) => {
i = i.trim();
if ((+i).toString() === i) return +i;
if (i.split(':')[0] === domainId) return +i.split(':')[1];
if (!i.includes(':')) throw new ValidationError('pids');
return i;
}).filter((i) => i);
const pids = _pids.replace(/,/g, ',').split(',').map((i) => +i).filter((i) => i);
const beginAtMoment = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
if (!beginAtMoment.isValid()) throw new ValidationError('beginAtDate', 'beginAtTime');
const endAt = beginAtMoment.clone().add(duration, 'hours').toDate();
@@ -308,8 +302,10 @@ export class ContestProblemFileDownloadHandler extends ContestProblemHandler {
@param('filename', Types.Name)
@param('noDisposition', Types.Boolean)
async get(domainId: string, filename: string, noDisposition = false) {
// @ts-ignore
if (typeof this.pdoc.docId === 'string') this.pdoc.docId = this.pdoc.docId.split(':')[1];
if (this.pdoc.reference) {
this.pdoc = await problem.get(this.pdoc.reference.domainId, this.pdoc.reference.pid);
if (!this.pdoc) throw new ProblemNotFoundError(this.pdoc.reference.domainId, this.pdoc.reference.pid);
}
const target = `problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${filename}`;
const file = await storage.getMeta(target);
if (!file) {
@@ -334,25 +330,16 @@ export class ContestProblemFileDownloadHandler extends ContestProblemHandler {
}
export class ContestDetailProblemSubmitHandler extends ContestProblemHandler {
pdomainId: string;
ppid: number;
pdomain: DomainDoc;
@param('pid', Types.Name)
async prepare(domainId: string, _pid: string) {
const pid = this.tdoc.pids[parseInt(_pid, 36) - 10];
async prepare() {
if (!this.attended) throw new ContestNotAttendedError(this.tdoc.docId);
if (!contest.isOngoing(this.tdoc)) throw new ContestNotLiveError(this.tdoc.docId);
this.pdomainId = typeof pid === 'string' ? pid.split(':')[0] : domainId;
this.ppid = typeof pid === 'number' ? pid : +pid.split(':')[1];
this.pdomain = await domain.get(this.pdomainId);
if (this.pdomain.langs) {
if (this.domain.langs) {
this.response.body.pdoc.config.langs = intersection(
this.response.body.pdoc.config.langs || this.pdomain.langs.split(','),
this.pdomain.langs.split(','),
this.response.body.pdoc.config.langs || this.domain.langs.split(','),
this.domain.langs.split(','),
);
}
this.response.body.pdoc.config.domainId = this.pdomainId;
}
@param('tid', Types.ObjectID)
@@ -387,7 +374,7 @@ export class ContestDetailProblemSubmitHandler extends ContestProblemHandler {
if (!pretest) {
await Promise.all([
(this.tsdoc.journal || []).filter((i) => i.pid === pid).length
&& problem.inc(this.pdomainId, this.ppid, 'nSubmit', 1),
&& 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),
]);
+11 -23
View File
@@ -10,7 +10,7 @@ import {
HomeworkNotLiveError, ProblemNotFoundError, ValidationError,
} from '../error';
import {
DomainDoc, PenaltyRules, ProblemDoc, ProblemId, Tdoc, User,
PenaltyRules, ProblemDoc, Tdoc, User,
} from '../interface';
import paginate from '../lib/paginate';
import { PERM, PRIV, STATUS } from '../model/builtin';
@@ -114,7 +114,7 @@ class HomeworkDetailHandler extends Handler {
class HomeworkDetailProblemHandler extends Handler {
tdoc: Tdoc<30>;
pdoc: ProblemDoc;
pid: ProblemId;
pid: number;
tsdoc: any;
udoc: User;
attended: boolean;
@@ -173,8 +173,10 @@ export class HomeworkProblemFileDownloadHandler extends HomeworkDetailProblemHan
@param('noDisposition', Types.Boolean)
// @ts-ignore
async get(domainId: string, filename: string, noDisposition = false) {
// @ts-ignore
if (typeof this.pdoc.docId === 'string') this.pdoc.docId = this.pdoc.docId.split(':')[1];
if (this.pdoc.reference) {
this.pdoc = await problem.get(this.pdoc.reference.domainId, this.pdoc.reference.pid);
if (!this.pdoc) throw new ProblemNotFoundError(this.pdoc.reference.domainId, this.pdoc.reference.pid);
}
const target = `problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${filename}`;
const file = await storage.getMeta(target);
if (!file) {
@@ -199,24 +201,16 @@ export class HomeworkProblemFileDownloadHandler extends HomeworkDetailProblemHan
}
class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler {
pdomainId: string;
ppid: number;
pdomain: DomainDoc;
@param('tid', Types.ObjectID)
async prepare(domainId: string, tid: ObjectID) {
if (!this.attended) throw new HomeworkNotAttendedError(tid);
if (!contest.isOngoing(this.tdoc)) throw new HomeworkNotLiveError(tid);
this.pdomainId = typeof this.pid === 'string' ? this.pid.split(':')[0] : domainId;
this.ppid = typeof this.pid === 'number' ? this.pid : +this.pid.split(':')[1];
this.pdomain = await domain.get(this.pdomainId);
if (this.pdomain.langs) {
if (this.domain.langs) {
this.response.body.pdoc.config.langs = intersection(
this.response.body.pdoc.config.langs || this.pdomain.langs.split(','),
this.pdomain.langs.split(','),
this.response.body.pdoc.config.langs || this.domain.langs.split(','),
this.domain.langs.split(','),
);
}
this.response.body.pdoc.config.domainId = this.pdomainId;
}
@param('tid', Types.ObjectID)
@@ -247,7 +241,7 @@ class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler {
if (!pretest) {
await Promise.all([
(this.tsdoc.journal || []).filter((i) => i.pid === this.pid).length
&& problem.inc(this.pdomainId, this.ppid, 'nSubmit', 1),
&& 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),
]);
@@ -308,13 +302,7 @@ class HomeworkEditHandler extends Handler {
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 = i.trim();
if ((+i).toString() === i) return +i;
if (i.split(':')[0] === domainId) return +i.split(':')[1];
if (!i.includes(':')) throw new ValidationError('pids');
return i;
}).filter((i) => i);
const pids = _pids.replace(/,/g, ',').split(',').map((i) => +i).filter((i) => i);
const tdoc = tid ? await contest.get(domainId, tid) : null;
if (!tid) this.checkPerm(PERM.PERM_CREATE_HOMEWORK);
else if (!this.user.own(tdoc)) this.checkPerm(PERM.PERM_EDIT_HOMEWORK);
+4 -5
View File
@@ -23,17 +23,16 @@ const logger = new Logger('judge');
export async function postJudge(rdoc: RecordDoc) {
if (typeof rdoc.input === 'string') return;
const accept = rdoc.status === builtin.STATUS.STATUS_ACCEPTED;
const updated = await problem.updateStatus(rdoc.pdomain, rdoc.pid, rdoc.uid, rdoc._id, rdoc.status, rdoc.score);
const updated = await problem.updateStatus(rdoc.domainId, rdoc.pid, rdoc.uid, rdoc._id, rdoc.status, rdoc.score);
if (rdoc.contest) {
await contest.updateStatus(
rdoc.domainId, rdoc.contest, rdoc.uid, rdoc._id,
rdoc.domainId === rdoc.pdomain ? rdoc.pid : `${rdoc.pdomain}:${rdoc.pid}`,
rdoc.status, rdoc.score,
rdoc.pid, rdoc.status, rdoc.score,
);
} else if (accept && updated) await domain.incUserInDomain(rdoc.domainId, rdoc.uid, 'nAccept', 1);
const pdoc = (accept && updated)
? await problem.inc(rdoc.pdomain, rdoc.pid, 'nAccept', 1)
: await problem.get(rdoc.pdomain, rdoc.pid);
? await problem.inc(rdoc.domainId, rdoc.pid, 'nAccept', 1)
: await problem.get(rdoc.domainId, rdoc.pid);
if (pdoc) {
const difficulty = difficultyAlgorithm(pdoc.nSubmit, pdoc.nAccept);
await Promise.all([
+17 -13
View File
@@ -265,6 +265,15 @@ export class ProblemDetailHandler extends ProblemHandler {
this.back();
}
@param('target', Types.String)
@param('pid', Types.String, true)
async postCopy(domainId: string, target: string, pid: string) {
const dudoc = await user.getById(target, this.user._id);
dudoc.checkPerm(PERM.PERM_CREATE_PROBLEM);
const docId = await problem.copy(domainId, this.user.docId, target, pid);
this.response.redirect = this.url('problem_detail', { domainId: target, pid: docId });
}
async postDelete() {
if (!this.user.own(this.pdoc, PERM.PERM_EDIT_PROBLEM_SELF)) this.checkPerm(PERM.PERM_EDIT_PROBLEM);
const tdocs = await contest.getRelated(this.domainId, this.pdoc.docId);
@@ -372,6 +381,7 @@ export class ProblemFilesHandler extends ProblemDetailHandler {
@param('pjax', Types.Boolean)
async get(domainId: string, getTestdata = true, getAdditionalFile = true, pjax = false) {
this.response.body.testdata = getTestdata ? sortFiles(this.pdoc.data || []) : [];
this.response.body.reference = getTestdata ? this.pdoc.reference : '';
this.response.body.additional_file = getAdditionalFile ? sortFiles(this.pdoc.additional_file || []) : [];
if (pjax) {
const { testdata, additional_file } = this.response.body;
@@ -407,6 +417,7 @@ export class ProblemFilesHandler extends ProblemDetailHandler {
@post('filename', Types.Name, true)
@post('type', Types.Range(['testdata', 'additional_file']), true)
async postUploadFile(domainId: string, filename: string, type = 'testdata') {
if (this.pdoc.reference) throw new ForbiddenError('Cannot delete files of a referenced problem.');
if (!this.request.files.file) throw new ValidationError('file');
if (!filename) filename = this.request.files.file.name || String.random(16);
if (filename.includes('/') || filename.includes('..')) throw new ValidationError('filename', null, 'Bad filename');
@@ -463,6 +474,7 @@ export class ProblemFilesHandler extends ProblemDetailHandler {
@post('files', Types.Array)
@post('type', Types.Range(['testdata', 'additional_file']), true)
async postDeleteFiles(domainId: string, files: string[], type = 'testdata') {
if (this.pdoc.reference) throw new ForbiddenError('Cannot delete files of a referenced problem.');
if (!this.user.own(this.pdoc, PERM.PERM_EDIT_PROBLEM_SELF)) this.checkPerm(PERM.PERM_EDIT_PROBLEM);
if (type === 'testdata') await problem.delTestdata(domainId, this.pdoc.docId, files);
else await problem.delAdditionalFile(domainId, this.pdoc.docId, files);
@@ -475,6 +487,11 @@ export class ProblemFileDownloadHandler extends ProblemDetailHandler {
@param('filename', Types.Name)
@param('noDisposition', Types.Boolean)
async get(domainId: string, type = 'additional_file', filename: string, noDisposition = false) {
if (this.pdoc.reference) {
if (type === 'testdata') throw new ForbiddenError('Cannot download testdata');
this.pdoc = await problem.get(this.pdoc.reference.domainId, this.pdoc.reference.pid);
if (!this.pdoc) throw new ProblemNotFoundError(this.pdoc.reference.domainId, this.pdoc.reference.pid);
}
if (type === 'testdata' && !this.user.own(this.pdoc)) {
if (!this.user.hasPriv(PRIV.PRIV_READ_PROBLEM_DATA)) this.checkPerm(PERM.PERM_READ_PROBLEM_DATA);
}
@@ -644,18 +661,6 @@ export class ProblemCreateHandler extends Handler {
export class ProblemPrefixListHandler extends Handler {
@param('prefix', Types.Name)
async get(domainId: string, prefix: string) {
let cors = false;
if (prefix.includes(':')) {
const [pdomain, s] = prefix.split(':');
const ddoc = await domain.get(pdomain);
this.response.body = [];
if (!ddoc || ddoc._id === domainId || !ddoc.share || !s.trim()) return;
ddoc.share = ddoc.share.replace(/,/g, ',').split(',').map((q) => q.trim()).join(',');
if (ddoc.share !== '*' && !`,${ddoc.share},`.includes(`,${domainId},`)) return;
domainId = pdomain;
prefix = s;
cors = true;
}
const pdocs = await problem.getPrefixList(domainId, prefix);
if (!Number.isNaN(+prefix)) {
const pdoc = await problem.get(domainId, +prefix, ['domainId', 'docId', 'pid', 'title']);
@@ -670,7 +675,6 @@ export class ProblemPrefixListHandler extends Handler {
}
}
this.response.body = pdocs;
if (cors) this.response.body.forEach((v) => { v.docId = `${domainId}:${v.docId}`; });
}
}
+7 -14
View File
@@ -76,9 +76,7 @@ class RecordListHandler extends Handler {
: await Promise.all([
user.getList(domainId, rdocs.map((rdoc) => rdoc.uid)),
canViewProblem
? problem.getList(domainId, rdocs.map(
(rdoc) => (rdoc.domainId === domainId ? rdoc.pid : `${rdoc.pdomain}:${rdoc.pid}`),
), canViewProblemHidden || this.user._id, false)
? problem.getList(domainId, rdocs.map((rdoc) => rdoc.pid), canViewProblemHidden || this.user._id, false)
: Object.fromEntries([rdocs.map((rdoc) => [rdoc.pid, { ...problem.default, pid: rdoc.pid }])]),
]);
this.response.body = {
@@ -125,7 +123,7 @@ class RecordDetailHandler extends Handler {
}
// eslint-disable-next-line prefer-const
let [pdoc, udoc] = await Promise.all([
problem.get(rdoc.pdomain, rdoc.pid),
problem.get(rdoc.domainId, rdoc.pid),
user.getById(domainId, rdoc.uid),
]);
if (
@@ -181,7 +179,6 @@ class RecordMainConnectionHandler extends ConnectionHandler {
all = false;
tid: string;
uid: number;
pdomain: string;
pid: number;
status: number;
pretest = false;
@@ -216,13 +213,9 @@ class RecordMainConnectionHandler extends ConnectionHandler {
}
}
if (pid) {
const pdomain = pid.includes(':') ? pid.split(':')[0] : domainId;
const ppid = pid.includes(':') ? +pid.split(':')[1] : pid;
const pdoc = await problem.get(pdomain, ppid);
if (pdoc) {
this.pdomain = pdoc.domainId;
this.pid = pdoc.docId;
} else throw new ProblemNotFoundError(domainId, pid);
const pdoc = await problem.get(domainId, pid);
if (pdoc) this.pid = pdoc.docId;
else throw new ProblemNotFoundError(domainId, pid);
}
if (status) this.status = status;
if (all) {
@@ -251,7 +244,7 @@ class RecordMainConnectionHandler extends ConnectionHandler {
// eslint-disable-next-line prefer-const
let [udoc, pdoc] = await Promise.all([
user.getById(this.domainId, rdoc.uid),
problem.get(rdoc.pdomain, rdoc.pid),
problem.get(rdoc.domainId, rdoc.pid),
]);
const tdoc = this.tid ? this.tdoc || await contest.get(rdoc.domainId, new ObjectID(this.tid)) : null;
if (pdoc && !rdoc.contest) {
@@ -290,7 +283,7 @@ class RecordDetailConnectionHandler extends ConnectionHandler {
rdoc.compilerTexts = [];
}
}
const pdoc = await problem.get(rdoc.pdomain, rdoc.pid);
const pdoc = await problem.get(rdoc.domainId, rdoc.pid);
let canView = pdoc && this.user.own(pdoc);
canView ||= !pdoc?.hidden && this.user.hasPerm(PERM.PERM_VIEW_PROBLEM);
canView ||= this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);