优化结构

This commit is contained in:
undefined committed 2020-04-30 18:07:35 +08:00
1 parent 75ebc1a746
commit 2554cf660a
34 files changed
+401 -150

No files matched your search

+14 -14
View File
@@ -9,7 +9,10 @@ process.stdin.on('data', async (input) => {
}
});
global.Hydro = {};
global.Hydro = {
handler: {},
template: {}
};
require('./lib/i18n');
require('./utils');
@@ -31,21 +34,18 @@ async function run() {
require('./service/gridfs');
require('./service/queue');
const server = require('./service/server');
const HandlerHome = require('./handler/home');
const HandlerProblem = require('./handler/problem');
const HandlerRecord = require('./handler/record');
const HandlerJudge = require('./handler/judge');
const HandlerUser = require('./handler/user');
const HandlerContest = require('./handler/contest');
const HandlerTraining = require('./handler/training');
const HandlerDiscussion = require('./handler/discussion');
const HandlerManage = require('./handler/manage');
const HandlerImport = require('./handler/import');
const builtinHandler = [
'home', 'problem', 'record', 'judge', 'user',
'contest', 'training', 'discussion', 'manage', 'import',
];
for (const i of builtinHandler) require(`./handler/${i}`);
await loader.model();
await loader.handler();
HandlerContest.apply();
HandlerDiscussion.apply();
HandlerImport.apply();
for (const i in global.Hydro.handler) {
await global.Hydro.handler[i].apply();
}
const notfound = require(`./handler/notfound`);
await notfound.apply();
server.start();
}
run().catch((e) => {
+2 -2
View File
@@ -267,7 +267,7 @@ class ContestCreateHandler extends ContestHandler {
}
}
function apply() {
async function apply() {
Route('/c', module.exports.ContestListHandler);
Route('/c/:tid', module.exports.ContestDetailHandler);
Route('/c/:tid/edit', module.exports.ContestEditHandler);
@@ -278,7 +278,7 @@ function apply() {
Route('/contest/create', module.exports.ContestCreateHandler);
}
module.exports = {
global.Hydro.handler.contest = module.exports = {
ContestListHandler,
ContestDetailHandler,
ContestEditHandler,
+15 -10
View File
@@ -34,7 +34,9 @@ class DiscussionHandler extends Handler {
if (drrid) {
[, this.drrdoc] = await discussion.getTailReply(drid, drrid);
if (!this.drrdoc) throw new DiscussionNotFoundError(drrid);
if (this.drrdoc.parent !== this.drdoc._id) throw new DocumentNotFoundError(drid);
if (this.drrdoc.parent !== this.drdoc._id) {
throw new DocumentNotFoundError(drid);
}
}
}
}
@@ -60,7 +62,11 @@ class DiscussionHandler extends Handler {
class DiscussionMainHandler extends DiscussionHandler {
async get({ page = 1 }) {
const [ddocs, dpcount] = await paginate(discussion.getMulti(), page, constants.DISCUSSION_PER_PAGE);
const [ddocs, dpcount] = await paginate(
discussion.getMulti(),
page,
constants.DISCUSSION_PER_PAGE,
);
const udict = await user.getList(ddocs.map((ddoc) => ddoc.owner));
this.response.template = 'discussion_main_or_node.html';
this.response.body = {
@@ -114,12 +120,11 @@ class DiscussionCreateHandler extends DiscussionHandler {
type, docId, title, content, highlight,
}) {
this.limitRate('add_discussion', 3600, 30);
const flags = {};
if (highlight) {
this.checkPerm(PERM_HIGHLIGHT_DISCUSSION);
flags.highlight = true;
}
const did = await discussion.add(type, docId, this.user._id, title, content, this.request.ip, flags);
if (highlight) this.checkPerm(PERM_HIGHLIGHT_DISCUSSION);
const did = await discussion.add(
type, docId, this.user._id,
title, content, this.request.ip, highlight,
);
this.response.body = { did };
this.response.redirect = `/discuss/${did}`;
}
@@ -257,7 +262,7 @@ class DiscussionEditHandler extends DiscussionHandler {
}
}
function apply() {
async function apply() {
Route('/discuss', module.exports.DiscussionMainHandler);
Route('/discuss/:did', module.exports.DiscussionDetailHandler);
Route('/discuss/:did/edit', module.exports.DiscussionEditHandler);
@@ -268,7 +273,7 @@ function apply() {
Route('/discuss/:type/:docId/create', module.exports.DiscussionCreateHandler);
}
module.exports = {
global.Hydro.handler.discussion = module.exports = {
DiscussionMainHandler,
DiscussionDetailHandler,
DiscussionEditHandler,
+9 -4
View File
@@ -171,11 +171,16 @@ class UserChangemailWithCodeHandler extends Handler {
}
}
Route('/', HomeHandler);
Route('/home/security', HomeSecurityHandler);
Route('/home/changeMail/:code', UserChangemailWithCodeHandler);
Route('/home/settings/:category', HomeSettingsHandler);
async function apply() {
Route('/', module.exports.HomeHandler);
Route('/home/security', module.exports.HomeSecurityHandler);
Route('/home/changeMail/:code', module.exports.UserChangemailWithCodeHandler);
Route('/home/settings/:category', module.exports.HomeSettingsHandler);
}
global.Hydro.handler.home = module.exports = {
HomeHandler, HomeSecurityHandler, HomeSettingsHandler, UserChangemailWithCodeHandler, apply,
};
/*
@app.route('/home/messages', 'home_messages', global_route=True)
class HomeMessagesHandler(base.OperationHandler):
+5 -4
View File
@@ -1,4 +1,3 @@
const problem = require('../model/problem');
const { Route, Handler } = require('../service/server');
const { PERM_CREATE_PROBLEM } = require('../permission');
@@ -23,8 +22,10 @@ class ProblemImportHandler extends Handler {
async post({
url, pid, hidden, remoteType,
}) {
if (remoteType !== 'syzoj') throw new ValidationError('remoteType');
const [pdoc, testdata] = await this.syzoj(url);
if (typeof this[`from_${remoteType}`] !== 'function') {
throw new ValidationError('remoteType');
}
const [pdoc, testdata] = await this[`from_${remoteType}`](url);
if (pid) pdoc.pid = pid;
if (hidden) pdoc.hidden = true;
const _id = await problem.add(pdoc);
@@ -39,6 +40,6 @@ async function apply() {
Route('/problem/import', module.exports.ProblemImportHandler);
}
global.Hydro['handler.import'] = module.exports = {
global.Hydro.handler.import = module.exports = {
ProblemImportHandler, apply,
};
+8 -2
View File
@@ -183,5 +183,11 @@ class JudgeConnectionHandler extends ConnectionHandler {
}
}
Route('/judge', JudgeHandler);
Connection('/judge/conn', JudgeConnectionHandler);
async function apply() {
Route('/judge', module.exports.JudgeHandler);
Connection('/judge/conn', module.exports.JudgeConnectionHandler);
}
global.Hydro.handler.judge = module.exports = {
JudgeHandler, JudgeConnectionHandler, apply,
};
+18 -6
View File
@@ -120,9 +120,21 @@ class ManageRoleHandler extends ManageHandler {
}
}
Route('/manage', ManageMainHandler);
Route('/manage/dashboard', ManageDashboardHandler);
Route('/manage/edit', ManageEditHandler);
Route('/manage/user', ManageUserHandler);
Route('/manage/permission', ManagePermissionHandler);
Route('/manage/role', ManageRoleHandler);
async function apply() {
Route('/manage', module.exports.ManageMainHandler);
Route('/manage/dashboard', module.exports.ManageDashboardHandler);
Route('/manage/edit', module.exports.ManageEditHandler);
Route('/manage/user', module.exports.ManageUserHandler);
Route('/manage/permission', module.exports.ManagePermissionHandler);
Route('/manage/role', module.exports.ManageRoleHandler);
}
global.Hydro.handler.manage = module.exports = {
ManageMainHandler,
ManageDashboardHandler,
ManageEditHandler,
ManageUserHandler,
ManagePermissionHandler,
ManageRoleHandler,
apply,
};
+19
View File
@@ -0,0 +1,19 @@
const { Route, Handler } = require('../service/server');
const { NotFoundError } = require('../error');
class NotFoundHandler extends Handler {
async prepare() {
throw new NotFoundError();
}
}
async function apply() {
Route('/:param1', module.exports.NotFoundHandler);
Route('/:param1/:param2', module.exports.NotFoundHandler);
Route('/:param1/:param2/:param3', module.exports.NotFoundHandler);
Route('/:param1/:param2/:param3/:param4', module.exports.NotFoundHandler);
}
global.Hydro.handler.notfound = module.exports = {
NotFoundHandler, apply,
};
+39 -16
View File
@@ -71,7 +71,9 @@ 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,
@@ -376,18 +378,39 @@ class ProblemCreateHandler extends Handler {
}
}
Route('/p', ProblemHandler);
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);
async function apply() {
Route('/p', module.exports.ProblemHandler);
Route('/problem/random', module.exports.ProblemRandomHandler);
Route('/p/:pid', module.exports.ProblemDetailHandler);
Route('/p/:pid/submit', module.exports.ProblemSubmitHandler);
Route('/p/:pid/pretest', module.exports.ProblemPretestHandler);
Route('/p/:pid/settings', module.exports.ProblemSettingsHandler);
Route('/p/:pid/statistics', module.exports.ProblemStatisticsHandler);
Route('/p/:pid/edit', module.exports.ProblemEditHandler);
Route('/p/:pid/upload', module.exports.ProblemDataUploadHandler);
Route('/p/:pid/data', module.exports.ProblemDataDownloadHandler);
Route('/p/:pid/solution', module.exports.ProblemSolutionHandler);
Route('/p/:pid/solution/:psid/raw', module.exports.ProblemSolutionRawHandler);
Route('/p/:pid/solution/:psid/:psrid/raw', module.exports.ProblemSolutionReplyRawHandler);
Route('/problem/create', module.exports.ProblemCreateHandler);
Connection('/p/:pid/pretest-conn', module.exports.ProblemPretestConnectionHandler);
}
global.Hydro.handler.problem = module.exports = {
ProblemHandler,
ProblemRandomHandler,
ProblemDetailHandler,
ProblemSubmitHandler,
ProblemPretestHandler,
ProblemSettingsHandler,
ProblemStatisticsHandler,
ProblemEditHandler,
ProblemDataUploadHandler,
ProblemDataDownloadHandler,
ProblemSolutionHandler,
ProblemSolutionRawHandler,
ProblemSolutionReplyRawHandler,
ProblemCreateHandler,
ProblemPretestConnectionHandler,
apply,
};
+25 -13
View File
@@ -34,18 +34,16 @@ class RecordListHandler extends Handler {
for (const pdoc of plist) {
pdict[pdoc._id] = pdoc;
}
const path = [
['Hydro', '/'],
['record_main', null],
];
this.response.body = {
path: [
['Hydro', '/'],
['record_main', null],
],
page,
rdocs,
pdict,
udict,
path, page, rdocs, pdict, udict,
};
}
}
class RecordDetailHandler extends Handler {
async get({ rid }) {
this.response.template = 'record_detail.html';
@@ -66,6 +64,7 @@ class RecordDetailHandler extends Handler {
};
}
}
class RecordRejudgeHandler extends Handler {
async post({ rid }) {
this.checkPerm(PERM_REJUDGE);
@@ -77,6 +76,7 @@ class RecordRejudgeHandler extends Handler {
this.back();
}
}
class RecordConnectionHandler extends ConnectionHandler {
async prepare() {
bus.subscribe(['record_change'], this.onRecordChange);
@@ -102,6 +102,7 @@ class RecordConnectionHandler extends ConnectionHandler {
this.send({ html: await this.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) });
}
}
class RecordDetailConnectionHandler extends contest.ContestHandlerMixin(ConnectionHandler) {
async prepare({ rid }) {
const rdoc = await record.get(rid);
@@ -131,8 +132,19 @@ class RecordDetailConnectionHandler extends contest.ContestHandlerMixin(Connecti
}
}
Route('/r', RecordListHandler);
Route('/r/:rid', RecordDetailHandler);
Route('/r/:rid/rejudge', RecordRejudgeHandler);
Connection('/record-conn', RecordConnectionHandler);
Connection('/record-detail-conn', RecordDetailConnectionHandler);
async function apply() {
Route('/r', module.exports.RecordListHandler);
Route('/r/:rid', module.exports.RecordDetailHandler);
Route('/r/:rid/rejudge', module.exports.RecordRejudgeHandler);
Connection('/record-conn', module.exports.RecordConnectionHandler);
Connection('/record-detail-conn', module.exports.RecordDetailConnectionHandler);
}
global.Hydro.handler.record = module.exports = {
RecordListHandler,
RecordDetailHandler,
RecordRejudgeHandler,
RecordConnectionHandler,
RecordDetailConnectionHandler,
apply,
};
+14 -4
View File
@@ -237,7 +237,17 @@ class TrainingEditHandler extends TrainingHandler {
}
}
Route('/t', TrainingMainHandler);
Route('/t/:tid', TrainingDetailHandler);
Route('/t/:tid/edit', TrainingEditHandler);
Route('/training/create', TrainingCreateHandler);
async function apply() {
Route('/t', module.exports.TrainingMainHandler);
Route('/t/:tid', module.exports.TrainingDetailHandler);
Route('/t/:tid/edit', module.exports.TrainingEditHandler);
Route('/training/create', module.exports.TrainingCreateHandler);
}
global.Hydro.handler.training = module.exports = {
TrainingMainHandler,
TrainingDetailHandler,
TrainingEditHandler,
TrainingCreateHandler,
apply,
};
+22 -8
View File
@@ -181,11 +181,25 @@ class UserSearchHandler extends Handler {
}
}
Route('/login', UserLoginHandler);
Route('/register', UserRegisterHandler);
Route('/register/:code', UserRegisterWithCodeHandler);
Route('/logout', UserLogoutHandler);
Route('/lostpass', UserLostPassHandler);
Route('/lostpass/:code', UserLostPassWithCodeHandler);
Route('/user/search', UserSearchHandler);
Route('/user/:uid', UserDetailHandler);
async function apply() {
Route('/login', module.exports.UserLoginHandler);
Route('/register', module.exports.UserRegisterHandler);
Route('/register/:code', module.exports.UserRegisterWithCodeHandler);
Route('/logout', module.exports.UserLogoutHandler);
Route('/lostpass', module.exports.UserLostPassHandler);
Route('/lostpass/:code', module.exports.UserLostPassWithCodeHandler);
Route('/user/search', module.exports.UserSearchHandler);
Route('/user/:uid', module.exports.UserDetailHandler);
}
global.Hydro.handler.user = module.exports = {
UserLoginHandler,
UserRegisterHandler,
UserRegisterWithCodeHandler,
UserLogoutHandler,
UserLostPassHandler,
UserLostPassWithCodeHandler,
UserSearchHandler,
UserDetailHandler,
apply,
};
+11 -5
View File
@@ -14,9 +14,15 @@ function exist(name) {
return true;
}
const superRequire = (name) => (__non_webpack_require__
? __non_webpack_require__(root(name))
: require(root(name)));
const superRequire = (name) => {
let m;
try {
m = require(root(name)); // eslint-disable-line import/no-dynamic-require
} catch (e) {
m = __non_webpack_require__(root(name)); // eslint-disable-line no-undef
}
return m;
};
async function prepare() {
installed = fs.readdirSync(root('.build/module'));
@@ -26,8 +32,8 @@ async function handler() {
for (const i of installed) {
if (exist(`.build/module/${i}/handler.js`)) {
superRequire(`.build/module/${i}/handler.js`);
console.log(`Handler init: ${i}`);
}
console.log(`Handler init: ${i}`);
}
}
@@ -35,8 +41,8 @@ async function model() {
for (const i of installed) {
if (exist(`.build/module/${i}/model.js`)) {
superRequire(`.build/module/${i}/model.js`);
console.log(`Model init: ${i}`);
}
console.log(`Model init: ${i}`);
}
}
+7 -2
View File
@@ -51,7 +51,11 @@ async function edit(tid, $set) {
if ($set.title) validator.checkTitle($set.title);
if ($set.content) validator.checkIntro($set.content);
if ($set.rule) { if (!this.RULES[$set.rule]) throw new ValidationError('rule'); }
if ($set.beginAt && $set.endAt) if ($set.beginAt >= $set.endAt) throw new ValidationError('beginAt', 'endAt');
if ($set.beginAt && $set.endAt) {
if ($set.beginAt >= $set.endAt) {
throw new ValidationError('beginAt', 'endAt');
}
}
const tdoc = await coll.findOne({ tid });
if (!tdoc) throw new ContestNotFoundError(tid);
this.RULES[$set.rule || tdoc.rule].check(Object.assign(tdoc, $set));
@@ -84,7 +88,8 @@ function getStatus(tid, uid) {
}
async function getListStatus(uid, tids) {
const r = {};
for (const tid of tids) r[tid] = await getStatus(tid, uid); // eslint-disable-line no-await-in-loop
// eslint-disable-next-line no-await-in-loop
for (const tid of tids) r[tid] = await getStatus(tid, uid);
return r;
}
async function attend(tid, uid) {
+2 -1
View File
@@ -65,7 +65,7 @@ function editReply(drid, content) {
async function delReply(drid) {
const drdoc = await getReply(drid);
if (!drdoc) throw new DocumentNotFoundError(drid);
return await Promise.all([
return await Promise.all([ // eslint-disable-line no-return-await
collReply.deleteOne({ _id: drid }),
coll.updateOne({ _id: drdoc.did }, { $inc: { nReply: -1 } }),
]);
@@ -112,6 +112,7 @@ async function editTailReply(drid, drrid, content) {
break;
}
}
// eslint-disable-next-line no-return-await
return await collReply.findOneAndUpdate({ _id: drdoc._id }, { $set: { reply } });
}
+1 -1
View File
@@ -157,7 +157,7 @@ async function setTestdata(_id, readStream) {
f.once('error', reject);
});
if (pdoc.data && typeof pdoc.data === 'object') gridfs.delete(this.pdoc.data);
return await edit(_id, { data: f.id });
return await edit(_id, { data: f.id }); // eslint-disable-line no-return-await
}
module.exports = {
+6 -4
View File
@@ -28,13 +28,14 @@ async function get(psid) {
}
function getMany(query, sort, page, limit) {
return coll.find(query).sort(sort).skip((page - 1) * limit).limit(limit)
return coll.find(query).sort(sort)
.skip((page - 1) * limit).limit(limit)
.toArray();
}
async function edit(_id, content) {
validator.checkContent(content);
await coll.findOneAndUpdate({ _id }, { $set: { content } });
await coll.updateOne({ _id }, { $set: { content } });
const psdoc = await get(_id);
if (!psdoc) throw new SolutionNotFoundError(_id);
return psdoc;
@@ -79,7 +80,8 @@ async function editReply(psid, psrid, content) {
break;
}
}
return await coll.findOneAndUpdate({ _id: psdoc._id }, { $set: { reply } });
// eslint-disable-next-line no-return-await
return await coll.updateOne({ _id: psdoc._id }, { $set: { reply } });
}
function delReply(psid, psrid) {
@@ -91,7 +93,7 @@ async function vote(psid, uid, value) {
if (pssdoc) await collStatus.deleteOne({ psid, uid });
await collStatus.insertOne({ psid, uid, vote: value });
if (pssdoc) value += -pssdoc.vote;
await coll.findOneAndUpdate({ _id: psid }, { $inc: { vote: value } });
await coll.updateOne({ _id: psid }, { $inc: { vote: value } });
pssdoc = await collStatus.findOne({ psid, uid });
const psdoc = await coll.findOne({ _id: psid });
return [psdoc, pssdoc];
+3 -3
View File
@@ -27,12 +27,12 @@ async function enroll(tid, uid) {
} catch (e) {
throw new TrainingAlreadyEnrollError(tid, uid);
}
await coll.findOneAndUpdate({ _id: tid }, { $inc: { enroll: 1 } });
await coll.updateOne({ _id: tid }, { $inc: { enroll: 1 } });
}
async function setStatus(tid, uid, $set) {
await collStatus.findOneAndUpdate({ tid, uid }, { $set });
return await collStatus.findOne({ tid, uid });
await collStatus.updateOne({ tid, uid }, { $set });
return await collStatus.findOne({ tid, uid }); // eslint-disable-line no-return-await
}
module.exports = {
+54
View File
@@ -0,0 +1,54 @@
const { defaults } = require('lodash');
const db = require('../service/db');
const builtin = require('../model/builtin');
const pwhash = require('../lib/pwhash');
const { udoc } = require('../interface');
async function run() {
const collUser = db.collection('user');
const collRole = db.collection('role');
const collBlacklist = db.collection('blacklist');
const collToken = db.collection('token');
async function createUser() {
const salt = pwhash.salt();
await collUser.insertMany([
defaults({
_id: 0,
uname: 'Hydro',
unameLower: 'hydro',
mail: 'hydro@hydro',
mailLower: 'hydro@hydro',
role: 'guest',
}, udoc),
defaults({
_id: 1,
mail: 'guest@hydro',
mailLower: 'guest@hydro',
uname: 'Guest',
unameLower: 'guest',
role: 'guest',
}, udoc),
defaults({
_id: -1,
mail: 'root@hydro',
mailLower: 'root@hydro',
uname: 'Root',
unameLower: 'root',
hash: pwhash.hash('rootroot', salt),
salt,
gravatar: 'root@hydro',
role: 'root',
}, udoc),
]);
}
await collUser.createIndex('unameLower', { unique: true });
await collUser.createIndex('mailLower', { sparse: true });
await collRole.insertMany(builtin.BUILTIN_ROLES);
await collBlacklist.createIndex('expireAt', { expireAfterSeconds: 0 });
await collToken.createIndex([{ uid: 1 }, { tokenType: 1 }, { updateAt: -1 }], { sparse: true });
await collToken.createIndex('expireAt', { expireAfterSeconds: 0 });
await createUser();
console.log('Installed');
}
module.exports = { run };
+8
View File
@@ -0,0 +1,8 @@
const db = require('../service/db.js');
async function run() {
await db.dropDatabase();
console.log('Dropped');
}
module.exports = { run };
+8 -14
View File
@@ -17,7 +17,7 @@ const token = require('../model/token');
const opcount = require('../model/opcount');
const {
UserNotFoundError, BlacklistedError, PermissionError,
NotFoundError, UserFacingError, ValidationError,
UserFacingError, ValidationError,
} = require('../error');
const app = new Koa();
@@ -166,19 +166,13 @@ class Handler {
}
async renderBody() {
if (!this.response.redirect) {
if (!(this.response.body || this.response.template)) {
this.response.body = { error: new NotFoundError() };
this.response.template = 'error.html';
}
if (!this.preferJson) {
if (this.response.body || this.response.template) {
if (this.request.query.noTemplate || this.preferJson) return;
const templateName = this.request.query.template || this.response.template;
if (templateName) {
this.response.body = this.response.body || {};
await this.render(templateName, this.response.body);
}
if (!this.response.redirect && !this.preferJson) {
if (this.response.body || this.response.template) {
if (this.request.query.noTemplate || this.preferJson) return;
const templateName = this.request.query.template || this.response.template;
if (templateName) {
this.response.body = this.response.body || {};
await this.render(templateName, this.response.body);
}
}
}