Skip to content

Commit

Permalink
Inbox: Add API to list conversations (#11110)
Browse files Browse the repository at this point in the history
* Add API to list inbox conversations

* fix test + add api doc

* use `.lean()`

* orderBy after the the grouped conversations are loaded

* fix ordering
  • Loading branch information
negue authored and paglias committed Apr 26, 2019
1 parent 3be075a commit 83070e2
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 5 deletions.
6 changes: 6 additions & 0 deletions test/api/v3/integration/inbox/GET-inbox_messages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,10 @@ describe('GET /inbox/messages', () => {

expect(messages.length).to.equal(4);
});

it('returns only the messages of one conversation', async () => {
const messages = await user.get(`/inbox/messages?conversation=${otherUser.id}`);

expect(messages.length).to.equal(3);
});
});
44 changes: 44 additions & 0 deletions test/api/v4/inbox/GET-inbox-conversations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
generateUser,
} from '../../../helpers/api-integration/v4';

describe('GET /inbox/conversations', () => {
let user;
let otherUser;
let thirdUser;

before(async () => {
[user, otherUser, thirdUser] = await Promise.all([generateUser(), generateUser(), generateUser()]);

await otherUser.post('/members/send-private-message', {
toUserId: user.id,
message: 'first',
});
await user.post('/members/send-private-message', {
toUserId: otherUser.id,
message: 'second',
});
await user.post('/members/send-private-message', {
toUserId: thirdUser.id,
message: 'third',
});
await otherUser.post('/members/send-private-message', {
toUserId: user.id,
message: 'fourth',
});

// message to yourself
await user.post('/members/send-private-message', {
toUserId: user.id,
message: 'fifth',
});
});

it('returns the conversations', async () => {
const result = await user.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);
});
});
4 changes: 3 additions & 1 deletion website/server/controllers/api-v3/inbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let api = {};
* @apiDescription Get inbox messages for a user
*
* @apiParam (Query) {Number} page Load the messages of the selected Page - 10 Messages per Page
* @apiParam (Query) {GUID} conversation Loads only the messages of a conversation
*
* @apiSuccess {Array} data An array of inbox messages
*/
Expand All @@ -22,9 +23,10 @@ api.getInboxMessages = {
async handler (req, res) {
const user = res.locals.user;
const page = req.query.page;
const conversation = req.query.conversation;

const userInbox = await inboxLib.getUserInbox(user, {
page,
page, conversation,
});

res.respond(200, userInbox);
Expand Down
21 changes: 21 additions & 0 deletions website/server/controllers/api-v4/inbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,25 @@ api.clearMessages = {
},
};

/**
* @api {get} /inbox/conversations Get the conversations for a user
* @apiName conversations
* @apiGroup Inbox
* @apiDescription Get the conversations for a user
*
* @apiSuccess {Array} data An array of inbox conversations
*/
api.conversations = {
method: 'GET',
middlewares: [authWithHeaders()],
url: '/inbox/conversations',
async handler (req, res) {
const user = res.locals.user;

const result = await inboxLib.listConversations(user);

res.respond(200, result);
},
};

module.exports = api;
52 changes: 48 additions & 4 deletions website/server/libs/inbox/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { inboxModel as Inbox } from '../../models/message';
import {inboxModel as Inbox} from '../../models/message';
import {
model as User,
} from '../../models/user';
import orderBy from 'lodash/orderBy';
import keyBy from 'lodash/keyBy';

const PM_PER_PAGE = 10;

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

const findObj = {ownerId: user._id};

if (options.conversation) {
findObj.uuid = options.conversation;
}

let query = Inbox
.find({ownerId: user._id})
.find(findObj)
.sort({timestamp: -1});

if (typeof options.page !== 'undefined') {
Expand All @@ -29,12 +40,45 @@ export async function getUserInbox (user, options = {asArray: true, page: 0}) {
}
}

export async function listConversations (user) {
let query = Inbox
.aggregate([
{
$match: {
ownerId: user._id,
},
},
{
$group: {
_id: '$uuid',
timestamp: {$max: '$timestamp'}, // sort before group doesn't work - use the max value to sort it again after
},
},
]);

const conversationsList = orderBy(await query.exec(), ['timestamp'], ['desc']).map(c => c._id);

const users = await User.find({_id: {$in: conversationsList}})
.select('_id profile.name auth.local.username')
.lean()
.exec();

const usersMap = keyBy(users, '_id');
const conversations = conversationsList.map(userId => ({
uuid: usersMap[userId]._id,
user: usersMap[userId].profile.name,
username: usersMap[userId].auth.local.username,
}));

return conversations;
}

export async function getUserInboxMessage (user, messageId) {
return Inbox.findOne({ownerId: user._id, _id: messageId}).exec();
}

export async function deleteMessage (user, messageId) {
const message = await Inbox.findOne({_id: messageId, ownerId: user._id }).exec();
const message = await Inbox.findOne({_id: messageId, ownerId: user._id}).exec();
if (!message) return false;
await Inbox.remove({_id: message._id, ownerId: user._id}).exec();

Expand Down

0 comments on commit 83070e2

Please sign in to comment.