2018-08-30 19:50:03 +00:00
|
|
|
import { authWithHeaders } from '../../middlewares/auth';
|
2018-09-21 13:12:20 +00:00
|
|
|
import * as inboxLib from '../../libs/inbox';
|
2025-03-04 23:00:24 +00:00
|
|
|
import { sanitizeText as sanitizeMessageText } from '../../models/message';
|
|
|
|
|
import highlightMentions from '../../libs/highlightMentions';
|
|
|
|
|
import { model as User } from '../../models/user';
|
|
|
|
|
import { NotAuthorized, NotFound } from '../../libs/errors';
|
|
|
|
|
import { sentMessage } from '../../libs/inbox';
|
2018-08-30 19:50:03 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const api = {};
|
2018-08-30 19:50:03 +00:00
|
|
|
|
|
|
|
|
/* NOTE most inbox routes are either in the user or members controller */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @api {get} /api/v3/inbox/messages Get inbox messages for a user
|
|
|
|
|
* @apiName GetInboxMessages
|
|
|
|
|
* @apiGroup Inbox
|
|
|
|
|
* @apiDescription Get inbox messages for a user
|
|
|
|
|
*
|
2019-03-31 18:52:53 +00:00
|
|
|
* @apiParam (Query) {Number} page Load the messages of the selected Page - 10 Messages per Page
|
2019-04-26 16:45:05 +00:00
|
|
|
* @apiParam (Query) {GUID} conversation Loads only the messages of a conversation
|
2019-03-31 18:52:53 +00:00
|
|
|
*
|
2018-08-30 19:50:03 +00:00
|
|
|
* @apiSuccess {Array} data An array of inbox messages
|
|
|
|
|
*/
|
|
|
|
|
api.getInboxMessages = {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
url: '/inbox/messages',
|
2024-08-12 21:45:35 +00:00
|
|
|
middlewares: [authWithHeaders({ userFieldsToInclude: ['profile', 'contributor', 'backer', 'inbox'] })],
|
2018-08-30 19:50:03 +00:00
|
|
|
async handler (req, res) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const { user } = res.locals;
|
|
|
|
|
const { page } = req.query;
|
|
|
|
|
const { conversation } = req.query;
|
2018-08-30 19:50:03 +00:00
|
|
|
|
2019-03-31 18:52:53 +00:00
|
|
|
const userInbox = await inboxLib.getUserInbox(user, {
|
2019-04-26 16:45:05 +00:00
|
|
|
page, conversation,
|
2019-03-31 18:52:53 +00:00
|
|
|
});
|
2018-09-21 13:12:20 +00:00
|
|
|
|
|
|
|
|
res.respond(200, userInbox);
|
2018-08-30 19:50:03 +00:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-04 23:00:24 +00:00
|
|
|
/**
|
|
|
|
|
* @api {post} /api/v3/members/send-private-message Send a private message to a member
|
|
|
|
|
* @apiName SendPrivateMessage
|
|
|
|
|
* @apiGroup Member
|
|
|
|
|
*
|
|
|
|
|
* @apiParam (Body) {String} message The message
|
|
|
|
|
* @apiParam (Body) {UUID} toUserId The id of the user to contact
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {Object} data.message The message just sent
|
|
|
|
|
*
|
|
|
|
|
* @apiUse UserNotFound
|
|
|
|
|
*/
|
|
|
|
|
api.sendPrivateMessage = {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
url: '/members/send-private-message',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
async handler (req, res) {
|
|
|
|
|
req.checkBody('message', res.t('messageRequired')).notEmpty();
|
|
|
|
|
req.checkBody('toUserId', res.t('toUserIDRequired')).notEmpty().isUUID();
|
|
|
|
|
|
|
|
|
|
const validationErrors = req.validationErrors();
|
|
|
|
|
if (validationErrors) throw validationErrors;
|
|
|
|
|
|
|
|
|
|
const sender = res.locals.user;
|
|
|
|
|
const sanitizedMessageText = sanitizeMessageText(req.body.message);
|
|
|
|
|
const message = (await highlightMentions(sanitizedMessageText))[0];
|
|
|
|
|
|
|
|
|
|
const receiver = await User.findById(req.body.toUserId).exec();
|
|
|
|
|
if (!receiver) throw new NotFound(res.t('userNotFound'));
|
|
|
|
|
if (!receiver.flags.verifiedUsername) delete receiver.auth.local.username;
|
|
|
|
|
|
|
|
|
|
const objections = sender.getObjectionsToInteraction('send-private-message', receiver);
|
|
|
|
|
if (objections.length > 0 && !sender.hasPermission('moderator')) throw new NotAuthorized(res.t(objections[0]));
|
|
|
|
|
|
|
|
|
|
const messageSent = await sentMessage(sender, receiver, message, res.t);
|
|
|
|
|
|
|
|
|
|
res.respond(200, { message: messageSent });
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-02 17:45:27 +00:00
|
|
|
export default api;
|