performance: private messages - API (#11077)

* paging for inbox

* clean up
This commit is contained in:
negue
2019-03-31 20:52:53 +02:00
committed by Matteo Pagliazzi
parent f35ef3a046
commit 0b82722d27
6 changed files with 45 additions and 12 deletions

View File

@@ -27,8 +27,6 @@ describe('GET /inbox/messages', () => {
toUserId: user.id, toUserId: user.id,
message: 'fourth', message: 'fourth',
}); });
await user.sync();
}); });
it('returns the user inbox messages as an array of ordered messages (from most to least recent)', async () => { it('returns the user inbox messages as an array of ordered messages (from most to least recent)', async () => {
@@ -45,4 +43,21 @@ describe('GET /inbox/messages', () => {
expect(messages[2].text).to.equal('second'); expect(messages[2].text).to.equal('second');
expect(messages[3].text).to.equal('first'); expect(messages[3].text).to.equal('first');
}); });
it('returns four messages when using page-query ', async () => {
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(user.post('/members/send-private-message', {
toUserId: user.id,
message: 'fourth',
}));
}
await Promise.all(promises);
const messages = await user.get('/inbox/messages?page=1');
expect(messages.length).to.equal(4);
});
}); });

View File

@@ -11,6 +11,8 @@ let api = {};
* @apiGroup Inbox * @apiGroup Inbox
* @apiDescription Get inbox messages for a user * @apiDescription Get inbox messages for a user
* *
* @apiParam (Query) {Number} page Load the messages of the selected Page - 10 Messages per Page
*
* @apiSuccess {Array} data An array of inbox messages * @apiSuccess {Array} data An array of inbox messages
*/ */
api.getInboxMessages = { api.getInboxMessages = {
@@ -19,8 +21,11 @@ api.getInboxMessages = {
middlewares: [authWithHeaders()], middlewares: [authWithHeaders()],
async handler (req, res) { async handler (req, res) {
const user = res.locals.user; const user = res.locals.user;
const page = req.query.page;
const userInbox = await inboxLib.getUserInbox(user); const userInbox = await inboxLib.getUserInbox(user, {
page,
});
res.respond(200, userInbox); res.respond(200, userInbox);
}, },

View File

@@ -1421,7 +1421,7 @@ api.deleteMessage = {
await inboxLib.deleteMessage(user, req.params.id); await inboxLib.deleteMessage(user, req.params.id);
res.respond(200, ...[await inboxLib.getUserInbox(user, false)]); res.respond(200, ...[await inboxLib.getUserInbox(user, {asArray: false})]);
}, },
}; };

View File

@@ -95,7 +95,7 @@ async function _getUserDataForExport (user, xmlMode = false) {
userId: user._id, userId: user._id,
}).exec(), }).exec(),
inboxLib.getUserInbox(user, false), inboxLib.getUserInbox(user, { asArray: false }),
]); ]);
userData.inbox.messages = messages; userData.inbox.messages = messages;

View File

@@ -1,12 +1,25 @@
import { inboxModel as Inbox } from '../../models/message'; import { inboxModel as Inbox } from '../../models/message';
export async function getUserInbox (user, asArray = true) { const PM_PER_PAGE = 10;
const messages = (await Inbox
.find({ownerId: user._id})
.sort({timestamp: -1})
.exec()).map(msg => msg.toJSON());
if (asArray) { export async function getUserInbox (user, options = {asArray: true, page: 0}) {
if (typeof options.asArray === 'undefined') {
options.asArray = true;
}
let query = Inbox
.find({ownerId: user._id})
.sort({timestamp: -1});
if (typeof options.page !== 'undefined') {
query = query
.limit(PM_PER_PAGE)
.skip(PM_PER_PAGE * Number(options.page));
}
const messages = (await query.exec()).map(msg => msg.toJSON());
if (options.asArray) {
return messages; return messages;
} else { } else {
const messagesObj = {}; const messagesObj = {};

View File

@@ -408,7 +408,7 @@ schema.methods.toJSONWithInbox = async function userToJSONWithInbox () {
const toJSON = user.toJSON(); const toJSON = user.toJSON();
if (toJSON.inbox) { if (toJSON.inbox) {
toJSON.inbox.messages = await inboxLib.getUserInbox(user, false); toJSON.inbox.messages = await inboxLib.getUserInbox(user, {asArray: false});
} }
return toJSON; return toJSON;