移除可以与同一用户创建多个会话的特性

This commit is contained in:
masnn committed 2020-05-15 13:10:41 +08:00
1 parent d04628148f
commit 3c5fdb9032
16 files changed
+120 -118

No files matched your search

+25 -31
View File
@@ -1,26 +1,39 @@
const { ObjectID } = require('bson');
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(),
async function send(from, to, content) {
await coll.updateOne({
from: Math.min(from, to),
to: Math.max(from, to),
}, {
$push: {
reply: {
_id: new ObjectID(),
from,
content,
unread: true,
at: new Date(),
},
],
},
$setOnInsert: {
from: Math.min(from, to),
to: Math.max(from, to),
fromUnread: false,
toUnread: true,
},
}, { upsert: true });
return await coll.findOne({ // eslint-disable-line no-return-await
from: Math.min(from, to),
to: Math.max(from, to),
});
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();
if (!doc) throw new MessageNotFoundError(_id);
return doc;
}
@@ -42,23 +55,6 @@ 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
}
async function send(from, to, content) {
const sdoc = await coll.findOne({ $or: [{ from, to }, { from: to, to: from }] });
await addReply(sdoc._id, from, content);
return sdoc._id;
}
function index() {
return Promise.all([
coll.createIndex({ to: 1, _id: -1 }),
@@ -68,12 +64,10 @@ function index() {
global.Hydro.model.message = module.exports = {
count,
add,
get,
del,
getMany,
getMulti,
addReply,
send,
index,
};