Skip to content

Commit

Permalink
Merge cdc38ec into fe636b9
Browse files Browse the repository at this point in the history
  • Loading branch information
negue committed Mar 25, 2019
2 parents fe636b9 + cdc38ec commit 9f1210e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 11 deletions.
19 changes: 17 additions & 2 deletions test/api/v3/integration/inbox/GET-inbox_messages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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);
});
});
7 changes: 6 additions & 1 deletion website/server/controllers/api-v3/inbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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);
},
Expand Down
2 changes: 1 addition & 1 deletion website/server/controllers/api-v3/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})]);
},
};

Expand Down
2 changes: 1 addition & 1 deletion website/server/controllers/top-level/dataexport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 18 additions & 5 deletions website/server/libs/inbox/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import { inboxModel as Inbox } from '../../models/message';

export async function getUserInbox (user, asArray = true) {
const messages = (await Inbox
const PM_PER_PAGE = 10;

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})
.exec()).map(msg => msg.toJSON());
.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 (asArray) {
if (options.asArray) {
return messages;
} else {
const messagesObj = {};
Expand Down
2 changes: 1 addition & 1 deletion website/server/models/user/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9f1210e

Please sign in to comment.