题目搜索,收藏功能

This commit is contained in:
undefined committed 2020-06-24 13:39:32 +08:00
1 parent a248425db6
commit 441eeadaf3
23 files changed
+158 -97

No files matched your search

+17 -2
View File
@@ -14,6 +14,7 @@ const document = require('../model/document');
const system = require('../model/system');
const user = require('../model/user');
const setting = require('../model/setting');
const domain = require('../model/domain');
const discussion = require('../model/discussion');
const token = require('../model/token');
const training = require('../model/training');
@@ -87,10 +88,24 @@ class HomeHandler extends Handler {
this.homework(domainId), this.contest(domainId),
this.training(domainId), this.discussion(domainId),
]);
const udict = await user.getList(domainId, ddocs.map((ddoc) => ddoc.owner));
const [udict, dodoc, vnodes] = await Promise.all([
user.getList(domainId, ddocs.map((ddoc) => ddoc.owner)),
domain.get(domainId),
discussion.getNodes(domainId),
]);
this.response.template = 'main.html';
this.response.body = {
htdocs, htsdict, tdocs, tsdict, trdocs, trsdict, ddocs, vndict, udict,
htdocs,
htsdict,
tdocs,
tsdict,
trdocs,
trsdict,
ddocs,
vndict,
udict,
domain: dodoc,
vnodes,
};
}
}
+11 -9
View File
@@ -32,16 +32,18 @@ class ProblemImportHandler extends Handler {
if (pid) pdoc.pid = pid;
if (hidden) pdoc.hidden = true;
const docId = await problem.add(domainId, pdoc.title, pdoc.content, this.user._id, pdoc);
const file = path.resolve(os.tmpdir(), 'hydro', `import_${domainId}_${pid}.zip`);
const w = fs.createWriteStream(file);
await new Promise((resolve, reject) => {
w.on('finish', resolve);
w.on('error', reject);
testdata.pipe(w);
});
await problem.setTestdata(domainId, docId, file);
if (testdata) {
const file = path.resolve(os.tmpdir(), 'hydro', `import_${domainId}_${pid || pdoc.pid || docId}.zip`);
const w = fs.createWriteStream(file);
await new Promise((resolve, reject) => {
w.on('finish', resolve);
w.on('error', reject);
testdata.pipe(w);
});
await problem.setTestdata(domainId, docId, file);
}
this.response.body = { pid: pid || docId };
this.response.redirect = `/p/${pid || docId}/settings`;
this.response.redirect = this.url('problem_settings', { pid: pid || docId });
}
}
+25 -10
View File
@@ -27,14 +27,23 @@ class ProblemHandler extends Handler {
this.checkPerm(PERM_VIEW_PROBLEM);
}
async get({ domainId, page = 1, category = '' }) {
async get({ domainId, page = 1, q = '' }) {
this.response.template = 'problem_main.html';
const q = {};
const query = {};
let psdict = {};
if (category) q.$or = [{ category }, { tag: category }];
if (!this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
const path = [
['Hydro', 'homepage'],
['problem_main', null],
];
if (q) {
q = q.toLowerCase();
const $regex = new RegExp(`\\A\\Q${q.replace(/\\E/gmi, /\\E\\E\\Q/gmi)}\\E`, 'gmi');
query.title = { $regex };
path.push([q, null, null, true]);
}
if (!this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) query.hidden = false;
const [pdocs, ppcount, pcount] = await paginate(
problem.getMulti(domainId, q).sort({ pid: 1 }),
problem.getMulti(domainId, query).sort({ pid: 1 }),
page,
await system.get('PROBLEM_PER_PAGE'),
);
@@ -43,15 +52,21 @@ class ProblemHandler extends Handler {
domainId, this.user._id, pdocs.map((pdoc) => pdoc.docId),
);
}
const path = [
['Hydro', 'homepage'],
['problem_main', null],
];
this.response.body = {
path, page, pcount, ppcount, pdocs, psdict, category,
path, page, pcount, ppcount, pdocs, psdict, category: q,
};
}
async postStar({ domainId, pid }) {
await problem.setStar(domainId, pid, this.user._id, true);
this.back({ star: true });
}
async postUnstar({ domainId, pid }) {
await problem.setStar(domainId, pid, this.user._id, false);
this.back({ star: false });
}
async cleanup() {
if (this.response.template === 'problem_main.html' && this.preferJson) {
const {
+4 -4
View File
@@ -102,7 +102,7 @@ class TrainingDetailHandler extends TrainingHandler {
const tdoc = await training.get(domainId, tid);
const pids = training.getPids(tdoc);
const f = this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN) ? {} : { hidden: false };
const [udoc, pdict] = await Promise.all([
const [owner, pdict] = await Promise.all([
user.getById(domainId, tdoc.owner),
problem.getList(domainId, pids, f),
]);
@@ -146,7 +146,7 @@ class TrainingDetailHandler extends TrainingHandler {
];
this.response.template = 'training_detail.html';
this.response.body = {
path, tdoc, tsdoc, pids, pdict, psdict, ndict, nsdict, udoc,
path, tdoc, tsdoc, pids, pdict, psdict, ndict, nsdict, owner,
};
}
@@ -193,7 +193,7 @@ class TrainingCreateHandler extends TrainingHandler {
}
const tid = await training.add(domainId, title, content, this.user._id, dag, description);
this.response.body = { tid };
this.response.redirect = `/t/${tid}`;
this.response.redirect = this.url('training_detail', { tid });
}
}
@@ -245,7 +245,7 @@ class TrainingEditHandler extends TrainingHandler {
title, content, dag, description,
});
this.response.body = { tid };
this.response.redirect = `/t/${tid}`;
this.response.redirect = this.url('training_detail', { tid });
}
}