challenges: fixed joining / leaving groups. Back to the old method of Group.query({type:'public'}), etc - since ngResource can only work with top-level objects. This approach is bad performance (two calls), I'll fix when I can think of a better way of organizing groups requested at GET /groups

This commit is contained in:
Tyler Renelle
2013-10-30 14:34:59 -07:00
parent da7ab9f1c3
commit 28cdeb0ac6
4 changed files with 39 additions and 22 deletions

View File

@@ -41,11 +41,13 @@ api.list = function(req, res) {
var user = res.locals.user;
var groupFields = 'name description memberCount';
var sort = '-memberCount';
var type = (req.query.type || 'party,guilds,public,tavern').split(',');
async.parallel({
// unecessary given our ui-router setup
party: function(cb){
if (!~type.indexOf('party')) return cb(null, {});
Group.findOne({type: 'party', members: {'$in': [user._id]}})
.select(groupFields).exec(function(err, party){
if (err) return cb(err);
@@ -54,11 +56,13 @@ api.list = function(req, res) {
},
guilds: function(cb) {
if (!~type.indexOf('guilds')) return cb(null, []);
Group.find({members: {'$in': [user._id]}, type:'guild'})
.select(groupFields).sort(sort).exec(cb);
},
'public': function(cb) {
if (!~type.indexOf('public')) return cb(null, []);
Group.find({privacy: 'public'})
.select(groupFields + ' members')
.sort(sort)
@@ -75,6 +79,7 @@ api.list = function(req, res) {
// unecessary given our ui-router setup
tavern: function(cb) {
if (!~type.indexOf('tavern')) return cb(null, {});
Group.findById('habitrpg').select(groupFields).exec(function(err, tavern){
if (err) return cb(err);
cb(null, [tavern]); // return as an array for consistent ngResource use
@@ -83,6 +88,14 @@ api.list = function(req, res) {
}, function(err, results){
if (err) return res.json(500, {err: err});
// If they're requesting a specific type, let's return it as an array so that $ngResource
// can utilize it properly
if (req.query.type) {
results = _.reduce(type, function(m,t){
return m.concat(_.isArray(results[t]) ? results[t] : [results[t]]);
}, []);
}
res.json(results);
})
};