From a2d2972898ca3ae43ef42f574f89f8e27dc76f4a Mon Sep 17 00:00:00 2001 From: negue Date: Sun, 25 Aug 2019 18:16:27 +0200 Subject: [PATCH 1/4] 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; From ba196f70a3be5340541b9de186b38831f6a8daad Mon Sep 17 00:00:00 2001 From: negue Date: Sun, 25 Aug 2019 18:18:08 +0200 Subject: [PATCH 2/4] remove orderBy-import --- website/server/libs/inbox/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/website/server/libs/inbox/index.js b/website/server/libs/inbox/index.js index 2171df88f0..c51fa9e67e 100644 --- a/website/server/libs/inbox/index.js +++ b/website/server/libs/inbox/index.js @@ -1,5 +1,4 @@ import {mapInboxMessage, inboxModel as Inbox} from '../../models/message'; -import orderBy from 'lodash/orderBy'; import {getUserInfo, sendTxn as sendTxnEmail} from '../email'; import {sendNotification as sendPushNotification} from '../pushNotifications'; From 403efd4ddcf1031d302ef463a5e8e61912008978 Mon Sep 17 00:00:00 2001 From: negue Date: Tue, 27 Aug 2019 18:33:27 +0200 Subject: [PATCH 3/4] add api doc --- website/server/controllers/api-v4/inbox.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/website/server/controllers/api-v4/inbox.js b/website/server/controllers/api-v4/inbox.js index bb2163ad3f..d37eb62e35 100644 --- a/website/server/controllers/api-v4/inbox.js +++ b/website/server/controllers/api-v4/inbox.js @@ -79,6 +79,20 @@ api.clearMessages = { * @apiDescription Get the conversations for a user * * @apiSuccess {Array} data An array of inbox conversations + * + * @apiSuccessExample {json} Success-Response: + * {"success":true,"data":[ + * { + * "_id":"8a9d461b-f5eb-4a16-97d3-c03380c422a3", + * "user":"user display name", + * "username":"some_user_name", + * "timestamp":"12315123123", + * "text":"last message of conversation", + * "userStyles": {}, + * "contributor": {}, + * "count":1 + * } + * } */ api.conversations = { method: 'GET', From ca5439d2c50f6099beefaed96fad40ab6384b169 Mon Sep 17 00:00:00 2001 From: negue Date: Tue, 27 Aug 2019 18:41:59 +0200 Subject: [PATCH 4/4] test on conversations having a message --- test/api/v4/inbox/GET-inbox-conversations.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/api/v4/inbox/GET-inbox-conversations.test.js b/test/api/v4/inbox/GET-inbox-conversations.test.js index 749755721b..ce94e919d5 100644 --- a/test/api/v4/inbox/GET-inbox-conversations.test.js +++ b/test/api/v4/inbox/GET-inbox-conversations.test.js @@ -40,6 +40,7 @@ describe('GET /inbox/conversations', () => { expect(result.length).to.be.equal(3); expect(result[0].user).to.be.equal(user.profile.name); expect(result[0].username).to.be.equal(user.auth.local.username); + expect(result[0].text).to.be.not.empty; }); it('returns the user inbox messages as an array of ordered messages (from most to least recent)', async () => {