You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/socket.io/admin/categories.js

140 lines
3.6 KiB
JavaScript

8 years ago
'use strict';
9 years ago
var async = require('async');
9 years ago
var db = require('../../database');
var groups = require('../../groups');
var categories = require('../../categories');
var privileges = require('../../privileges');
var plugins = require('../../plugins');
8 years ago
var events = require('../../events');
var Categories = module.exports;
Categories.create = function (socket, data, callback) {
9 years ago
if (!data) {
return callback(new Error('[[error:invalid-data]]'));
}
categories.create(data, callback);
};
Categories.getAll = function (socket, data, callback) {
10 years ago
async.waterfall([
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
10 years ago
async.apply(categories.getCategoriesData),
function (categories, next) {
// Hook changes, there is no req, and res
plugins.fireHook('filter:admin.categories.get', { categories: categories }, next);
10 years ago
},
function (result, next) {
10 years ago
next(null, categories.getTree(result.categories, 0));
},
8 years ago
], callback);
10 years ago
};
Categories.getNames = function (socket, data, callback) {
10 years ago
categories.getAllCategoryFields(['cid', 'name'], callback);
};
Categories.purge = function (socket, cid, callback) {
8 years ago
var name;
async.waterfall([
function (next) {
categories.getCategoryField(cid, 'name', next);
},
function (_name, next) {
name = _name;
categories.purge(cid, socket.uid, next);
},
function (next) {
events.log({
type: 'category-purge',
uid: socket.uid,
ip: socket.ip,
cid: cid,
name: name,
});
setImmediate(next);
},
], callback);
};
Categories.update = function (socket, data, callback) {
9 years ago
if (!data) {
return callback(new Error('[[error:invalid-data]]'));
}
categories.update(data, callback);
};
Categories.setPrivilege = function (socket, data, callback) {
if (!data) {
return callback(new Error('[[error:invalid-data]]'));
}
if (Array.isArray(data.privilege)) {
async.each(data.privilege, function (privilege, next) {
groups[data.set ? 'join' : 'leave']('cid:' + data.cid + ':privileges:' + privilege, data.member, next);
7 years ago
}, onSetComplete);
} else {
7 years ago
groups[data.set ? 'join' : 'leave']('cid:' + data.cid + ':privileges:' + data.privilege, data.member, onSetComplete);
}
function onSetComplete() {
events.log({
uid: socket.uid,
type: 'privilege-change',
7 years ago
ip: socket.ip,
privilege: data.privilege.toString(),
cid: data.cid,
7 years ago
action: data.set ? 'grant' : 'rescind',
target: data.member,
7 years ago
}, callback);
}
};
Categories.getPrivilegeSettings = function (socket, cid, callback) {
7 years ago
if (!parseInt(cid, 10)) {
privileges.global.list(callback);
} else {
privileges.categories.list(cid, callback);
}
};
Categories.copyPrivilegesToChildren = function (socket, cid, callback) {
8 years ago
async.waterfall([
function (next) {
categories.getChildren([cid], socket.uid, next);
8 years ago
},
function (children, next) {
children = children[0];
8 years ago
async.eachSeries(children, function (child, next) {
8 years ago
copyPrivilegesToChildrenRecursive(cid, child, next);
}, next);
},
], callback);
};
9 years ago
function copyPrivilegesToChildrenRecursive(parentCid, category, callback) {
8 years ago
async.waterfall([
function (next) {
categories.copyPrivilegesFrom(parentCid, category.cid, next);
},
function (next) {
async.eachSeries(category.children, function (child, next) {
copyPrivilegesToChildrenRecursive(parentCid, child, next);
}, next);
},
], callback);
}
Categories.copySettingsFrom = function (socket, data, callback) {
9 years ago
categories.copySettingsFrom(data.fromCid, data.toCid, true, callback);
9 years ago
};
Categories.copyPrivilegesFrom = function (socket, data, callback) {
9 years ago
categories.copyPrivilegesFrom(data.fromCid, data.toCid, callback);
};