From 257d9814a9fd51ffcca9817230b985939aca37fd Mon Sep 17 00:00:00 2001 From: masnn Date: Fri, 29 May 2020 20:00:40 +0800 Subject: [PATCH] =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hydro/handler/contest.js | 6 +-- hydro/handler/judge.js | 2 +- hydro/handler/record.js | 4 +- hydro/loader.js | 9 +++-- hydro/model/file.js | 2 +- hydro/model/problem.js | 10 ++--- hydro/model/task.js | 1 + hydro/model/user.js | 4 ++ hydro/script/install.js | 38 ++++++++++--------- hydro/utils.js | 34 ++++++----------- templates/partials/problem_list.html | 2 +- .../partials/problem_sidebar_normal.html | 2 +- templates/problem_settings.html | 2 +- templates/record_detail.html | 2 +- templates/training_detail.html | 2 +- 15 files changed, 57 insertions(+), 63 deletions(-) diff --git a/hydro/handler/contest.js b/hydro/handler/contest.js index 81f32309..c13d2b8c 100644 --- a/hydro/handler/contest.js +++ b/hydro/handler/contest.js @@ -43,8 +43,8 @@ class ContestListHandler extends ContestHandler { } class ContestDetailHandler extends ContestHandler { - async _prepare({ tid }) { - this.tdoc = await contest.get(tid); + async _prepare({ domainId, tid }) { + this.tdoc = await contest.get(domainId, tid); } async get({ domainId, page = 1 }) { @@ -66,7 +66,7 @@ class ContestDetailHandler extends ContestHandler { for (const i in psdict) rdict[psdict[i].rid] = { _id: psdict[i].rid }; } } else attended = false; - const udict = await user.getList([this.tdoc.owner]); + const udict = await user.getList(domainId, [this.tdoc.owner]); const path = [ ['Hydro', '/'], ['contest_main', '/c'], diff --git a/hydro/handler/judge.js b/hydro/handler/judge.js index 8f2ebf59..11233b78 100644 --- a/hydro/handler/judge.js +++ b/hydro/handler/judge.js @@ -19,7 +19,7 @@ async function _postJudge(rdoc) { contest.updateStatus(rdoc.domainId, rdoc.tid, rdoc.uid, rdoc._id, rdoc.pid, accept, rdoc.score), ); } - if (await problem.updateStatus(rdoc.rdoc.pid, rdoc.uid, rdoc._id, rdoc.status)) { + if (await problem.updateStatus(rdoc.pid, rdoc.uid, rdoc._id, rdoc.status)) { if (accept && !rdoc.rejudged) { tasks.push( problem.inc(rdoc.domainId, rdoc.pid, 'nAccept', 1), diff --git a/hydro/handler/record.js b/hydro/handler/record.js index 0116199a..c59c2553 100644 --- a/hydro/handler/record.js +++ b/hydro/handler/record.js @@ -73,7 +73,7 @@ class RecordConnectionHandler extends ConnectionHandler { async message(msg) { for (const rid of msg.rids) { - const rdoc = await record.get(rid); // eslint-disable-line no-await-in-loop + const rdoc = await record.get(this.domainId, rid); // eslint-disable-line no-await-in-loop await this.onRecordChange({ value: rdoc }); // eslint-disable-line no-await-in-loop } } @@ -88,7 +88,7 @@ class RecordConnectionHandler extends ConnectionHandler { // eslint-disable-next-line prefer-const let [udoc, pdoc] = await Promise.all([ user.getById(this.domainId, rdoc.uid), - problem.getById(this.domainId, rdoc.pid), + problem.get(this.domainId, 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 }) }); diff --git a/hydro/loader.js b/hydro/loader.js index 23e73d55..2027209f 100644 --- a/hydro/loader.js +++ b/hydro/loader.js @@ -238,9 +238,10 @@ async function load() { await server.prepare(); await service(); const builtinModel = [ - 'document', 'blacklist', 'builtin', 'contest', 'message', - 'opcount', 'problem', 'record', 'setting', 'solution', - 'token', 'training', 'user', 'file', 'discussion', + 'builtin', 'document', 'domain', 'blacklist', 'opcount', + 'setting', 'token', 'user', 'problem', 'record', + 'contest', 'message', 'solution', 'training', 'file', + 'discussion', ]; for (const i of builtinModel) { const m = require(`./model/${i}`); @@ -250,7 +251,7 @@ async function load() { const dbVer = await system.get('db.ver'); if (dbVer !== 1) { const ins = require('./script/install'); - await ins.run(); + await ins.run('Root', 'rootroot'); } const builtinHandler = [ 'home', 'problem', 'record', 'judge', 'user', diff --git a/hydro/model/file.js b/hydro/model/file.js index 55809ff8..18f589e1 100644 --- a/hydro/model/file.js +++ b/hydro/model/file.js @@ -44,7 +44,7 @@ async function dec(_id) { return file.count; } -async function get(_id, secret = null) { +async function get(_id, secret) { const file = await coll.findOne({ _id }); if (typeof secret !== 'undefined') { const timestamp = _timestamp(); diff --git a/hydro/model/problem.js b/hydro/model/problem.js index 0f3cf210..8df78f35 100644 --- a/hydro/model/problem.js +++ b/hydro/model/problem.js @@ -1,10 +1,8 @@ -const { ObjectID } = require('bson'); const { STATUS_ACCEPTED } = require('./builtin').STATUS; const file = require('./file'); const document = require('./document'); const domain = require('./domain'); const { ProblemNotFoundError } = require('../error'); -const validator = require('../lib/validator'); /** * @typedef {import('../interface').Pdoc} Pdoc @@ -30,8 +28,6 @@ async function add(domainId, title, content, owner, { tag = [], hidden = false, }) { - validator.checkTitle(title); - validator.checkContent(content); const d = await domain.inc(domainId, 'pidCounter', 1); if (!pid) pid = d.pidCounter.toString(); return await document.add(domainId, content, owner, document.TYPE_PROBLEM, d.pidCounter, null, null, { @@ -46,7 +42,9 @@ async function add(domainId, title, content, owner, { * @returns {Pdoc} */ async function get(domainId, pid, uid = null) { - const pdoc = await document.get(domainId, document.TYPE_PROBLEM, pid); + 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) throw new ProblemNotFoundError(pid); if (uid) { pdoc.psdoc = await document.getStatus(domainId, document.TYPE_PROBLEM, pdoc.docId, uid); @@ -92,8 +90,6 @@ function getMultiStatus(domainId, query) { * @returns {Pdoc} */ function edit(domainId, _id, $set) { - if ($set.title) validator.checkTitle($set.title); - if ($set.content) validator.checkContent($set.content); return document.set(domainId, document.TYPE_CONTEST, _id, $set); } diff --git a/hydro/model/task.js b/hydro/model/task.js index 65c90215..95ccc40b 100644 --- a/hydro/model/task.js +++ b/hydro/model/task.js @@ -3,6 +3,7 @@ const db = require('../service/db'); const coll = db.collection('task'); async function add(task) { + console.log(task); const res = await coll.insertOne(task); return res.insertedId; } diff --git a/hydro/model/user.js b/hydro/model/user.js index edf19dc8..6d35e70e 100644 --- a/hydro/model/user.js +++ b/hydro/model/user.js @@ -38,6 +38,10 @@ class USER { } async function getPerm(domainId, udoc) { + if (udoc._id === 1) { // Is Guest + const p = await document.get(domainId, document.TYPE_DOMAIN_ROLE, 'guest'); + return p.content; + } if (udoc.priv === 1) { const p = await document.get(domainId, document.TYPE_DOMAIN_ROLE, 'admin'); return p.content; diff --git a/hydro/script/install.js b/hydro/script/install.js index 72ecbe47..18bd4bd2 100644 --- a/hydro/script/install.js +++ b/hydro/script/install.js @@ -4,7 +4,6 @@ const builtin = require('../model/builtin'); const discussion = require('../model/discussion'); const system = require('../model/system'); const domain = require('../model/domain'); -const user = require('../model/user'); const pwhash = require('../lib/hash.hydro'); const { udoc } = require('../interface'); @@ -20,7 +19,7 @@ const allowFail = async (func, ...args) => { return res; }; -async function run() { +async function run(username, password) { const def = { PROBLEM_PER_PAGE: 100, RECORD_PER_PAGE: 100, @@ -52,10 +51,8 @@ async function run() { tasks.push(system.set(key, def[key])); } await Promise.all(tasks); - const salt = String.random(); tasks = [ - allowFail(domain.add, 'system', 1), - user.setRole('system', 0, 'guest'), + allowFail(domain.add, 'system', 0), collUser.updateOne({ _id: 0 }, { $set: defaults({ _id: 0, @@ -74,20 +71,25 @@ async function run() { unameLower: 'guest', }, udoc), }, { upsert: true }), - collUser.updateOne({ _id: -1 }, { - $set: defaults({ - _id: -1, - mail: 'root@hydro', - mailLower: 'root@hydro', - uname: 'Root', - unameLower: 'root', - hash: pwhash.hash('rootroot', salt), - salt, - gravatar: 'root@hydro', - priv: 1, - }, udoc), - }, { upsert: true }), ]; + if (username && password) { + const salt = String.random(); + tasks.push( + collUser.updateOne({ _id: -1 }, { + $set: defaults({ + _id: -1, + mail: 'root@hydro', + mailLower: 'root@hydro', + uname: username, + unameLower: username.trim().toLowerCase(), + hash: pwhash.hash(password, salt), + salt, + gravatar: 'root@hydro', + priv: 1, + }, udoc), + }, { upsert: true }), + ); + } for (const category in builtin.DEFAULT_NODES) { const nodes = builtin.DEFAULT_NODES[category]; for (const node of nodes) { diff --git a/hydro/utils.js b/hydro/utils.js index b6f20bc1..35326136 100644 --- a/hydro/utils.js +++ b/hydro/utils.js @@ -1,23 +1,11 @@ -const map = [ - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', - 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', - 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', - 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', - 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', - 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', - '9', '0', -]; +const dict = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; String.random = function random(digit) { let str = ''; - for (let i = 1; i <= digit; i++) str += map[Math.floor(Math.random() * 62)]; + for (let i = 1; i <= digit; i++) str += dict[Math.floor(Math.random() * 62)]; return str; }; -/** - * @param {Array} a - * @param {Array} b - */ Array.isDiff = function isDiff(a, b) { if (a.length !== b.length) return true; a.sort(); @@ -43,15 +31,17 @@ Set.isSuperset = function isSuperset(set, subset) { } return true; }; -Set.union = function union(setA, setB) { - const _union = new Set(setA); - for (const elem of setB) _union.add(elem); - return _union; + +Set.union = function Union(setA, setB) { + const union = new Set(setA); + for (const elem of setB) union.add(elem); + return union; }; -Set.intersection = function intersection(setA, setB) { - const _intersection = new Set(); + +Set.intersection = function Intersection(setA, setB) { + const intersection = new Set(); for (const elem of setB) { - if (setA.has(elem)) _intersection.add(elem); + if (setA.has(elem)) intersection.add(elem); } - return _intersection; + return intersection; }; diff --git a/templates/partials/problem_list.html b/templates/partials/problem_list.html index f7577b7f..3e59946a 100644 --- a/templates/partials/problem_list.html +++ b/templates/partials/problem_list.html @@ -29,7 +29,7 @@ {% for pdoc in pdocs %} - {% set psdoc = psdict[pdoc['_id']] %} + {% set psdoc = psdict[pdoc['docId']] %} {% if handler.hasPerm(perm.PERM_LOGGEDIN) %} {% if psdoc['rid'] %} diff --git a/templates/partials/problem_sidebar_normal.html b/templates/partials/problem_sidebar_normal.html index a1468074..e64b92dc 100644 --- a/templates/partials/problem_sidebar_normal.html +++ b/templates/partials/problem_sidebar_normal.html @@ -129,7 +129,7 @@ {% if page_name != 'discussion_node' and page_name != 'discussion_detail' %} {% if handler.hasPerm(perm.PERM_LOGGEDIN) %}
{{ _('# My Submissions') }}
-
{{ pdoc['psdoc'].nSubmit|default(0) }}
+
{{ pdoc['psdoc'].nSubmit|default(0) }}
{% endif %} {% endif %}
{{ _('Accepted') }}
diff --git a/templates/problem_settings.html b/templates/problem_settings.html index 8353471a..a9c44381 100644 --- a/templates/problem_settings.html +++ b/templates/problem_settings.html @@ -10,7 +10,7 @@

{{ _('Hint') }}: {{ _('Dataset Format') }}, {{ _('An example of dataset') }}: {{ _('Download') }}

- {% if pdoc and (pdoc.owner == user._id or handler.hasPerm(perm.PERM_READ_PROBLEM_DATA)) %} + {% if pdoc and (pdoc.owner == handler.user._id or handler.hasPerm(perm.PERM_READ_PROBLEM_DATA)) %}

{{ _('Download Dataset') }}

{% endif %}
diff --git a/templates/record_detail.html b/templates/record_detail.html index 587487ff..0eac7d61 100644 --- a/templates/record_detail.html +++ b/templates/record_detail.html @@ -69,7 +69,7 @@ {{ tdoc.title }} {% endif %} - {% if pdoc and (user._id == pdoc.owner or handler.hasPerm(perm.PERM_READ_PROBLEM_DATA)) %} + {% if pdoc and (handler.user._id == pdoc.owner or handler.hasPerm(perm.PERM_READ_PROBLEM_DATA)) %}
{{ _('Problem Data') }}
{{ _('Download') }}
{% endif %} diff --git a/templates/training_detail.html b/templates/training_detail.html index 156de1d6..dcbf084e 100644 --- a/templates/training_detail.html +++ b/templates/training_detail.html @@ -115,7 +115,7 @@ {% if handler.hasPerm(perm.PERM_LOGGEDIN) %}
- +