添加站内消息,修复讨论节点丢失的问题

This commit is contained in:
undefined committed 2020-05-03 15:33:54 +08:00
1 parent 33e277f28f
commit 627e64a901
44 files changed
+582 -540

No files matched your search

+5 -2
View File
@@ -1,5 +1,7 @@
const { ObjectID } = require('bson');
const problem = require('./problem');
const contest = require('./contest');
const { PERM_VIEW_PROBLEM_HIDDEN } = require('../permission');
const { DocumentNotFoundError } = require('../error');
const db = require('../service/db.js');
@@ -8,7 +10,7 @@ const collReply = db.collection('discussion.reply');
const collStatus = db.collection('discussion.status');
async function add(parentType, parentId, owner, title, content, ip = null, highlight = false) {
let res = await coll.insertOne({
const res = await coll.insertOne({
owner,
title,
content,
@@ -145,7 +147,8 @@ async function getVnode(ddoc, handler) {
async function getListVnodes(ddocs, handler) {
const res = {};
for (const ddoc of ddocs) {
res[ddoc._id] = await getVnode(ddoc, handler);
// FIXME no-await-in-loop
res[ddoc._id] = await getVnode(ddoc, handler); // eslint-disable-line no-await-in-loop
}
return res;
}
+72
View File
@@ -0,0 +1,72 @@
const { MessageNotFoundError } = require('../error');
const db = require('../service/db.js');
const coll = db.collection('message');
async function add(from, to, content) {
const res = await coll.insertOne({
from,
to,
fromUnread: false,
toUnread: true,
reply: [
{
from, unread: true, content, at: new Date(),
},
],
});
return await coll.findOne({ _id: res.insertedId }); // eslint-disable-line no-return-await
}
async function get(_id) {
const doc = await coll.findOne({ _id });
if (!doc) throw new MessageNotFoundError();
return doc;
}
function getMany(query, sort, page, limit) {
return coll.find(query).sort(sort)
.skip((page - 1) * limit).limit(limit)
.toArray();
}
function del(_id) {
return coll.deleteOne({ _id });
}
function count(query) {
return coll.find(query).count();
}
function getMulti(uid) {
return coll.find({ $or: [{ from: uid }, { to: uid }] });
}
async function addReply(_id, from, content) {
const reply = {
from,
content,
unread: true,
at: new Date(),
};
await coll.updateOne({ _id }, { $push: { reply } });
return await coll.findOne({ _id }); // eslint-disable-line no-return-await
}
function index() {
return Promise.all([
coll.createIndex({ to: 1, _id: -1 }),
coll.createIndex({ from: 1, _id: -1 }),
]);
}
global.Hydro.model.message = module.exports = {
count,
add,
get,
del,
getMany,
getMulti,
addReply,
index,
};
+12 -8
View File
@@ -26,8 +26,8 @@ class USER {
this.nSubmit = user.nSubmit || 0;
}
hasPerm(perm) {
return this.perm.includes(perm);
hasPerm(p) {
return this.perm.includes(p);
}
checkPassword(password) {
@@ -144,7 +144,7 @@ async function getPrefixList(prefix, limit = 50) {
async function setRole(uid, role) {
const udoc = await getById(uid);
return await Promise.all([
return await Promise.all([ // eslint-disable-line no-return-await
coll.findOneAndUpdate({ _id: uid }, { $set: { role } }),
collRole.updateOne({ _id: udoc.role }, { $inc: { count: -1 } }),
collRole.updateOne({ _id: role }, { $inc: { count: 1 } }),
@@ -159,8 +159,8 @@ function getRole(name) {
return collRole.findOne({ _id: name });
}
function addRole(name, perm) {
return collRole.insertOne({ _id: name, perm, count: 0 });
function addRole(name, permission) {
return collRole.insertOne({ _id: name, perm: permission, count: 0 });
}
function deleteRoles(roles) {
@@ -170,8 +170,12 @@ function deleteRoles(roles) {
]);
}
function init() {
return collRole.update({ _id: 'root' }, { $set: { perm: perm.PERM_ALL } });
function index() {
return Promise.all([
collRole.updateOne({ _id: 'root' }, { $set: { perm: perm.PERM_ALL } }),
coll.createIndex('unameLower', { unique: true }),
coll.createIndex('mailLower', { sparse: true }),
]);
}
module.exports = {
@@ -192,5 +196,5 @@ module.exports = {
getRoles,
addRole,
deleteRoles,
init,
index,
};