Files
NodeBB/src/categories/create.js

173 lines
5.7 KiB
JavaScript
Raw Normal View History

2014-11-08 23:54:21 -05:00
'use strict';
2016-04-01 15:02:17 +03:00
var async = require('async');
var db = require('../database');
var groups = require('../groups');
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
2014-11-08 23:54:21 -05:00
module.exports = function(Categories) {
Categories.create = function(data, callback) {
2015-05-11 16:13:09 -04:00
var category;
var parentCid = data.parentCid ? data.parentCid : 0;
2015-05-11 16:13:09 -04:00
async.waterfall([
function(next) {
db.incrObjectField('global', 'nextCid', next);
},
function(cid, next) {
2016-04-01 15:02:17 +03:00
data.name = data.name || 'Category ' + cid;
2016-03-15 15:38:16 +02:00
var slug = cid + '/' + utils.slugify(data.name);
var order = data.order || cid; // If no order provided, place it at the end
var colours = Categories.assignColours();
2015-05-11 16:13:09 -04:00
category = {
cid: cid,
name: data.name,
2016-03-15 15:38:16 +02:00
description: data.description ? data.description : '',
2016-08-07 18:45:54 +03:00
descriptionParsed: data.descriptionParsed ? data.descriptionParsed : '',
2016-03-15 15:38:16 +02:00
icon: data.icon ? data.icon : '',
2015-05-11 16:13:09 -04:00
bgColor: data.bgColor || colours[0],
color: data.color || colours[1],
slug: slug,
parentCid: parentCid,
2015-05-11 16:13:09 -04:00
topic_count: 0,
post_count: 0,
disabled: 0,
order: order,
link: '',
numRecentReplies: 1,
class: ( data.class ? data.class : 'col-md-3 col-xs-6' ),
2015-09-10 16:35:37 -04:00
imageClass: 'cover'
2015-05-11 16:13:09 -04:00
};
plugins.fireHook('filter:category.create', {category: category, data: data}, next);
2015-06-19 15:11:22 -04:00
},
function(data, next) {
category = data.category;
var defaultPrivileges = ['find', 'read', 'topics:read', 'topics:create', 'topics:reply', 'posts:edit', 'posts:delete', 'topics:delete', 'upload:post:image'];
2016-08-26 19:13:05 +03:00
var privileges = require('../privileges');
2015-05-11 16:13:09 -04:00
async.series([
2015-06-19 15:11:22 -04:00
async.apply(db.setObject, 'category:' + category.cid, category),
2016-08-07 18:45:54 +03:00
function (next) {
if (category.descriptionParsed) {
return next();
}
Categories.parseDescription(category.cid, category.description, next);
},
2015-06-19 15:11:22 -04:00
async.apply(db.sortedSetAdd, 'categories:cid', category.order, category.cid),
async.apply(db.sortedSetAdd, 'cid:' + parentCid + ':children', category.order, category.cid),
2015-06-19 15:11:22 -04:00
async.apply(privileges.categories.give, defaultPrivileges, category.cid, 'administrators'),
async.apply(privileges.categories.give, defaultPrivileges, category.cid, 'registered-users'),
async.apply(privileges.categories.give, ['find', 'read', 'topics:read'], category.cid, 'guests')
2015-05-11 16:13:09 -04:00
], next);
},
function(results, next) {
2016-04-01 15:02:17 +03:00
if (data.cloneFromCid && parseInt(data.cloneFromCid, 10)) {
2016-05-29 22:27:37 +03:00
return Categories.copySettingsFrom(data.cloneFromCid, category.cid, !data.parentCid, next);
2016-04-01 15:02:17 +03:00
}
next(null, category);
},
function(category, next) {
2015-06-19 15:39:33 -04:00
plugins.fireHook('action:category.create', category);
2015-05-11 16:13:09 -04:00
next(null, category);
2014-11-08 23:54:21 -05:00
}
2015-05-11 16:13:09 -04:00
], callback);
2014-11-08 23:54:21 -05:00
};
Categories.assignColours = function() {
2016-04-01 15:02:17 +03:00
var backgrounds = ['#AB4642', '#DC9656', '#F7CA88', '#A1B56C', '#86C1B9', '#7CAFC2', '#BA8BAF', '#A16946'];
var text = ['#fff', '#fff', '#333', '#fff', '#333', '#fff', '#fff', '#fff'];
var index = Math.floor(Math.random() * backgrounds.length);
return [backgrounds[index], text[index]];
};
2016-04-01 15:02:17 +03:00
2016-05-29 22:27:37 +03:00
Categories.copySettingsFrom = function(fromCid, toCid, copyParent, callback) {
2016-04-01 15:02:17 +03:00
var destination;
async.waterfall([
function (next) {
async.parallel({
source: async.apply(db.getObject, 'category:' + fromCid),
destination: async.apply(db.getObject, 'category:' + toCid)
}, next);
},
function (results, next) {
if (!results.source) {
return next(new Error('[[error:invalid-cid]]'));
}
destination = results.destination;
var tasks = [];
2016-06-01 11:48:25 +03:00
if (copyParent && utils.isNumber(destination.parentCid)) {
2016-04-01 15:02:17 +03:00
tasks.push(async.apply(db.sortedSetRemove, 'cid:' + destination.parentCid + ':children', toCid));
}
2016-06-01 11:48:25 +03:00
if (copyParent && utils.isNumber(results.source.parentCid)) {
tasks.push(async.apply(db.sortedSetAdd, 'cid:' + results.source.parentCid + ':children', results.source.order, toCid));
}
2016-06-01 11:48:25 +03:00
2016-04-01 15:02:17 +03:00
destination.description = results.source.description;
destination.descriptionParsed = results.source.descriptionParsed;
destination.icon = results.source.icon;
destination.bgColor = results.source.bgColor;
destination.color = results.source.color;
destination.link = results.source.link;
destination.numRecentReplies = results.source.numRecentReplies;
destination.class = results.source.class;
destination.imageClass = results.source.imageClass;
2016-05-29 22:27:37 +03:00
if (copyParent) {
destination.parentCid = results.source.parentCid || 0;
2016-05-29 22:27:37 +03:00
}
2016-04-01 15:02:17 +03:00
tasks.push(async.apply(db.setObject, 'category:' + toCid, destination));
async.series(tasks, next);
},
function (results, next) {
Categories.copyPrivilegesFrom(fromCid, toCid, next);
}
], function(err) {
callback(err, destination);
});
};
Categories.copyPrivilegesFrom = function(fromCid, toCid, callback) {
2016-08-26 19:13:05 +03:00
var privileges = require('../privileges');
async.each(privileges.privilegeList, function(privilege, next) {
2016-04-01 15:02:17 +03:00
copyPrivilege(privilege, fromCid, toCid, next);
}, callback);
};
function copyPrivilege(privilege, fromCid, toCid, callback) {
async.waterfall([
function (next) {
db.getSortedSetRange('group:cid:' + toCid + ':privileges:' + privilege + ':members', 0, -1, next);
},
function (currentMembers, next) {
async.eachSeries(currentMembers, function(member, next) {
groups.leave('cid:' + toCid + ':privileges:' + privilege, member, next);
}, next);
},
function (next) {
db.getSortedSetRange('group:cid:' + fromCid + ':privileges:' + privilege + ':members', 0, -1, next);
},
function (members, next) {
if (!members || !members.length) {
return callback();
}
async.eachSeries(members, function(member, next) {
groups.join('cid:' + toCid + ':privileges:' + privilege, member, next);
}, next);
}
], callback);
}
2014-11-08 23:54:21 -05:00
};