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
+17 -6
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 = {};
});
}