You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/handler/problem.js

465 lines
16 KiB
JavaScript

5 years ago
const fs = require('fs');
const paginate = require('../lib/paginate');
const validator = require('../lib/validator');
const file = require('../model/file');
5 years ago
const problem = require('../model/problem');
const record = require('../model/record');
const user = require('../model/user');
const solution = require('../model/solution');
const system = require('../model/system');
const bus = require('../service/bus');
const {
Route, Connection, Handler, ConnectionHandler,
} = require('../service/server');
5 years ago
const {
NoProblemError, ProblemDataNotFoundError, BadRequestError,
SolutionNotFoundError,
} = require('../error');
const {
PERM_VIEW_PROBLEM, PERM_VIEW_PROBLEM_HIDDEN, PERM_SUBMIT_PROBLEM,
PERM_CREATE_PROBLEM, PERM_READ_PROBLEM_DATA, PERM_EDIT_PROBLEM,
PERM_JUDGE, PERM_VIEW_PROBLEM_SOLUTION, PERM_CREATE_PROBLEM_SOLUTION,
PERM_EDIT_PROBLEM_SOLUTION, PERM_DELETE_PROBLEM_SOLUTION, PERM_EDIT_PROBLEM_SOLUTION_REPLY,
5 years ago
PERM_REPLY_PROBLEM_SOLUTION, PERM_LOGGEDIN,
5 years ago
} = require('../permission');
class ProblemHandler extends Handler {
async _prepare() {
this.checkPerm(PERM_VIEW_PROBLEM);
5 years ago
}
5 years ago
async get({ domainId, page = 1, category = '' }) {
this.response.template = 'problem_main.html';
5 years ago
const q = {};
5 years ago
let psdict = {};
if (category) q.$or = [{ category }, { tag: category }];
if (!this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
const [pdocs, ppcount, pcount] = await paginate(
problem.getMulti(domainId, q).sort({ pid: 1 }),
5 years ago
page,
5 years ago
await system.get('PROBLEM_PER_PAGE'),
5 years ago
);
5 years ago
if (this.user.hasPerm(PERM_LOGGEDIN)) {
psdict = await problem.getListStatus(
domainId, this.user._id, pdocs.map((pdoc) => pdoc.docId),
);
5 years ago
}
5 years ago
const path = [
['Hydro', '/'],
['problem_main', null],
];
this.response.body = {
path, page, pcount, ppcount, pdocs, psdict, category,
5 years ago
};
}
async cleanup() {
if (this.response.template === 'problem_main.html' && this.preferJson) {
const {
path, page, pcount, ppcount, pdocs, psdict, category,
} = this.response.body;
this.response.body = {
title: this.renderTitle(category),
fragments: (await Promise.all([
this.renderHTML('partials/problem_list.html', {
page, ppcount, pcount, pdocs, psdict,
}),
this.renderHTML('partials/problem_stat.html', { pcount }),
this.renderHTML('partials/problem_lucky.html', { category }),
this.renderHTML('partials/path.html', { path }),
])).map((i) => ({ html: i })),
raw: {
path, page, pcount, ppcount, pdocs, psdict, category,
},
};
}
}
}
class ProblemCategoryHandler extends ProblemHandler {
async get({ domainId, page = 1, category }) {
this.response.template = 'problem_main.html';
const q = {
$or: [{ category }, { tag: category }],
};
let psdict = {};
if (!this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
const [pdocs, ppcount, pcount] = await paginate(
problem.getMulti(domainId, q).sort({ pid: 1 }),
page,
await system.get('PROBLEM_PER_PAGE'),
);
if (this.user.hasPerm(PERM_LOGGEDIN)) {
psdict = await problem.getListStatus(
domainId, this.user._id, pdocs.map((pdoc) => pdoc.docId),
);
}
const path = [
['Hydro', '/'],
['problem_main', '/p'],
[category, null, true],
];
this.response.body = {
path, page, pcount, ppcount, pdocs, psdict, category,
};
}
5 years ago
}
class ProblemRandomHandler extends ProblemHandler {
async get({ domainId }) {
5 years ago
const q = {};
if (!this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
const pid = await problem.random(domainId, q);
5 years ago
if (!pid) throw new NoProblemError();
this.response.body = { pid };
this.response.redirect = `/p/${pid}`;
5 years ago
}
}
class ProblemDetailHandler extends ProblemHandler {
async _prepare({ domainId, pid }) {
this.response.template = 'problem_detail.html';
this.uid = this.user._id;
this.pid = pid;
if (pid) {
this.pdoc = await problem.get(domainId, pid, this.uid);
if (this.pdoc.hidden && this.pdoc.owner !== this.uid) {
this.checkPerm(PERM_VIEW_PROBLEM_HIDDEN);
}
if (this.pdoc) this.udoc = await user.getById(domainId, this.pdoc.owner);
5 years ago
}
this.response.body = {
5 years ago
pdoc: this.pdoc,
udoc: this.udoc,
5 years ago
title: (this.pdoc || {}).title || '',
5 years ago
};
}
5 years ago
5 years ago
async get() {
this.response.body.path = [
['Hydro', '/'],
['problem_main', '/p'],
5 years ago
[this.pdoc.title, null, true],
5 years ago
];
}
}
class ProblemSubmitHandler extends ProblemDetailHandler {
async prepare() {
this.checkPerm(PERM_SUBMIT_PROBLEM);
5 years ago
}
5 years ago
async get({ domainId }) {
this.response.template = 'problem_submit.html';
const rdocs = await record.getUserInProblemMulti(domainId, this.uid, this.pdoc.docId)
5 years ago
.sort({ _id: -1 })
.limit(10)
.toArray();
this.response.body = {
5 years ago
path: [
['Hydro', '/'],
['problem_main', '/p'],
[this.pdoc.title, `/p/${this.pid}`, true],
5 years ago
['problem_submit', null],
5 years ago
],
pdoc: this.pdoc,
udoc: this.udoc,
rdocs,
5 years ago
title: this.pdoc.title,
5 years ago
};
}
5 years ago
async post({ domainId }) {
5 years ago
const { lang, code } = this.request.body;
const rid = await record.add(domainId, {
uid: this.uid, lang, code, pid: this.pdoc.docId,
5 years ago
});
await Promise.all([
record.judge(domainId, rid),
user.incDomain(domainId, this.user._id, 'nSubmit'),
]);
this.response.body = { rid };
this.response.redirect = `/r/${rid}`;
5 years ago
}
}
class ProblemPretestHandler extends ProblemDetailHandler {
async post({
domainId, lang, code, input,
}) {
this.limitRate('add_record', 60, 100);
const rid = await record.add(domainId, {
uid: this.uid, lang, code, pid: this.pdoc.docId, input,
});
await record.judge(domainId, rid);
this.response.body = { rid };
}
}
class ProblemPretestConnectionHandler extends ConnectionHandler {
async prepare({ domainId, pid }) {
this.pid = pid.toString();
this.domainId = domainId;
bus.subscribe(['record_change'], this.onRecordChange);
}
async onRecordChange(data) {
const rdoc = data.value;
if (
rdoc.uid !== this.user._id
|| rdoc.pid.toString() !== this.pid
|| rdoc.domainId !== this.domainId
) return;
if (rdoc.tid) return;
this.send({ rdoc });
}
async cleanup() {
bus.unsubscribe(['record_change'], this.onRecordChange);
}
}
class ProblemStatisticsHandler extends ProblemDetailHandler {
async get({ domainId }) {
const udoc = await user.getById(domainId, this.pdoc.owner);
const path = [
['problem_main', '/p'],
[this.pdoc.title, `/p/${this.pdoc.pid}`, true],
['problem_statistics', null],
];
this.response.template = 'problem_statistics.html';
this.response.body = { pdoc: this.pdoc, udoc, path };
}
}
class ProblemManageHandler extends ProblemDetailHandler {
5 years ago
async prepare() {
5 years ago
if (this.pdoc.owner !== this.uid) this.checkPerm(PERM_EDIT_PROBLEM);
5 years ago
}
}
class ProblemSettingsHandler extends ProblemManageHandler {
5 years ago
async get() {
this.response.template = 'problem_settings.html';
this.response.body.path = [
['Hydro', '/'],
['problem_main', '/p'],
5 years ago
[this.pdoc.title, `/p/${this.pid}`, true],
5 years ago
['problem_settings', null],
5 years ago
];
}
5 years ago
5 years ago
async post() {
// TODO(masnn)
this.back();
5 years ago
}
}
class ProblemEditHandler extends ProblemManageHandler {
async get({ pid }) {
this.response.template = 'problem_edit.html';
this.response.body.path = [
['Hydro', '/'],
['problem_main', '/p'],
[this.pdoc.title, `/p/${pid}`, true],
5 years ago
['problem_edit', null],
5 years ago
];
this.response.body.page_name = 'problem_edit';
5 years ago
}
5 years ago
async post({ domainId, title, content }) {
5 years ago
const pid = validator.checkPid(this.request.body.pid);
const pdoc = await problem.get(domainId, this.params.pid);
await problem.edit(domainId, pdoc.docId, { title, content, pid });
this.response.redirect = `/p/${pid}`;
5 years ago
}
}
class ProblemDataUploadHandler extends ProblemManageHandler {
5 years ago
async prepare() {
this.response.template = 'problem_upload.html';
5 years ago
}
5 years ago
async get() {
5 years ago
if (this.pdoc.data && typeof this.pdoc.data === 'object') {
const f = await file.getMeta(this.pdoc.data);
this.md5 = f.md5;
5 years ago
}
this.response.body.md5 = this.md5;
5 years ago
}
5 years ago
async post({ domainId }) {
if (!this.request.files.file) throw new BadRequestError();
const r = fs.createReadStream(this.request.files.file.path);
await problem.setTestdata(domainId, this.pdoc.docId, r);
5 years ago
if (this.pdoc.data && typeof this.pdoc.data === 'object') {
const f = await file.getMeta(this.pdoc.data);
this.md5 = f.md5;
5 years ago
}
this.response.body.md5 = this.md5;
5 years ago
}
}
5 years ago
class ProblemDataDownloadHandler extends ProblemDetailHandler {
async get({ pid }) {
5 years ago
if (this.uid !== this.pdoc.owner) this.checkPerm([PERM_READ_PROBLEM_DATA, PERM_JUDGE]);
5 years ago
if (!this.pdoc.data) throw new ProblemDataNotFoundError(pid);
else if (typeof this.pdoc.data === 'string') [, this.response.redirect] = this.pdoc.data.split('from:');
this.response.redirect = file.url(this.pdoc.data, this.pdoc.title);
5 years ago
}
}
5 years ago
class ProblemSolutionHandler extends ProblemDetailHandler {
async get({ domainId, page = 1 }) {
this.response.template = 'problem_solution.html';
this.checkPerm(PERM_VIEW_PROBLEM_SOLUTION);
5 years ago
const [psdocs, pcount, pscount] = await paginate(
solution.getMulti(domainId, this.pdoc.docId),
5 years ago
page,
await system.get('SOLUTION_PER_PAGE'),
5 years ago
);
const uids = [this.pdoc.owner];
const docids = [];
5 years ago
for (const psdoc of psdocs) {
docids.push(psdoc.docId);
5 years ago
uids.push(psdoc.owner);
if (psdoc.reply.length) {
for (const psrdoc of psdoc.reply) uids.push(psrdoc.owner);
}
5 years ago
}
5 years ago
const udict = await user.getList(uids);
const pssdict = solution.getListStatus(domainId, docids, this.uid);
5 years ago
const path = [
5 years ago
['problem_main', '/p'],
[this.pdoc.title, `/p/${this.pdoc.pid}`, true],
5 years ago
['problem_solution', null],
5 years ago
];
this.response.body = {
5 years ago
path, psdocs, page, pcount, pscount, udict, pssdict, pdoc: this.pdoc,
};
5 years ago
}
5 years ago
async post({ domainId, psid }) {
if (psid) this.psdoc = await solution.get(domainId, psid);
5 years ago
}
5 years ago
async postSubmit({ domainId, content }) {
this.checkPerm(PERM_CREATE_PROBLEM_SOLUTION);
await solution.add(domainId, this.pdoc.docId, this.uid, content);
this.back();
5 years ago
}
5 years ago
async postEditSolution({ domainId, content }) {
5 years ago
if (this.psdoc.owner !== this.uid) this.checkPerm(PERM_EDIT_PROBLEM_SOLUTION);
this.psdoc = await solution.edit(domainId, this.psdoc.docId, content);
5 years ago
this.ctx.body.psdoc = this.psdoc;
this.back();
5 years ago
}
5 years ago
async postDeleteSolution({ domainId }) {
5 years ago
if (this.psdoc.owner !== this.uid) this.checkPerm(PERM_DELETE_PROBLEM_SOLUTION);
await solution.del(domainId, this.psdoc.docId);
this.back();
5 years ago
}
5 years ago
async postReply({ domainId, psid, content }) {
this.checkPerm(PERM_REPLY_PROBLEM_SOLUTION);
const psdoc = await solution.get(domainId, psid);
await solution.reply(domainId, psdoc.docId, this.uid, content);
5 years ago
}
5 years ago
async postEditReply({
domainId, content, psid, psrid,
}) {
const [psdoc, psrdoc] = await solution.getReply(domainId, psid, psrid);
if ((!psdoc) || psdoc.pid !== this.pdoc.docId) throw new SolutionNotFoundError(psid);
5 years ago
if (psrdoc.owner !== this.uid) this.checkPerm(PERM_EDIT_PROBLEM_SOLUTION_REPLY);
await solution.editReply(domainId, psid, psrid, content);
5 years ago
}
5 years ago
async postDeleteReply({ domainId, psid, psrid }) {
const [psdoc, psrdoc] = await solution.getReply(domainId, psid, psrid);
if ((!psdoc) || psdoc.pid !== this.pdoc.docId) throw new SolutionNotFoundError(psid);
5 years ago
if (psrdoc.owner !== this.uid) this.checkPerm(PERM_EDIT_PROBLEM_SOLUTION_REPLY);
await solution.delReply(domainId, psid, psrid);
this.back();
5 years ago
}
5 years ago
async postUpvote({ domainId }) {
const [psdoc, pssdoc] = await solution.vote(domainId, this.psdoc.docId, this.uid, 1);
this.response.body = { vote: psdoc.vote, user_vote: pssdoc.vote };
this.back();
}
5 years ago
async postDownvote({ domainId }) {
const [psdoc, pssdoc] = await solution.vote(domainId, this.psdoc.docId, this.uid, -1);
this.response.body = { vote: psdoc.vote, user_vote: pssdoc.vote };
this.back();
}
5 years ago
}
5 years ago
class ProblemSolutionRawHandler extends ProblemDetailHandler {
async get({ domainId, psid }) {
this.checkPerm(PERM_VIEW_PROBLEM_SOLUTION);
const psdoc = await solution.get(domainId, psid);
this.response.type = 'text/markdown';
this.response.body = psdoc.content;
5 years ago
}
}
5 years ago
class ProblemSolutionReplyRawHandler extends ProblemDetailHandler {
async get({ domainId, psid }) {
this.checkPerm(PERM_VIEW_PROBLEM_SOLUTION);
const [psdoc, psrdoc] = await solution.getReply(domainId, psid);
if ((!psdoc) || psdoc.pid !== this.pdoc.docId) throw new SolutionNotFoundError(psid);
this.response.type = 'text/markdown';
this.response.body = psrdoc.content;
5 years ago
}
}
class ProblemCreateHandler extends Handler {
5 years ago
async get() {
this.response.template = 'problem_edit.html';
this.checkPerm(PERM_CREATE_PROBLEM);
this.response.body = {
5 years ago
path: [
['Hydro', '/'],
['problem_main', '/p'],
5 years ago
['problem_create', null],
],
page_name: 'problem_create',
5 years ago
};
}
5 years ago
async post({
domainId, title, pid, content, hidden,
5 years ago
}) {
pid = await problem.add(domainId, title, content, this.user._id, {
pid, hidden,
5 years ago
});
this.response.body = { pid };
this.response.redirect = `/p/${pid}/settings`;
5 years ago
}
}
5 years ago
async function apply() {
4 years ago
Route('/p', ProblemHandler);
Route('/p/category/:category', ProblemCategoryHandler);
Route('/problem/random', ProblemRandomHandler);
Route('/p/:pid', ProblemDetailHandler);
Route('/p/:pid/submit', ProblemSubmitHandler);
Route('/p/:pid/pretest', ProblemPretestHandler);
Route('/p/:pid/settings', ProblemSettingsHandler);
Route('/p/:pid/statistics', ProblemStatisticsHandler);
Route('/p/:pid/edit', ProblemEditHandler);
Route('/p/:pid/upload', ProblemDataUploadHandler);
Route('/p/:pid/data', ProblemDataDownloadHandler);
Route('/p/:pid/solution', ProblemSolutionHandler);
Route('/p/:pid/solution/:psid/raw', ProblemSolutionRawHandler);
Route('/p/:pid/solution/:psid/:psrid/raw', ProblemSolutionReplyRawHandler);
Route('/problem/create', ProblemCreateHandler);
Connection('/p/:pid/pretest-conn', ProblemPretestConnectionHandler);
5 years ago
}
4 years ago
global.Hydro.handler.problem = module.exports = apply;