From c297e2f2f8b10bec593f6d633c2c8f76593e98e9 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 14 May 2014 16:13:30 -0400 Subject: [PATCH] ephemeral groups - guests --- src/groups.js | 77 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/src/groups.js b/src/groups.js index 6501b3dc66..030df7d0a4 100644 --- a/src/groups.js +++ b/src/groups.js @@ -7,32 +7,49 @@ db = require('./database'), utils = require('../public/src/utils'), - filterGroups = function(groups, options) { - // Remove system, hidden, or deleted groups from this list - if (groups && !options.showAllGroups) { - return groups.filter(function (group) { - if (group.deleted || (group.hidden && !group.system) || (!options.showSystemGroups && group.system)) { - return false; + ephemeralGroups = ['guests'], + + internals = { + filterGroups: function(groups, options) { + // Remove system, hidden, or deleted groups from this list + if (groups && !options.showAllGroups) { + return groups.filter(function (group) { + if (group.deleted || (group.hidden && !group.system) || (!options.showSystemGroups && group.system)) { + return false; + } else { + return true; + } + }); + } else { + return groups; + } + }, + getEphemeralGroup: function(groupName, options, callback) { + Groups.exists(groupName, function(err, exists) { + if (!err && exists) { + Groups.get.apply(null, arguments); } else { - return true; + callback(null, { + name: groupName, + description: '', + deleted: '0', + hidden: '0', + system: '1' + }); } }); - } else { - return groups; } }; Groups.list = function(options, callback) { db.getSetMembers('groups', function (err, groupNames) { - if (groupNames.length > 0) { - async.map(groupNames, function (groupName, next) { - Groups.get(groupName, options, next); - }, function (err, groups) { - callback(err, filterGroups(groups, options)); - }); - } else { - callback(null, []); - } + groupNames = groupNames.concat(ephemeralGroups); + + async.map(groupNames, function (groupName, next) { + Groups.get(groupName, options, next); + }, function (err, groups) { + callback(err, internals.filterGroups(groups, options)); + }); }); }; @@ -42,15 +59,19 @@ async.parallel({ base: function (next) { - db.getObject('group:' + groupName, function(err, groupObj) { - if (err) { - next(err); - } else if (!groupObj) { - next('group-not-found'); - } else { - next(err, groupObj); - } - }); + if (ephemeralGroups.indexOf(groupName) === -1) { + db.getObject('group:' + groupName, function(err, groupObj) { + if (err) { + next(err); + } else if (!groupObj) { + next('group-not-found'); + } else { + next(err, groupObj); + } + }); + } else { + internals.getEphemeralGroup(groupName, options, next); + } }, users: function (next) { db.getSetMembers('group:' + groupName + ':members', function (err, uids) { @@ -103,7 +124,7 @@ async.map(groups, function(groupName, next) { Groups.get(groupName, options, next); }, function(err, groups) { - callback(err, filterGroups(groups, options)); + callback(err, internals.filterGroups(groups, options)); }); }); } else {