Skip to content

Commit

Permalink
move mapMessage-call to getUserInbox
Browse files Browse the repository at this point in the history
  • Loading branch information
negue committed Jul 12, 2019
1 parent 805706b commit e7d46ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions website/server/controllers/api-v4/inbox.js
Expand Up @@ -113,9 +113,9 @@ api.getInboxMessages = {
const page = req.query.page;
const conversation = req.query.conversation;

const userInbox = (await inboxLib.getUserInbox(user, {
page, conversation,
})).map(newChat => newChat.mapMessage(user));
const userInbox = await inboxLib.getUserInbox(user, {
page, conversation, mapProps: true,
});

res.respond(200, userInbox);
},
Expand Down
14 changes: 12 additions & 2 deletions website/server/libs/inbox/index.js
Expand Up @@ -30,11 +30,15 @@ export async function sentMessage (sender, receiver, message, translate) {
return messageSent;
}

export async function getUserInbox (user, options = {asArray: true, page: 0, conversation: null}) {
export async function getUserInbox (user, options = {asArray: true, page: 0, conversation: null, mapProps: false}) {
if (typeof options.asArray === 'undefined') {
options.asArray = true;
}

if (typeof options.mapProps === 'undefined') {
options.mapProps = false;
}

const findObj = {ownerId: user._id};

if (options.conversation) {
Expand All @@ -51,7 +55,13 @@ export async function getUserInbox (user, options = {asArray: true, page: 0, con
.skip(PM_PER_PAGE * Number(options.page));
}

const messages = (await query.exec()).map(msg => msg.toJSON());
const messages = (await query.exec()).map(msg => {
if (options.mapProps) {
msg.mapMessage(user);
}

return msg.toJSON();
});

if (options.asArray) {
return messages;
Expand Down

0 comments on commit e7d46ee

Please sign in to comment.