2018-06-18 12:40:25 +00:00
|
|
|
import { authWithHeaders } from '../../middlewares/auth';
|
2024-03-11 14:59:57 +00:00
|
|
|
import { apiError } from '../../libs/apiError';
|
2025-03-04 23:00:24 +00:00
|
|
|
import { NotFound } from '../../libs/errors';
|
2020-01-12 18:34:40 +00:00
|
|
|
import { listConversations } from '../../libs/inbox/conversation.methods';
|
2025-03-04 23:00:24 +00:00
|
|
|
import {
|
|
|
|
|
applyLikeToMessages,
|
|
|
|
|
clearPMs, deleteMessage, getUserInbox,
|
|
|
|
|
} from '../../libs/inbox';
|
|
|
|
|
import { chatReporterFactory } from '../../libs/chatReporting/chatReporterFactory';
|
|
|
|
|
import * as inboxLib from '../../libs/inbox';
|
|
|
|
|
import logger from '../../libs/logger';
|
2018-06-18 12:40:25 +00:00
|
|
|
|
2018-08-30 19:50:03 +00:00
|
|
|
const api = {};
|
2018-06-18 12:40:25 +00:00
|
|
|
|
|
|
|
|
/* NOTE most inbox routes are either in the user or members controller */
|
|
|
|
|
|
2018-08-30 19:50:03 +00:00
|
|
|
/* NOTE the getInboxMessages route is implemented in v3 only */
|
|
|
|
|
|
|
|
|
|
/* NOTE this route has also an API v3 version */
|
|
|
|
|
|
2018-06-18 12:40:25 +00:00
|
|
|
/**
|
2020-06-25 14:07:03 +00:00
|
|
|
* @apiIgnore
|
2018-08-30 19:50:03 +00:00
|
|
|
* @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
|
2018-06-18 12:40:25 +00:00
|
|
|
*
|
2018-08-30 19:50:03 +00:00
|
|
|
* @apiSuccess {Object} data Empty object
|
|
|
|
|
* @apiSuccessExample {json}
|
|
|
|
|
* {
|
|
|
|
|
* "success": true,
|
|
|
|
|
* "data": {}
|
|
|
|
|
* }
|
2018-06-18 12:40:25 +00:00
|
|
|
*/
|
2018-08-30 19:50:03 +00:00
|
|
|
api.deleteMessage = {
|
|
|
|
|
method: 'DELETE',
|
2018-06-18 12:40:25 +00:00
|
|
|
middlewares: [authWithHeaders()],
|
2018-08-30 19:50:03 +00:00
|
|
|
url: '/inbox/messages/:messageId',
|
2018-06-18 12:40:25 +00:00
|
|
|
async handler (req, res) {
|
2018-08-30 19:50:03 +00:00
|
|
|
req.checkParams('messageId', apiError('messageIdRequired')).notEmpty().isUUID();
|
|
|
|
|
|
|
|
|
|
const validationErrors = req.validationErrors();
|
|
|
|
|
if (validationErrors) throw validationErrors;
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const { messageId } = req.params;
|
|
|
|
|
const { user } = res.locals;
|
2018-08-30 19:50:03 +00:00
|
|
|
|
2020-01-12 18:34:40 +00:00
|
|
|
const deleted = await deleteMessage(user, messageId);
|
2018-08-30 19:50:03 +00:00
|
|
|
if (!deleted) throw new NotFound(res.t('messageGroupChatNotFound'));
|
2018-06-18 12:40:25 +00:00
|
|
|
|
2018-08-30 19:50:03 +00:00
|
|
|
res.respond(200);
|
2018-06-18 12:40:25 +00:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-21 13:12:20 +00:00
|
|
|
/* NOTE this route has also an API v3 version */
|
|
|
|
|
|
|
|
|
|
/**
|
2020-06-25 14:07:03 +00:00
|
|
|
* @apiIgnore
|
2018-09-21 13:12:20 +00:00
|
|
|
* @api {delete} /api/v4/inbox/clear Delete all messages
|
|
|
|
|
* @apiName clearMessages
|
|
|
|
|
* @apiGroup User
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {Object} data Empty object
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccessExample {json}
|
|
|
|
|
* {"success":true,"data":{},"notifications":[]}
|
|
|
|
|
*/
|
|
|
|
|
api.clearMessages = {
|
|
|
|
|
method: 'DELETE',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
url: '/inbox/clear',
|
|
|
|
|
async handler (req, res) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const { user } = res.locals;
|
2018-09-21 13:12:20 +00:00
|
|
|
|
2020-01-12 18:34:40 +00:00
|
|
|
await clearPMs(user);
|
2018-09-21 13:12:20 +00:00
|
|
|
|
|
|
|
|
res.respond(200, {});
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-26 16:45:05 +00:00
|
|
|
/**
|
2020-06-25 14:07:03 +00:00
|
|
|
* @apiIgnore
|
2019-07-15 09:25:22 +00:00
|
|
|
* @api {get} /api/v4/inbox/conversations Get the conversations for a user
|
2019-04-26 16:45:05 +00:00
|
|
|
* @apiName conversations
|
|
|
|
|
* @apiGroup Inbox
|
2020-02-08 04:26:01 +00:00
|
|
|
* @apiDescription Get the conversations for a user.
|
|
|
|
|
* This is for API v4 which must not be used in third-party tools.
|
|
|
|
|
* For API v3, use "Get inbox messages for a user".
|
2019-04-26 16:45:05 +00:00
|
|
|
*
|
2020-04-20 14:32:54 +00:00
|
|
|
* @apiParam (Query) {Number} page (optional) Load the conversations of the selected Page
|
|
|
|
|
* - 10 conversations per Page
|
|
|
|
|
*
|
2019-04-26 16:45:05 +00:00
|
|
|
* @apiSuccess {Array} data An array of inbox conversations
|
2019-08-27 16:33:27 +00:00
|
|
|
*
|
|
|
|
|
* @apiSuccessExample {json} Success-Response:
|
|
|
|
|
* {"success":true,"data":[
|
|
|
|
|
* {
|
|
|
|
|
* "_id":"8a9d461b-f5eb-4a16-97d3-c03380c422a3",
|
2025-03-04 23:00:24 +00:00
|
|
|
* "uuid":"8a9d461b-f5eb-4a16-97d3-c03380c422a3",
|
2019-08-27 16:33:27 +00:00
|
|
|
* "user":"user display name",
|
|
|
|
|
* "username":"some_user_name",
|
|
|
|
|
* "timestamp":"12315123123",
|
|
|
|
|
* "text":"last message of conversation",
|
|
|
|
|
* "userStyles": {},
|
|
|
|
|
* "contributor": {},
|
2020-03-04 16:50:08 +00:00
|
|
|
* "canReceive": true,
|
2019-08-27 16:33:27 +00:00
|
|
|
* "count":1
|
|
|
|
|
* }
|
|
|
|
|
* }
|
2019-04-26 16:45:05 +00:00
|
|
|
*/
|
|
|
|
|
api.conversations = {
|
|
|
|
|
method: 'GET',
|
2024-08-12 21:45:35 +00:00
|
|
|
middlewares: [authWithHeaders({ userFieldsToInclude: ['profile', 'contributor', 'backer', 'inbox'] })],
|
2019-04-26 16:45:05 +00:00
|
|
|
url: '/inbox/conversations',
|
|
|
|
|
async handler (req, res) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const { user } = res.locals;
|
2020-04-20 14:32:54 +00:00
|
|
|
const { page } = req.query;
|
2019-04-26 16:45:05 +00:00
|
|
|
|
2020-04-20 14:32:54 +00:00
|
|
|
const result = await listConversations(user, page);
|
2019-04-26 16:45:05 +00:00
|
|
|
|
|
|
|
|
res.respond(200, result);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-15 09:25:22 +00:00
|
|
|
/**
|
2020-06-25 14:07:03 +00:00
|
|
|
* @apiIgnore
|
2019-07-15 09:25:22 +00:00
|
|
|
* @api {get} /api/v4/inbox/paged-messages Get inbox messages for a user
|
|
|
|
|
* @apiName GetInboxMessages
|
|
|
|
|
* @apiGroup Inbox
|
2019-10-11 11:03:05 +00:00
|
|
|
* @apiDescription Get inbox messages for a user.
|
|
|
|
|
* Entries already populated with the correct `sent` - information
|
2019-07-15 09:25:22 +00:00
|
|
|
*
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
|
|
|
|
api.getInboxMessages = {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
url: '/inbox/paged-messages',
|
2024-08-12 21:45:35 +00:00
|
|
|
middlewares: [authWithHeaders({ userFieldsToInclude: ['profile', 'contributor', 'backer', 'inbox'] })],
|
2019-07-15 09:25:22 +00:00
|
|
|
async handler (req, res) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const { user } = res.locals;
|
2020-04-20 14:32:54 +00:00
|
|
|
const { page, conversation } = req.query;
|
2019-07-15 09:25:22 +00:00
|
|
|
|
2020-01-12 18:34:40 +00:00
|
|
|
const userInbox = await getUserInbox(user, {
|
2019-07-15 09:25:22 +00:00
|
|
|
page, conversation, mapProps: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.respond(200, userInbox);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-04 23:00:24 +00:00
|
|
|
/**
|
|
|
|
|
* @apiIgnore
|
|
|
|
|
* @api {post} /api/v4/members/flag-private-message/:messageId Flag a private message
|
|
|
|
|
* @apiDescription Moderators are notified about every flagged message,
|
|
|
|
|
* including the sender, recipient, and full content of the message.
|
|
|
|
|
* This is for API v4 which must not be used in third-party tools as it can change without notice.
|
|
|
|
|
* There is no equivalent route in API v3.
|
|
|
|
|
* @apiName FlagPrivateMessage
|
|
|
|
|
* @apiGroup Member
|
|
|
|
|
*
|
|
|
|
|
* @apiParam (Path) {UUID} messageId The private message id
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {Object} data The flagged private message
|
|
|
|
|
* @apiSuccess {UUID} data.id The id of the message
|
|
|
|
|
* @apiSuccess {String} data.text The text of the message
|
|
|
|
|
* @apiSuccess {Number} data.timestamp The timestamp of the message in milliseconds
|
|
|
|
|
* @apiSuccess {Object} data.likes The likes of the message (always an empty object)
|
|
|
|
|
* @apiSuccess {Object} data.flags The flags of the message
|
|
|
|
|
* @apiSuccess {Number} data.flagCount The number of flags the message has
|
|
|
|
|
* @apiSuccess {UUID} data.uuid The User ID of the author of the message,
|
|
|
|
|
* or of the recipient if `sent` is true
|
|
|
|
|
* @apiSuccess {String} data.user The Display Name of the author of the message,
|
|
|
|
|
* or of the recipient if `sent` is true
|
|
|
|
|
* @apiSuccess {String} data.username The Username of the author of the message,
|
|
|
|
|
* or of the recipient if `sent` is true
|
|
|
|
|
*
|
|
|
|
|
* @apiUse MessageNotFound
|
|
|
|
|
* @apiUse MessageIdRequired
|
|
|
|
|
* @apiError (400) {BadRequest} messageGroupChatFlagAlreadyReported You have already
|
|
|
|
|
* reported this message
|
|
|
|
|
*/
|
|
|
|
|
api.flagPrivateMessage = {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
url: '/members/flag-private-message/:messageId',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
async handler (req, res) {
|
|
|
|
|
const chatReporter = chatReporterFactory('Inbox', req, res);
|
|
|
|
|
const message = await chatReporter.flag();
|
|
|
|
|
res.respond(200, {
|
|
|
|
|
ok: true,
|
|
|
|
|
message,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @api {post} /api/v4//inbox/like-private-message/:uniqueMessageId Like a private message
|
|
|
|
|
* @apiName LikePrivateMessage
|
|
|
|
|
* @apiGroup Inbox
|
|
|
|
|
* @apiDescription Likes a private message, this uses the uniqueMessageId which is a shared ID
|
|
|
|
|
* between message copies of both chat participants
|
|
|
|
|
*
|
|
|
|
|
* @apiParam (Path) {UUID} uniqueMessageId This is NOT private message.id,
|
|
|
|
|
* but rather message.uniqueMessageId
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {Object} data The liked <a href='https://github.com/HabitRPG/habitica/blob/develop/website/server/models/message.js#L42' target='_blank'>private message</a>
|
|
|
|
|
*
|
|
|
|
|
* @apiUse MessageNotFound
|
|
|
|
|
*/
|
|
|
|
|
api.likePrivateMessage = {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
url: '/inbox/like-private-message/:uniqueMessageId',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
async handler (req, res) {
|
|
|
|
|
req.checkParams('uniqueMessageId', apiError('messageIdRequired')).notEmpty();
|
|
|
|
|
|
|
|
|
|
const validationErrors = req.validationErrors();
|
|
|
|
|
if (validationErrors) throw validationErrors;
|
|
|
|
|
|
|
|
|
|
const { user } = res.locals;
|
|
|
|
|
const { uniqueMessageId } = req.params;
|
|
|
|
|
|
|
|
|
|
const messages = await inboxLib.getInboxMessagesByUniqueId(uniqueMessageId);
|
|
|
|
|
|
|
|
|
|
if (messages.length === 0) {
|
|
|
|
|
throw new NotFound(res.t('messageGroupChatNotFound'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (messages.length > 2) {
|
|
|
|
|
logger.error(`More than 2 Messages exist with this uniqueMessageId: ${uniqueMessageId} check in Database!`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await applyLikeToMessages(user, messages);
|
|
|
|
|
|
|
|
|
|
const messageToReturn = messages.find(m => m.uuid === user._id);
|
|
|
|
|
|
|
|
|
|
res.respond(200, messageToReturn);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-02 17:45:27 +00:00
|
|
|
export default api;
|