From 1fc21167dc07be9d1c4807bfdcbdcefaf41b6c0e Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 10 Nov 2015 08:15:54 -0600 Subject: [PATCH] Send back flag data if user had flagged the message. --- .../groups/chat/POST-groups_id_chat_id_flag.test.js | 11 +++++++++++ website/src/controllers/api-v2/groups.js | 9 ++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/test/api/v2/groups/chat/POST-groups_id_chat_id_flag.test.js b/test/api/v2/groups/chat/POST-groups_id_chat_id_flag.test.js index ac6e48dcac..6557486353 100644 --- a/test/api/v2/groups/chat/POST-groups_id_chat_id_flag.test.js +++ b/test/api/v2/groups/chat/POST-groups_id_chat_id_flag.test.js @@ -39,6 +39,17 @@ describe('POST /groups/:id/chat/:id/flag', () => { expect(message.flagCount).to.eql(1); }); }); + + it('cannot flag the same message twice', () => { + let api = requester(user); + + return expect(api.post(`/groups/${group._id}/chat/${message.id}/flag`).then((messages) => { + return api.post(`/groups/${group._id}/chat/${message.id}/flag`); + })).to.eventually.be.rejected.and.eql({ + code: 401, + text: t('messageGroupChatFlagAlreadyReported'), + }); + }); }); context('own message', () => { diff --git a/website/src/controllers/api-v2/groups.js b/website/src/controllers/api-v2/groups.js index 89028f7aff..f1b1b8a0fc 100644 --- a/website/src/controllers/api-v2/groups.js +++ b/website/src/controllers/api-v2/groups.js @@ -151,7 +151,7 @@ api.get = function(req, res, next) { } if (!user.contributor.admin) { - _purgeFlagInfoFromChat(group); + _purgeFlagInfoFromChat(group, user); } //Since we have a limit on how many members are populate to the group, we want to make sure the user is always in the group @@ -260,7 +260,7 @@ api.attachGroup = function(req, res, next) { if(!group) return res.json(404, {err: shared.i18n.t('messageGroupNotFound')}); if (!user.contributor.admin) { - _purgeFlagInfoFromChat(group); + _purgeFlagInfoFromChat(group, user); } res.locals.group = group; @@ -1100,9 +1100,12 @@ api.questLeave = function(req, res, next) { }); } -function _purgeFlagInfoFromChat(group) { +function _purgeFlagInfoFromChat(group, user) { group.chat = _.filter(group.chat, function(message) { return !message.flagCount || message.flagCount < 2; }); _.each(group.chat, function (message) { + var userHasFlagged = message.flags[user._id]; message.flags = {}; + + if (userHasFlagged) message.flags[user._id] = userHasFlagged; }); }