!UNSTABLE 优化数据库结构,为domain功能准备

This commit is contained in:
masnn committed 2020-05-29 13:05:52 +08:00
1 parent 7d27e56018
commit fff2172c38
40 files changed
+1411 -1012

No files matched your search

+53 -46
View File
@@ -14,24 +14,24 @@ const { Route, Handler } = require('../service/server');
const ContestHandler = contest.ContestHandlerMixin(Handler);
class ContestListHandler extends ContestHandler {
async get({ rule = 0, page = 1 }) {
async get({ domainId, rule = 0, page = 1 }) {
this.response.template = 'contest_main.html';
let tdocs;
let qs;
let tpcount;
if (!rule) {
tdocs = contest.getMulti().sort({ beginAt: -1 });
tdocs = contest.getMulti(domainId).sort({ beginAt: -1 });
qs = '';
} else {
if (!contest.CONTEST_RULES.includes(rule)) throw new ValidationError('rule');
tdocs = contest.getMulti({ rule }).sort({ beginAt: -1 });
tdocs = contest.getMulti(domainId, { rule }).sort({ beginAt: -1 });
qs = 'rule={0}'.format(rule);
}
// eslint-disable-next-line prefer-const
[tdocs, tpcount] = await paginate(tdocs, page, await system.get('CONTEST_PER_PAGE'));
const tids = [];
for (const tdoc of tdocs) tids.push(tdoc._id);
const tsdict = await contest.getListStatus(this.user._id, tids);
for (const tdoc of tdocs) tids.push(tdoc.docId);
const tsdict = await contest.getListStatus(domainId, this.user._id, tids);
const path = [
['Hydro', '/'],
['contest_main', null],
@@ -47,11 +47,11 @@ class ContestDetailHandler extends ContestHandler {
this.tdoc = await contest.get(tid);
}
async get({ page = 1 }) {
async get({ domainId, page = 1 }) {
this.response.template = 'contest_detail.html';
const [tsdoc, pdict] = await Promise.all([
contest.getStatus(this.tdoc._id, this.user._id),
problem.getList(this.tdoc.pids),
contest.getStatus(domainId, this.tdoc.docId, this.user._id),
problem.getList(domainId, this.tdoc.pids),
]);
const psdict = {}; let rdict = {}; let
attended;
@@ -61,7 +61,7 @@ class ContestDetailHandler extends ContestHandler {
if (this.canShowRecord(this.tdoc)) {
const q = [];
for (const i in psdict) q.push(psdict[i].rid);
rdict = await record.getList(q);
rdict = await record.getList(domainId, q);
} else {
for (const i in psdict) rdict[psdict[i].rid] = { _id: psdict[i].rid };
}
@@ -77,16 +77,16 @@ class ContestDetailHandler extends ContestHandler {
};
}
async postAttend() {
if (contest.isDone(this.tdoc)) throw new ContestNotLiveError(this.tdoc._id);
await contest.attend(this.tdoc._id, this.user._id);
async postAttend({ domainId }) {
if (contest.isDone(this.tdoc)) throw new ContestNotLiveError(this.tdoc.docId);
await contest.attend(domainId, this.tdoc.docId, this.user._id);
this.back();
}
}
class ContestScoreboardHandler extends ContestDetailHandler {
async get({ tid }) {
const [tdoc, rows, udict] = await this.getScoreboard(tid);
async get({ domainId, tid }) {
const [tdoc, rows, udict] = await this.getScoreboard(domainId, tid);
const path = [
['Hydro', '/'],
['contest_main', '/c'],
@@ -101,32 +101,33 @@ class ContestScoreboardHandler extends ContestDetailHandler {
}
class ContestScoreboardDownloadHandler extends ContestDetailHandler {
async get({ tid, ext }) {
async get({ domainId, tid, ext }) {
const getContent = {
csv: async (rows) => `\uFEFF${rows.map((c) => (c.map((i) => i.value).join(','))).join('\n')}`,
html: (rows) => this.renderHTML('contest_scoreboard_download_html.html', { rows }),
};
if (!getContent[ext]) throw new ValidationError('ext');
const [, rows] = await this.getScoreboard(tid, true);
const [, rows] = await this.getScoreboard(domainId, tid, true);
this.binary(await getContent[ext](rows), `${this.tdoc.title}.${ext}`);
}
}
class ContestEditHandler extends ContestDetailHandler {
async prepare({ tid }) {
const tdoc = await contest.get(tid);
if (!tdoc.owner !== this.user._id) this.checkPerm(PERM_EDIT_CONTEST);
async prepare() {
if (this.tdoc.owner !== this.user._id) this.checkPerm(PERM_EDIT_CONTEST);
}
async get() {
this.response.template = 'contest_edit.html';
const rules = {};
for (const i in contest.RULES) { rules[i] = contest.RULES[i].TEXT; }
for (const i in contest.RULES) {
rules[i] = contest.RULES[i].TEXT;
}
const duration = (this.tdoc.endAt.getTime() - this.tdoc.beginAt.getTime()) / 3600 / 1000;
const path = [
['Hydro', '/'],
['contest_main', '/c'],
[this.tdoc.title, `/c/${this.tdoc._id}`, true],
[this.tdoc.title, `/c/${this.tdoc.docId}`, true],
['contest_edit', null],
];
const dt = this.tdoc.beginAt;
@@ -143,7 +144,7 @@ class ContestEditHandler extends ContestDetailHandler {
}
async post({
beginAtDate, beginAtTime, duration, title, content, rule, pids,
domainId, beginAtDate, beginAtTime, duration, title, content, rule, pids,
}) {
let beginAt;
try {
@@ -153,36 +154,35 @@ class ContestEditHandler extends ContestDetailHandler {
}
const endAt = new Date(beginAt + duration * 3600 * 1000);
if (beginAt >= endAt) throw new ValidationError('duration');
pids = await this.verifyProblems(pids);
await contest.edit(this.tdoc._id, title, content, rule, beginAt, endAt, pids);
pids = await this.verifyProblems(domainId, pids);
await contest.edit(domainId, this.tdoc.docId, title, content, rule, beginAt, endAt, pids);
if (this.tdoc.beginAt !== beginAt || this.tdoc.endAt !== endAt
|| Array.isDiff(this.tdoc.pids, pids) || this.tdoc.rule !== rule) {
await contest.recalcStatus(this.tdoc._id);
await contest.recalcStatus(this.tdoc.docId);
}
if (this.preferJson) this.response.body = { tid: this.tdoc._id };
else this.response.redirect = `/c/${this.tdoc._id}`;
if (this.preferJson) this.response.body = { tid: this.tdoc.docId };
else this.response.redirect = `/c/${this.tdoc.docId}`;
}
}
class ContestProblemHandler extends ContestDetailHandler {
async prepare({ tid, pid }) {
async prepare({ domainId, tid, pid }) {
[this.tdoc, this.pdoc] = await Promise.all([
contest.get(tid),
problem.get(pid, this.user._id),
contest.get(domainId, tid),
problem.get(domainId, pid, this.user._id),
]);
[this.tsdoc, this.udoc] = await Promise.all([
contest.getStatus(this.tdoc._id, this.user._id),
user.getById(this.tdoc.owner),
contest.getStatus(domainId, this.tdoc.docId, this.user._id),
user.getById(domainId, this.tdoc.owner),
]);
this.attended = this.tsdoc && this.tsdoc.attend === 1;
this.response.template = 'problem_detail.html';
if (contest.isDone(this.tdoc)) {
if (!this.attended) throw new ContestNotAttendedError(this.tdoc._id);
if (!contest.isOngoing(this.tdoc)) throw new ContestNotLiveError(this.tdoc._id);
if (!this.attended) throw new ContestNotAttendedError(this.tdoc.docId);
if (!contest.isOngoing(this.tdoc)) throw new ContestNotLiveError(this.tdoc.docId);
}
console.log(this.tdoc.pids, this.pdoc._id);
if (!this.tdoc.pids.map((s) => s.toString()).includes(this.pdoc._id.toString())) {
throw new ProblemNotFoundError(pid, this.tdoc._id);
if (!this.tdoc.pids.map((s) => s.toString()).includes(this.pdoc.docId.toString())) {
throw new ProblemNotFoundError(pid, this.tdoc.docId);
}
}
@@ -205,10 +205,10 @@ class ContestProblemHandler extends ContestDetailHandler {
}
class ContestProblemSubmitHandler extends ContestProblemHandler {
async get({ tid, pid }) {
async get({ domainId, tid, pid }) {
let rdocs = [];
if (this.canShowRecord(this.tdoc)) {
rdocs = await record.getUserInProblemMulti(this.user._id, this.pdoc._id, true)
rdocs = await record.getUserInProblemMulti(domainId, this.user._id, this.pdoc.docId, true)
.sort({ _id: -1 }).limit(10).toArray();
}
this.response.template = 'problem_submit.html';
@@ -230,12 +230,19 @@ class ContestProblemSubmitHandler extends ContestProblemHandler {
};
}
async post({ tid, lang, code }) {
async post({
domainId, tid, lang, code,
}) {
await this.limitRate('add_record', 60, 100);
const rid = await record.add({
pid: this.pdoc._id, uid: this.user._id, lang, code, tid: this.tdoc._id, hidden: true,
const rid = await record.add(domainId, {
pid: this.pdoc.docId,
uid: this.user._id,
tid: this.tdoc.docId,
lang,
code,
hidden: true,
});
await contest.updateStatus(this.tdoc._id, this.user._id, rid, this.pdoc._id, false, 0);
await contest.updateStatus(domainId, this.tdoc.docId, this.user._id, rid, this.pdoc.docId, false, 0);
if (!this.canShowRecord(this.tdoc)) {
this.response.body = { tid };
this.response.redirect = `/c/${tid}`;
@@ -275,7 +282,7 @@ class ContestCreateHandler extends ContestHandler {
}
async post({
title, content, rule, beginAtDate, beginAtTime, duration, pids,
domainId, title, content, rule, beginAtDate, beginAtTime, duration, pids,
}) {
let beginAt;
try {
@@ -285,8 +292,8 @@ class ContestCreateHandler extends ContestHandler {
}
const endAt = new Date(beginAt.getTime() + duration * 3600 * 1000);
if (beginAt >= endAt) throw new ValidationError('duration');
pids = await this.verifyProblems(pids);
const tid = await contest.add(title, content, this.user._id, rule, beginAt, endAt, pids);
pids = await this.verifyProblems(domainId, pids);
const tid = await contest.add(domainId, title, content, this.user._id, rule, beginAt, endAt, pids);
this.response.body = { tid };
this.response.redirect = `/c/${tid}`;
}
+44 -47
View File
@@ -2,12 +2,8 @@ const paginate = require('../lib/paginate');
const system = require('../model/system');
const user = require('../model/user');
const discussion = require('../model/discussion');
const {
Route, Handler,
} = require('../service/server');
const {
DiscussionNotFoundError, DocumentNotFoundError,
} = require('../error');
const { Route, Handler } = require('../service/server');
const { DiscussionNotFoundError, DocumentNotFoundError } = require('../error');
const {
PERM_VIEW_DISCUSSION, PERM_EDIT_DISCUSSION, PERM_EDIT_DISCUSSION_REPLY,
PERM_DELETE_DISCUSSION, PERM_DELETE_DISCUSSION_REPLY, PERM_HIGHLIGHT_DISCUSSION,
@@ -16,21 +12,21 @@ const {
class DiscussionHandler extends Handler {
async _prepare({
type = 'node', docId, name, did, drid, drrid,
domainId, type = 'node', docId, name, did, drid, drrid,
}) {
this.checkPerm(PERM_VIEW_DISCUSSION);
docId = docId || name;
if (did) {
this.ddoc = await discussion.get(did);
this.ddoc = await discussion.get(domainId, did);
if (!this.ddoc) throw new DiscussionNotFoundError(did);
type = this.ddoc.parentType;
docId = this.ddoc.parentId;
if (drid) {
this.drdoc = await discussion.getReply(drid, did);
this.drdoc = await discussion.getReply(domainId, drid, did);
if (!this.drdoc) throw new DiscussionNotFoundError(drid);
if (this.drdoc.parent !== this.ddoc._id) throw new DocumentNotFoundError(drid);
if (drrid) {
[, this.drrdoc] = await discussion.getTailReply(drid, drrid);
[, this.drrdoc] = await discussion.getTailReply(domainId, drid, drrid);
if (!this.drrdoc) throw new DiscussionNotFoundError(drrid);
if (this.drrdoc.parent !== this.drdoc._id) {
throw new DocumentNotFoundError(drid);
@@ -41,23 +37,22 @@ class DiscussionHandler extends Handler {
// TODO(twd2): do more visibility check eg. contest
// TODO(twd2): exclude problem/contest discussions?
// TODO(iceboy): continuation based pagination.
this.vnode = await discussion.getVnode({ parentType: type, parentId: docId }, this);
this.vnode = await discussion.getVnode(domainId, { parentType: type, parentId: docId }, this);
if (this.ddoc) {
this.ddoc.parentType = this.ddoc.parentType || this.vnode.type;
this.ddoc.parentId = this.ddoc.parentId || this.vnode.id;
}
console.log(this.vnode);
}
}
class DiscussionMainHandler extends DiscussionHandler {
async get({ page = 1 }) {
async get({ domainId, page = 1 }) {
const [ddocs, dpcount] = await paginate(
discussion.getMulti(),
discussion.getMulti(domainId),
page,
await system.get('DISCUSSION_PER_PAGE'),
);
const udict = await user.getList(ddocs.map((ddoc) => ddoc.owner));
const udict = await user.getList(domainId, ddocs.map((ddoc) => ddoc.owner));
const path = [
['Hydro', '/'],
['discussion_main', null],
@@ -71,15 +66,15 @@ class DiscussionMainHandler extends DiscussionHandler {
class DiscussionNodeHandler extends DiscussionHandler {
async get({
type = 'node', docId, name, page = 1,
domainId, type = 'node', docId, name, page = 1,
}) {
docId = docId || name;
const [ddocs, dpcount] = await paginate(
discussion.getMulti({ type, docId }),
discussion.getMulti(domainId, { parentType: type, parentId: docId }),
page,
await system.get('DISCUSSION_PER_PAGE'),
);
const udict = await user.getList(ddocs.map((ddoc) => ddoc.owner));
const udict = await user.getList(domainId, ddocs.map((ddoc) => ddoc.owner));
const path = [
['Hydro', '/'],
['discussion_main', '/discuss'],
@@ -117,13 +112,13 @@ class DiscussionCreateHandler extends DiscussionHandler {
}
async post({
type = 'node', docId, name, title, content, highlight,
domainId, type = 'node', docId, name, title, content, highlight,
}) {
docId = docId || name;
this.limitRate('add_discussion', 3600, 30);
if (highlight) this.checkPerm(PERM_HIGHLIGHT_DISCUSSION);
const did = await discussion.add(
type, docId, this.user._id,
domainId, type, docId, this.user._id,
title, content, this.request.ip, highlight,
);
this.response.body = { did };
@@ -132,16 +127,16 @@ class DiscussionCreateHandler extends DiscussionHandler {
}
class DiscussionDetailHandler extends DiscussionHandler {
async get({ did, page = 1 }) {
async get({ domainId, did, page = 1 }) {
const dsdoc = this.user.hasPerm(PERM_LOGGEDIN)
? await discussion.getStatus(did, this.user._id)
? await discussion.getStatus(domainId, did, this.user._id)
: null;
const [drdocs, pcount, drcount] = await paginate(
discussion.getMultiReply(did),
discussion.getMultiReply(domainId, did),
page,
await system.get('REPLY_PER_PAGE'),
);
const uids = drdocs.map((drdoc) => drdoc.owner);
const uids = drdocs.map(domainId, (drdoc) => drdoc.owner);
uids.push(this.ddoc.owner);
for (const drdoc of drdocs) {
if (drdoc.reply) {
@@ -150,7 +145,7 @@ class DiscussionDetailHandler extends DiscussionHandler {
}
}
}
const udict = await user.getList(uids);
const udict = await user.getList(domainId, uids);
const path = [
['Hydro', '/'],
['discussion_main', '/discuss'],
@@ -163,54 +158,56 @@ class DiscussionDetailHandler extends DiscussionHandler {
};
}
async postReply({ did, content }) {
async postReply({ domainId, did, content }) {
this.checkPerm(PERM_LOGGEDIN);
this.checkPerm(PERM_REPLY_DISCUSSION);
this.limitRate('add_discussion', 3600, 30);
await discussion.addReply(did, this.user._id, content, this.request.ip);
await discussion.addReply(domainId, did, this.user._id, content, this.request.ip);
this.back();
}
async postTailReply({ drid, content }) {
async postTailReply({ domainId, drid, content }) {
this.checkPerm(PERM_LOGGEDIN);
this.checkPerm(PERM_REPLY_DISCUSSION);
this.limitRate('add_discussion', 3600, 30);
await discussion.addTailReply(drid, this.user._id, content, this.request.ip);
await discussion.addTailReply(domainId, drid, this.user._id, content, this.request.ip);
this.back();
}
async postEditReply({ drid, content }) {
async postEditReply({ domainId, drid, content }) {
if (this.drdoc.owner !== this.user._id) this.checkPerm(PERM_EDIT_DISCUSSION_REPLY);
await discussion.editReply(drid, content);
await discussion.editReply(domainId, drid, content);
this.back();
}
async postDeleteReply({ drid }) {
async postDeleteReply({ domainId, drid }) {
if (this.drdoc.owner !== this.user._id) this.checkPerm(PERM_DELETE_DISCUSSION_REPLY);
await discussion.deleteReply(drid);
await discussion.deleteReply(domainId, drid);
this.back();
}
async postEditTailReply({ drid, drrid, content }) {
async postEditTailReply({
domainId, drid, drrid, content,
}) {
if (this.drdoc.owner !== this.user._id) this.checkPerm(PERM_EDIT_DISCUSSION_REPLY);
await discussion.editTailReply(drid, drrid, content);
await discussion.editTailReply(domainId, drid, drrid, content);
this.back();
}
async postDeleteTailReply({ drid, drrid }) {
async postDeleteTailReply({ domainId, drid, drrid }) {
if (this.drrdoc.owner !== this.user._id) this.checkPerm(PERM_DELETE_DISCUSSION_REPLY);
await discussion.deleteTailReply(drid, drrid);
await discussion.deleteTailReply(domainId, drid, drrid);
this.back();
}
async postStar({ did, star }) {
await discussion.setStar(did, this.user._id, true);
async postStar({ domainId, did, star }) {
await discussion.setStar(domainId, did, this.user._id, true);
this.response.body = { star };
this.response.direct = this.request.path;
}
async postUnstar({ did, star }) {
await discussion.setStar(did, this.user._id, false);
async postUnstar({ domainId, did, star }) {
await discussion.setStar(domainId, did, this.user._id, false);
this.response.body = { star };
this.response.direct = this.request.path;
}
@@ -245,7 +242,7 @@ class DiscussionEditHandler extends DiscussionHandler {
['Hydro', '/'],
['discussion_main', '/discuss'],
[this.vnode.title, `/discuss/${this.ddoc.parentType}`, true],
[this.ddoc.title, `/discuss/${this.ddoc._id}`, true],
[this.ddoc.title, `/discuss/${this.ddoc.docId}`, true],
['discussion_edit', null],
];
this.response.template = 'discussion_edit.html';
@@ -253,20 +250,20 @@ class DiscussionEditHandler extends DiscussionHandler {
}
async post({
did, title, content, highlight, operation,
domainId, did, title, content, highlight, operation,
}) {
if (operation) return;
if (this.ddoc.owner !== this.user._id) this.checkPerm(PERM_EDIT_DISCUSSION);
if (highlight && !this.ddoc.highlight) this.checkPerm(PERM_HIGHLIGHT_DISCUSSION);
await discussion.edit(did, title, content, highlight);
await discussion.edit(domainId, did, title, content, highlight);
this.response.body = { did };
this.response.redirect = `/discuss/${did}`;
}
async postDelete({ did }) {
async postDelete({ domainId, did }) {
if (this.ddoc.owner !== this.user._id) this.checkPerm(PERM_DELETE_DISCUSSION);
await discussion.delete(did);
this.response.body = { type: this.ddoc.type, parent: this.ddoc.parent };
await discussion.delete(domainId, did);
this.response.body = { type: this.ddoc.parentType, parent: this.ddoc.parentId };
this.response.redirect = `/discuss/${this.ddoc.type}/${this.ddoc.parent}`;
}
}
+14 -14
View File
@@ -22,49 +22,49 @@ const {
} = require('../permission');
class HomeHandler extends Handler {
async contest() {
async contest(domainId) {
if (this.user.hasPerm(PERM_VIEW_CONTEST)) {
const tdocs = await contest.getMulti()
const tdocs = await contest.getMulti(domainId)
.limit(await system.get('CONTESTS_ON_MAIN'))
.toArray();
const tsdict = await contest.getListStatus(
this.user._id, tdocs.map((tdoc) => tdoc._id),
domainId, this.user._id, tdocs.map((tdoc) => tdoc.docId),
);
return [tdocs, tsdict];
}
return [[], {}];
}
async training() {
async training(domainId) {
if (this.user.hasPerm(PERM_VIEW_TRAINING)) {
const tdocs = await training.getMulti()
const tdocs = await training.getMulti(domainId)
.sort('_id', 1)
.limit(await system.get('TRAININGS_ON_MAIN'))
.toArray();
const tsdict = await training.getListStatus(
this.user._id, tdocs.map((tdoc) => tdoc._id),
domainId, this.user._id, tdocs.map((tdoc) => tdoc.docId),
);
return [tdocs, tsdict];
}
return [[], {}];
}
async discussion() {
async discussion(domainId) {
if (this.user.hasPerm(PERM_VIEW_DISCUSSION)) {
const ddocs = await discussion.getMulti()
const ddocs = await discussion.getMulti(domainId)
.limit(await system.get('DISCUSSIONS_ON_MAIN'))
.toArray();
const vndict = await discussion.getListVnodes(ddocs, this);
const vndict = await discussion.getListVnodes(domainId, ddocs, this);
return [ddocs, vndict];
}
return [[], {}];
}
async get() {
async get({ domainId }) {
const [[tdocs, tsdict], [trdocs, trsdict], [ddocs, vndict]] = await Promise.all([
this.contest(), this.training(), this.discussion(),
this.contest(domainId), this.training(domainId), this.discussion(domainId),
]);
const udict = await user.getList(ddocs.map((ddoc) => ddoc.owner));
const udict = await user.getList(domainId, ddocs.map((ddoc) => ddoc.owner));
this.response.template = 'main.html';
this.response.body = {
tdocs, tsdict, trdocs, trsdict, ddocs, vndict, udict,
@@ -153,7 +153,7 @@ class HomeSettingsHandler extends Handler {
async post(args) {
// FIXME validation
await user.setById(this.user._id, args);
await user.setById(domainId, this.user._id, args);
this.back();
}
}
@@ -206,7 +206,7 @@ class HomeMessagesHandler extends Handler {
}
async postSend({ uid, content, type = 'full' }) {
const udoc = await user.getById(uid);
const udoc = await user.getById('system', uid);
let mdoc = await message.send(this.user._id, uid, content);
if (type === 'single') {
mdoc = mdoc.reply[mdoc.reply.length - 1];
+5 -5
View File
@@ -20,7 +20,7 @@ class ProblemImportHandler extends Handler {
}
async post({
url, pid, hidden, remoteType,
domainId, url, pid, hidden, remoteType,
}) {
if (typeof global.Hydro.lib[`import.${remoteType}`] !== 'function') {
throw new ValidationError('remoteType');
@@ -28,10 +28,10 @@ class ProblemImportHandler extends Handler {
const [pdoc, testdata] = await global.Hydro.lib[`import.${remoteType}`](url, this);
if (pid) pdoc.pid = pid;
if (hidden) pdoc.hidden = true;
const _id = await problem.add(pdoc);
await problem.setTestdata(_id, testdata);
this.response.body = { pid: pid || _id };
this.response.redirect = `/p/${pid || _id}/settings`;
const docId = await problem.add(domainId, pdoc.title, pdoc.content, this.user._id, pdoc);
await problem.setTestdata(docId, testdata);
this.response.body = { pid: pid || docId };
this.response.redirect = `/p/${pid || docId}/settings`;
}
}
+9 -9
View File
@@ -16,14 +16,14 @@ async function _postJudge(rdoc) {
const tasks = [];
if (rdoc.tid) {
tasks.push(
contest.updateStatus(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.pid, rdoc.uid, rdoc._id, rdoc.status)) {
if (await problem.updateStatus(rdoc.rdoc.pid, rdoc.uid, rdoc._id, rdoc.status)) {
if (accept && !rdoc.rejudged) {
tasks.push(
problem.inc(rdoc.pid, 'nAccept', 1),
user.inc(rdoc.uid, 'nAccept', 1),
problem.inc(rdoc.domainId, rdoc.pid, 'nAccept', 1),
user.inc(rdoc.domainId, rdoc.uid, 'nAccept', 1),
);
}
}
@@ -31,7 +31,7 @@ async function _postJudge(rdoc) {
}
async function next(body) {
let rdoc = await record.get(body.rid);
let rdoc = await record.get(body.domainId, body.rid);
const $set = {};
const $push = {};
if (body.case) {
@@ -50,12 +50,12 @@ async function next(body) {
if (body.score) $set.score = body.score;
if (body.time_ms) $set.time = body.time_ms;
if (body.memory_kb) $set.memory = body.memory_kb;
rdoc = await record.update(body.rid, $set, $push);
rdoc = await record.update(body.domainId, body.rid, $set, $push);
bus.publish('record_change', rdoc);
}
async function end(body) {
let rdoc = await record.get(body.rid);
let rdoc = await record.get(body.domainId, body.rid);
const $set = {};
const $push = {};
if (body.case) {
@@ -76,9 +76,9 @@ async function end(body) {
if (body.memory_kb) $set.memory = body.memory_kb;
$set.judgeAt = new Date();
$set.judger = body.judger;
rdoc = await record.update(body.rid, $set, $push);
rdoc = await record.update(body.domainId, body.rid, $set, $push);
await _postJudge(rdoc);
rdoc = await record.update(body.rid, $set, $push);
rdoc = await record.update(body.domainId, body.rid, $set, $push);
}
class JudgeHandler extends Handler {
+13 -13
View File
@@ -8,9 +8,9 @@ const loader = require('../loader');
const { RoleAlreadyExistError, ValidationError } = require('../error');
class ManageHandler extends Handler {
async prepare() {
async prepare({ domainId }) {
this.checkPerm(PERM_MANAGE);
this.system = await user.getById(0);
this.system = await user.getById(domainId, 0);
}
}
@@ -53,13 +53,13 @@ class ManageEditHandler extends ManageHandler {
}
class ManageUserHandler extends ManageHandler {
async get() {
async get({ domainId }) {
const uids = [];
const rudocs = {};
const [udocs, roles, sys] = await Promise.all([
user.getMulti({ role: { $nin: ['default', 'guest'] } }).toArray(),
user.getRoles(),
user.getById(0),
user.getMulti(domainId, { role: { $nin: ['default', 'guest'] } }).toArray(),
user.getRoles(domainId),
user.getById(domainId, 0),
]);
for (const role of roles) rudocs[role._id] = [];
for (const udoc of udocs) {
@@ -67,7 +67,7 @@ class ManageUserHandler extends ManageHandler {
rudocs[udoc.role].push(udoc);
}
const rolesSelect = roles.map((role) => [role._id, role._id]);
const udict = await user.getList(uids);
const udict = await user.getList(domainId, uids);
const path = [
['Hydro', '/'],
['manage', '/manage'],
@@ -86,10 +86,10 @@ class ManageUserHandler extends ManageHandler {
}
class ManagePermissionHandler extends ManageHandler {
async get() {
async get({ domainId }) {
const [roles, sys] = await Promise.all([
user.getRoles(),
user.getById(0),
user.getRoles(domainId),
user.getById(domainId, 0),
]);
const path = [
['Hydro', '/'],
@@ -114,10 +114,10 @@ class ManagePermissionHandler extends ManageHandler {
}
class ManageRoleHandler extends ManageHandler {
async get() {
async get({ domainId }) {
const [roles, sys] = await Promise.all([
user.getRoles(),
user.getById(0),
user.getRoles(domainId),
user.getById(domainId, 0),
]);
const path = [
['Hydro', '/'],
+84 -71
View File
@@ -28,19 +28,21 @@ class ProblemHandler extends Handler {
this.checkPerm(PERM_VIEW_PROBLEM);
}
async get({ page = 1, category = '' }) {
async get({ domainId, page = 1, category = '' }) {
this.response.template = 'problem_main.html';
const q = {};
let psdict = {};
if (category) q.$or = [{ category }, { tag: category }];
if (!this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
const [pdocs, ppcount, pcount] = await paginate(
problem.getMulti(q).sort({ pid: 1 }),
problem.getMulti(domainId, q).sort({ pid: 1 }),
page,
await system.get('PROBLEM_PER_PAGE'),
);
if (this.user.hasPerm(PERM_LOGGEDIN)) {
psdict = await problem.getListStatus(this.user._id, pdocs.map((pdoc) => pdoc._id));
psdict = await problem.getListStatus(
domainId, this.user._id, pdocs.map((pdoc) => pdoc.docId),
);
}
const path = [
['Hydro', '/'],
@@ -75,7 +77,7 @@ class ProblemHandler extends Handler {
}
class ProblemCategoryHandler extends ProblemHandler {
async get({ page = 1, category }) {
async get({ domainId, page = 1, category }) {
this.response.template = 'problem_main.html';
const q = {
$or: [{ category }, { tag: category }],
@@ -83,12 +85,14 @@ class ProblemCategoryHandler extends ProblemHandler {
let psdict = {};
if (!this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
const [pdocs, ppcount, pcount] = await paginate(
problem.getMulti(q).sort({ pid: 1 }),
problem.getMulti(domainId, q).sort({ pid: 1 }),
page,
await system.get('PROBLEM_PER_PAGE'),
);
if (this.user.hasPerm(PERM_LOGGEDIN)) {
psdict = await problem.getListStatus(this.user._id, pdocs.map((pdoc) => pdoc._id));
psdict = await problem.getListStatus(
domainId, this.user._id, pdocs.map((pdoc) => pdoc.docId),
);
}
const path = [
['Hydro', '/'],
@@ -102,10 +106,10 @@ class ProblemCategoryHandler extends ProblemHandler {
}
class ProblemRandomHandler extends ProblemHandler {
async get() {
async get({ domainId }) {
const q = {};
if (!this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
const pid = await problem.random(q);
const pid = await problem.random(domainId, q);
if (!pid) throw new NoProblemError();
this.response.body = { pid };
this.response.redirect = `/p/${pid}`;
@@ -113,15 +117,17 @@ class ProblemRandomHandler extends ProblemHandler {
}
class ProblemDetailHandler extends ProblemHandler {
async _prepare({ pid }) {
async _prepare({ domainId, pid }) {
this.response.template = 'problem_detail.html';
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 (pid) {
this.pdoc = await problem.get(domainId, pid, this.uid);
if (this.pdoc.hidden && this.pdoc.owner !== this.uid) {
this.checkPerm(PERM_VIEW_PROBLEM_HIDDEN);
}
if (this.pdoc) this.udoc = await user.getById(domainId, this.pdoc.owner);
}
if (this.pdoc) this.udoc = await user.getById(this.pdoc.owner);
this.response.body = {
pdoc: this.pdoc,
udoc: this.udoc,
@@ -143,9 +149,9 @@ class ProblemSubmitHandler extends ProblemDetailHandler {
this.checkPerm(PERM_SUBMIT_PROBLEM);
}
async get() {
async get({ domainId }) {
this.response.template = 'problem_submit.html';
const rdocs = await record.getUserInProblemMulti(this.uid, this.pdoc._id)
const rdocs = await record.getUserInProblemMulti(domainId, this.uid, this.pdoc.docId)
.sort({ _id: -1 })
.limit(10)
.toArray();
@@ -163,13 +169,13 @@ class ProblemSubmitHandler extends ProblemDetailHandler {
};
}
async post() {
async post({ domainId }) {
const { lang, code } = this.request.body;
const rid = await record.add({
uid: this.uid, lang, code, pid: this.pdoc._id,
const rid = await record.add(domainId, {
uid: this.uid, lang, code, pid: this.pdoc.docId,
});
await Promise.all([
record.judge(rid),
record.judge(domainId, rid),
user.inc(this.user._id, 'nSubmit'),
]);
this.response.body = { rid };
@@ -178,25 +184,32 @@ class ProblemSubmitHandler extends ProblemDetailHandler {
}
class ProblemPretestHandler extends ProblemDetailHandler {
async post({ lang, code, input }) {
async post({
domainId, lang, code, input,
}) {
this.limitRate('add_record', 60, 100);
const rid = await record.add({
uid: this.uid, lang, code, pid: this.pdoc._id, input,
const rid = await record.add(domainId, {
uid: this.uid, lang, code, pid: this.pdoc.docId, input,
});
await record.judge(rid);
await record.judge(domainId, rid);
this.response.body = { rid };
}
}
class ProblemPretestConnectionHandler extends ConnectionHandler {
async prepare({ pid }) {
async prepare({ domainId, pid }) {
this.pid = pid.toString();
this.domainId = domainId;
bus.subscribe(['record_change'], this.onRecordChange);
}
async onRecordChange(data) {
const rdoc = data.value;
if (rdoc.uid !== this.user._id || rdoc.pid.toString() !== this.pid) return;
if (
rdoc.uid !== this.user._id
|| rdoc.pid.toString() !== this.pid
|| rdoc.domainId !== this.domainId
) return;
if (rdoc.tid) return;
this.send({ rdoc });
}
@@ -207,8 +220,8 @@ class ProblemPretestConnectionHandler extends ConnectionHandler {
}
class ProblemStatisticsHandler extends ProblemDetailHandler {
async get() {
const udoc = await user.getById(this.pdoc.owner);
async get({ domainId }) {
const udoc = await user.getById(domainId, this.pdoc.owner);
const path = [
['problem_main', '/p'],
[this.pdoc.title, `/p/${this.pdoc.pid}`, true],
@@ -254,10 +267,10 @@ class ProblemEditHandler extends ProblemManageHandler {
this.response.body.page_name = 'problem_edit';
}
async post({ title, content }) {
async post({ domainId, title, content }) {
const pid = validator.checkPid(this.request.body.pid);
const pdoc = await problem.get(this.params.pid);
await problem.edit(pdoc._id, { title, content, pid });
const pdoc = await problem.get(domainId, this.params.pid);
await problem.edit(domainId, pdoc.docId, { title, content, pid });
this.response.redirect = `/p/${pid}`;
}
}
@@ -275,10 +288,10 @@ class ProblemDataUploadHandler extends ProblemManageHandler {
this.response.body.md5 = this.md5;
}
async post() {
async post({ domainId }) {
if (!this.request.files.file) throw new BadRequestError();
const r = fs.createReadStream(this.request.files.file.path);
await problem.setTestdata(this.pdoc._id, r);
await problem.setTestdata(domainId, this.pdoc.docId, r);
if (this.pdoc.data && typeof this.pdoc.data === 'object') {
const f = await file.getMeta(this.pdoc.data);
this.md5 = f.md5;
@@ -297,25 +310,25 @@ class ProblemDataDownloadHandler extends ProblemDetailHandler {
}
class ProblemSolutionHandler extends ProblemDetailHandler {
async get({ page = 1 }) {
async get({ domainId, page = 1 }) {
this.response.template = 'problem_solution.html';
this.checkPerm(PERM_VIEW_PROBLEM_SOLUTION);
const [psdocs, pcount, pscount] = await paginate(
solution.getMulti(this.pdoc._id),
solution.getMulti(domainId, this.pdoc.docId),
page,
await system.get('SOLUTION_PER_PAGE'),
);
const uids = [this.pdoc.owner];
const docids = [];
for (const psdoc of psdocs) {
docids.push(psdoc._id);
docids.push(psdoc.docId);
uids.push(psdoc.owner);
if (psdoc.reply.length) {
for (const psrdoc of psdoc.reply) uids.push(psrdoc.owner);
}
}
const udict = await user.getList(uids);
const pssdict = solution.getListStatus(docids, this.uid);
const pssdict = solution.getListStatus(domainId, docids, this.uid);
const path = [
['problem_main', '/p'],
[this.pdoc.title, `/p/${this.pdoc.pid}`, true],
@@ -326,77 +339,79 @@ class ProblemSolutionHandler extends ProblemDetailHandler {
};
}
async post({ psid }) {
if (psid) this.psdoc = await solution.get(psid);
async post({ domainId, psid }) {
if (psid) this.psdoc = await solution.get(domainId, psid);
}
async postSubmit({ content }) {
async postSubmit({ domainId, content }) {
this.checkPerm(PERM_CREATE_PROBLEM_SOLUTION);
await solution.add(this.pdoc._id, this.uid, content);
await solution.add(domainId, this.pdoc.docId, this.uid, content);
this.back();
}
async postEditSolution({ content }) {
async postEditSolution({ domainId, content }) {
if (this.psdoc.owner !== this.uid) this.checkPerm(PERM_EDIT_PROBLEM_SOLUTION);
this.psdoc = await solution.edit(this.psdoc._id, content);
this.psdoc = await solution.edit(domainId, this.psdoc.docId, content);
this.ctx.body.psdoc = this.psdoc;
this.back();
}
async postDeleteSolution() {
async postDeleteSolution({ domainId }) {
if (this.psdoc.owner !== this.uid) this.checkPerm(PERM_DELETE_PROBLEM_SOLUTION);
await solution.del(this.psdoc._id);
await solution.del(domainId, this.psdoc.docId);
this.back();
}
async postReply({ psid, content }) {
async postReply({ domainId, psid, content }) {
this.checkPerm(PERM_REPLY_PROBLEM_SOLUTION);
const psdoc = await solution.get(psid);
await solution.reply(psdoc._id, this.uid, content);
const psdoc = await solution.get(domainId, psid);
await solution.reply(domainId, psdoc.docId, this.uid, content);
}
async postEditReply({ content, psid, psrid }) {
const [psdoc, psrdoc] = await solution.getReply(psid, psrid);
if ((!psdoc) || psdoc.pid !== this.pdoc._id) throw new SolutionNotFoundError(psid);
async postEditReply({
domainId, content, psid, psrid,
}) {
const [psdoc, psrdoc] = await solution.getReply(domainId, psid, psrid);
if ((!psdoc) || psdoc.pid !== this.pdoc.docId) throw new SolutionNotFoundError(psid);
if (psrdoc.owner !== this.uid) this.checkPerm(PERM_EDIT_PROBLEM_SOLUTION_REPLY);
await solution.editReply(psid, psrid, content);
await solution.editReply(domainId, psid, psrid, content);
}
async postDeleteReply({ psid, psrid }) {
const [psdoc, psrdoc] = await solution.getReply(psid, psrid);
if ((!psdoc) || psdoc.pid !== this.pdoc._id) throw new SolutionNotFoundError(psid);
async postDeleteReply({ domainId, psid, psrid }) {
const [psdoc, psrdoc] = await solution.getReply(domainId, psid, psrid);
if ((!psdoc) || psdoc.pid !== this.pdoc.docId) throw new SolutionNotFoundError(psid);
if (psrdoc.owner !== this.uid) this.checkPerm(PERM_EDIT_PROBLEM_SOLUTION_REPLY);
await solution.delReply(psid, psrid);
await solution.delReply(domainId, psid, psrid);
this.back();
}
async postUpvote() {
const [psdoc, pssdoc] = await solution.vote(this.psdoc._id, this.uid, 1);
async postUpvote({ domainId }) {
const [psdoc, pssdoc] = await solution.vote(domainId, this.psdoc.docId, this.uid, 1);
this.response.body = { vote: psdoc.vote, user_vote: pssdoc.vote };
if (!this.preferJson) this.back();
this.back();
}
async postDownvote() {
const [psdoc, pssdoc] = await solution.vote(this.psdoc._id, this.uid, -1);
async postDownvote({ domainId }) {
const [psdoc, pssdoc] = await solution.vote(domainId, this.psdoc.docId, this.uid, -1);
this.response.body = { vote: psdoc.vote, user_vote: pssdoc.vote };
if (!this.preferJson) this.back();
this.back();
}
}
class ProblemSolutionRawHandler extends ProblemDetailHandler {
async get({ psid }) {
async get({ domainId, psid }) {
this.checkPerm(PERM_VIEW_PROBLEM_SOLUTION);
const psdoc = await solution.get(psid);
const psdoc = await solution.get(domainId, psid);
this.response.type = 'text/markdown';
this.response.body = psdoc.content;
}
}
class ProblemSolutionReplyRawHandler extends ProblemDetailHandler {
async get({ psid }) {
async get({ domainId, psid }) {
this.checkPerm(PERM_VIEW_PROBLEM_SOLUTION);
const [psdoc, psrdoc] = await solution.getReply(psid);
if ((!psdoc) || psdoc.pid !== this.pdoc._id) throw new SolutionNotFoundError(psid);
const [psdoc, psrdoc] = await solution.getReply(domainId, psid);
if ((!psdoc) || psdoc.pid !== this.pdoc.docId) throw new SolutionNotFoundError(psid);
this.response.type = 'text/markdown';
this.response.body = psrdoc.content;
}
@@ -417,12 +432,10 @@ class ProblemCreateHandler extends Handler {
}
async post({
title, pid, content, hidden,
domainId, title, pid, content, hidden,
}) {
validator.checkPid(pid);
pid = pid || await system.inc('pid');
await problem.add({
title, content, owner: this.user._id, pid, hidden,
pid = await problem.add(domainId, title, content, this.user._id, {
pid, hidden,
});
this.response.body = { pid };
this.response.redirect = `/p/${pid}/settings`;
+23 -15
View File
@@ -13,13 +13,13 @@ const {
} = require('../service/server');
class RecordListHandler extends Handler {
async get({ page = 1 }) {
async get({ domainId, page = 1 }) {
this.response.template = 'record_main.html';
const q = {};
const rdocs = await record.getMany(q, { _id: -1 }, page, await system.get('RECORD_PER_PAGE'));
const rdocs = await record.getMany(domainId, q, { _id: -1 }, page, await system.get('RECORD_PER_PAGE'));
const [udict, pdict] = await Promise.all([
user.getList(rdocs.map((rdoc) => rdoc.uid)),
problem.getList(rdocs.map((rdoc) => rdoc.pid)),
user.getList(domainId, rdocs.map((rdoc) => rdoc.uid)),
problem.getList(domainId, rdocs.map((rdoc) => rdoc.pid)),
]);
const path = [
['Hydro', '/'],
@@ -32,19 +32,23 @@ class RecordListHandler extends Handler {
}
class RecordDetailHandler extends Handler {
async get({ rid }) {
async get({ domainId, rid }) {
this.response.template = 'record_detail.html';
const rdoc = await record.get(rid);
const rdoc = await record.get(domainId, rid);
if (rdoc.hidden) this.checkPerm(PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
if (rdoc.uid !== this.user.uid && !this.user.hasPerm(PERM_READ_RECORD_CODE)) {
rdoc.code = null;
}
const pdoc = await problem.getById(rdoc.pid);
const [pdoc, udoc] = await Promise.all([
problem.get(domainId, rdoc.pid),
user.getById(domainId, rdoc.uid),
]);
this.response.body = {
path: [
['Hydro', '/'],
['record_detail', null],
],
udoc,
rdoc,
pdoc,
show_status: true,
@@ -53,16 +57,17 @@ class RecordDetailHandler extends Handler {
}
class RecordRejudgeHandler extends Handler {
async post({ rid }) {
async post({ domainId, rid }) {
this.checkPerm(PERM_REJUDGE);
const rdoc = await record.get(rid);
if (rdoc) await record.rejudge(rid);
const rdoc = await record.get(domainId, rid);
if (rdoc) await record.rejudge(domainId, rid);
this.back();
}
}
class RecordConnectionHandler extends ConnectionHandler {
async prepare() {
async prepare({ domainId }) {
this.domainId = domainId;
bus.subscribe(['record_change'], this.onRecordChange);
}
@@ -81,17 +86,20 @@ class RecordConnectionHandler extends ConnectionHandler {
const rdoc = data.value;
if (rdoc.tid && rdoc.tid.toString() !== this.tid) return;
// eslint-disable-next-line prefer-const
let [udoc, pdoc] = await Promise.all([user.getById(rdoc.uid), problem.getById(rdoc.pid)]);
let [udoc, pdoc] = await Promise.all([
user.getById(this.domainId, rdoc.uid),
problem.getById(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 }) });
}
}
class RecordDetailConnectionHandler extends contest.ContestHandlerMixin(ConnectionHandler) {
async prepare({ rid }) {
const rdoc = await record.get(rid);
async prepare({ domainId, rid }) {
const rdoc = await record.get(domainId, rid);
if (rdoc.tid) {
const tdoc = await contest.get(rdoc.tid);
const tdoc = await contest.get(domainId, rdoc.tid);
if (!this.canShowRecord(tdoc)) {
this.close();
return;
+15 -15
View File
@@ -32,7 +32,7 @@ async function _parseDagJson(dag) {
// eslint-disable-next-line no-await-in-loop
const pdoc = await problem.get(node.pids[i]); // FIXME no-await-in-loop
assert(pdoc, `Problem not found: ${node.pids[i]}`);
node.pids[i] = pdoc._id;
node.pids[i] = pdoc.docId;
}
const newNode = {
_id: parseInt(node._id),
@@ -63,7 +63,7 @@ class TrainingMainHandler extends TrainingHandler {
await system.get('TRAINING_PER_PAGE'),
);
const tids = new Set();
for (const tdoc of tdocs) tids.add(tdoc._id);
for (const tdoc of tdocs) tids.add(tdoc.docId);
const tsdict = {};
let tdict = {};
if (this.user.hasPerm(PERM_LOGGEDIN)) {
@@ -79,7 +79,7 @@ class TrainingMainHandler extends TrainingHandler {
for (const tid of tids) enrolledTids.delete(tid);
if (enrolledTids.size) tdict = await training.getList(Array.from(enrolledTids));
}
for (const tdoc in tdocs) tdict[tdoc._id] = tdoc;
for (const tdoc in tdocs) tdict[tdoc.docId] = tdoc;
const path = [
['Hydro', '/'],
['training_main', null],
@@ -92,16 +92,16 @@ class TrainingMainHandler extends TrainingHandler {
}
class TrainingDetailHandler extends TrainingHandler {
async get({ tid }) {
const tdoc = await training.get(tid);
async get({ domainId, tid }) {
const tdoc = await training.get(domainId, tid);
const pids = training.getPids(tdoc);
// TODO(twd2): check status, eg. test, hidden problem, ...
const f = this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN) ? {} : { hidden: false };
const [udoc, pdict] = await Promise.all([
user.getById(tdoc.owner),
problem.getList(pids, f),
user.getById(domainId, tdoc.owner),
problem.getList(domainId, pids, f),
]);
const psdict = await problem.getListStatus(this.user._id, Object.keys(pdict));
const psdict = await problem.getListStatus(domainId, this.user._id, Object.keys(pdict));
const donePids = new Set();
const progPids = new Set();
for (const pid in psdict) {
@@ -128,7 +128,7 @@ class TrainingDetailHandler extends TrainingHandler {
if (nsdoc.isDone) doneNids.add(node._id);
nsdict[node._id] = nsdoc;
}
const tsdoc = await training.setStatus(tdoc._id, this.user._id, {
const tsdoc = await training.setStatus(domainId, tdoc.docId, this.user._id, {
doneNids: Array.from(doneNids),
donePids: Array.from(donePids),
done: doneNids.size === tdoc.dag.length,
@@ -143,10 +143,10 @@ class TrainingDetailHandler extends TrainingHandler {
};
}
async postEnroll({ tid }) {
async postEnroll({ domainId, tid }) {
this.checkPerm(PERM_LOGGEDIN);
const tdoc = await training.get(tid);
await training.enroll(tdoc._id, this.user._id);
const tdoc = await training.get(domainId, tid);
await training.enroll(domainId, tdoc.docId, this.user._id);
this.back();
}
}
@@ -176,7 +176,7 @@ class TrainingCreateHandler extends TrainingHandler {
const pdocs = await problem.getMulti({
$or: [{ _id: { $in: pids } }, { pid: { $in: pids } }],
}).sort('_id', 1).toArray();
const existPids = pdocs.map((pdoc) => pdoc._id);
const existPids = pdocs.map((pdoc) => pdoc.docId);
const existPnames = pdocs.map((pdoc) => pdoc.pid);
if (pids.length !== existPids.length) {
for (const pid in pids) {
@@ -202,7 +202,7 @@ class TrainingEditHandler extends TrainingHandler {
const dag = JSON.stringify(tdoc.dag, null, 2);
const path = [
['training_main', '/t'],
[tdoc.title, `/t/${tdoc._id}`, true],
[tdoc.title, `/t/${tdoc.docId}`, true],
['training_edit', null],
];
this.response.template = 'training_edit.html';
@@ -225,7 +225,7 @@ class TrainingEditHandler extends TrainingHandler {
{ pid: { $in: pids } },
],
}).sort('_id', 1).toArray();
const existPids = pdocs.map((pdoc) => pdoc._id);
const existPids = pdocs.map((pdoc) => pdoc.docId);
const existPnames = pdocs.map((pdoc) => pdoc.pid);
if (pids.length !== existPids.length) {
for (const pid in pids) {
+10 -8
View File
@@ -15,8 +15,10 @@ class UserLoginHandler extends Handler {
this.response.template = 'user_login.html';
}
async post({ uname, password, rememberme = false }) {
const udoc = await user.getByUname(uname);
async post({
domainId, uname, password, rememberme = false,
}) {
const udoc = await user.getByUname(domainId, uname);
if (!udoc) throw new LoginError(uname);
if (udoc) udoc.checkPassword(password);
await user.setById(udoc._id, { loginat: new Date(), loginip: this.request.ip });
@@ -120,10 +122,10 @@ class UserLostPassHandler extends Handler {
}
class UserLostPassWithCodeHandler extends Handler {
async get({ code }) {
async get({ domainId, code }) {
const tdoc = await token.get(code, token.TYPE_LOSTPASS);
if (!tdoc) throw new InvalidTokenError(token.TYPE_LOSTPASS, code);
const udoc = await user.getById(tdoc.uid);
const udoc = await user.getById(domainId, tdoc.uid);
this.response.body = { uname: udoc.uname };
}
@@ -138,9 +140,9 @@ class UserLostPassWithCodeHandler extends Handler {
}
class UserDetailHandler extends Handler {
async get({ uid }) {
async get({ domainId, uid }) {
const isSelfProfile = this.user._id === uid;
const udoc = await user.getById(uid);
const udoc = await user.getById(domainId, uid);
if (!udoc) throw new UserNotFoundError(uid);
const sdoc = await token.getMostRecentSessionByUid(uid);
this.response.template = 'user_detail.html';
@@ -149,12 +151,12 @@ class UserDetailHandler extends Handler {
}
class UserSearchHandler extends Handler {
async get({ q, exactMatch = false }) {
async get({ domainId, q, exactMatch = false }) {
let udocs;
if (exactMatch) udocs = [];
else udocs = await user.getPrefixList(q, 20);
try {
const udoc = await user.getById(parseInt(q));
const udoc = await user.getById(domainId, parseInt(q));
if (udoc) udocs.push(udoc);
} catch (e) {
/* Ignore */