New inbox client (#10644)

* new inbox client

* add tests for sendPrivateMessage returning the message

* update DELETE user message tests

* port v3 GET-inbox_messages

* use v4 delete message route

* sendPrivateMessage: return sent message

* fix
This commit is contained in:
Matteo Pagliazzi
2018-08-30 21:50:03 +02:00
committed by Sabe Jones
parent 64507a161e
commit 84329e5fad
14 changed files with 287 additions and 99 deletions

View File

@@ -1,28 +1,49 @@
import { authWithHeaders } from '../../middlewares/auth';
import { toArray, orderBy } from 'lodash';
import apiError from '../../libs/apiError';
import * as inboxLib from '../../libs/inbox';
import {
NotFound,
} from '../../libs/errors';
let api = {};
const api = {};
/* NOTE most inbox routes are either in the user or members controller */
/**
* @api {get} /api/v3/inbox/messages Get inbox messages for a user
* @apiPrivate
* @apiName GetInboxMessages
* @apiGroup Inbox
* @apiDescription Get inbox messages for a user
*
* @apiSuccess {Array} data An array of inbox messages
*/
api.getInboxMessages = {
method: 'GET',
url: '/inbox/messages',
middlewares: [authWithHeaders()],
async handler (req, res) {
const messagesObj = res.locals.user.inbox.messages;
const messagesArray = orderBy(toArray(messagesObj), ['timestamp'], ['desc']);
/* NOTE the getInboxMessages route is implemented in v3 only */
res.respond(200, messagesArray);
/* NOTE this route has also an API v3 version */
/**
* @api {delete} /api/v4/inbox/messages/:messageId Delete a message
* @apiName deleteMessage
* @apiGroup User
*
* @apiParam (Path) {UUID} messageId The id of the message to delete
*
* @apiSuccess {Object} data Empty object
* @apiSuccessExample {json}
* {
* "success": true,
* "data": {}
* }
*/
api.deleteMessage = {
method: 'DELETE',
middlewares: [authWithHeaders()],
url: '/inbox/messages/:messageId',
async handler (req, res) {
req.checkParams('messageId', apiError('messageIdRequired')).notEmpty().isUUID();
const validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
const messageId = req.params.messageId;
const user = res.locals.user;
const deleted = await inboxLib.deleteMessage(user, messageId);
if (!deleted) throw new NotFound(res.t('messageGroupChatNotFound'));
res.respond(200);
},
};