From a2d2972898ca3ae43ef42f574f89f8e27dc76f4a Mon Sep 17 00:00:00 2001 From: negue Date: Sun, 25 Aug 2019 18:16:27 +0200 Subject: [PATCH] refactor: conversation mongo query - add text/userStyle/contributor - sort inside --- website/server/libs/inbox/index.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/website/server/libs/inbox/index.js b/website/server/libs/inbox/index.js index 2f2e4b6575..2171df88f0 100644 --- a/website/server/libs/inbox/index.js +++ b/website/server/libs/inbox/index.js @@ -86,20 +86,23 @@ export async function listConversations (owner) { { $group: { _id: '$uuid', - user: {$first: '$user' }, - username: {$first: '$username' }, - timestamp: {$max: '$timestamp'}, // sort before group doesn't work - use the max value to sort it again after + user: {$last: '$user' }, + username: {$last: '$username' }, + timestamp: {$last: '$timestamp'}, + text: {$last: '$text'}, + userStyles: {$last: '$userStyles'}, + contributor: {$last: '$contributor'}, + count: {$sum: 1}, }, }, + { $sort: {timestamp: -1}}, // sort by latest message ]); - const conversationsList = orderBy(await query.exec(), ['timestamp'], ['desc']); + const conversationsList = await query.exec(); - const conversations = conversationsList.map(({_id, user, username, timestamp}) => ({ - uuid: _id, - user, - username, - timestamp, + const conversations = conversationsList.map((res) => ({ + uuid: res._id, + ...res, })); return conversations;