2014-04-15 02:33:31 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
2015-10-13 19:28:55 -04:00
|
|
|
var async = require('async');
|
2014-04-15 02:33:31 -04:00
|
|
|
var groups = require('../../groups'),
|
|
|
|
|
Groups = {};
|
|
|
|
|
|
|
|
|
|
Groups.create = function(socket, data, callback) {
|
|
|
|
|
if(!data) {
|
|
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 10:33:54 -05:00
|
|
|
groups.create({
|
2015-05-28 13:33:23 -04:00
|
|
|
name: data.name,
|
2015-01-09 10:33:54 -05:00
|
|
|
description: data.description,
|
|
|
|
|
ownerUid: socket.uid
|
|
|
|
|
}, callback);
|
2014-04-15 02:33:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Groups.join = function(socket, data, callback) {
|
2015-07-08 12:20:07 -04:00
|
|
|
if (!data) {
|
2014-04-15 02:33:31 -04:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-13 19:28:55 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
groups.isMember(data.uid, data.groupName, next);
|
|
|
|
|
},
|
|
|
|
|
function (isMember, next) {
|
|
|
|
|
if (isMember) {
|
|
|
|
|
return next(new Error('[[error:group-already-member]]'));
|
|
|
|
|
}
|
|
|
|
|
groups.join(data.groupName, data.uid, next);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
2014-04-15 02:33:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Groups.leave = function(socket, data, callback) {
|
2015-07-08 12:20:07 -04:00
|
|
|
if (!data) {
|
2014-04-15 02:33:31 -04:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 12:20:07 -04:00
|
|
|
if (socket.uid === parseInt(data.uid, 10) && data.groupName === 'administrators') {
|
|
|
|
|
return callback(new Error('[[error:cant-remove-self-as-admin]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-13 19:28:55 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
groups.isMember(data.uid, data.groupName, next);
|
|
|
|
|
},
|
|
|
|
|
function (isMember, next) {
|
|
|
|
|
if (!isMember) {
|
|
|
|
|
return next(new Error('[[error:group-not-member]]'));
|
|
|
|
|
}
|
|
|
|
|
groups.leave(data.groupName, data.uid, next);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
2014-04-15 02:33:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Groups.update = function(socket, data, callback) {
|
2015-06-22 13:10:18 -04:00
|
|
|
if (!data) {
|
2014-04-15 02:33:31 -04:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-22 13:10:18 -04:00
|
|
|
groups.update(data.groupName, data.values, callback);
|
2014-04-15 02:33:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = Groups;
|