Refactor and add tests to flagged messages.

This commit is contained in:
Blade Barringer
2015-11-03 08:00:14 -06:00
parent 24480e5f04
commit 1a68b5e9ee
4 changed files with 19 additions and 13 deletions

View File

@@ -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);

View File

@@ -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);
});
});
});

View File

@@ -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 = {};
});
}

View File

@@ -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;
};