v1.18.x
barisusakli 10 years ago
parent 73a226e2ed
commit 6332f47808

@ -39,7 +39,9 @@ module.exports = function(Groups) {
}, next); }, next);
}); });
} }
], callback); ], function(err) {
callback(err);
});
}); });
}; };
}; };

@ -13,13 +13,38 @@ var db = require('./../database');
module.exports = function(Groups) { module.exports = function(Groups) {
Groups.join = function(groupName, uid, callback) { Groups.join = function(groupName, uid, callback) {
function join() { callback = callback || function() {};
var tasks = [
async.apply(db.sortedSetAdd, 'group:' + groupName + ':members', Date.now(), uid), if (!groupName) {
async.apply(db.incrObjectField, 'group:' + groupName, 'memberCount') return callback(new Error('[[error:invalid-data]]'));
]; }
async.waterfall([ async.waterfall([
function(next) {
Groups.isMember(uid, groupName, next);
},
function(isMember, next) {
if (isMember) {
return callback();
}
Groups.exists(groupName, next);
},
function(exists, next) {
if (exists) {
return next();
}
Groups.create({
name: groupName,
description: '',
hidden: 1
}, function(err) {
if (err && err.message !== '[[error:group-already-exists]]') {
winston.error('[groups.join] Could not create new hidden group: ' + err.message);
return callback(err);
}
next();
});
},
function(next) { function(next) {
async.parallel({ async.parallel({
isAdmin: function(next) { isAdmin: function(next) {
@ -31,6 +56,10 @@ module.exports = function(Groups) {
}, next); }, next);
}, },
function(results, next) { function(results, next) {
var tasks = [
async.apply(db.sortedSetAdd, 'group:' + groupName + ':members', Date.now(), uid),
async.apply(db.incrObjectField, 'group:' + groupName, 'memberCount')
];
if (results.isAdmin) { if (results.isAdmin) {
tasks.push(async.apply(db.setAdd, 'group:' + groupName + ':owners', uid)); tasks.push(async.apply(db.setAdd, 'group:' + groupName + ':owners', uid));
} }
@ -50,35 +79,6 @@ module.exports = function(Groups) {
next(); next();
} }
], callback); ], callback);
}
callback = callback || function() {};
if (!groupName) {
return callback(new Error('[[error:invalid-data]]'));
}
Groups.exists(groupName, function(err, exists) {
if (err) {
return callback(err);
}
if (exists) {
return join();
}
Groups.create({
name: groupName,
description: '',
hidden: 1
}, function(err) {
if (err && err.message !== '[[error:group-already-exists]]') {
winston.error('[groups.join] Could not create new hidden group: ' + err.message);
return callback(err);
}
join();
});
});
}; };
function setGroupTitleIfNotSet(groupName, uid, callback) { function setGroupTitleIfNotSet(groupName, uid, callback) {
@ -204,38 +204,52 @@ module.exports = function(Groups) {
Groups.leave = function(groupName, uid, callback) { Groups.leave = function(groupName, uid, callback) {
callback = callback || function() {}; callback = callback || function() {};
var tasks = [ async.waterfall([
function(next) {
Groups.isMember(uid, groupName, next);
},
function(isMember, next) {
if (!isMember) {
return callback();
}
Groups.exists(groupName, next);
},
function(exists, next) {
if (!exists) {
return callback();
}
async.parallel([
async.apply(db.sortedSetRemove, 'group:' + groupName + ':members', uid), async.apply(db.sortedSetRemove, 'group:' + groupName + ':members', uid),
async.apply(db.setRemove, 'group:' + groupName + ':owners', uid), async.apply(db.setRemove, 'group:' + groupName + ':owners', uid),
async.apply(db.decrObjectField, 'group:' + groupName, 'memberCount') async.apply(db.decrObjectField, 'group:' + groupName, 'memberCount')
]; ], next);
},
async.parallel(tasks, function(err) { function(results, next) {
if (err) { Groups.getGroupFields(groupName, ['hidden', 'memberCount'], next);
return callback(err); },
function(groupData, next) {
if (!groupData) {
return callback();
} }
if (parseInt(groupData.hidden, 10) === 1 && parseInt(groupData.memberCount, 10) === 0) {
Groups.destroy(groupName, next);
} else {
if (parseInt(groupData.hidden, 10) !== 1) {
db.sortedSetAdd('groups:visible:memberCount', groupData.memberCount, groupName, next);
} else {
next();
}
}
},
function(next) {
plugins.fireHook('action:group.leave', { plugins.fireHook('action:group.leave', {
groupName: groupName, groupName: groupName,
uid: uid uid: uid
}); });
next();
Groups.getGroupFields(groupName, ['hidden', 'memberCount'], function(err, groupData) {
if (err || !groupData) {
return callback(err);
} }
], callback);
if (parseInt(groupData.hidden, 10) === 1 && parseInt(groupData.memberCount, 10) === 0) {
Groups.destroy(groupName, callback);
} else {
if (parseInt(groupData.hidden, 10) !== 1) {
db.sortedSetAdd('groups:visible:memberCount', groupData.memberCount, groupName, callback);
} else {
callback();
}
}
});
});
}; };
Groups.leaveAllGroups = function(uid, callback) { Groups.leaveAllGroups = function(uid, callback) {

Loading…
Cancel
Save