diff --git a/test/api/v3/integration/chat/POST-chat.like.test.js b/test/api/v3/integration/chat/POST-chat.like.test.js new file mode 100644 index 0000000000..b5f70d6ba4 --- /dev/null +++ b/test/api/v3/integration/chat/POST-chat.like.test.js @@ -0,0 +1,68 @@ +import { + generateUser, + requester, + translate as t, +} from '../../../../helpers/api-integration.helper'; + +describe('POST /chat', () => { + let user; + let api; + let group; + let testMessage = 'Test Message'; + + before(() => { + let groupName = 'Test Guild'; + let groupType = 'guild'; + let groupPrivacy = 'public'; + + return generateUser({balance: 1}).then((generatedUser) => { + user = generatedUser; + api = requester(user); + }) + .then(() => { + return api.post('/groups', { + name: groupName, + type: groupType, + privacy: groupPrivacy, + }); + }) + .then((generatedGroup) => { + group = generatedGroup; + }); + }); + + it('Returns an error when chat message is not found', () => { + return expect(api.post(`/groups/${group._id}/chat/incorrectMessage/like`)) + .to.eventually.be.rejected.and.eql({ + code: 404, + error: 'NotFound', + message: t('messageGroupChatNotFound'), + }); + }); + + it('Returns an error when user tries to like their own message', () => { + return api.post(`/groups/${group._id}/chat`, { message: testMessage}) + .then((result) => { + return expect(api.post(`/groups/${group._id}/chat/${result.message.id}/like`)) + .to.eventually.be.rejected.and.eql({ + code: 404, + error: 'NotFound', + message: t('messageGroupChatLikeOwnMessage'), + }); + }); + }); + + it('Likes a chat', () => { + let api2; + return generateUser().then((generatedUser) => { + api2 = requester(generatedUser); + return api2.post(`/groups/${group._id}/chat`, { message: testMessage}); + }) + .then((result) => { + return api.post(`/groups/${group._id}/chat/${result.message.id}/like`); + }) + .then((result) => { + expect(result[0].likes[user._id]).to.equal(true); + }); + }); +}); diff --git a/website/src/controllers/api-v3/chat.js b/website/src/controllers/api-v3/chat.js index d3a22a1fc8..f04bd031d9 100644 --- a/website/src/controllers/api-v3/chat.js +++ b/website/src/controllers/api-v3/chat.js @@ -4,6 +4,7 @@ import { model as Group } from '../../models/group'; import { NotFound, } from '../../libs/api-v3/errors'; +import _ from 'lodash'; let api = {}; @@ -95,4 +96,57 @@ api.postChat = { }, }; +/** + * @api {post} /groups/:groupId/chat/:chatId/like Like a group chat message + * @apiVersion 3.0.0 + * @apiName LikeChat + * @apiGroup Chat + * + * @apiParam {groupId} groupId The group _id + * @apiParam {chatId} chatId The chat message _id + * + * @apiSuccess {Array} chat An array of chat messages + */ +api.likeChat = { + method: 'Post', + url: '/groups/:groupId/chat/:chatId/like', + middlewares: [authWithHeaders(), cron], + handler (req, res, next) { + let user = res.locals.user; + let groupId = req.params.groupId; + + req.checkParams('groupId', res.t('groupIdRequired')).notEmpty(); + req.checkParams('chatId', res.t('chatIdRequired')).notEmpty(); + + let validationErrors = req.validationErrors(); + if (validationErrors) return next(validationErrors); + + Group.getGroup(user, groupId) + .then((group) => { + if (!group) throw new NotFound(res.t('groupNotFound')); + let message = _.find(group.chat, {id: req.params.chatId}); + if (!message) throw new NotFound(res.t('messageGroupChatNotFound')); + + if (message.uuid === user._id) throw new NotFound(res.t('messageGroupChatLikeOwnMessage')); + + if (!message.likes) message.likes = {}; + if (message.likes[user._id]) { + delete message.likes[user._id]; + } else { + message.likes[user._id] = true; + } + + let messageIndex = group.chat.indexOf(message); + group.chat[messageIndex].likes = message.likes; + + return group.save(); + }) + .then((group) => { + if (!group) throw new NotFound(res.t('groupNotFound')); + res.respond(200, group.chat); + }) + .catch(next); + }, +}; + export default api;