diff --git a/.gitpod.yml b/.gitpod.yml index 80515169..0a147cca 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -5,7 +5,3 @@ vscode: extensions: - dbaeumer.vscode-eslint@2.1.3:1NRvj3UKNTNwmYjptmUmIw== - vscode-icons-team.vscode-icons@10.2.0:DrQUJPtFm0woSFiyitha8Q== -ports: - - port: 8888 - onOpen: open-preview - - port: 27017 diff --git a/README.md b/README.md index 0ea1c01c..e1ebdfcc 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,7 @@ Hydro是一个高效的信息学在线测评系统。特点:易于部署,轻量,功能强大且易于扩展。 -[部署说明](docs/deploy.md) -[附加组件开发说明](docs/addon.md) -[Hydro开发说明](docs/development.md) +[中文文档](https://hydro-dev.github.io) [Hydro UI 传送门](https://github.com/hydro-dev/ui-default) 如果您认为本项目有价值,欢迎 star 。 diff --git a/hydro/entry/master.ts b/hydro/entry/master.ts index 1f12dfe0..9588005d 100644 --- a/hydro/entry/master.ts +++ b/hydro/entry/master.ts @@ -26,16 +26,9 @@ export async function load(call, args) { bus.subscribe(['system_database_connected'], h); require('../service/db'); }); - for (const i of builtinLib) require(`../lib/${i}`); - await lib(pending, fail); - require('../service/gridfs'); require('../service/monitor'); - const server = require('../service/server'); - await server.prepare(); - await service(pending, fail); for (const i of builtinModel) require(`../model/${i}`); for (const i of builtinHandler) require(`../handler/${i}`); - await model(pending, fail); for (const m in global.Hydro.model) { if (global.Hydro.model[m].ensureIndexes) { await global.Hydro.model[m].ensureIndexes(); diff --git a/hydro/handler/problem.ts b/hydro/handler/problem.ts index b1a3791f..e3ce3d79 100644 --- a/hydro/handler/problem.ts +++ b/hydro/handler/problem.ts @@ -172,7 +172,8 @@ class ProblemDetailHandler extends ProblemHandler { @param('pid', Types.String, null, parsePid) async _prepare(domainId: string, pid: number | string) { this.response.template = 'problem_detail.html'; - this.pdoc = await problem.get(domainId, pid, this.user._id, true); + this.pdoc = await problem.get(domainId, pid, this.user._id); + if (!this.pdoc) throw new ProblemNotFoundError(domainId, pid); if (this.pdoc.hidden && this.pdoc.owner !== this.user._id) { this.checkPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN); } diff --git a/hydro/handler/record.ts b/hydro/handler/record.ts index 19cc5fe4..f7b8e32e 100644 --- a/hydro/handler/record.ts +++ b/hydro/handler/record.ts @@ -72,7 +72,7 @@ class RecordDetailHandler extends RecordHandler { } if (rdoc.type !== 'run') rdoc.stdout = rdoc.stderr = ''; const [pdoc, udoc] = await Promise.all([ - problem.get(domainId, rdoc.pid, null, false), + problem.get(domainId, rdoc.pid, null), user.getById(domainId, rdoc.uid), ]); this.response.body = { @@ -138,7 +138,7 @@ class RecordMainConnectionHandler extends RecordConnectionHandler { // eslint-disable-next-line prefer-const let [udoc, pdoc] = await Promise.all([ user.getById(this.domainId, rdoc.uid), - problem.get(this.domainId, rdoc.pid, null, false), + problem.get(this.domainId, rdoc.pid, null), ]); if (pdoc && pdoc.hidden && !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) pdoc = null; else this.send({ html: await this.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) }); diff --git a/hydro/handler/user.ts b/hydro/handler/user.ts index 5f792fc5..ba6523c7 100644 --- a/hydro/handler/user.ts +++ b/hydro/handler/user.ts @@ -102,9 +102,7 @@ class UserRegisterWithCodeHandler extends Handler { if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code); if (password !== verifyPassword) throw new VerifyPasswordError(); const uid = await system.inc('user'); - await user.create({ - uid, uname, password, mail, regip: this.request.ip, - }); + await user.create(mail, uname, password, uid, this.request.ip); await token.del(code, token.TYPE_REGISTRATION); this.session.uid = uid; this.response.redirect = this.url('homepage'); @@ -331,9 +329,10 @@ class OauthCallbackHandler extends Handler { break; } } - const uid = await user.create({ - mail: r.email, uname: username, password: String.random(32), regip: this.request.ip, - }); + const uid = await user.create( + r.email, username, String.random(32), + undefined, this.request.ip, + ); const $set: any = { oauth: args.type, }; diff --git a/hydro/lib/template.ts b/hydro/lib/template.ts index aff95751..97811fd2 100644 --- a/hydro/lib/template.ts +++ b/hydro/lib/template.ts @@ -2,13 +2,14 @@ import fs from 'fs'; import path from 'path'; import serialize from 'serialize-javascript'; import nunjucks from 'nunjucks'; +import { argv } from 'yargs'; import * as markdown from './markdown'; import * as misc from './misc'; class Loader extends nunjucks.Loader { // eslint-disable-next-line class-methods-use-this getSource(name: string) { - if (!process.env.debug) { + if (!argv.template) { if (!global.Hydro.ui.template[name]) throw new Error(`Cannot get template ${name}`); return { src: global.Hydro.ui.template[name], @@ -17,12 +18,11 @@ class Loader extends nunjucks.Loader { }; } let fullpath = null; - const base = path.join(process.cwd(), 'templates'); - const p = path.resolve(base, name); + const p = path.resolve(argv.template as string, name); if (fs.existsSync(p)) fullpath = p; if (!fullpath) throw new Error(`Cannot get template ${name}`); return { - src: fs.readFileSync(fullpath, 'utf-8'), + src: fs.readFileSync(fullpath, 'utf-8').toString(), path: fullpath, noCache: true, }; diff --git a/hydro/model/problem.ts b/hydro/model/problem.ts index 2569e382..1c787045 100644 --- a/hydro/model/problem.ts +++ b/hydro/model/problem.ts @@ -42,7 +42,7 @@ export async function add( export async function get( domainId: string, pid: string | number, - uid: number = null, doThrow = true, + uid: number = null, ): Promise { if (typeof pid !== 'number') { if (!Number.isNaN(parseInt(pid, 10))) pid = parseInt(pid, 10); @@ -50,10 +50,7 @@ export async function get( const pdoc = Number.isInteger(pid) ? await document.get(domainId, document.TYPE_PROBLEM, pid) : (await document.getMulti(domainId, document.TYPE_PROBLEM, { pid }).toArray())[0]; - if (!pdoc) { - if (doThrow) throw new ProblemNotFoundError(domainId, pid); - return null; - } + if (!pdoc) return null; if (uid) { pdoc.psdoc = await document.getStatus(domainId, document.TYPE_PROBLEM, pdoc.docId, uid); } diff --git a/hydro/model/record.ts b/hydro/model/record.ts index 9d2e4978..1c4b5fce 100644 --- a/hydro/model/record.ts +++ b/hydro/model/record.ts @@ -62,7 +62,7 @@ export async function add(domainId: string, data: RdocBase, addTask: boolean): P judgeAt: null, }); const [pdoc, res] = await Promise.all([ - problem.get(domainId, data.pid, null, false), + problem.get(domainId, data.pid, null), coll.insertOne(data), ]); if (addTask) { diff --git a/hydro/model/user.ts b/hydro/model/user.ts index da1df668..bd3c9355 100644 --- a/hydro/model/user.ts +++ b/hydro/model/user.ts @@ -164,9 +164,10 @@ export async function inc(_id: number, field: string, n = 1) { return udoc; } -export async function create({ - uid = null, mail, uname, password, regip = '127.0.0.1', priv = PRIV.PRIV_DEFAULT, -}) { +export async function create( + mail: string, uname: string, password: string, + uid: number, regip = '127.0.0.1', priv = PRIV.PRIV_DEFAULT, +) { const salt = String.random(); if (!uid) uid = await system.inc('user'); try { diff --git a/hydro/script/install.ts b/hydro/script/install.ts index c4c3b43a..7ee98069 100644 --- a/hydro/script/install.ts +++ b/hydro/script/install.ts @@ -11,14 +11,7 @@ export async function run({ username = '', password = '' } = {}) { if (username && password) { const udoc = await user.getById('system', -1); if (!udoc) { - await user.create({ - uid: -1, - mail: 'root@hydro.local', - uname: username, - password, - regip: '127.0.0.1', - priv: PRIV.PRIV_ALL, - }); + await user.create('root@hydro.local', username, password, -1, '127.0.0.1', PRIV.PRIV_ALL); } else { const salt = String.random(); await user.setById(-1, { diff --git a/hydro/script/register.ts b/hydro/script/register.ts index 52e0b402..c9ff877e 100644 --- a/hydro/script/register.ts +++ b/hydro/script/register.ts @@ -1,17 +1,13 @@ import * as user from '../model/user'; -import * as system from '../model/system'; export const description = 'Create a new user'; export async function run({ uname, password, mail, uid, }) { - if (!uid) uid = await system.inc('user'); - else uid = parseInt(uid, 10); - if (Number.isNaN(uid)) throw new Error('uid'); - await user.create({ - uid, uname, password, mail, regip: '127.0.0.1', - }); + if (uid) uid = parseInt(uid, 10); + if (uid && Number.isNaN(uid)) throw new Error('uid'); + uid = await user.create(mail, uname, password, uid); return uid; } diff --git a/package.json b/package.json index e61d4874..63f5ee4b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hydrooj", - "version": "2.9.8", + "version": "2.9.9", "main": "dist/loader.js", "bin": "bin/hydrooj.js", "repository": "https://github.com/hydro-dev/Hydro.git", @@ -49,7 +49,7 @@ "@types/koa-morgan": "^1.0.4", "@types/koa-router": "^7.4.1", "@types/koa-static-cache": "^5.1.0", - "@types/lodash": "^4.14.157", + "@types/lodash": "^4.14.158", "@types/lru-cache": "^5.1.0", "@types/markdown-it": "^10.0.1", "@types/mongodb": "^3.5.25", diff --git a/yarn.lock b/yarn.lock index 6e94ddbf..b3769275 100644 --- a/yarn.lock +++ b/yarn.lock @@ -205,10 +205,10 @@ resolved "https://registry.yarnpkg.com/@types/linkify-it/-/linkify-it-2.1.0.tgz#ea3dd64c4805597311790b61e872cbd1ed2cd806" integrity sha512-Q7DYAOi9O/+cLLhdaSvKdaumWyHbm7HAk/bFwwyTuU0arR5yyCeW5GOoqt4tJTpDRxhpx9Q8kQL6vMpuw9hDSw== -"@types/lodash@^4.14.157": - version "4.14.157" - resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.157.tgz#fdac1c52448861dfde1a2e1515dbc46e54926dc8" - integrity sha512-Ft5BNFmv2pHDgxV5JDsndOWTRJ+56zte0ZpYLowp03tW+K+t8u8YMOzAnpuqPgzX6WO1XpDIUm7u04M8vdDiVQ== +"@types/lodash@^4.14.158": + version "4.14.158" + resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.158.tgz#b38ea8b6fe799acd076d7a8d7ab71c26ef77f785" + integrity sha512-InCEXJNTv/59yO4VSfuvNrZHt7eeNtWQEgnieIA+mIC+MOWM9arOWG2eQ8Vhk6NbOre6/BidiXhkZYeDY9U35w== "@types/lru-cache@^5.1.0": version "5.1.0"