From 081462983a2bb82452a15127b87bf70d4dd5c2fe Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 11 Feb 2015 11:34:33 -0500 Subject: [PATCH] refactored the groups update method a bit, and now if a group has pending members and it becomes a public group, those users become members --- src/groups.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/groups.js b/src/groups.js index 7567c662f5..f82e452b96 100644 --- a/src/groups.js +++ b/src/groups.js @@ -503,7 +503,11 @@ var async = require('async'), 'private': values.private === false ? '0' : '1' }; - db.setObject('group:' + groupName, payload, function(err) { + async.series([ + async.apply(updatePrivacy, groupName, values.private), + async.apply(db.setObject, 'group:' + groupName, payload), + async.apply(renameGroup, groupName, values.name) + ], function(err) { if (err) { return callback(err); } @@ -512,11 +516,37 @@ var async = require('async'), name: groupName, values: values }); - renameGroup(groupName, values.name, callback); + callback(); }); }); }; + function updatePrivacy(groupName, newValue, callback) { + // Grab the group's current privacy value + Groups.getGroupFields(groupName, ['private'], function(err, currentValue) { + currentValue = currentValue.private === '1'; // Now a Boolean + + if (currentValue !== newValue && currentValue === true) { + // Group is now public, so all pending users are automatically considered members + db.getSetMembers('group:' + groupName + ':pending', function(err, uids) { + if (err) { return callback(err); } + else if (!uids) { return callback(); } // No pending users, we're good to go + + var now = Date.now(), + scores = uids.map(function() { return now; }); // There's probably a better way to initialise an Array of size x with the same value... + + winston.verbose('[groups.update] Group is now public, automatically adding ' + uids.length + ' new members, who were pending prior.'); + async.series([ + async.apply(db.sortedSetAdd, 'group:' + groupName + ':members', scores, uids), + async.apply(db.delete, 'group:' + groupName + ':pending') + ], callback); + }); + } else { + callback(); + } + }); + } + function renameGroup(oldName, newName, callback) { if (oldName === newName || !newName || newName.length === 0) { return callback();