修复discussion模块

This commit is contained in:
undefined committed 2020-05-01 18:43:40 +08:00
1 parent 2554cf660a
commit 33e277f28f
30 files changed
+2460 -187

No files matched your search

+26 -2
View File
@@ -1,4 +1,5 @@
const { ObjectID } = require('bson');
const problem = require('./problem');
const { DocumentNotFoundError } = require('../error');
const db = require('../service/db.js');
@@ -6,8 +7,8 @@ const coll = db.collection('discussion');
const collReply = db.collection('discussion.reply');
const collStatus = db.collection('discussion.status');
function add(parentType, parentId, owner, title, content, ip = null, highlight = false) {
return coll.insertOne({
async function add(parentType, parentId, owner, title, content, ip = null, highlight = false) {
let res = await coll.insertOne({
owner,
title,
content,
@@ -18,6 +19,7 @@ function add(parentType, parentId, owner, title, content, ip = null, highlight =
nReply: 0,
updateAt: new Date(),
});
return res.insertedId;
}
function get(did) {
@@ -128,6 +130,26 @@ function getStatus(did, uid) {
return collStatus.findOne({ did, uid });
}
async function getVnode(ddoc, handler) {
if (ddoc.parentType === 'problem') {
const pdoc = await problem.getById(ddoc.parentId);
if (!pdoc) return null;
if (pdoc.hidden && handler) handler.checkPerm(PERM_VIEW_PROBLEM_HIDDEN);
return { ...pdoc, parentType: ddoc.parentType, parentId: ddoc.parentId };
} if (ddoc.parentType === 'contest') {
const tdoc = await contest.get(ddoc.parentId);
return { ...tdoc, parentType: ddoc.parentType, parentId: ddoc.parentId };
} return null;
}
async function getListVnodes(ddocs, handler) {
const res = {};
for (const ddoc of ddocs) {
res[ddoc._id] = await getVnode(ddoc, handler);
}
return res;
}
module.exports = {
add,
get,
@@ -147,4 +169,6 @@ module.exports = {
delTailReply,
setStar,
getStatus,
getVnode,
getListVnodes,
};
+3 -2
View File
@@ -15,12 +15,13 @@ class USER {
this.salt = user.salt;
this.hash = user.hash;
this.perm = user.perm;
this.lang = user.language || 'zh_CN';
this.viewLang = user.language || 'zh_CN';
this.codeLang = user.codeLang || 'c';
this.codeTemplate = user.codeTemplate || '';
this.regat = user.regat;
this.loginat = user.loginat;
this.bio = user.bio || '';
this.gravatar = user.gravatar || '';
this.nAccept = user.nAccept || 0;
this.nSubmit = user.nSubmit || 0;
}
@@ -144,7 +145,7 @@ async function getPrefixList(prefix, limit = 50) {
async function setRole(uid, role) {
const udoc = await getById(uid);
return await Promise.all([
coll.findOneAndUpdate({ _id: uid }, { role }),
coll.findOneAndUpdate({ _id: uid }, { $set: { role } }),
collRole.updateOne({ _id: udoc.role }, { $inc: { count: -1 } }),
collRole.updateOne({ _id: role }, { $inc: { count: 1 } }),
]);