mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
移除可以与同一用户创建多个会话的特性
This commit is contained in:
1 parent
d04628148f
commit
3c5fdb9032
16 files changed
+120
-118
No files matched your search
+15
-19
@@ -1,6 +1,6 @@
|
||||
const {
|
||||
VerifyPasswordError, UserAlreadyExistError, InvalidTokenError,
|
||||
NotFoundError, MessageNotFoundError,
|
||||
NotFoundError,
|
||||
} = require('../error');
|
||||
const bus = require('../service/bus');
|
||||
const {
|
||||
@@ -200,38 +200,34 @@ class HomeMessagesHandler extends Handler {
|
||||
this.udoc(udict, mdoc.from);
|
||||
this.udoc(udict, mdoc.to);
|
||||
}
|
||||
this.response.body = { messages: mdocs, udict };
|
||||
const path = [
|
||||
['Hydro', '/'],
|
||||
['home_messages', null],
|
||||
];
|
||||
this.response.body = { messages: mdocs, udict, path };
|
||||
this.response.template = 'home_messages.html';
|
||||
}
|
||||
|
||||
async postSendMessage({ uid, content }) {
|
||||
async postSend({ uid, content, type = 'full' }) {
|
||||
const udoc = await user.getById(uid);
|
||||
const mdoc = await message.add(this.user._id, udoc._id, content);
|
||||
let mdoc = await message.send(this.user._id, uid, content);
|
||||
if (type === 'single') {
|
||||
mdoc = mdoc.reply[mdoc.reply.length - 1];
|
||||
}
|
||||
// TODO(twd2): improve here:
|
||||
// projection
|
||||
mdoc.from_udoc = this.user;
|
||||
this.udoc(mdoc, 'from');
|
||||
mdoc.to_udoc = udoc;
|
||||
this.udoc(mdoc, 'from');
|
||||
this.udoc(mdoc, 'to');
|
||||
if (this.user._id !== uid) {
|
||||
await bus.publish(`user_message-${uid}`, { type: 'new', data: mdoc });
|
||||
}
|
||||
this.back({ mdoc });
|
||||
this.back(type === 'full' ? { mdoc } : { reply: mdoc });
|
||||
}
|
||||
|
||||
async postReplyMessage({ message_id, content }) {
|
||||
const [mdoc, reply] = await message.addReply(message_id, this.user._id, content);
|
||||
if (!mdoc) throw new MessageNotFoundError(message_id);
|
||||
if (mdoc.from !== mdoc.to) {
|
||||
const other = mdoc.from === this.user._id ? mdoc.from : mdoc.to;
|
||||
await bus.publish(`user_message-${other}`, { type: 'reply', data: mdoc });
|
||||
}
|
||||
mdoc.reply = [reply];
|
||||
this.back({ reply });
|
||||
}
|
||||
|
||||
async postDeleteMessage({ message_id }) {
|
||||
await message.delete(message_id, this.user._id);
|
||||
async postDeleteMessage({ messageId }) {
|
||||
await message.delete(messageId, this.user._id);
|
||||
this.back();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,10 +22,10 @@ class ProblemImportHandler extends Handler {
|
||||
async post({
|
||||
url, pid, hidden, remoteType,
|
||||
}) {
|
||||
if (typeof this[`from_${remoteType}`] !== 'function') {
|
||||
if (typeof global.Hydro.lib[`import.${remoteType}`] !== 'function') {
|
||||
throw new ValidationError('remoteType');
|
||||
}
|
||||
const [pdoc, testdata] = await this[`from_${remoteType}`](url);
|
||||
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);
|
||||
|
||||
@@ -118,8 +118,10 @@ class ProblemSubmitHandler extends ProblemDetailHandler {
|
||||
const rid = await record.add({
|
||||
uid: this.uid, lang, code, pid: this.pdoc._id,
|
||||
});
|
||||
await record.judge(rid);
|
||||
this.user.nSubmit++;
|
||||
await Promise.all([
|
||||
record.judge(rid),
|
||||
user.inc(this.user._id, 'nSubmit'),
|
||||
]);
|
||||
this.response.body = { rid };
|
||||
this.response.redirect = `/r/${rid}`;
|
||||
}
|
||||
|
||||
+4
-16
@@ -17,22 +17,10 @@ class RecordListHandler extends Handler {
|
||||
this.response.template = 'record_main.html';
|
||||
const q = {};
|
||||
const rdocs = await record.getMany(q, { _id: -1 }, page, await system.get('RECORD_PER_PAGE'));
|
||||
const pdict = {};
|
||||
const udict = {};
|
||||
const ulist = [];
|
||||
const plist = [];
|
||||
for (const rdoc of rdocs) {
|
||||
ulist[rdoc.uid] = user.getById(rdoc.uid);
|
||||
plist[rdoc.pid] = problem.get(rdoc.pid, this.user._id);
|
||||
}
|
||||
await Promise.all(ulist);
|
||||
await Promise.all(plist);
|
||||
for (const udoc of ulist) {
|
||||
udict[udoc._id] = udoc;
|
||||
}
|
||||
for (const pdoc of plist) {
|
||||
pdict[pdoc._id] = pdoc;
|
||||
}
|
||||
const [udict, pdict] = await Promise.all([
|
||||
user.getList(rdocs.map((rdoc) => rdoc.uid)),
|
||||
problem.getList(rdocs.map((rdoc) => rdoc.pid)),
|
||||
]);
|
||||
const path = [
|
||||
['Hydro', '/'],
|
||||
['record_main', null],
|
||||
|
||||
+25
-31
@@ -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,
|
||||
};
|
||||
@@ -63,9 +63,9 @@ module.exports = {
|
||||
const now = new Date();
|
||||
const res = await coll.findOneAndUpdate({ _id: tokenId, tokenType }, {
|
||||
$set: {
|
||||
...data,
|
||||
updateAt: now,
|
||||
expireAt: new Date(now.getTime() + expireSeconds * 1000),
|
||||
...data,
|
||||
tokenType,
|
||||
},
|
||||
});
|
||||
|
||||
+4
-3
@@ -96,9 +96,10 @@ async function changePassword(uid, currentPassword, newPassword) {
|
||||
});
|
||||
}
|
||||
|
||||
async function inc(_id, field, n) {
|
||||
await coll.findOneAndUpdate({ _id }, { $inc: { [field]: n } });
|
||||
const udoc = await getById(_id);
|
||||
async function inc(_id, field, n = 1) {
|
||||
const udoc = await coll.findOne({ _id });
|
||||
udoc[field] = udoc[field] + n || n;
|
||||
await coll.updateOne({ _id }, { $set: { [field]: udoc[field] } });
|
||||
return udoc;
|
||||
}
|
||||
|
||||
|
||||
@@ -151,7 +151,10 @@ class Handler {
|
||||
if (bdoc) throw new BlacklistedError(this.request.ip);
|
||||
this.user = await user.getById(this.session.uid);
|
||||
if (!this.user) throw new UserNotFoundError(this.session.uid);
|
||||
[this.csrfToken] = await token.add(token.TYPE_CSRF_TOKEN, 600, { path: this.request.path });
|
||||
[this.csrfToken] = await token.add(token.TYPE_CSRF_TOKEN, 600, {
|
||||
path: this.request.path,
|
||||
uid: this.session.uid,
|
||||
});
|
||||
this.preferJson = (this.request.headers.accept || '').includes('application/json');
|
||||
}
|
||||
|
||||
@@ -321,7 +324,6 @@ class ConnectionHandler {
|
||||
_: (str) => (str ? str.toString().translate(this.user.language) : ''),
|
||||
user: this.user,
|
||||
}));
|
||||
console.timeEnd(name);
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -335,9 +337,7 @@ class ConnectionHandler {
|
||||
|
||||
async ___prepare() {
|
||||
try {
|
||||
this.session = {
|
||||
uid: await token.get(this.request.params.token, token.TYPE_CSRF_TOKEN),
|
||||
};
|
||||
this.session = await token.get(this.request.params.token, token.TYPE_CSRF_TOKEN);
|
||||
await token.delete(this.request.params.token, token.TYPE_CSRF_TOKEN);
|
||||
} catch (e) {
|
||||
this.session = { uid: 1 };
|
||||
|
||||
Reference in new issue
Block a user