mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
Hello airbnb
This commit is contained in:
1 parent
f9468536de
commit
8c195661d7
78 files changed
+2032
-1471
No files matched your search
+114
-78
@@ -1,20 +1,23 @@
|
||||
const
|
||||
{ ContestNotLiveError, ValidationError, ProblemNotFoundError,
|
||||
ContestNotAttendedError } = require('../error'),
|
||||
{ PERM_CREATE_CONTEST, PERM_EDIT_CONTEST } = require('../permission'),
|
||||
{ constants } = require('../options'),
|
||||
paginate = require('../lib/paginate'),
|
||||
contest = require('../model/contest'),
|
||||
problem = require('../model/problem'),
|
||||
record = require('../model/record'),
|
||||
user = require('../model/user'),
|
||||
{ Route, Handler } = require('../service/server');
|
||||
const {
|
||||
ContestNotLiveError, ValidationError, ProblemNotFoundError,
|
||||
ContestNotAttendedError,
|
||||
} = require('../error');
|
||||
const { PERM_CREATE_CONTEST, PERM_EDIT_CONTEST } = require('../permission');
|
||||
const { constants } = require('../options');
|
||||
const paginate = require('../lib/paginate');
|
||||
const contest = require('../model/contest');
|
||||
const problem = require('../model/problem');
|
||||
const record = require('../model/record');
|
||||
const user = require('../model/user');
|
||||
const { Route, Handler } = require('../service/server');
|
||||
|
||||
const ContestHandler = contest.ContestHandlerMixin(Handler);
|
||||
class ContestListHandler extends ContestHandler {
|
||||
async get({ rule = 0, page = 1 }) {
|
||||
this.response.template = 'contest_main.html';
|
||||
let tdocs, qs, tpcount;
|
||||
let tdocs;
|
||||
let qs;
|
||||
let tpcount;
|
||||
if (!rule) {
|
||||
tdocs = contest.getMulti().sort({ beginAt: -1 });
|
||||
qs = '';
|
||||
@@ -23,12 +26,13 @@ class ContestListHandler extends ContestHandler {
|
||||
tdocs = contest.getMulti({ rule }).sort({ beginAt: -1 });
|
||||
qs = 'rule={0}'.format(rule);
|
||||
}
|
||||
// eslint-disable-next-line prefer-const
|
||||
[tdocs, tpcount] = await paginate(tdocs, page, constants.CONTEST_PER_PAGE);
|
||||
let tids = [];
|
||||
for (let tdoc of tdocs) tids.push(tdoc._id);
|
||||
let tsdict = await contest.getListStatus(this.user._id, tids);
|
||||
const tids = [];
|
||||
for (const tdoc of tdocs) tids.push(tdoc._id);
|
||||
const tsdict = await contest.getListStatus(this.user._id, tids);
|
||||
this.response.body = {
|
||||
page, tpcount, qs, rule, tdocs, tsdict
|
||||
page, tpcount, qs, rule, tdocs, tsdict,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -36,35 +40,35 @@ class ContestDetailHandler extends ContestHandler {
|
||||
async _prepare({ tid }) {
|
||||
this.tdoc = await contest.get(tid);
|
||||
}
|
||||
|
||||
async get({ page = 1 }) {
|
||||
this.response.template = 'contest_detail.html';
|
||||
let [tsdoc, pdict] = await Promise.all([
|
||||
const [tsdoc, pdict] = await Promise.all([
|
||||
contest.getStatus(this.tdoc._id, this.user._id),
|
||||
problem.getList(this.tdoc.pids)
|
||||
problem.getList(this.tdoc.pids),
|
||||
]);
|
||||
let psdict = {}, rdict = {}, attended;
|
||||
const psdict = {}; let rdict = {}; let
|
||||
attended;
|
||||
if (tsdoc) {
|
||||
attended = tsdoc.attend == 1;
|
||||
for (let pdetail of tsdoc.journal || [])
|
||||
psdict[pdetail.pid] = pdetail;
|
||||
attended = tsdoc.attend === 1;
|
||||
for (const pdetail of tsdoc.journal || []) { psdict[pdetail.pid] = pdetail; }
|
||||
if (this.canShowRecord(this.tdoc)) {
|
||||
let q = [];
|
||||
for (let i in psdict) q.push(psdict[i].rid);
|
||||
const q = [];
|
||||
for (const i in psdict) q.push(psdict[i].rid);
|
||||
rdict = await record.getList(q);
|
||||
} else
|
||||
for (let i in psdict)
|
||||
rdict[psdict[i].rid] = { _id: psdict[i].rid };
|
||||
} else { for (const i in psdict) rdict[psdict[i].rid] = { _id: psdict[i].rid }; }
|
||||
} else attended = false;
|
||||
let udict = await user.getList([this.tdoc.owner]);
|
||||
let path = [
|
||||
const udict = await user.getList([this.tdoc.owner]);
|
||||
const path = [
|
||||
['contest_main', '/c'],
|
||||
[this.tdoc.title, null, true]
|
||||
[this.tdoc.title, null, true],
|
||||
];
|
||||
this.response.body = {
|
||||
path, tdoc: this.tdoc, tsdoc, attended, udict, pdict, psdict, rdict, page
|
||||
path, tdoc: this.tdoc, tsdoc, attended, udict, pdict, psdict, rdict, page,
|
||||
};
|
||||
}
|
||||
async post_attend() {
|
||||
|
||||
async postAttend() {
|
||||
if (contest.is_done(this.tdoc)) throw new ContestNotLiveError(this.tdoc._id);
|
||||
await contest.attend(this.tdoc._id, this.user._id);
|
||||
this.back();
|
||||
@@ -72,108 +76,136 @@ class ContestDetailHandler extends ContestHandler {
|
||||
}
|
||||
class ContestScoreboardHandler extends ContestDetailHandler {
|
||||
async get({ tid }) {
|
||||
let [tdoc, rows, udict] = await this.getScoreboard(tid);
|
||||
let path = [
|
||||
const [tdoc, rows, udict] = await this.getScoreboard(tid);
|
||||
const path = [
|
||||
['contest_main', '/c'],
|
||||
[tdoc.title, `/c/${tid}`, true],
|
||||
['contest_scoreboard', null]
|
||||
['contest_scoreboard', null],
|
||||
];
|
||||
this.response.template = 'contest_scoreboard.html';
|
||||
this.response.body = { tdoc, rows, path, udict };
|
||||
this.response.body = {
|
||||
tdoc, rows, path, udict,
|
||||
};
|
||||
}
|
||||
}
|
||||
class ContestEditHandler extends ContestDetailHandler {
|
||||
async prepare({ tid }) {
|
||||
let tdoc = await contest.get(tid);
|
||||
if (!tdoc.owner != this.user._id) this.checkPerm(PERM_EDIT_CONTEST);
|
||||
const tdoc = await contest.get(tid);
|
||||
if (!tdoc.owner !== this.user._id) this.checkPerm(PERM_EDIT_CONTEST);
|
||||
}
|
||||
|
||||
async get() {
|
||||
this.response.template = 'contest_edit.html';
|
||||
let rules = {};
|
||||
for (let i in contest.RULES)
|
||||
rules[i] = contest.RULES[i].TEXT;
|
||||
let duration = (this.tdoc.endAt.getTime() - this.tdoc.beginAt.getTime()) / 3600 / 1000;
|
||||
let path = [
|
||||
const rules = {};
|
||||
for (const i in contest.RULES) { rules[i] = contest.RULES[i].TEXT; }
|
||||
const duration = (this.tdoc.endAt.getTime() - this.tdoc.beginAt.getTime()) / 3600 / 1000;
|
||||
const path = [
|
||||
['contest_main', '/c'],
|
||||
[this.tdoc.title, `/c/${this.tdoc._id}`, true],
|
||||
['contest_edit', null]
|
||||
['contest_edit', null],
|
||||
];
|
||||
let dt = this.tdoc.beginAt;
|
||||
const dt = this.tdoc.beginAt;
|
||||
this.response.body = {
|
||||
rules, tdoc: this.tdoc, duration, path, pids: this.tdoc.pids.join(','),
|
||||
rules,
|
||||
tdoc: this.tdoc,
|
||||
duration,
|
||||
path,
|
||||
pids: this.tdoc.pids.join(','),
|
||||
date_text: `${dt.getFullYear()}-${dt.getMonth() + 1}-${dt.getDate()}`,
|
||||
time_text: `${dt.getHours()}:${dt.getMinutes()}`,
|
||||
page_name: 'contest_edit'
|
||||
page_name: 'contest_edit',
|
||||
};
|
||||
}
|
||||
async post({ beginAtDate, beginAtTime, duration, title, content, rule, pids }) {
|
||||
let beginAt, endAt;
|
||||
|
||||
async post({
|
||||
beginAtDate, beginAtTime, duration, title, content, rule, pids,
|
||||
}) {
|
||||
let beginAt;
|
||||
try {
|
||||
beginAt = new Date(Date.parse(`${beginAtDate} ${beginAtTime.replace('-', ':')}`));
|
||||
} catch (e) {
|
||||
throw new ValidationError('beginAtDate', 'beginAtTime');
|
||||
}
|
||||
endAt = new Date(beginAt + duration * 3600 * 1000);
|
||||
const endAt = new Date(beginAt + duration * 3600 * 1000);
|
||||
if (beginAt >= endAt) throw new ValidationError('duration');
|
||||
pids = await this.verifyProblems(pids);
|
||||
await contest.edit(this.tdoc._id, title, content, rule, beginAt, endAt, pids);
|
||||
if (this.tdoc.beginAt != beginAt || this.tdoc.endAt != endAt
|
||||
|| Array.isDiff(this.tdoc.pids, pids) || this.tdoc.rule != rule)
|
||||
if (this.tdoc.beginAt !== beginAt || this.tdoc.endAt !== endAt
|
||||
|| Array.isDiff(this.tdoc.pids, pids) || this.tdoc.rule !== rule) {
|
||||
await contest.recalcStatus(this.tdoc._id);
|
||||
}
|
||||
if (this.preferJson) this.response.body = { tid: this.tdoc._id };
|
||||
else this.response.redirect = `/c/${this.tdoc._id}`;
|
||||
}
|
||||
}
|
||||
class ContestProblemHandler extends ContestDetailHandler {
|
||||
async prepare({ tid, pid }) {
|
||||
[this.tdoc, this.pdoc] = await Promise.all([contest.get(tid), problem.get(pid, this.user._id)]);
|
||||
[this.tdoc, this.pdoc] = await Promise.all([
|
||||
contest.get(tid),
|
||||
problem.get(pid, this.user._id),
|
||||
]);
|
||||
[this.tsdoc, this.udoc] = await Promise.all([
|
||||
contest.getStatus(this.tdoc._id, this.user._id),
|
||||
user.getById(this.tdoc.owner)
|
||||
user.getById(this.tdoc.owner),
|
||||
]);
|
||||
this.attended = this.tsdoc && this.tsdoc.attend == 1;
|
||||
this.attended = this.tsdoc && this.tsdoc.attend === 1;
|
||||
this.response.template = 'problem_detail.html';
|
||||
if (contest.is_done(this.tdoc)) {
|
||||
if (!this.attended) throw new ContestNotAttendedError(this.tdoc._id);
|
||||
if (!contest.is_ongoing(this.tdoc)) throw new ContestNotLiveError(this.tdoc._id);
|
||||
}
|
||||
console.log(this.tdoc.pids, this.pdoc._id);
|
||||
if (!this.tdoc.pids.map(s => s.toString()).includes(this.pdoc._id.toString()))
|
||||
if (!this.tdoc.pids.map((s) => s.toString()).includes(this.pdoc._id.toString())) {
|
||||
throw new ProblemNotFoundError(pid, this.tdoc._id);
|
||||
}
|
||||
}
|
||||
|
||||
async get({ tid }) {
|
||||
let path = [
|
||||
const path = [
|
||||
['contest_main', '/c'],
|
||||
[this.tdoc.title, `/c/${tid}`, true],
|
||||
[this.pdoc.title, null, true]
|
||||
[this.pdoc.title, null, true],
|
||||
];
|
||||
this.response.body = {
|
||||
tdoc: this.tdoc, pdoc: this.pdoc, tsdoc: this.tsdoc,
|
||||
udoc: this.udoc, attended: this.attended, path
|
||||
tdoc: this.tdoc,
|
||||
pdoc: this.pdoc,
|
||||
tsdoc: this.tsdoc,
|
||||
udoc: this.udoc,
|
||||
attended: this.attended,
|
||||
path,
|
||||
};
|
||||
}
|
||||
}
|
||||
class ContestProblemSubmitHandler extends ContestProblemHandler {
|
||||
async get({ tid, pid }) {
|
||||
let rdocs = [];
|
||||
if (this.canShowRecord(this.tdoc))
|
||||
if (this.canShowRecord(this.tdoc)) {
|
||||
rdocs = await record.getUserInProblemMulti(this.user._id, this.pdoc._id, true)
|
||||
.sort({ _id: -1 }).limit(10).toArray();
|
||||
}
|
||||
this.response.template = 'problem_submit.html';
|
||||
let path = [
|
||||
const path = [
|
||||
['contest_main', '/c'],
|
||||
[this.tdoc.title, `/c/${tid}`, true],
|
||||
[this.pdoc.title, `/c/${tid}/p/${pid}`, true],
|
||||
['contest_detail_problem_submit', null]
|
||||
['contest_detail_problem_submit', null],
|
||||
];
|
||||
this.response.body = {
|
||||
tdoc: this.tdoc, pdoc: this.pdoc, tsdoc: this.tsdoc,
|
||||
udoc: this.udoc, attended: this.attend, path, rdocs
|
||||
tdoc: this.tdoc,
|
||||
pdoc: this.pdoc,
|
||||
tsdoc: this.tsdoc,
|
||||
udoc: this.udoc,
|
||||
attended: this.attend,
|
||||
path,
|
||||
rdocs,
|
||||
};
|
||||
}
|
||||
|
||||
async post({ tid, lang, code }) {
|
||||
await this.limitRate('add_record', 60, 100);
|
||||
let rid = await record.add({ pid: this.pdoc._id, uid: this.user._id, lang, code, tid: this.tdoc._id, hidden: true });
|
||||
const rid = await record.add({
|
||||
pid: this.pdoc._id, uid: this.user._id, lang, code, tid: this.tdoc._id, hidden: true,
|
||||
});
|
||||
await contest.updateStatus(this.tdoc._id, this.user._id, rid, this.pdoc._id, false, 0);
|
||||
if (!this.canShowRecord(this.tdoc)) {
|
||||
this.response.body = { tid };
|
||||
@@ -188,33 +220,37 @@ class ContestCreateHandler extends ContestHandler {
|
||||
async prepare() {
|
||||
this.checkPerm(PERM_CREATE_CONTEST);
|
||||
}
|
||||
|
||||
async get() {
|
||||
this.response.template = 'contest_edit.html';
|
||||
let rules = {};
|
||||
for (let i in contest.RULES)
|
||||
rules[i] = contest.RULES[i].TEXT;
|
||||
let now = new Date();
|
||||
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;
|
||||
let dt = new Date(ts);
|
||||
ts = ts - (ts % (15 * 60 * 1000)) + 15 * 60 * 1000;
|
||||
const dt = new Date(ts);
|
||||
this.response.body = {
|
||||
rules, page_name: 'contest_create',
|
||||
rules,
|
||||
page_name: 'contest_create',
|
||||
date_text: `${dt.getFullYear()}-${dt.getMonth() + 1}-${dt.getDate()}`,
|
||||
time_text: `${dt.getHours()}:${dt.getMinutes()}`,
|
||||
pids: '1000, 1001'
|
||||
pids: '1000, 1001',
|
||||
};
|
||||
}
|
||||
async post({ title, content, rule, beginAtDate, beginAtTime, duration, pids }) {
|
||||
let beginAt, endAt;
|
||||
|
||||
async post({
|
||||
title, content, rule, beginAtDate, beginAtTime, duration, pids,
|
||||
}) {
|
||||
let beginAt;
|
||||
try {
|
||||
beginAt = new Date(Date.parse(`${beginAtDate} ${beginAtTime.replace('-', ':')}`));
|
||||
} catch (e) {
|
||||
throw new ValidationError('beginAtDate', 'beginAtTime');
|
||||
}
|
||||
endAt = new Date(beginAt.getTime() + duration * 3600 * 1000);
|
||||
const endAt = new Date(beginAt.getTime() + duration * 3600 * 1000);
|
||||
if (beginAt >= endAt) throw new ValidationError('duration');
|
||||
pids = await this.verifyProblems(pids);
|
||||
let tid = await contest.add(title, content, this.user._id, rule, beginAt, endAt, pids);
|
||||
const tid = await contest.add(title, content, this.user._id, rule, beginAt, endAt, pids);
|
||||
this.response.body = { tid };
|
||||
this.response.redirect = `/c/${tid}`;
|
||||
}
|
||||
|
||||
+33
-25
@@ -1,16 +1,17 @@
|
||||
const
|
||||
{ PERM_JUDGE } = require('../permission'),
|
||||
record = require('../model/record'),
|
||||
problem = require('../model/problem'),
|
||||
bus = require('../service/bus'),
|
||||
queue = require('../service/queue'),
|
||||
{ Route, Handler, Connection, ConnectionHandler } = require('../service/server');
|
||||
const { PERM_JUDGE } = require('../permission');
|
||||
const record = require('../model/record');
|
||||
const problem = require('../model/problem');
|
||||
const bus = require('../service/bus');
|
||||
const queue = require('../service/queue');
|
||||
const {
|
||||
Route, Handler, Connection, ConnectionHandler,
|
||||
} = require('../service/server');
|
||||
|
||||
queue.assert('judge');
|
||||
|
||||
async function next(body) {
|
||||
let rdoc = await record.get(body.rid);
|
||||
let $set = {};
|
||||
const $set = {};
|
||||
if (body.case) {
|
||||
rdoc.cases.push(body.case);
|
||||
$set.cases = rdoc.cases;
|
||||
@@ -32,7 +33,7 @@ async function next(body) {
|
||||
}
|
||||
async function end(body) {
|
||||
let rdoc = await record.get(body.rid);
|
||||
let $set = {};
|
||||
const $set = {};
|
||||
if (body.case) {
|
||||
rdoc.cases.push(body.case);
|
||||
$set.cases = rdoc.cases;
|
||||
@@ -59,29 +60,33 @@ class JudgeHandler extends Handler {
|
||||
async prepare() {
|
||||
this.checkPerm(PERM_JUDGE);
|
||||
}
|
||||
|
||||
async get({ check = false }) {
|
||||
this.response.body = {};
|
||||
if (check) return;
|
||||
let rid = await queue.get('judge', false);
|
||||
const rid = await queue.get('judge', false);
|
||||
if (rid) {
|
||||
let rdoc = await record.get(rid);
|
||||
let pdoc = await problem.getById(rdoc.pid);
|
||||
let task = {
|
||||
const rdoc = await record.get(rid);
|
||||
const pdoc = await problem.getById(rdoc.pid);
|
||||
const task = {
|
||||
event: 'judge',
|
||||
rid, type: 0,
|
||||
rid,
|
||||
type: 0,
|
||||
pid: rdoc.pid,
|
||||
data: pdoc.data,
|
||||
lang: rdoc.lang,
|
||||
code: rdoc.code
|
||||
code: rdoc.code,
|
||||
};
|
||||
this.response.body = { task };
|
||||
}
|
||||
}
|
||||
async post_next() {
|
||||
|
||||
async postNext() {
|
||||
await next(this.request.body);
|
||||
this.response.body = {};
|
||||
}
|
||||
async post_end() {
|
||||
|
||||
async postEnd() {
|
||||
await end(this.request.body);
|
||||
this.response.body = {};
|
||||
}
|
||||
@@ -90,26 +95,29 @@ class JudgeConnectionHandler extends ConnectionHandler {
|
||||
async prepare() {
|
||||
this.checkPerm(PERM_JUDGE);
|
||||
}
|
||||
|
||||
async message(msg) {
|
||||
if (msg.key == 'next') await next(msg);
|
||||
else if (msg.key == 'end') {
|
||||
if (msg.key === 'next') await next(msg);
|
||||
else if (msg.key === 'end') {
|
||||
await end({ judger: this.user._id, ...msg });
|
||||
this.processing = null;
|
||||
let rid = await queue.get('judge');
|
||||
let rdoc = await record.get(rid);
|
||||
let pdoc = await problem.getById(rdoc.pid);
|
||||
let task = {
|
||||
const rid = await queue.get('judge');
|
||||
const rdoc = await record.get(rid);
|
||||
const pdoc = await problem.getById(rdoc.pid);
|
||||
const task = {
|
||||
event: 'judge',
|
||||
rid, type: 0,
|
||||
rid,
|
||||
type: 0,
|
||||
pid: rdoc.pid,
|
||||
data: pdoc.data,
|
||||
lang: rdoc.lang,
|
||||
code: rdoc.code
|
||||
code: rdoc.code,
|
||||
};
|
||||
this.send({ task });
|
||||
this.processing = task.rid;
|
||||
}
|
||||
}
|
||||
|
||||
async cleanup() {
|
||||
if (this.processing) {
|
||||
await record.reset(this.processing);
|
||||
|
||||
+121
-89
@@ -1,23 +1,26 @@
|
||||
const
|
||||
fs = require('fs'),
|
||||
paginate = require('../lib/paginate'),
|
||||
validator = require('../lib/validator'),
|
||||
problem = require('../model/problem'),
|
||||
record = require('../model/record'),
|
||||
user = require('../model/user'),
|
||||
solution = require('../model/solution'),
|
||||
system = require('../model/system'),
|
||||
{ Route, Handler } = require('../service/server'),
|
||||
gridfs = require('../service/gridfs'),
|
||||
queue = require('../service/queue'),
|
||||
{ NoProblemError, ProblemDataNotFoundError, BadRequestError,
|
||||
SolutionNotFoundError } = require('../error'),
|
||||
{ constants } = require('../options'),
|
||||
{ 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,
|
||||
PERM_REPLY_PROBLEM_SOLUTION } = require('../permission');
|
||||
const fs = require('fs');
|
||||
const paginate = require('../lib/paginate');
|
||||
const validator = require('../lib/validator');
|
||||
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 { Route, Handler } = require('../service/server');
|
||||
const gridfs = require('../service/gridfs');
|
||||
const queue = require('../service/queue');
|
||||
const {
|
||||
NoProblemError, ProblemDataNotFoundError, BadRequestError,
|
||||
SolutionNotFoundError,
|
||||
} = require('../error');
|
||||
const { constants } = require('../options');
|
||||
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,
|
||||
PERM_REPLY_PROBLEM_SOLUTION,
|
||||
} = require('../permission');
|
||||
|
||||
queue.assert('judge');
|
||||
|
||||
@@ -25,27 +28,30 @@ class ProblemHandler extends Handler {
|
||||
async _prepare() {
|
||||
this.checkPerm(PERM_VIEW_PROBLEM);
|
||||
}
|
||||
|
||||
async get({ page = 1, category = null }) {
|
||||
this.response.template = 'problem_main.html';
|
||||
let q = {};
|
||||
const q = {};
|
||||
if (category) q.category = category;
|
||||
if (!this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
|
||||
let pdocs = await problem.getMany(q, { pid: 1 }, page, constants.PROBLEM_PER_PAGE);
|
||||
const pdocs = await problem.getMany(q, { pid: 1 }, page, constants.PROBLEM_PER_PAGE);
|
||||
this.response.body = {
|
||||
path: [
|
||||
['Hydro', '/'],
|
||||
['problem_main', null]
|
||||
['problem_main', null],
|
||||
],
|
||||
page, pdocs, category: ''
|
||||
page,
|
||||
pdocs,
|
||||
category: '',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class ProblemRandomHandler extends ProblemHandler {
|
||||
async get() {
|
||||
let q = {};
|
||||
const q = {};
|
||||
if (!this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
|
||||
let pid = await problem.random(q);
|
||||
const pid = await problem.random(q);
|
||||
if (!pid) throw new NoProblemError();
|
||||
this.response.body = { pid };
|
||||
}
|
||||
@@ -57,19 +63,20 @@ class ProblemDetailHandler extends ProblemHandler {
|
||||
this.uid = this.user._id;
|
||||
this.pid = pid;
|
||||
if (pid) this.pdoc = await problem.get(pid, this.uid);
|
||||
if (this.pdoc.hidden && this.pdoc.owner != this.uid) this.checkPerm(PERM_VIEW_PROBLEM_HIDDEN);
|
||||
if (this.pdoc.hidden && this.pdoc.owner !== this.uid) this.checkPerm(PERM_VIEW_PROBLEM_HIDDEN);
|
||||
if (this.pdoc) this.udoc = await user.getById(this.pdoc.owner);
|
||||
this.response.body = {
|
||||
pdoc: this.pdoc,
|
||||
udoc: this.udoc,
|
||||
title: (this.pdoc || {}).title || ''
|
||||
title: (this.pdoc || {}).title || '',
|
||||
};
|
||||
}
|
||||
|
||||
async get() {
|
||||
this.response.body.path = [
|
||||
['Hydro', '/'],
|
||||
['problem_main', '/p'],
|
||||
[this.pdoc.title, null, true]
|
||||
[this.pdoc.title, null, true],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -78,26 +85,31 @@ class ProblemSubmitHandler extends ProblemDetailHandler {
|
||||
async prepare() {
|
||||
this.checkPerm(PERM_SUBMIT_PROBLEM);
|
||||
}
|
||||
|
||||
async get() {
|
||||
this.response.template = 'problem_submit.html';
|
||||
let rdocs = await record.getUserInProblemMulti(this.uid, this.pdoc._id).sort({ _id: -1 }).limit(10).toArray();
|
||||
const rdocs = await record.getUserInProblemMulti(this.uid, this.pdoc._id)
|
||||
.sort({ _id: -1 })
|
||||
.limit(10)
|
||||
.toArray();
|
||||
this.response.body = {
|
||||
path: [
|
||||
['Hydro', '/'],
|
||||
['problem_main', '/p'],
|
||||
[this.pdoc.title, `/p/${this.pid}`, true],
|
||||
['problem_submit', null]
|
||||
['problem_submit', null],
|
||||
],
|
||||
pdoc: this.pdoc,
|
||||
udoc: this.udoc,
|
||||
rdocs,
|
||||
title: this.pdoc.title
|
||||
title: this.pdoc.title,
|
||||
};
|
||||
}
|
||||
|
||||
async post() {
|
||||
let { lang, code } = this.request.body;
|
||||
let rid = await record.add({
|
||||
uid: this.uid, lang, code, pid: this.pdoc._id
|
||||
const { lang, code } = this.request.body;
|
||||
const rid = await record.add({
|
||||
uid: this.uid, lang, code, pid: this.pdoc._id,
|
||||
});
|
||||
await queue.push('judge', rid);
|
||||
this.user.nSubmit++;
|
||||
@@ -108,7 +120,7 @@ class ProblemSubmitHandler extends ProblemDetailHandler {
|
||||
|
||||
class ProblemManageHandler extends ProblemDetailHandler {
|
||||
async prepare() {
|
||||
if (this.pdoc.owner != this.uid) this.checkPerm(PERM_EDIT_PROBLEM);
|
||||
if (this.pdoc.owner !== this.uid) this.checkPerm(PERM_EDIT_PROBLEM);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,9 +131,10 @@ class ProblemSettingsHandler extends ProblemManageHandler {
|
||||
['Hydro', '/'],
|
||||
['problem_main', '/p'],
|
||||
[this.pdoc.title, `/p/${this.pid}`, true],
|
||||
['problem_settings', null]
|
||||
['problem_settings', null],
|
||||
];
|
||||
}
|
||||
|
||||
async post() {
|
||||
// TODO(masnn)
|
||||
this.back();
|
||||
@@ -135,43 +148,45 @@ class ProblemEditHandler extends ProblemManageHandler {
|
||||
['Hydro', '/'],
|
||||
['problem_main', '/p'],
|
||||
[this.pdoc.title, `/p/${this.request.params.pid}`, true],
|
||||
['problem_edit', null]
|
||||
['problem_edit', null],
|
||||
];
|
||||
this.response.body.page_name = 'problem_edit';
|
||||
}
|
||||
|
||||
async post({ title, content }) {
|
||||
let pid = validator.checkPid(this.request.body.pid);
|
||||
let pdoc = await problem.get(this.params.pid);
|
||||
const pid = validator.checkPid(this.request.body.pid);
|
||||
const pdoc = await problem.get(this.params.pid);
|
||||
await problem.edit(pdoc._id, { title, content, pid });
|
||||
this.response.redirect = `/p/${pid}`;
|
||||
}
|
||||
}
|
||||
|
||||
class ProblemDataUploadHandler extends ProblemManageHandler {
|
||||
async prepare(){
|
||||
async prepare() {
|
||||
this.response.template = 'problem_upload.html';
|
||||
}
|
||||
|
||||
async get() {
|
||||
if (this.pdoc.data && typeof this.pdoc.data == 'object') {
|
||||
let files = await gridfs.find({ _id: this.pdoc.data }).toArray();
|
||||
if (this.pdoc.data && typeof this.pdoc.data === 'object') {
|
||||
const files = await gridfs.find({ _id: this.pdoc.data }).toArray();
|
||||
this.md5 = files[0].md5;
|
||||
}
|
||||
this.response.body.md5 = this.md5;
|
||||
}
|
||||
|
||||
async post() {
|
||||
if (!this.request.files.file) throw new BadRequestError();
|
||||
const r = fs.createReadStream(this.request.files.file.path);
|
||||
let f = gridfs.openUploadStream('data.zip');
|
||||
const f = gridfs.openUploadStream('data.zip');
|
||||
await new Promise((resolve, reject) => {
|
||||
r.pipe(f);
|
||||
f.once('finish', resolve);
|
||||
f.once('error', reject);
|
||||
});
|
||||
if (this.pdoc.data && typeof this.pdoc.data == 'object')
|
||||
gridfs.delete(this.pdoc.data);
|
||||
if (this.pdoc.data && typeof this.pdoc.data === 'object') { gridfs.delete(this.pdoc.data); }
|
||||
this.pdoc = await problem.edit(this.pdoc._id, { data: f.id });
|
||||
if (this.pdoc.data && typeof this.pdoc.data == 'object') {
|
||||
let files = await gridfs.find({ _id: this.pdoc.data }).toArray();
|
||||
if (this.pdoc.data && typeof this.pdoc.data === 'object') {
|
||||
const files = await gridfs.find({ _id: this.pdoc.data }).toArray();
|
||||
this.md5 = files[0].md5;
|
||||
}
|
||||
this.response.body.md5 = this.md5;
|
||||
@@ -180,9 +195,9 @@ class ProblemDataUploadHandler extends ProblemManageHandler {
|
||||
|
||||
class ProblemDataDownloadHandler extends ProblemDetailHandler {
|
||||
async get({ pid }) {
|
||||
if (this.uid != this.pdoc.owner) this.checkPerm([PERM_READ_PROBLEM_DATA, PERM_JUDGE]);
|
||||
if (this.uid !== this.pdoc.owner) this.checkPerm([PERM_READ_PROBLEM_DATA, PERM_JUDGE]);
|
||||
if (!this.pdoc.data) throw new ProblemDataNotFoundError(pid);
|
||||
else if (typeof this.pdoc.data == 'string') this.ctx.setRedirect = this.pdoc.data.split('from:')[1];
|
||||
else if (typeof this.pdoc.data === 'string') [, this.ctx.setRedirect] = this.pdoc.data.split('from:');
|
||||
this.response.attachment(`${this.pdoc.title}.zip`);
|
||||
this.response.body = gridfs.openDownloadStream(this.pdoc.data);
|
||||
}
|
||||
@@ -192,70 +207,81 @@ class ProblemSolutionHandler extends ProblemDetailHandler {
|
||||
async get({ page = 1 }) {
|
||||
this.response.template = 'problem_solution.html';
|
||||
this.checkPerm(PERM_VIEW_PROBLEM_SOLUTION);
|
||||
let [psdocs, pcount, pscount] = await paginate(solution.getMulti(this.pdoc._id), page, constants.SOLUTION_PER_PAGE);
|
||||
let uids = [this.pdoc.owner], docids = [];
|
||||
for (let psdoc of psdocs) {
|
||||
const [psdocs, pcount, pscount] = await paginate(
|
||||
solution.getMulti(this.pdoc._id),
|
||||
page, constants.SOLUTION_PER_PAGE,
|
||||
);
|
||||
const uids = [this.pdoc.owner]; const
|
||||
docids = [];
|
||||
for (const psdoc of psdocs) {
|
||||
docids.push(psdoc._id);
|
||||
uids.push(psdoc.owner);
|
||||
if (psdoc.reply.length)
|
||||
for (let psrdoc of psdoc.reply)
|
||||
uids.push(psrdoc.owner);
|
||||
if (psdoc.reply.length) { for (const psrdoc of psdoc.reply) uids.push(psrdoc.owner); }
|
||||
}
|
||||
let udict = await user.getList(uids);
|
||||
let pssdict = solution.getListStatus(docids, this.uid);
|
||||
let path = [
|
||||
const udict = await user.getList(uids);
|
||||
const pssdict = solution.getListStatus(docids, this.uid);
|
||||
const path = [
|
||||
['problem_main', '/p'],
|
||||
[this.pdoc.title, `/p/${this.pdoc.pid}`, true],
|
||||
['problem_solution', null]
|
||||
['problem_solution', null],
|
||||
];
|
||||
this.response.body = {
|
||||
path, psdocs, page, pcount, pscount, udict, pssdict
|
||||
path, psdocs, page, pcount, pscount, udict, pssdict,
|
||||
};
|
||||
}
|
||||
|
||||
async post({ psid }) {
|
||||
if (psid) this.psdoc = await solution.get(psid);
|
||||
}
|
||||
async post_submit({ content }) {
|
||||
|
||||
async postSubmit({ content }) {
|
||||
this.checkPerm(PERM_CREATE_PROBLEM_SOLUTION);
|
||||
await solution.add(this.pdoc._id, this.uid, content);
|
||||
this.back();
|
||||
}
|
||||
async post_edit_solution({ content }) {
|
||||
if (this.psdoc.owner != this.uid) this.checkPerm(PERM_EDIT_PROBLEM_SOLUTION);
|
||||
|
||||
async postEditSolution({ content }) {
|
||||
if (this.psdoc.owner !== this.uid) this.checkPerm(PERM_EDIT_PROBLEM_SOLUTION);
|
||||
this.psdoc = await solution.edit(this.psdoc._id, content);
|
||||
this.ctx.body.psdoc = this.psdoc;
|
||||
this.back();
|
||||
}
|
||||
async post_delete_solution() {
|
||||
if (this.psdoc.owner != this.uid) this.checkPerm(PERM_DELETE_PROBLEM_SOLUTION);
|
||||
|
||||
async postDeleteSolution() {
|
||||
if (this.psdoc.owner !== this.uid) this.checkPerm(PERM_DELETE_PROBLEM_SOLUTION);
|
||||
await solution.del(this.psdoc._id);
|
||||
this.back();
|
||||
}
|
||||
async post_reply({ psid, content }) {
|
||||
|
||||
async postReply({ psid, content }) {
|
||||
this.checkPerm(PERM_REPLY_PROBLEM_SOLUTION);
|
||||
let psdoc = await solution.get(psid);
|
||||
const psdoc = await solution.get(psid);
|
||||
await solution.reply(psdoc._id, this.uid, content);
|
||||
}
|
||||
async post_edit_reply({ content, psid, psrid }) {
|
||||
let [psdoc, psrdoc] = await solution.getReply(psid, psrid);
|
||||
if ((!psdoc) || psdoc.pid != this.pdoc._id) throw new SolutionNotFoundError(psid);
|
||||
if (psrdoc.owner != this.uid) this.checkPerm(PERM_EDIT_PROBLEM_SOLUTION_REPLY);
|
||||
|
||||
async postEditReply({ content, psid, psrid }) {
|
||||
const [psdoc, psrdoc] = await solution.getReply(psid, psrid);
|
||||
if ((!psdoc) || psdoc.pid !== this.pdoc._id) throw new SolutionNotFoundError(psid);
|
||||
if (psrdoc.owner !== this.uid) this.checkPerm(PERM_EDIT_PROBLEM_SOLUTION_REPLY);
|
||||
await solution.editReply(psid, psrid, content);
|
||||
}
|
||||
async post_delete_reply({ psid, psrid }) {
|
||||
let [psdoc, psrdoc] = await solution.getReply(psid, psrid);
|
||||
if ((!psdoc) || psdoc.pid != this.pdoc._id) throw new SolutionNotFoundError(psid);
|
||||
if (psrdoc.owner != this.uid) this.checkPerm(PERM_EDIT_PROBLEM_SOLUTION_REPLY);
|
||||
|
||||
async postDeleteReply({ psid, psrid }) {
|
||||
const [psdoc, psrdoc] = await solution.getReply(psid, psrid);
|
||||
if ((!psdoc) || psdoc.pid !== this.pdoc._id) throw new SolutionNotFoundError(psid);
|
||||
if (psrdoc.owner !== this.uid) this.checkPerm(PERM_EDIT_PROBLEM_SOLUTION_REPLY);
|
||||
await solution.delReply(psid, psrid);
|
||||
this.back();
|
||||
}
|
||||
async post_upvote() {
|
||||
let [psdoc, pssdoc] = await solution.vote(this.psdoc._id, this.uid, 1);
|
||||
|
||||
async postUpvote() {
|
||||
const [psdoc, pssdoc] = await solution.vote(this.psdoc._id, this.uid, 1);
|
||||
this.response.body = { vote: psdoc.vote, user_vote: pssdoc.vote };
|
||||
if (!this.preferJson) this.back();
|
||||
}
|
||||
async post_downvote() {
|
||||
let [psdoc, pssdoc] = await solution.vote(this.psdoc._id, this.uid, -1);
|
||||
|
||||
async postDownvote() {
|
||||
const [psdoc, pssdoc] = await solution.vote(this.psdoc._id, this.uid, -1);
|
||||
this.response.body = { vote: psdoc.vote, user_vote: pssdoc.vote };
|
||||
if (!this.preferJson) this.back();
|
||||
}
|
||||
@@ -264,7 +290,7 @@ class ProblemSolutionHandler extends ProblemDetailHandler {
|
||||
class ProblemSolutionRawHandler extends ProblemDetailHandler {
|
||||
async get({ psid }) {
|
||||
this.checkPerm(PERM_VIEW_PROBLEM_SOLUTION);
|
||||
let psdoc = await solution.get(psid);
|
||||
const psdoc = await solution.get(psid);
|
||||
this.response.type = 'text/markdown';
|
||||
this.response.body = psdoc.content;
|
||||
}
|
||||
@@ -273,7 +299,7 @@ class ProblemSolutionRawHandler extends ProblemDetailHandler {
|
||||
class ProblemSolutionReplyRawHandler extends ProblemDetailHandler {
|
||||
async get({ psid }) {
|
||||
this.checkPerm(PERM_VIEW_PROBLEM_SOLUTION);
|
||||
let [psdoc, psrdoc] = await solution.getReply(psid);
|
||||
const [psdoc, psrdoc] = await solution.getReply(psid);
|
||||
if ((!psdoc) || psdoc.pid != this.pdoc._id) throw new SolutionNotFoundError(psid);
|
||||
this.response.type = 'text/markdown';
|
||||
this.response.body = psrdoc.content;
|
||||
@@ -288,14 +314,20 @@ class ProblemCreateHandler extends Handler {
|
||||
path: [
|
||||
['Hydro', '/'],
|
||||
['problem_main', '/p'],
|
||||
['problem_create', null]
|
||||
], page_name: 'problem_create'
|
||||
['problem_create', null],
|
||||
],
|
||||
page_name: 'problem_create',
|
||||
};
|
||||
}
|
||||
async post({ title, pid, content, hidden }) {
|
||||
|
||||
async post({
|
||||
title, pid, content, hidden,
|
||||
}) {
|
||||
validator.checkPid(pid);
|
||||
pid = pid || await system.incPidCounter();
|
||||
await problem.add({ title, content, owner: this.user._id, pid, hidden });
|
||||
await problem.add({
|
||||
title, content, owner: this.user._id, pid, hidden,
|
||||
});
|
||||
this.response.body = { pid };
|
||||
this.response.redirect = `/p/${pid}/settings`;
|
||||
}
|
||||
@@ -322,7 +354,7 @@ class ProblemPretestHandler(base.Handler):
|
||||
@base.requirePerm(builtin.PERM_SUBMIT_PROBLEM)
|
||||
@base.route_argument
|
||||
@base.post_argument
|
||||
@base.require_csrf_token
|
||||
@base.require_csrfToken
|
||||
@base.sanitize
|
||||
@base.limitRate('add_record', 60, 100)
|
||||
async def post(self, *, pid: document.convert_doc_id, lang: str, code: str,
|
||||
@@ -379,7 +411,7 @@ class ProblemPretestConnection(record_handler.RecordVisibilityMixin, base.Connec
|
||||
@base.requirePriv(builtin.PRIV_USER_PROFILE)
|
||||
@base.route_argument
|
||||
@base.post_argument
|
||||
@base.require_csrf_token
|
||||
@base.require_csrfToken
|
||||
@base.sanitize
|
||||
async def post(self, *, pid: document.convert_doc_id, title: str, content: str):
|
||||
pdoc = await problem.get(self.domainId, pid)
|
||||
@@ -408,4 +440,4 @@ class ProblemStatisticsHandler(base.Handler):
|
||||
self.render('problem_statistics.html', pdoc=pdoc, udoc=udoc, dudoc=dudoc,
|
||||
page_title=pdoc['title'], path_components=path_components)
|
||||
|
||||
*/
|
||||
*/
|
||||
+57
-34
@@ -1,53 +1,71 @@
|
||||
const
|
||||
{ constants } = require('../options'),
|
||||
{ PERM_READ_RECORD_CODE, PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD,
|
||||
PERM_REJUDGE, PERM_VIEW_PROBLEM_HIDDEN } = require('../permission'),
|
||||
problem = require('../model/problem'),
|
||||
record = require('../model/record'),
|
||||
contest = require('../model/contest'),
|
||||
user = require('../model/user'),
|
||||
bus = require('../service/bus'),
|
||||
queue = require('../service/queue'),
|
||||
{ Route, Handler, Connection, ConnectionHandler } = require('../service/server');
|
||||
const { constants } = require('../options');
|
||||
const {
|
||||
PERM_READ_RECORD_CODE, PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD,
|
||||
PERM_REJUDGE, PERM_VIEW_PROBLEM_HIDDEN,
|
||||
} = require('../permission');
|
||||
const problem = require('../model/problem');
|
||||
const record = require('../model/record');
|
||||
const contest = require('../model/contest');
|
||||
const user = require('../model/user');
|
||||
const bus = require('../service/bus');
|
||||
const queue = require('../service/queue');
|
||||
const {
|
||||
Route, Handler, Connection, ConnectionHandler,
|
||||
} = require('../service/server');
|
||||
|
||||
class RecordListHandler extends Handler {
|
||||
async get({ page = 1 }) {
|
||||
this.response.template = 'record_main.html';
|
||||
let q = {};
|
||||
let rdocs = await record.getMany(q, { _id: -1 }, page, constants.RECORD_PER_PAGE);
|
||||
let pdict = {}, udict = {};
|
||||
for (let rdoc of rdocs) {
|
||||
udict[rdoc.uid] = await user.getById(rdoc.uid);
|
||||
pdict[rdoc.pid] = await problem.get(rdoc.pid, this.user._id);
|
||||
const q = {};
|
||||
const rdocs = await record.getMany(q, { _id: -1 }, page, constants.RECORD_PER_PAGE);
|
||||
const pdict = {};
|
||||
const udict = {};
|
||||
const ulist = [];
|
||||
const plist = [];
|
||||
for (const rdoc of rdocs) {
|
||||
ulist[rdoc.uid] = user.getById(rdoc.uid);
|
||||
plist[rdoc.pid] = problem.get(rdoc.pid, this.user._id);
|
||||
}
|
||||
await Promise.all(ulist);
|
||||
await Promise.all(plist);
|
||||
for (const udoc of ulist) {
|
||||
udict[udoc._id] = udoc;
|
||||
}
|
||||
for (const pdoc of plist) {
|
||||
pdict[pdoc._id] = pdoc;
|
||||
}
|
||||
this.response.body = {
|
||||
path: [
|
||||
['Hydro', '/'],
|
||||
['record_main', null]
|
||||
['record_main', null],
|
||||
],
|
||||
page, rdocs, pdict, udict
|
||||
page,
|
||||
rdocs,
|
||||
pdict,
|
||||
udict,
|
||||
};
|
||||
}
|
||||
}
|
||||
class RecordDetailHandler extends Handler {
|
||||
async get({ rid }) {
|
||||
this.response.template = 'record_detail.html';
|
||||
let rdoc = await record.get(rid);
|
||||
const rdoc = await record.get(rid);
|
||||
if (rdoc.hidden) this.checkPerm(PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
|
||||
if (rdoc.uid != this.user.uid && !this.user.hasPerm(PERM_READ_RECORD_CODE)) rdoc.code = null;
|
||||
if (rdoc.uid !== this.user.uid && !this.user.hasPerm(PERM_READ_RECORD_CODE)) rdoc.code = null;
|
||||
this.response.body = {
|
||||
path: [
|
||||
['Hydro', '/'],
|
||||
['record_detail', null]
|
||||
['record_detail', null],
|
||||
],
|
||||
rdoc, show_status: true
|
||||
rdoc,
|
||||
show_status: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
class RecordRejudgeHandler extends Handler {
|
||||
async post({ rid }) {
|
||||
this.checkPerm(PERM_REJUDGE);
|
||||
let rdoc = await record.get(rid);
|
||||
const rdoc = await record.get(rid);
|
||||
if (rdoc) {
|
||||
await record.reset(rid);
|
||||
await queue.push('judge', rid);
|
||||
@@ -59,18 +77,21 @@ class RecordConnectionHandler extends ConnectionHandler {
|
||||
async prepare() {
|
||||
bus.subscribe(['record_change'], this.onRecordChange);
|
||||
}
|
||||
|
||||
async message(msg) {
|
||||
for (let rid of msg.rids) {
|
||||
let rdoc = await record.get(rid);
|
||||
await this.onRecordChange({ value: rdoc });
|
||||
for (const rid of msg.rids) {
|
||||
const rdoc = await record.get(rid); // eslint-disable-line no-await-in-loop
|
||||
await this.onRecordChange({ value: rdoc }); // eslint-disable-line no-await-in-loop
|
||||
}
|
||||
}
|
||||
|
||||
async cleanup() {
|
||||
bus.unsubscribe(['record_change'], this.onRecordChange);
|
||||
}
|
||||
|
||||
async onRecordChange(data) {
|
||||
let rdoc = data.value;
|
||||
if (rdoc.tid && rdoc.tid.toString() != this.tid) return;
|
||||
const rdoc = data.value;
|
||||
if (rdoc.tid && rdoc.tid.toString() !== this.tid) return;
|
||||
let [udoc, pdoc] = await Promise.all([user.getById(rdoc.uid), problem.getById(rdoc.pid)]);
|
||||
if (pdoc.hidden && !this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) pdoc = null;
|
||||
this.send({ html: await this.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) });
|
||||
@@ -78,9 +99,9 @@ class RecordConnectionHandler extends ConnectionHandler {
|
||||
}
|
||||
class RecordDetailConnectionHandler extends contest.ContestHandlerMixin(ConnectionHandler) {
|
||||
async prepare({ rid }) {
|
||||
let rdoc = await record.get(rid);
|
||||
const rdoc = await record.get(rid);
|
||||
if (rdoc.tid) {
|
||||
let tdoc = await contest.get(rdoc.tid);
|
||||
const tdoc = await contest.get(rdoc.tid);
|
||||
if (!this.canShowRecord(tdoc)) {
|
||||
this.close();
|
||||
return;
|
||||
@@ -90,14 +111,16 @@ class RecordDetailConnectionHandler extends contest.ContestHandlerMixin(Connecti
|
||||
bus.subscribe(['record_change'], this.onRecordChange);
|
||||
this.onRecordChange({ value: rdoc });
|
||||
}
|
||||
|
||||
async onRecordChange(data) {
|
||||
let rdoc = data.value;
|
||||
if (rdoc._id.toString() != this.rid) return;
|
||||
const rdoc = data.value;
|
||||
if (rdoc._id.toString() !== this.rid) return;
|
||||
this.send({
|
||||
status_html: await this.renderHTML('record_detail_status.html', { rdoc }),
|
||||
summary_html: await this.renderHTML('record_detail_summary.html', { rdoc })
|
||||
summary_html: await this.renderHTML('record_detail_summary.html', { rdoc }),
|
||||
});
|
||||
}
|
||||
|
||||
async cleanup() {
|
||||
bus.unsubscribe(['record_change'], this.onRecordChange);
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
const
|
||||
opcount = require('../model/opcount'),
|
||||
{ PermissionError } = require('../error');
|
||||
|
||||
module.exports = {
|
||||
requirePerm(perm) {
|
||||
return async (ctx, next) => {
|
||||
for (let i in arguments) {
|
||||
if (arguments[i] instanceof Array) {
|
||||
let p = false;
|
||||
for (let j in arguments)
|
||||
if (ctx.state.user.hasPerm(arguments[i][j])) {
|
||||
p = true;
|
||||
break;
|
||||
}
|
||||
if (!p) throw new PermissionError([arguments[i]]);
|
||||
} else {
|
||||
if (ctx.state.user.hasPerm(arguments[i])) continue;
|
||||
else throw new PermissionError([arguments[i]]);
|
||||
}
|
||||
}
|
||||
await next();
|
||||
};
|
||||
},
|
||||
|
||||
};
|
||||
+61
-39
@@ -1,22 +1,24 @@
|
||||
const
|
||||
{ Route, Handler } = require('../service/server.js'),
|
||||
user = require('../model/user'),
|
||||
token = require('../model/token'),
|
||||
system = require('../model/system'),
|
||||
{ sendMail } = require('../lib/mail'),
|
||||
misc = require('../lib/misc'),
|
||||
validator = require('../lib/validator'),
|
||||
options = require('../options'),
|
||||
{ PERM_REGISTER_USER, PERM_LOGGEDIN } = require('../permission'),
|
||||
{ UserAlreadyExistError, InvalidTokenError, VerifyPasswordError,
|
||||
UserNotFoundError, LoginError, SystemError } = require('../error');
|
||||
const { Route, Handler } = require('../service/server.js');
|
||||
const user = require('../model/user');
|
||||
const token = require('../model/token');
|
||||
const system = require('../model/system');
|
||||
const { sendMail } = require('../lib/mail');
|
||||
const misc = require('../lib/misc');
|
||||
const validator = require('../lib/validator');
|
||||
const options = require('../options');
|
||||
const { PERM_REGISTER_USER, PERM_LOGGEDIN } = require('../permission');
|
||||
const {
|
||||
UserAlreadyExistError, InvalidTokenError, VerifyPasswordError,
|
||||
UserNotFoundError, LoginError, SystemError,
|
||||
} = require('../error');
|
||||
|
||||
class UserLoginHandler extends Handler {
|
||||
async get() {
|
||||
this.response.template = 'user_login.html';
|
||||
}
|
||||
|
||||
async post({ uname, password, rememberme = false }) {
|
||||
let udoc = await user.getByUname(uname);
|
||||
const udoc = await user.getByUname(uname);
|
||||
if (!udoc) throw new LoginError(uname);
|
||||
if (udoc) udoc.checkPassword(password);
|
||||
await user.setById(udoc._id, { loginat: new Date(), loginip: this.request.ip });
|
||||
@@ -25,7 +27,7 @@ class UserLoginHandler extends Handler {
|
||||
this.session.uid = udoc._id;
|
||||
this.session.rememberme = rememberme;
|
||||
this.response.body = {};
|
||||
let referer = this.request.headers.referer || '/';
|
||||
const referer = this.request.headers.referer || '/';
|
||||
this.response.redirect = referer.endsWith('/login') ? '/' : referer;
|
||||
}
|
||||
}
|
||||
@@ -34,9 +36,11 @@ class UserLogoutHandler extends Handler {
|
||||
super(ctx);
|
||||
this.checkPerm(PERM_LOGGEDIN);
|
||||
}
|
||||
|
||||
async get() {
|
||||
this.response.template = 'user_logout.html';
|
||||
}
|
||||
|
||||
async post() {
|
||||
this.session = { uid: 1 };
|
||||
this.response.body = {};
|
||||
@@ -47,16 +51,22 @@ class UserRegisterHandler extends Handler {
|
||||
super(ctx);
|
||||
this.checkPerm(PERM_REGISTER_USER);
|
||||
}
|
||||
|
||||
async get() {
|
||||
this.response.template = 'user_register.html';
|
||||
}
|
||||
|
||||
async post({ mail }) {
|
||||
validator.checkEmail(mail);
|
||||
if (await user.getByEmail(mail, true)) throw new UserAlreadyExistError(mail);
|
||||
this.limitRate('send_mail', 3600, 30);
|
||||
let t = await token.add(token.TYPE_REGISTRATION, options.registration_token_expire_seconds, { mail });
|
||||
const t = await token.add(
|
||||
token.TYPE_REGISTRATION,
|
||||
options.registration_token_expire_seconds,
|
||||
{ mail },
|
||||
);
|
||||
if (options.smtp.user) {
|
||||
let m = await this.renderHTML('user_register_mail', { url: `/register/${t}` });
|
||||
const m = await this.renderHTML('user_register_mail', { url: `/register/${t}` });
|
||||
await sendMail(mail, 'Sign Up', 'user_register_mail', m);
|
||||
this.response.body = {};
|
||||
this.response.template = 'user_register_mail_sent.html';
|
||||
@@ -70,18 +80,24 @@ class UserRegisterWithCodeHandler extends Handler {
|
||||
super(ctx);
|
||||
this.checkPerm(PERM_REGISTER_USER);
|
||||
}
|
||||
|
||||
async get({ code }) {
|
||||
this.response.template = 'user_register_with_code.html';
|
||||
let { mail } = await token.get(code, token.TYPE_REGISTRATION);
|
||||
const { mail } = await token.get(code, token.TYPE_REGISTRATION);
|
||||
if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code);
|
||||
this.response.body = { mail };
|
||||
}
|
||||
async post({ code, password, verify_password, uname }) {
|
||||
let { mail } = await token.get(code, token.TYPE_REGISTRATION);
|
||||
|
||||
async post({
|
||||
code, password, verifyPassword, uname,
|
||||
}) {
|
||||
const { mail } = await token.get(code, token.TYPE_REGISTRATION);
|
||||
if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code);
|
||||
if (password != verify_password) throw new VerifyPasswordError();
|
||||
let uid = await system.incUserCounter();
|
||||
await user.create({ uid, uname, password, mail, regip: this.request.ip });
|
||||
if (password !== verifyPassword) throw new VerifyPasswordError();
|
||||
const uid = await system.incUserCounter();
|
||||
await user.create({
|
||||
uid, uname, password, mail, regip: this.request.ip,
|
||||
});
|
||||
await token.delete(code, token.TYPE_REGISTRATION);
|
||||
this.session.uid = uid;
|
||||
this.response.body = {};
|
||||
@@ -93,19 +109,21 @@ class UserLostPassHandler extends Handler {
|
||||
if (!options.smtp.user) throw new SystemError('Cannot send mail');
|
||||
super(ctx);
|
||||
}
|
||||
|
||||
async get() {
|
||||
this.response.template = 'user_lostpass.html';
|
||||
}
|
||||
|
||||
async post({ mail }) {
|
||||
validator.checkEmail(mail);
|
||||
let udoc = await user.getByEmail(mail);
|
||||
const udoc = await user.getByEmail(mail);
|
||||
if (!udoc) throw new UserNotFoundError(mail);
|
||||
let tid = await token.add(
|
||||
const tid = await token.add(
|
||||
token.TYPE_LOSTPASS,
|
||||
options.lostpass_token_expire_seconds,
|
||||
{ uid: udoc._id }
|
||||
{ uid: udoc._id },
|
||||
);
|
||||
let m = await this.renderHTML('user_lostpass_mail', { url: `/lostpass/${tid}`, uname: udoc.uname });
|
||||
const m = await this.renderHTML('user_lostpass_mail', { url: `/lostpass/${tid}`, uname: udoc.uname });
|
||||
await sendMail(mail, 'Lost Password', 'user_lostpass_mail', m);
|
||||
this.response.body = {};
|
||||
this.response.template = 'user_lostpass_mail_sent.html';
|
||||
@@ -113,15 +131,16 @@ class UserLostPassHandler extends Handler {
|
||||
}
|
||||
class UserLostPassWithCodeHandler extends Handler {
|
||||
async get({ code }) {
|
||||
let tdoc = await token.get(code, token.TYPE_LOSTPASS);
|
||||
const tdoc = await token.get(code, token.TYPE_LOSTPASS);
|
||||
if (!tdoc) throw new InvalidTokenError(token.TYPE_LOSTPASS, code);
|
||||
let udoc = await user.getById(tdoc.uid);
|
||||
const udoc = await user.getById(tdoc.uid);
|
||||
this.response.body = { uname: udoc.uname };
|
||||
}
|
||||
async post({ code, password, verify_password }) {
|
||||
let tdoc = await token.get(code, token.TYPE_LOSTPASS);
|
||||
|
||||
async post({ code, password, verifyPassword }) {
|
||||
const tdoc = await token.get(code, token.TYPE_LOSTPASS);
|
||||
if (!tdoc) throw new InvalidTokenError(token.TYPE_LOSTPASS, code);
|
||||
if (password != verify_password) throw new VerifyPasswordError();
|
||||
if (password !== verifyPassword) throw new VerifyPasswordError();
|
||||
await user.setPassword(tdoc.uid, password);
|
||||
await token.delete(code, token.TYPE_LOSTPASS);
|
||||
this.response.redirect = '/';
|
||||
@@ -132,28 +151,31 @@ class UserDetailHandler extends Handler {
|
||||
super(ctx);
|
||||
this.response.template = 'user_detail.html';
|
||||
}
|
||||
|
||||
async get({ uid }) {
|
||||
let isSelfProfile = this.ctx.state.user._id == uid;
|
||||
let udoc = await user.getById(uid);
|
||||
const isSelfProfile = this.ctx.state.user._id === uid;
|
||||
const udoc = await user.getById(uid);
|
||||
if (!udoc) throw new UserNotFoundError(uid);
|
||||
let sdoc = await token.getMostRecentSessionByUid(uid);
|
||||
const sdoc = await token.getMostRecentSessionByUid(uid);
|
||||
this.ctx.body = { isSelfProfile, udoc, sdoc };
|
||||
}
|
||||
}
|
||||
class UserSearchHandler extends Handler {
|
||||
async get({ q, exact_match = false }) {
|
||||
async get({ q, exactMatch = false }) {
|
||||
let udocs;
|
||||
if (exact_match) udocs = [];
|
||||
if (exactMatch) udocs = [];
|
||||
else udocs = await user.getPrefixList(q, 20);
|
||||
try {
|
||||
let udoc = await user.getById(parseInt(q));
|
||||
const udoc = await user.getById(parseInt(q));
|
||||
if (udoc) udocs.insert(0, udoc);
|
||||
} catch (e) {
|
||||
/* Ignore */
|
||||
}
|
||||
for (let i in udocs)
|
||||
if (udocs[i].gravatar)
|
||||
for (const i in udocs) {
|
||||
if (udocs[i].gravatar) {
|
||||
udocs[i].gravatar_url = misc.gravatar_url[udocs[i].gravatar];
|
||||
}
|
||||
}
|
||||
this.ctx.body = { udocs };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user