错误修复

This commit is contained in:
masnn committed 2020-05-29 20:00:40 +08:00
1 parent fff2172c38
commit 257d9814a9
15 files changed
+57 -63

No files matched your search

+3 -3
View File
@@ -43,8 +43,8 @@ class ContestListHandler extends ContestHandler {
} }
class ContestDetailHandler extends ContestHandler { class ContestDetailHandler extends ContestHandler {
async _prepare({ tid }) { async _prepare({ domainId, tid }) {
this.tdoc = await contest.get(tid); this.tdoc = await contest.get(domainId, tid);
} }
async get({ domainId, page = 1 }) { 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 }; for (const i in psdict) rdict[psdict[i].rid] = { _id: psdict[i].rid };
} }
} else attended = false; } else attended = false;
const udict = await user.getList([this.tdoc.owner]); const udict = await user.getList(domainId, [this.tdoc.owner]);
const path = [ const path = [
['Hydro', '/'], ['Hydro', '/'],
['contest_main', '/c'], ['contest_main', '/c'],
+1 -1
View File
@@ -19,7 +19,7 @@ async function _postJudge(rdoc) {
contest.updateStatus(rdoc.domainId, rdoc.tid, rdoc.uid, rdoc._id, rdoc.pid, accept, rdoc.score), 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) { if (accept && !rdoc.rejudged) {
tasks.push( tasks.push(
problem.inc(rdoc.domainId, rdoc.pid, 'nAccept', 1), problem.inc(rdoc.domainId, rdoc.pid, 'nAccept', 1),
+2 -2
View File
@@ -73,7 +73,7 @@ class RecordConnectionHandler extends ConnectionHandler {
async message(msg) { async message(msg) {
for (const rid of msg.rids) { 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 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 // eslint-disable-next-line prefer-const
let [udoc, pdoc] = await Promise.all([ let [udoc, pdoc] = await Promise.all([
user.getById(this.domainId, rdoc.uid), 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; 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 }) }); this.send({ html: await this.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) });
+5 -4
View File
@@ -238,9 +238,10 @@ async function load() {
await server.prepare(); await server.prepare();
await service(); await service();
const builtinModel = [ const builtinModel = [
'document', 'blacklist', 'builtin', 'contest', 'message', 'builtin', 'document', 'domain', 'blacklist', 'opcount',
'opcount', 'problem', 'record', 'setting', 'solution', 'setting', 'token', 'user', 'problem', 'record',
'token', 'training', 'user', 'file', 'discussion', 'contest', 'message', 'solution', 'training', 'file',
'discussion',
]; ];
for (const i of builtinModel) { for (const i of builtinModel) {
const m = require(`./model/${i}`); const m = require(`./model/${i}`);
@@ -250,7 +251,7 @@ async function load() {
const dbVer = await system.get('db.ver'); const dbVer = await system.get('db.ver');
if (dbVer !== 1) { if (dbVer !== 1) {
const ins = require('./script/install'); const ins = require('./script/install');
await ins.run(); await ins.run('Root', 'rootroot');
} }
const builtinHandler = [ const builtinHandler = [
'home', 'problem', 'record', 'judge', 'user', 'home', 'problem', 'record', 'judge', 'user',
+1 -1
View File
@@ -44,7 +44,7 @@ async function dec(_id) {
return file.count; return file.count;
} }
async function get(_id, secret = null) { async function get(_id, secret) {
const file = await coll.findOne({ _id }); const file = await coll.findOne({ _id });
if (typeof secret !== 'undefined') { if (typeof secret !== 'undefined') {
const timestamp = _timestamp(); const timestamp = _timestamp();
+3 -7
View File
@@ -1,10 +1,8 @@
const { ObjectID } = require('bson');
const { STATUS_ACCEPTED } = require('./builtin').STATUS; const { STATUS_ACCEPTED } = require('./builtin').STATUS;
const file = require('./file'); const file = require('./file');
const document = require('./document'); const document = require('./document');
const domain = require('./domain'); const domain = require('./domain');
const { ProblemNotFoundError } = require('../error'); const { ProblemNotFoundError } = require('../error');
const validator = require('../lib/validator');
/** /**
* @typedef {import('../interface').Pdoc} Pdoc * @typedef {import('../interface').Pdoc} Pdoc
@@ -30,8 +28,6 @@ async function add(domainId, title, content, owner, {
tag = [], tag = [],
hidden = false, hidden = false,
}) { }) {
validator.checkTitle(title);
validator.checkContent(content);
const d = await domain.inc(domainId, 'pidCounter', 1); const d = await domain.inc(domainId, 'pidCounter', 1);
if (!pid) pid = d.pidCounter.toString(); if (!pid) pid = d.pidCounter.toString();
return await document.add(domainId, content, owner, document.TYPE_PROBLEM, d.pidCounter, null, null, { 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} * @returns {Pdoc}
*/ */
async function get(domainId, pid, uid = null) { 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 (!pdoc) throw new ProblemNotFoundError(pid);
if (uid) { if (uid) {
pdoc.psdoc = await document.getStatus(domainId, document.TYPE_PROBLEM, pdoc.docId, uid); pdoc.psdoc = await document.getStatus(domainId, document.TYPE_PROBLEM, pdoc.docId, uid);
@@ -92,8 +90,6 @@ function getMultiStatus(domainId, query) {
* @returns {Pdoc} * @returns {Pdoc}
*/ */
function edit(domainId, _id, $set) { 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); return document.set(domainId, document.TYPE_CONTEST, _id, $set);
} }
+1
View File
@@ -3,6 +3,7 @@ const db = require('../service/db');
const coll = db.collection('task'); const coll = db.collection('task');
async function add(task) { async function add(task) {
console.log(task);
const res = await coll.insertOne(task); const res = await coll.insertOne(task);
return res.insertedId; return res.insertedId;
} }
+4
View File
@@ -38,6 +38,10 @@ class USER {
} }
async function getPerm(domainId, udoc) { 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) { if (udoc.priv === 1) {
const p = await document.get(domainId, document.TYPE_DOMAIN_ROLE, 'admin'); const p = await document.get(domainId, document.TYPE_DOMAIN_ROLE, 'admin');
return p.content; return p.content;
+20 -18
View File
@@ -4,7 +4,6 @@ const builtin = require('../model/builtin');
const discussion = require('../model/discussion'); const discussion = require('../model/discussion');
const system = require('../model/system'); const system = require('../model/system');
const domain = require('../model/domain'); const domain = require('../model/domain');
const user = require('../model/user');
const pwhash = require('../lib/hash.hydro'); const pwhash = require('../lib/hash.hydro');
const { udoc } = require('../interface'); const { udoc } = require('../interface');
@@ -20,7 +19,7 @@ const allowFail = async (func, ...args) => {
return res; return res;
}; };
async function run() { async function run(username, password) {
const def = { const def = {
PROBLEM_PER_PAGE: 100, PROBLEM_PER_PAGE: 100,
RECORD_PER_PAGE: 100, RECORD_PER_PAGE: 100,
@@ -52,10 +51,8 @@ async function run() {
tasks.push(system.set(key, def[key])); tasks.push(system.set(key, def[key]));
} }
await Promise.all(tasks); await Promise.all(tasks);
const salt = String.random();
tasks = [ tasks = [
allowFail(domain.add, 'system', 1), allowFail(domain.add, 'system', 0),
user.setRole('system', 0, 'guest'),
collUser.updateOne({ _id: 0 }, { collUser.updateOne({ _id: 0 }, {
$set: defaults({ $set: defaults({
_id: 0, _id: 0,
@@ -74,20 +71,25 @@ async function run() {
unameLower: 'guest', unameLower: 'guest',
}, udoc), }, udoc),
}, { upsert: true }), }, { 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) { for (const category in builtin.DEFAULT_NODES) {
const nodes = builtin.DEFAULT_NODES[category]; const nodes = builtin.DEFAULT_NODES[category];
for (const node of nodes) { for (const node of nodes) {
+12 -22
View File
@@ -1,23 +1,11 @@
const map = [ const dict = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
'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',
];
String.random = function random(digit) { String.random = function random(digit) {
let str = ''; 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; return str;
}; };
/**
* @param {Array} a
* @param {Array} b
*/
Array.isDiff = function isDiff(a, b) { Array.isDiff = function isDiff(a, b) {
if (a.length !== b.length) return true; if (a.length !== b.length) return true;
a.sort(); a.sort();
@@ -43,15 +31,17 @@ Set.isSuperset = function isSuperset(set, subset) {
} }
return true; return true;
}; };
Set.union = function union(setA, setB) {
const _union = new Set(setA); Set.union = function Union(setA, setB) {
for (const elem of setB) _union.add(elem); const union = new Set(setA);
return _union; 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) { for (const elem of setB) {
if (setA.has(elem)) _intersection.add(elem); if (setA.has(elem)) intersection.add(elem);
} }
return _intersection; return intersection;
}; };
+1 -1
View File
@@ -29,7 +29,7 @@
</thead> </thead>
<tbody> <tbody>
{% for pdoc in pdocs %} {% for pdoc in pdocs %}
{% set psdoc = psdict[pdoc['_id']] %} {% set psdoc = psdict[pdoc['docId']] %}
<tr> <tr>
{% if handler.hasPerm(perm.PERM_LOGGEDIN) %} {% if handler.hasPerm(perm.PERM_LOGGEDIN) %}
{% if psdoc['rid'] %} {% if psdoc['rid'] %}
@@ -129,7 +129,7 @@
{% if page_name != 'discussion_node' and page_name != 'discussion_detail' %} {% if page_name != 'discussion_node' and page_name != 'discussion_detail' %}
{% if handler.hasPerm(perm.PERM_LOGGEDIN) %} {% if handler.hasPerm(perm.PERM_LOGGEDIN) %}
<dt>{{ _('# My Submissions') }}</dt> <dt>{{ _('# My Submissions') }}</dt>
<dd><a href="/r?pid={{ pdoc.docId }}&uid_or_name={{ handler.user['_id'] }}">{{ pdoc['psdoc'].nSubmit|default(0) }}</a></dd> <dd><a href="/r?pid={{ pdoc.docId }}&uid_or_name={{ handler.user._id }}">{{ pdoc['psdoc'].nSubmit|default(0) }}</a></dd>
{% endif %} {% endif %}
{% endif %} {% endif %}
<dt>{{ _('Accepted') }}</dt> <dt>{{ _('Accepted') }}</dt>
+1 -1
View File
@@ -10,7 +10,7 @@
<div class="section__body"> <div class="section__body">
<iframe src="/p/{{ pdoc.pid }}/upload" frameborder="0" style="width: 100%; height: 100px;"></iframe> <iframe src="/p/{{ pdoc.pid }}/upload" frameborder="0" style="width: 100%; height: 100px;"></iframe>
<p class="help-text">{{ _('Hint') }}: <a href="/wiki/help#upload">{{ _('Dataset Format') }}</a>, {{ _('An example of dataset') }}: <a href="https://github.com/vijos/jd4/blob/master/jd4/testdata/aplusb-legacy.zip?raw=true">{{ _('Download') }}</a></p> <p class="help-text">{{ _('Hint') }}: <a href="/wiki/help#upload">{{ _('Dataset Format') }}</a>, {{ _('An example of dataset') }}: <a href="https://github.com/vijos/jd4/blob/master/jd4/testdata/aplusb-legacy.zip?raw=true">{{ _('Download') }}</a></p>
{% 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)) %}
<p><a href="/p/{{ pdoc.pid }}/data" class="expanded button">{{ _('Download Dataset') }}</a></p> <p><a href="/p/{{ pdoc.pid }}/data" class="expanded button">{{ _('Download Dataset') }}</a></p>
{% endif %} {% endif %}
</div> </div>
+1 -1
View File
@@ -69,7 +69,7 @@
<a href="/c/{{ tdoc.docId }}">{{ tdoc.title }}</a> <a href="/c/{{ tdoc.docId }}">{{ tdoc.title }}</a>
</dd> </dd>
{% endif %} {% 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)) %}
<dt>{{ _('Problem Data') }}</dt> <dt>{{ _('Problem Data') }}</dt>
<dd><a href="/p/{{ pdoc.pid }}/data"><span class="icon icon-download"></span> {{ _('Download') }}</a></dd> <dd><a href="/p/{{ pdoc.pid }}/data"><span class="icon icon-download"></span> {{ _('Download') }}</a></dd>
{% endif %} {% endif %}
+1 -1
View File
@@ -115,7 +115,7 @@
<td class="col--name col--problem-name"> <td class="col--name col--problem-name">
{% if handler.hasPerm(perm.PERM_LOGGEDIN) %} {% if handler.hasPerm(perm.PERM_LOGGEDIN) %}
<form class="form--inline" action="/p" method="post"> <form class="form--inline" action="/p" method="post">
<input type="hidden" name="pid" value="{{ pdoc['_id'] }}"> <input type="hidden" name="pid" value="{{ pdoc.docId }}">
<input type="hidden" name="operation" value="{% if not psdoc['star'] %}star{% else %}unstar{% endif %}"> <input type="hidden" name="operation" value="{% if not psdoc['star'] %}star{% else %}unstar{% endif %}">
<input type="hidden" name="csrfToken" value="{{ handler.csrfToken }}"> <input type="hidden" name="csrfToken" value="{{ handler.csrfToken }}">
<button class="star{% if psdoc['star'] %} activated{% endif %}" type="submit"> <button class="star{% if psdoc['star'] %} activated{% endif %}" type="submit">