From 1c8b62a6d9669f415b12df79c2440d56a3ee50b0 Mon Sep 17 00:00:00 2001 From: masnn Date: Mon, 8 Jun 2020 13:41:43 +0800 Subject: [PATCH] 2.0.0(preview) --- build/index.js | 6 +++ hydro/handler/contest.js | 28 ++++------- hydro/handler/discussion.js | 28 ++++------- hydro/handler/domain.js | 4 +- hydro/handler/home.js | 22 +++----- hydro/handler/homework.js | 10 +--- hydro/handler/import.js | 6 +-- hydro/handler/judge.js | 11 ++-- hydro/handler/manage.js | 83 +++++++++++++++---------------- hydro/handler/misc.js | 27 +++++++--- hydro/handler/notfound.js | 14 +++--- hydro/handler/problem.js | 52 +++++++------------ hydro/handler/record.js | 9 +--- hydro/handler/system.js | 13 ++--- hydro/handler/training.js | 16 ++---- hydro/handler/user.js | 28 ++++------- hydro/lib/nav.js | 3 +- hydro/lib/sysinfo.js | 12 ++--- hydro/loader.js | 4 +- hydro/model/document.js | 8 +++ hydro/model/user.js | 8 ++- hydro/script/recalcRating.js | 17 ++++++- hydro/service/monitor.js | 6 +-- module/module-pastebin/handler.js | 4 +- module/wiki/handler.js | 4 +- package.json | 2 +- templates/domain_rank.html | 2 +- templates/manage_base.html | 1 + templates/manage_script.html | 56 +++++++++++++++++++++ templates/status.html | 4 +- tool/install.js | 17 +++++++ ui/pages/manage_module.page.js | 3 -- ui/pages/manage_script.page.js | 40 +++++++++++++++ 33 files changed, 307 insertions(+), 241 deletions(-) create mode 100644 templates/manage_script.html create mode 100644 tool/install.js create mode 100644 ui/pages/manage_script.page.js diff --git a/build/index.js b/build/index.js index 02f7db1a4..55e83df20 100644 --- a/build/index.js +++ b/build/index.js @@ -67,8 +67,14 @@ async function build(type) { } } const f = fs.readFileSync(root('.build/development.js')).toString(); + const installer = fs.readFileSync(root('tool/install.js')).toString(); const file = `global._hydroModule=${JSON.stringify(j)};${f}`; fs.writeFileSync(root('.build/full.js'), file); + const hydro = {}; + hydro.app = Buffer.from(f).toString('base64'); + hydro.modules = j; + const d = zlib.gzipSync(Buffer.from(JSON.stringify(hydro)), { level: -1 }).toString('base64'); + fs.writeFileSync(root('.build/install.js'), `global.Hydro="${d}"; ${installer}`); } module.exports = build; diff --git a/hydro/handler/contest.js b/hydro/handler/contest.js index 14a13d500..be22881f3 100644 --- a/hydro/handler/contest.js +++ b/hydro/handler/contest.js @@ -302,24 +302,14 @@ class ContestCreateHandler extends ContestHandler { } async function apply() { - Route('/c', module.exports.ContestListHandler); - Route('/c/:tid', module.exports.ContestDetailHandler); - Route('/c/:tid/edit', module.exports.ContestEditHandler); - Route('/c/:tid/scoreboard', module.exports.ContestScoreboardHandler); - Route('/c/:tid/export/:ext', module.exports.ContestScoreboardDownloadHandler); - Route('/c/:tid/p/:pid', module.exports.ContestProblemHandler); - Route('/c/:tid/p/:pid/submit', module.exports.ContestProblemSubmitHandler); - Route('/contest/create', module.exports.ContestCreateHandler, PERM_CREATE_CONTEST); + Route('/c', ContestListHandler); + Route('/c/:tid', ContestDetailHandler); + Route('/c/:tid/edit', ContestEditHandler); + Route('/c/:tid/scoreboard', ContestScoreboardHandler); + Route('/c/:tid/export/:ext', ContestScoreboardDownloadHandler); + Route('/c/:tid/p/:pid', ContestProblemHandler); + Route('/c/:tid/p/:pid/submit', ContestProblemSubmitHandler); + Route('/contest/create', ContestCreateHandler, PERM_CREATE_CONTEST); } -global.Hydro.handler.contest = module.exports = { - ContestListHandler, - ContestDetailHandler, - ContestEditHandler, - ContestScoreboardHandler, - ContestScoreboardDownloadHandler, - ContestProblemHandler, - ContestProblemSubmitHandler, - ContestCreateHandler, - apply, -}; +global.Hydro.handler.contest = module.exports = apply; diff --git a/hydro/handler/discussion.js b/hydro/handler/discussion.js index 9f5056d78..b75b1de5c 100644 --- a/hydro/handler/discussion.js +++ b/hydro/handler/discussion.js @@ -283,24 +283,14 @@ class DiscussionEditHandler extends DiscussionHandler { } async function apply() { - Route('/discuss', module.exports.DiscussionMainHandler); - Route('/discuss/:did', module.exports.DiscussionDetailHandler); - Route('/discuss/:did/edit', module.exports.DiscussionEditHandler); - Route('/discuss/:did/raw', module.exports.DiscussionDetailRawHandler); - Route('/discuss/:did/:drid/raw', module.exports.DiscussionReplyRawHandler); - Route('/discuss/:did/:drid/:drrid/raw', module.exports.DiscussionTailReplyRawHandler); - Route('/discuss/:type/:name', module.exports.DiscussionNodeHandler); - Route('/discuss/:type/:name/create', module.exports.DiscussionCreateHandler); + Route('/discuss', DiscussionMainHandler); + Route('/discuss/:did', DiscussionDetailHandler); + Route('/discuss/:did/edit', DiscussionEditHandler); + Route('/discuss/:did/raw', DiscussionDetailRawHandler); + Route('/discuss/:did/:drid/raw', DiscussionReplyRawHandler); + Route('/discuss/:did/:drid/:drrid/raw', DiscussionTailReplyRawHandler); + Route('/discuss/:type/:name', DiscussionNodeHandler); + Route('/discuss/:type/:name/create', DiscussionCreateHandler); } -global.Hydro.handler.discussion = module.exports = { - DiscussionMainHandler, - DiscussionDetailHandler, - DiscussionEditHandler, - DiscussionDetailRawHandler, - DiscussionReplyRawHandler, - DiscussionTailReplyRawHandler, - DiscussionNodeHandler, - DiscussionCreateHandler, - apply, -}; +global.Hydro.handler.discussion = module.exports = apply; diff --git a/hydro/handler/domain.js b/hydro/handler/domain.js index d45b94443..c84ceac8a 100644 --- a/hydro/handler/domain.js +++ b/hydro/handler/domain.js @@ -192,6 +192,4 @@ async function apply() { Route('/domain/role', DomainRoleHandler); } -global.Hydro.handler.domain = module.exports = { - apply, -}; +global.Hydro.handler.domain = module.exports = apply; diff --git a/hydro/handler/home.js b/hydro/handler/home.js index ec9c120af..af14de74c 100644 --- a/hydro/handler/home.js +++ b/hydro/handler/home.js @@ -285,23 +285,15 @@ class HomeMessagesConnectionHandler extends ConnectionHandler { } 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); - Route('/home/messages', module.exports.HomeMessagesHandler); - Connection('/home/messages-conn', module.exports.HomeMessagesConnectionHandler); + Route('/', HomeHandler); + Route('/home/security', HomeSecurityHandler); + Route('/home/changeMail/:code', UserChangemailWithCodeHandler); + Route('/home/settings/:category', HomeSettingsHandler); + Route('/home/messages', HomeMessagesHandler); + Connection('/home/messages-conn', HomeMessagesConnectionHandler); } -global.Hydro.handler.home = module.exports = { - HomeHandler, - HomeSecurityHandler, - HomeSettingsHandler, - UserChangemailWithCodeHandler, - HomeMessagesHandler, - HomeMessagesConnectionHandler, - apply, -}; +global.Hydro.handler.home = module.exports = apply; /* @app.route('/home/file', 'home_file', global_route=True) diff --git a/hydro/handler/homework.js b/hydro/handler/homework.js index 1723a4ab4..072cbdffc 100644 --- a/hydro/handler/homework.js +++ b/hydro/handler/homework.js @@ -452,15 +452,7 @@ async function apply() { Route('/homework/:tid/edit', HomeworkEditHandler); } -global.Hydro.handler.homework = { - HomeworkMainHandler, - HomeworkCreateHandler, - HomeworkDetailHandler, - HomeworkDetailProblemHandler, - HomeworkDetailProblemSubmitHandler, - HomeworkEditHandler, - apply, -}; +global.Hydro.handler.homework = module.exports = apply; /* diff --git a/hydro/handler/import.js b/hydro/handler/import.js index 37b7e0ab2..17a05aa2d 100644 --- a/hydro/handler/import.js +++ b/hydro/handler/import.js @@ -36,9 +36,7 @@ class ProblemImportHandler extends Handler { } async function apply() { - Route('/problem/import', module.exports.ProblemImportHandler); + Route('/problem/import', ProblemImportHandler); } -global.Hydro.handler.import = module.exports = { - ProblemImportHandler, apply, -}; +global.Hydro.handler.import = module.exports = apply; diff --git a/hydro/handler/judge.js b/hydro/handler/judge.js index 1988915ca..f6d70156e 100644 --- a/hydro/handler/judge.js +++ b/hydro/handler/judge.js @@ -133,10 +133,11 @@ class JudgeConnectionHandler extends ConnectionHandler { } async function apply() { - Route('/judge', module.exports.JudgeHandler); - Connection('/judge/conn', module.exports.JudgeConnectionHandler); + Route('/judge', JudgeHandler); + Connection('/judge/conn', JudgeConnectionHandler); } -global.Hydro.handler.judge = module.exports = { - JudgeHandler, JudgeConnectionHandler, apply, next, end, -}; +apply.next = next; +apply.end = end; + +global.Hydro.handler.judge = module.exports = apply; diff --git a/hydro/handler/manage.js b/hydro/handler/manage.js index 7e73d73cd..27f707b81 100644 --- a/hydro/handler/manage.js +++ b/hydro/handler/manage.js @@ -1,57 +1,45 @@ -const domain = require('../model/domain'); const setting = require('../model/setting'); const system = require('../model/system'); const { Route, Handler } = require('../service/server'); -const { PERM_MANAGE } = require('../permission'); const hpm = require('../lib/hpm'); const loader = require('../loader'); const { PermissionError } = require('../error'); -class ManageHandler extends Handler { - async prepare({ domainId }) { - this.checkPerm(PERM_MANAGE); - this.domain = await domain.get(domainId); +class SystemHandler extends Handler { + async prepare() { + if (!this.user.priv) throw new PermissionError('???'); + this.body = { + path: [ + ['Hydro', '/'], + ['manage_main', '/manage'], + ], + }; } } -class ManageMainHandler extends ManageHandler { +class SystemMainHandler extends SystemHandler { async get() { this.response.redirect = '/manage/dashboard'; } } -class ManageDashboardHandler extends ManageHandler { +class SystemDashboardHandler extends SystemHandler { async get() { - const path = [ - ['Hydro', '/'], - ['manage', '/manage'], - ['manage_dashboard', null], - ]; this.response.template = 'manage_dashboard.html'; - this.response.body = { domain: this.domain, path }; - } -} - -class SystemHandler extends Handler { - async prepare() { - if (!this.user.priv) throw new PermissionError('???'); + this.response.body.path.push(['manage_dashboard', null]); } } class SystemModuleHandler extends SystemHandler { async get() { - const installed = await hpm.getInstalled(); - const path = [ - ['Hydro', '/'], - ['manage', '/manage'], - ['manage_module', null], - ]; - this.response.body = { installed, active: loader.active, path }; + this.response.body.path.push(['manage_module', null]); + this.response.body.installed = await hpm.getInstalled(); + this.response.active = loader.active; this.response.template = 'manage_module.html'; } - async postInstall({ url, id }) { - await hpm.install(url, id); + async postInstall({ url }) { + await hpm.install(url); this.back(); } @@ -61,15 +49,29 @@ class SystemModuleHandler extends SystemHandler { } } +class SystemScriptHandler extends SystemHandler { + async get() { + this.response.template = 'manage_script.html'; + this.response.body.scripts = global.Hydro.script; + this.response.body.path.push(['manage_script', null]); + } + + async post({ id, args }) { + // TODO Do not use console.log + await global.Hydro.script[id].run(args, console.log); + this.back(); + } +} + class SystemSettingHandler extends SystemHandler { async get() { this.response.template = 'manage_settings.html'; - const current = {}; - const settings = setting.SYSTEM_SETTINGS; - for (const s of settings) { - current[s.key] = await system.get(s.key); + this.response.body.path.push(['manage_settings', null]); + this.response.body.current = {}; + this.response.body.settings = setting.SYSTEM_SETTINGS; + for (const s of this.response.body.settings) { + this.response.body.current[s.key] = await system.get(s.key); } - this.response.body = { current, settings }; } async post(args) { @@ -99,16 +101,11 @@ class SystemSettingHandler extends SystemHandler { } async function apply() { - Route('/manage', module.exports.ManageMainHandler); - Route('/manage/dashboard', module.exports.ManageDashboardHandler); + Route('/manage', SystemMainHandler); + Route('/manage/dashboard', SystemDashboardHandler); + Route('/manage/script', SystemScriptHandler); Route('/manage/module', SystemModuleHandler); Route('/manage/setting', SystemSettingHandler); } -global.Hydro.handler.manage = module.exports = { - ManageMainHandler, - ManageDashboardHandler, - SystemModuleHandler, - SystemSettingHandler, - apply, -}; +global.Hydro.handler.manage = module.exports = apply; diff --git a/hydro/handler/misc.js b/hydro/handler/misc.js index 66e18e5cf..573a7e39a 100644 --- a/hydro/handler/misc.js +++ b/hydro/handler/misc.js @@ -24,7 +24,11 @@ class StatusHandler extends Handler { desc = desc || 'Online'; stats[i].status = desc; } - this.response.body = { stats }; + console.log(stats); + const path = [ + ['Hydro', '/'], + ]; + this.response.body = { stats, path }; this.response.template = 'status.html'; } } @@ -32,7 +36,12 @@ class StatusHandler extends Handler { class StatusUpdateHandler extends Handler { async post(args) { this.checkPerm(PERM_JUDGE); - await coll.updateOne({ _id: args._id }, { $set: { ...args, type: 'judger' } }, { upsert: true }); + args.type = 'judger'; + return coll.updateOne( + { mid: args.mid, type: 'judger' }, + { $set: args }, + { upsert: true }, + ); } } @@ -50,9 +59,13 @@ async function apply() { Route('/language/:lang', SwitchLanguageHandler); } -global.Hydro.handler.misc = module.exports = { - FileDownloadHandler, - StatusHandler, - StatusUpdateHandler, - apply, +apply.updateStatus = function updateStatus(args) { + args.type = 'judger'; + return coll.updateOne( + { mid: args.mid, type: 'judger' }, + { $set: args }, + { upsert: true }, + ); }; + +global.Hydro.handler.misc = module.exports = apply; diff --git a/hydro/handler/notfound.js b/hydro/handler/notfound.js index c186d153f..a8ece11c3 100644 --- a/hydro/handler/notfound.js +++ b/hydro/handler/notfound.js @@ -8,12 +8,12 @@ class NotFoundHandler extends Handler { } 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); + Route('/:param1', NotFoundHandler); + Route('/:param1/:param2', NotFoundHandler); + Route('/:param1/:param2/:param3', NotFoundHandler); + Route('/:param1/:param2/:param3/:param4', NotFoundHandler); + Route('/:param1/:param2/:param3/:param4/:param5', NotFoundHandler); + Route('/:param1/:param2/:param3/:param4/:param5/:param6', NotFoundHandler); } -global.Hydro.handler.notfound = module.exports = { - NotFoundHandler, apply, -}; +global.Hydro.handler.notfound = module.exports = apply; diff --git a/hydro/handler/problem.js b/hydro/handler/problem.js index 8e2efdc64..548f7d6ff 100644 --- a/hydro/handler/problem.js +++ b/hydro/handler/problem.js @@ -443,40 +443,22 @@ class ProblemCreateHandler extends Handler { } async function apply() { - Route('/p', module.exports.ProblemHandler); - Route('/p/category/:category', module.exports.ProblemCategoryHandler); - 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); + 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); } -global.Hydro.handler.problem = module.exports = { - ProblemHandler, - ProblemCategoryHandler, - ProblemRandomHandler, - ProblemDetailHandler, - ProblemSubmitHandler, - ProblemPretestHandler, - ProblemSettingsHandler, - ProblemStatisticsHandler, - ProblemEditHandler, - ProblemDataUploadHandler, - ProblemDataDownloadHandler, - ProblemSolutionHandler, - ProblemSolutionRawHandler, - ProblemSolutionReplyRawHandler, - ProblemCreateHandler, - ProblemPretestConnectionHandler, - apply, -}; +global.Hydro.handler.problem = module.exports = apply; diff --git a/hydro/handler/record.js b/hydro/handler/record.js index 43df34f36..b34119cca 100644 --- a/hydro/handler/record.js +++ b/hydro/handler/record.js @@ -160,11 +160,4 @@ async function apply() { Connection('/record-detail-conn', RecordDetailConnectionHandler); } -global.Hydro.handler.record = module.exports = { - RecordListHandler, - RecordDetailHandler, - RecordRejudgeHandler, - RecordMainConnectionHandler, - RecordDetailConnectionHandler, - apply, -}; +global.Hydro.handler.record = module.exports = apply; diff --git a/hydro/handler/system.js b/hydro/handler/system.js index d6d35a1bc..58894791a 100644 --- a/hydro/handler/system.js +++ b/hydro/handler/system.js @@ -78,14 +78,9 @@ class SystemSettingHandler extends SystemHandler { } async function apply() { - Route('/system', module.exports.SystemDashboardHandler); - Route('/system/module', module.exports.SystemModuleHandler); - Route('/system/setting', module.exports.SystemSettingHandler); + Route('/system', SystemDashboardHandler); + Route('/system/module', SystemModuleHandler); + Route('/system/setting', SystemSettingHandler); } -global.Hydro.handler.manage = module.exports = { - SystemDashboardHandler, - SystemModuleHandler, - SystemSettingHandler, - apply, -}; +global.Hydro.handler.manage = module.exports = apply; diff --git a/hydro/handler/training.js b/hydro/handler/training.js index 39b0482e7..5bb5e3e07 100644 --- a/hydro/handler/training.js +++ b/hydro/handler/training.js @@ -249,16 +249,10 @@ class TrainingEditHandler extends TrainingHandler { } 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); + Route('/t', TrainingMainHandler); + Route('/t/:tid', TrainingDetailHandler); + Route('/t/:tid/edit', TrainingEditHandler); + Route('/training/create', TrainingCreateHandler); } -global.Hydro.handler.training = module.exports = { - TrainingMainHandler, - TrainingDetailHandler, - TrainingEditHandler, - TrainingCreateHandler, - apply, -}; +global.Hydro.handler.training = module.exports = apply; diff --git a/hydro/handler/user.js b/hydro/handler/user.js index f61420364..bcad89c3e 100644 --- a/hydro/handler/user.js +++ b/hydro/handler/user.js @@ -168,24 +168,14 @@ class UserSearchHandler extends Handler { } async function apply() { - Route('/login', module.exports.UserLoginHandler); - Route('/register', module.exports.UserRegisterHandler); - Route('/register/:code', module.exports.UserRegisterWithCodeHandler); - Route('/logout', module.exports.UserLogoutHandler, PERM_LOGGEDIN); - Route('/lostpass', module.exports.UserLostPassHandler); - Route('/lostpass/:code', module.exports.UserLostPassWithCodeHandler); - Route('/user/search', module.exports.UserSearchHandler); - Route('/user/:uid', module.exports.UserDetailHandler); + Route('/login', UserLoginHandler); + Route('/register', UserRegisterHandler); + Route('/register/:code', UserRegisterWithCodeHandler); + Route('/logout', UserLogoutHandler, PERM_LOGGEDIN); + Route('/lostpass', UserLostPassHandler); + Route('/lostpass/:code', UserLostPassWithCodeHandler); + Route('/user/search', UserSearchHandler); + Route('/user/:uid', UserDetailHandler); } -global.Hydro.handler.user = module.exports = { - UserLoginHandler, - UserRegisterHandler, - UserRegisterWithCodeHandler, - UserLogoutHandler, - UserLostPassHandler, - UserLostPassWithCodeHandler, - UserSearchHandler, - UserDetailHandler, - apply, -}; +global.Hydro.handler.user = module.exports = apply; diff --git a/hydro/lib/nav.js b/hydro/lib/nav.js index 941d43784..174885536 100644 --- a/hydro/lib/nav.js +++ b/hydro/lib/nav.js @@ -3,7 +3,6 @@ const permission = require('../permission'); global.Hydro.ui.nav = []; const trueChecker = () => true; - const Item = (path, name, prefix, perm, checker) => { if (perm && checker) { checker = ((_chk) => (handler) => _chk(handler) && handler.user.hasPerm(perm))(checker); @@ -21,6 +20,8 @@ Item('/t', 'training_main', 'training', permission.PERM_VIEW_TRAINING); Item('/homework', 'homework_main', 'homework', permission.PERM_VIEW_HOMEWORK); Item('/discuss', 'discussion_main', 'discussion', permission.PERM_VIEW_DISCUSSION); Item('/c', 'contest_main', 'contest', permission.PERM_VIEW_CONTEST); +Item('/record', 'record_main', 'record'); +Item('/ranking', 'domain_rank', 'domain_rank'); Item('/domain/dashboard', 'domain_dashboard', 'domain', permission.PERM_MANAGE); Item('/manage/dashboard', 'manage', 'manage', null, (handler) => handler.user.priv === 1); diff --git a/hydro/lib/sysinfo.js b/hydro/lib/sysinfo.js index ef6cef685..9df703247 100644 --- a/hydro/lib/sysinfo.js +++ b/hydro/lib/sysinfo.js @@ -21,13 +21,13 @@ async function get() { let battery; if (!Battery.hasbattery) battery = 'No battery'; else battery = `${Battery.type} ${Battery.model} ${Battery.percent}%${Battery.ischarging ? ' Charging' : ''}`; - const _id = OsInfo.serial; + const mid = OsInfo.serial; cache.cpu = cpu; cache.osinfo = osinfo; cache.flags = flags; - cache._id = _id; + cache.mid = mid; return { - _id, cpu, memory, osinfo, load, flags, CpuTemp, battery, + mid, cpu, memory, osinfo, load, flags, CpuTemp, battery, }; } @@ -39,7 +39,7 @@ async function update() { systeminformation.battery(), ]); const { - _id, cpu, osinfo, flags, + mid, cpu, osinfo, flags, } = cache; const memory = `${size(Memory.active)}/${size(Memory.total)}`; const load = `${CurrentLoad.avgload}`; @@ -47,12 +47,12 @@ async function update() { if (!Battery.hasbattery) battery = 'No battery'; else battery = `${Battery.type} ${Battery.model} ${Battery.percent}%${Battery.ischarging ? ' Charging' : ''}`; return [ - _id, + mid, { memory, load, battery, CpuTemp, }, { - _id, cpu, memory, osinfo, load, flags, battery, CpuTemp, + mid, cpu, memory, osinfo, load, flags, battery, CpuTemp, }, ]; } diff --git a/hydro/loader.js b/hydro/loader.js index 9378b92b5..d024bbd43 100644 --- a/hydro/loader.js +++ b/hydro/loader.js @@ -258,10 +258,10 @@ async function load() { await model(); await handler(); for (const i in global.Hydro.handler) { - await global.Hydro.handler[i].apply(); + await global.Hydro.handler[i](); } const notfound = require('./handler/notfound'); - await notfound.apply(); + await notfound(); for (const i in global.Hydro.service) { if (global.Hydro.service[i].postInit) { try { diff --git a/hydro/model/document.js b/hydro/model/document.js index 14454aa05..2e97f52ab 100644 --- a/hydro/model/document.js +++ b/hydro/model/document.js @@ -190,6 +190,13 @@ async function setStatus(domainId, docType, docId, uid, args) { return res.value; } +function setMultiStatus(domainId, docType, query, args) { + return collStatus.updateMany( + { domainId, docType, ...query }, + { $set: args }, + ); +} + async function setIfNotStatus(domainId, docType, docId, uid, key, value, ifNot, args) { const res = await collStatus.findOneAndUpdate( { @@ -351,6 +358,7 @@ global.Hydro.model.document = module.exports = { set, setIfNotStatus, setStatus, + setMultiStatus, setSub, TYPE_DOMAIN_USER, diff --git a/hydro/model/user.js b/hydro/model/user.js index 8e0608a38..f9923dfeb 100644 --- a/hydro/model/user.js +++ b/hydro/model/user.js @@ -44,7 +44,8 @@ class USER { } async function getInDomain(domainId, udoc) { - const dudoc = await document.getStatus(domainId, document.TYPE_DOMAIN_USER, 0, udoc._id); + let dudoc = await document.getStatus(domainId, document.TYPE_DOMAIN_USER, 0, udoc._id); + dudoc = dudoc || {}; if (udoc._id === 1) dudoc.role = 'guest'; if (udoc.priv === 1) dudoc.role = 'admin'; const p = await document.get(domainId, document.TYPE_DOMAIN_USER, dudoc.role || 'default'); @@ -154,6 +155,10 @@ function setInDomain(domainId, uid, params) { return document.setStatus(domainId, document.TYPE_DOMAIN_USER, 0, uid, params); } +function setMultiInDomain(domainId, query, params) { + return document.setMultiStatus(domainId, document.TYPE_DOMAIN_USER, query, params); +} + async function getPrefixList(prefix, limit = 50) { prefix = prefix.toLowerCase(); const $regex = new RegExp(`\\A\\Q${prefix.replace(/\\E/gmi, /\\E\\E\\Q/gmi)}\\E`, 'gmi'); @@ -222,6 +227,7 @@ global.Hydro.model.user = module.exports = { setEmail, setPassword, setInDomain, + setMultiInDomain, getMultiInDomain, getPrefixList, setRole, diff --git a/hydro/script/recalcRating.js b/hydro/script/recalcRating.js index 854d99d5d..13ed2936a 100644 --- a/hydro/script/recalcRating.js +++ b/hydro/script/recalcRating.js @@ -1,11 +1,22 @@ /* eslint-disable no-await-in-loop */ +const domain = require('../model/domain'); const contest = require('../model/contest'); const user = require('../model/user'); const rating = require('../lib/rating'); -async function run({ domainId }, report) { +async function run({ domainId, isSub = false }, report) { + if (!domainId) { + const domains = await domain.getMulti().toArray(); + await report({ total: domains.length }); + for (const i in domains) { + await run({ domainId: domains[i]._id, isSub: true }, report); + await report({ current: parseInt(i) + 1 }); + } + return true; + } const contests = await contest.getMulti(domainId).sort('endAt', -1).toArray(); await report({ total: contests.length }); + await user.setMultiInDomain(domainId, {}, { rating: 1500 }); for (const i in contests) { const tdoc = contests[i]; const tsdocs = await contest.getMultiStatus(domainId, { docId: tdoc.docId }) @@ -22,8 +33,10 @@ async function run({ domainId }, report) { tasks.push(user.setInDomain(domainId, udoc.uid, { rating: udoc.new })); } await Promise.all(tasks); - await report({ current: i + 1 }); + if (isSub) await report({ subProgress: (i + 1) / contests.length }); + else await report({ current: parseInt(i) + 1 }); } + return true; } global.Hydro.script.recalcRating = module.exports = { run }; diff --git a/hydro/service/monitor.js b/hydro/service/monitor.js index 7b97a48f1..96cb79653 100644 --- a/hydro/service/monitor.js +++ b/hydro/service/monitor.js @@ -4,9 +4,9 @@ const sysinfo = require('../lib/sysinfo'); const coll = db.collection('status'); async function update() { - const [_id, $set] = await sysinfo.update(); + const [mid, $set] = await sysinfo.update(); await coll.updateOne( - { _id }, + { mid, type: 'server' }, { $set: { ...$set, updateAt: new Date() } }, { upsert: true }, ); @@ -15,7 +15,7 @@ async function update() { async function postInit() { const info = await sysinfo.get(); await coll.updateOne( - { _id: info._id }, + { mid: info.mid, type: 'server' }, { $set: { ...info, updateAt: new Date(), type: 'server' } }, { upsert: true }, ); diff --git a/module/module-pastebin/handler.js b/module/module-pastebin/handler.js index 65acdc1cf..244a41ef9 100644 --- a/module/module-pastebin/handler.js +++ b/module/module-pastebin/handler.js @@ -47,6 +47,4 @@ async function apply() { nav('/paste/create', 'pastebin', 'pastebin', PERM_LOGGEDIN); } -global.Hydro.handler.pastebin = module.exports = { - PasteCreateHandler, PasteShowHandler, apply, -}; +global.Hydro.handler.pastebin = module.exports = apply; diff --git a/module/wiki/handler.js b/module/wiki/handler.js index f655ca37a..bfaeff48d 100644 --- a/module/wiki/handler.js +++ b/module/wiki/handler.js @@ -23,6 +23,4 @@ async function apply() { Route('/wiki/:name', module.exports.WikiHandler); } -global.Hydro.handler.wiki = module.exports = { - WikiHandler, apply, -}; +global.Hydro.handler.wiki = module.exports = apply; diff --git a/package.json b/package.json index 2f7ef5fa6..e3b7dd96c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hydro", - "version": "1.0.0", + "version": "2.0.0", "main": "hydro/development.js", "repository": "https://github.com/hydro-dev/Hydro.git", "author": "masnn", diff --git a/templates/domain_rank.html b/templates/domain_rank.html index 2b5d18243..6c6dfa554 100644 --- a/templates/domain_rank.html +++ b/templates/domain_rank.html @@ -7,7 +7,7 @@
- {% if not udocs %} + {% if not udocs.length %} {{ nothing.render('Sorry, there is no user in this domain.') }} {% else %} diff --git a/templates/manage_base.html b/templates/manage_base.html index b58020369..d261d0da9 100644 --- a/templates/manage_base.html +++ b/templates/manage_base.html @@ -13,6 +13,7 @@ diff --git a/templates/manage_script.html b/templates/manage_script.html new file mode 100644 index 000000000..4f0b5df86 --- /dev/null +++ b/templates/manage_script.html @@ -0,0 +1,56 @@ +{% extends "manage_base.html" %} +{% block manage_content %} + +
+
+

{{ _('Scripts') }}

+ +
+
+ {{ noscript_note.render() }} +
+ + + + + + + + + + + + + + {% for id, script in scripts %} + {% if not script.hidden %} + + + + + + {% endif %} + {% endfor %} + +
{{ _('ID') }}{{ _('Description') }}{{ _('Action') }}
{{ id }}{{ _(script.description|default(_('None'))) }} + {{ _('Run') }} +
+
+
+{% endblock %} diff --git a/templates/status.html b/templates/status.html index c982b34b3..f766fac23 100644 --- a/templates/status.html +++ b/templates/status.html @@ -25,11 +25,11 @@ {% for doc in stats %} - {{ doc._id|truncate(6, True, '') }} + {{ doc._id }} {{ doc.status }} {{ doc.osinfo }} {{ doc.cpu }} - {{ doc.memory }} + {{ doc.memory }} {% endfor %} diff --git a/tool/install.js b/tool/install.js new file mode 100644 index 000000000..f2f515b44 --- /dev/null +++ b/tool/install.js @@ -0,0 +1,17 @@ +const zlib = require('zlib'); +const fs = require('fs'); + +async function install() { + if (!global.Hydro) throw new Error('Data missing'); + if (!fs.existsSync('hydro')) fs.mkdirSync('hydro'); + const hydro = JSON.parse(zlib.gunzipSync(global.Hydro).toString()); + fs.writeFileSync('development.js', hydro.app); + for (const i in hydro.modules) { + fs.writeFileSync(`${i}.hydro`, Buffer.from(hydro.modules[i], 'base64')); + } +} + +install().catch((e) => { + console.error(e); + process.exit(1); +}); diff --git a/ui/pages/manage_module.page.js b/ui/pages/manage_module.page.js index a199221d8..9af79945f 100644 --- a/ui/pages/manage_module.page.js +++ b/ui/pages/manage_module.page.js @@ -18,7 +18,6 @@ const page = new NamedPage('manage_module', () => { }); addModuleDialog.clear = function () { this.$dom.find('[name="url"]').val(''); - this.$dom.find('[name="id"]').val(''); return this; }; @@ -26,12 +25,10 @@ const page = new NamedPage('manage_module', () => { const action = await addModuleDialog.clear().open(); if (action !== 'ok') return; const url = addModuleDialog.$dom.find('[name="url"]').val(); - const id = addModuleDialog.$dom.find('[name="id"]').val(); try { await request.post('', { operation: 'install', url, - id, }); window.location.reload(); } catch (error) { diff --git a/ui/pages/manage_script.page.js b/ui/pages/manage_script.page.js new file mode 100644 index 000000000..483ee1897 --- /dev/null +++ b/ui/pages/manage_script.page.js @@ -0,0 +1,40 @@ +import { NamedPage } from 'vj/misc/PageLoader'; +import Notification from 'vj/components/notification'; +import { ActionDialog } from 'vj/components/dialog'; + +import request from 'vj/utils/request'; + +const page = new NamedPage('manage_script', () => { + const runScriptDialog = new ActionDialog({ + $body: $('.dialog__body--run-script > div'), + onDispatch(action) { + const $args = runScriptDialog.$dom.find('[name="args"]'); + if (action === 'ok' && $args.val() === '') { + $args.focus(); + return false; + } + return true; + }, + }); + runScriptDialog.clear = function () { + this.$dom.find('[name="args"]').val(''); + return this; + }; + + window.runScript = async function (id) { + const action = await runScriptDialog.clear().open(); + if (action !== 'ok') return; + const args = runScriptDialog.$dom.find('[name="args"]').val(); + try { + await request.post('', { + args, + id, + }); + window.location.reload(); + } catch (error) { + Notification.error(error.message); + } + }; +}); + +export default page;