diff --git a/test/api/groups/GET-groups_id.test.js b/test/api/groups/GET-groups_id.test.js index aecd6b4c90..f2f69e0d3a 100644 --- a/test/api/groups/GET-groups_id.test.js +++ b/test/api/groups/GET-groups_id.test.js @@ -197,7 +197,7 @@ describe('GET /groups/:id', () => { }); }); - xit('TODO: Not yet implimented - includes user ids in flags object', () => { + it('includes user ids in flags object', () => { return api.get(`/groups/${group._id}`).then((_group) => { let chatWithOneFlag = _group.chat[2]; expect(chatWithOneFlag.id).to.eql(chat3.id); diff --git a/test/api/groups/chat/POST-groups_id_chat_id_flag.test.js b/test/api/groups/chat/POST-groups_id_chat_id_flag.test.js index 16ab13f429..5c822ad081 100644 --- a/test/api/groups/chat/POST-groups_id_chat_id_flag.test.js +++ b/test/api/groups/chat/POST-groups_id_chat_id_flag.test.js @@ -36,7 +36,7 @@ describe('POST /groups/:id/chat/:id/flag', () => { return api.get(`/groups/${group._id}/chat`); }).then((messages) => { let message = messages[0]; - expect(message.flags[user._id]).to.eql(true); + expect(message.flagCount).to.eql(1); }); }); }); diff --git a/website/src/controllers/api-v2/groups.js b/website/src/controllers/api-v2/groups.js index 40a01f32a5..89028f7aff 100644 --- a/website/src/controllers/api-v2/groups.js +++ b/website/src/controllers/api-v2/groups.js @@ -149,10 +149,11 @@ api.get = function(req, res, next) { // so that users with no party don't get a 404 on every access to the site return res.json(group); } - //Remove flagged messages if the user is not mod + if (!user.contributor.admin) { - group.chat = _.filter(group.chat, function(message) { return !message.flagCount || message.flagCount < 2; }); + _purgeFlagInfoFromChat(group); } + //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 var userInGroup = _.find(group.members, function(member){ return member._id == user._id; }); //If the group is private or the group is a party, then the user must be a member of the group based on access restrictions above @@ -251,11 +252,17 @@ api.update = function(req, res, next) { // TODO remove from api object? api.attachGroup = function(req, res, next) { + var user = res.locals.user; var gid = req.params.gid; var q = (gid == 'party') ? Group.findOne({type: 'party', members: {'$in': [res.locals.user._id]}}) : Group.findById(gid); q.exec(function(err, group){ if(err) return next(err); if(!group) return res.json(404, {err: shared.i18n.t('messageGroupNotFound')}); + + if (!user.contributor.admin) { + _purgeFlagInfoFromChat(group); + } + res.locals.group = group; next(); }); @@ -275,10 +282,7 @@ api.getChat = function(req, res, next) { q.exec(function(err, group){ if (err) return next(err); if (!group && gid!=='party') return res.json(404,{err: shared.i18n.t('messageGroupNotFound')}); - //Remove flagged messages if the user is not mod - if (!user.contributor.admin) { - group.chat = _.filter(group.chat, function(message) { return !message.flagCount || message.flagCount < 2; }); - } + res.json(res.locals.group.chat); gid = null; }); @@ -1095,3 +1099,10 @@ api.questLeave = function(req, res, next) { return next(error); }); } + +function _purgeFlagInfoFromChat(group) { + group.chat = _.filter(group.chat, function(message) { return !message.flagCount || message.flagCount < 2; }); + _.each(group.chat, function (message) { + message.flags = {}; + }); +} diff --git a/website/src/models/group.js b/website/src/models/group.js index 17817994c1..7ba8dac173 100644 --- a/website/src/models/group.js +++ b/website/src/models/group.js @@ -475,11 +475,6 @@ GroupSchema.methods.leave = function(user, keep, mainCb){ GroupSchema.methods.toJSON = function() { var doc = this.toObject(); - if(doc.chat){ - doc.chat.forEach(function(msg){ - msg.flags = {}; - }); - } return doc; };