diff --git a/test/api/v3/integration/chat/POST-chat.flag.test.js b/test/api/v3/integration/chat/POST-chat.flag.test.js index 187a86614b..9c2d5849a9 100644 --- a/test/api/v3/integration/chat/POST-chat.flag.test.js +++ b/test/api/v3/integration/chat/POST-chat.flag.test.js @@ -8,7 +8,7 @@ describe('POST /chat/:chatId/flag', () => { let user, admin, anotherUser, group; const TEST_MESSAGE = 'Test Message'; - before(async () => { + beforeEach(async () => { user = await generateUser({balance: 1}); admin = await generateUser({balance: 1, 'contributor.admin': true}); anotherUser = await generateUser(); @@ -83,6 +83,41 @@ describe('POST /chat/:chatId/flag', () => { expect(messageToCheck.flagCount).to.equal(5); }); + it('allows admin to flag a message in a private group', async () => { + let privateGroup = await user.post('/groups', { + name: 'Test party', + type: 'party', + privacy: 'private', + }); + let { message } = await user.post(`/groups/${privateGroup._id}/chat`, {message: TEST_MESSAGE}); + + let flagResult = await admin.post(`/groups/${privateGroup._id}/chat/${message.id}/flag`); + + expect(flagResult.flags[admin._id]).to.equal(true); + expect(flagResult.flagCount).to.equal(5); + + let groupWithFlags = await user.get(`/groups/${privateGroup._id}`); + let messageToCheck = find(groupWithFlags.chat, {id: message.id}); + + expect(messageToCheck).to.not.exist; + }); + + it('does not allow non member to flag message in private group', async () => { + let privateGroup = await user.post('/groups', { + name: 'Test party', + type: 'party', + privacy: 'private', + }); + let { message } = await user.post(`/groups/${privateGroup._id}/chat`, {message: TEST_MESSAGE}); + + await expect(anotherUser.post(`/groups/${privateGroup._id}/chat/${message.id}/flag`)) + .to.eventually.be.rejected.and.eql({ + code: 404, + error: 'NotFound', + message: t('groupNotFound'), + }); + }); + it('Returns an error when user tries to flag a message that is already flagged', async () => { let { message } = await anotherUser.post(`/groups/${group._id}/chat`, {message: TEST_MESSAGE}); diff --git a/website/server/controllers/api-v3/chat.js b/website/server/controllers/api-v3/chat.js index 51838994bf..d1179b0ee2 100644 --- a/website/server/controllers/api-v3/chat.js +++ b/website/server/controllers/api-v3/chat.js @@ -159,14 +159,27 @@ api.likeChat = { /** * @api {post} /api/v3/groups/:groupId/chat/:chatId/flag Flag a group chat message - * @apiVersion 3.0.0 + * @apiDescription A message will be hidden from chat if two or more users flag a message. It will be hidden immediately if a moderator flags the message. An email is sent to the moderators about every flagged message. * @apiName FlagChat * @apiGroup Chat * - * @apiParam {UUID} groupId The group _id ('party' for the user party and 'habitrpg' for tavern are accepted) + * @apiParam {UUID} groupId The group id ('party' for the user party and 'habitrpg' for tavern are accepted) * @apiParam {UUID} chatId The chat message id * * @apiSuccess {Object} data The flagged chat 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 + * @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 + * @apiSuccess {String} data.user The username of the author of the message + * + * @apiError GroupNotFound Group could not be found or you don't have access + * @apiError ChatNotFound Chat message with specified id could not be found + * @apiError FlagOwnMessage Chat messages cannot be flagged by the author of the message + * @apiError AlreadyFlagged Chat messages cannot be flagged more than once by a user */ api.flagChat = { method: 'POST', @@ -182,7 +195,11 @@ api.flagChat = { let validationErrors = req.validationErrors(); if (validationErrors) throw validationErrors; - let group = await Group.getGroup({user, groupId}); + let group = await Group.getGroup({ + user, + groupId, + optionalMembership: user.contributor.admin, + }); if (!group) throw new NotFound(res.t('groupNotFound')); let message = _.find(group.chat, {id: req.params.chatId});