From 1a725c051545ee419f315591a4bf5a8d86937a23 Mon Sep 17 00:00:00 2001 From: kholliday Date: Mon, 23 Mar 2015 15:15:52 -0500 Subject: [PATCH 1/2] Added Conditional Semantics to group member query to always include the user --- website/src/controllers/groups.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/website/src/controllers/groups.js b/website/src/controllers/groups.js index 8eda273a34..fcf6c50f0d 100644 --- a/website/src/controllers/groups.js +++ b/website/src/controllers/groups.js @@ -30,13 +30,15 @@ var guildPopulate = {path: 'members', select: nameFields, options: {limit: 15} } * limited fields - and only a sampling of the members, beacuse they can be in the thousands * @param type: 'party' or otherwise * @param q: the Mongoose query we're building up - * @param additionalFields: if we want to populate some additional field not fetched normally + * @param additionalFields: if we want to populate some additional field not fetched normally * pass it as a string, parties only */ -var populateQuery = function(type, q, additionalFields){ +var populateQuery = function(type, q, additionalFields, user){ if (type == 'party') q.populate('members', partyFields + (additionalFields ? (' ' + additionalFields) : '')); else + //Use Conditional Semantics to always include the user + guildPopulate.match = {"_id": { "$ne" : null, "$in": [user._id] } }; q.populate(guildPopulate); q.populate('invites', nameFields); q.populate({ @@ -132,7 +134,7 @@ api.get = function(req, res, next) { {_id:gid, privacy:'public'}, {_id:gid, privacy:'private', members: {$in:[user._id]}} // if the group is private, only return if they have access ]}); - populateQuery(gid, q); + populateQuery(gid, q, null, user); q.exec(function(err, group){ if (err) return next(err); if (!group && gid!=='party') return res.json(404,{err: "Group not found or you don't have access."}); @@ -305,7 +307,7 @@ api.flagChatMessage = function(req, res, next){ group.save(function(err,_saved){ if(err) return next(err); var addressesToSendTo = JSON.parse(nconf.get('FLAG_REPORT_EMAIL')); - + if(Array.isArray(addressesToSendTo)){ addressesToSendTo = addressesToSendTo.map(function(email){ return {email: email, canSend: true} @@ -358,7 +360,7 @@ api.clearFlagCount = function(req, res, next){ }else{ return res.json(401, {err: "Only an admin can clear the flag count!"}) } - + } api.seenMessage = function(req,res,next){ @@ -577,7 +579,7 @@ var inviteByUUIDs = function(uuids, group, req, res, next){ cb(); }); } - }); + }); }, function(err){ if(err) return err.code ? res.json(err.code, {err: err.err}) : next(err); @@ -638,7 +640,7 @@ var inviteByEmails = function(invites, group, req, res, next){ inviteByUUIDs(usersAlreadyRegistered, group, req, res, next); }else{ - // Send only status code down the line because it doesn't need + // Send only status code down the line because it doesn't need // info on invited users since they are not yet registered res.send(200); } From 23e7a8e258108823b7bdd2d7c82e83098e46cf1d Mon Sep 17 00:00:00 2001 From: kholliday Date: Tue, 24 Mar 2015 08:54:45 -0500 Subject: [PATCH 2/2] Added check for user object to prevent errors --- website/src/controllers/groups.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/website/src/controllers/groups.js b/website/src/controllers/groups.js index fcf6c50f0d..438bcce0cd 100644 --- a/website/src/controllers/groups.js +++ b/website/src/controllers/groups.js @@ -37,8 +37,9 @@ var populateQuery = function(type, q, additionalFields, user){ if (type == 'party') q.populate('members', partyFields + (additionalFields ? (' ' + additionalFields) : '')); else - //Use Conditional Semantics to always include the user - guildPopulate.match = {"_id": { "$ne" : null, "$in": [user._id] } }; + if ( user ) + //Use Conditional Semantics to always include the user + guildPopulate.match = {"_id": { "$ne" : null, "$in": [user._id] } }; q.populate(guildPopulate); q.populate('invites', nameFields); q.populate({