mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 06:37:23 +01:00
performance: private messages - API (#11077)
* paging for inbox * clean up
This commit is contained in:
@@ -27,8 +27,6 @@ describe('GET /inbox/messages', () => {
|
||||
toUserId: user.id,
|
||||
message: 'fourth',
|
||||
});
|
||||
|
||||
await user.sync();
|
||||
});
|
||||
|
||||
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[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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,6 +11,8 @@ let api = {};
|
||||
* @apiGroup Inbox
|
||||
* @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
|
||||
*/
|
||||
api.getInboxMessages = {
|
||||
@@ -19,8 +21,11 @@ api.getInboxMessages = {
|
||||
middlewares: [authWithHeaders()],
|
||||
async handler (req, res) {
|
||||
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);
|
||||
},
|
||||
|
||||
@@ -1421,7 +1421,7 @@ api.deleteMessage = {
|
||||
|
||||
await inboxLib.deleteMessage(user, req.params.id);
|
||||
|
||||
res.respond(200, ...[await inboxLib.getUserInbox(user, false)]);
|
||||
res.respond(200, ...[await inboxLib.getUserInbox(user, {asArray: false})]);
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -95,7 +95,7 @@ async function _getUserDataForExport (user, xmlMode = false) {
|
||||
userId: user._id,
|
||||
}).exec(),
|
||||
|
||||
inboxLib.getUserInbox(user, false),
|
||||
inboxLib.getUserInbox(user, { asArray: false }),
|
||||
]);
|
||||
|
||||
userData.inbox.messages = messages;
|
||||
|
||||
@@ -1,12 +1,25 @@
|
||||
import { inboxModel as Inbox } from '../../models/message';
|
||||
|
||||
export async function getUserInbox (user, asArray = true) {
|
||||
const messages = (await Inbox
|
||||
.find({ownerId: user._id})
|
||||
.sort({timestamp: -1})
|
||||
.exec()).map(msg => msg.toJSON());
|
||||
const PM_PER_PAGE = 10;
|
||||
|
||||
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;
|
||||
} else {
|
||||
const messagesObj = {};
|
||||
|
||||
@@ -408,7 +408,7 @@ schema.methods.toJSONWithInbox = async function userToJSONWithInbox () {
|
||||
const toJSON = user.toJSON();
|
||||
|
||||
if (toJSON.inbox) {
|
||||
toJSON.inbox.messages = await inboxLib.getUserInbox(user, false);
|
||||
toJSON.inbox.messages = await inboxLib.getUserInbox(user, {asArray: false});
|
||||
}
|
||||
|
||||
return toJSON;
|
||||
|
||||
Reference in New Issue
Block a user