From 8a225ad719fef6cda4ef1bcbf0dfdaaf309fa00d Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 26 May 2015 12:37:31 -0400 Subject: [PATCH] starting groups organization --- src/groups/create.js | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/groups/create.js diff --git a/src/groups/create.js b/src/groups/create.js new file mode 100644 index 0000000000..efabac3fc5 --- /dev/null +++ b/src/groups/create.js @@ -0,0 +1,68 @@ +'use strict'; + +var async = require('async'), + meta = require('../meta'), + plugins = require('../plugins'), + utils = require('../../public/src/utils'), + db = require('./../database'); + +module.exports = function(Groups) { + Groups.create = function(data, callback) { + if (data.name.length === 0) { + return callback(new Error('[[error:group-name-too-short]]')); + } + + if (data.name === 'administrators' || data.name === 'registered-users' || Groups.internals.isPrivilegeGroup.test(data.name)) { + var system = true; + } + + meta.userOrGroupExists(data.name, function (err, exists) { + if (err) { + return callback(err); + } + + if (exists) { + return callback(new Error('[[error:group-already-exists]]')); + } + var timestamp = data.timestamp || Date.now(); + + var slug = utils.slugify(data.name), + groupData = { + name: data.name, + slug: slug, + createtime: timestamp, + userTitle: data.name, + description: data.description || '', + memberCount: 0, + deleted: '0', + hidden: data.hidden || '0', + system: system ? '1' : '0', + private: data.private || '1' + }, + tasks = [ + async.apply(db.sortedSetAdd, 'groups:createtime', timestamp, data.name), + async.apply(db.setObject, 'group:' + data.name, groupData) + ]; + + if (data.hasOwnProperty('ownerUid')) { + tasks.push(async.apply(db.setAdd, 'group:' + data.name + ':owners', data.ownerUid)); + tasks.push(async.apply(db.sortedSetAdd, 'group:' + data.name + ':members', timestamp, data.ownerUid)); + tasks.push(async.apply(db.setObjectField, 'group:' + data.name, 'memberCount', 1)); + + groupData.ownerUid = data.ownerUid; + } + + if (!data.hidden) { + tasks.push(async.apply(db.setObjectField, 'groupslug:groupname', slug, data.name)); + } + + async.series(tasks, function(err) { + if (!err) { + plugins.fireHook('action:group.create', groupData); + } + + callback(err, groupData); + }); + }); + }; +};