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.

168 lines
4.4 KiB
JavaScript

'use strict';
var async = require('async');
8 years ago
var db = require('../database');
8 years ago
var meta = require('../meta');
var utils = require('../../public/src/utils');
var translator = require('../../public/src/modules/translator');
var plugins = require('../plugins');
module.exports = function (Categories) {
Categories.update = function (modified, callback) {
10 years ago
var cids = Object.keys(modified);
async.each(cids, function (cid, next) {
10 years ago
updateCategory(cid, modified[cid], next);
}, function (err) {
10 years ago
callback(err, cids);
});
};
10 years ago
10 years ago
function updateCategory(cid, modifiedFields, callback) {
var category;
async.waterfall([
function (next) {
Categories.exists(cid, next);
},
function (exists, next) {
if (!exists) {
return callback();
}
if (modifiedFields.hasOwnProperty('name')) {
translator.translate(modifiedFields.name, function (translated) {
modifiedFields.slug = cid + '/' + utils.slugify(translated);
next();
});
} else {
next();
}
},
function (next) {
plugins.fireHook('filter:category.update', {category: modifiedFields}, next);
},
function (categoryData, next) {
category = categoryData.category;
10 years ago
var fields = Object.keys(category);
// move parent to front, so its updated first
var parentCidIndex = fields.indexOf('parentCid');
if (parentCidIndex !== -1 && fields.length > 1) {
fields.splice(0, 0, fields.splice(parentCidIndex, 1)[0]);
}
async.eachSeries(fields, function (key, next) {
10 years ago
updateCategoryField(cid, key, category[key], next);
}, next);
},
function (next) {
plugins.fireHook('action:category.update', {cid: cid, modified: category});
next();
},
], callback);
10 years ago
}
function updateCategoryField(cid, key, value, callback) {
if (key === 'parentCid') {
return updateParent(cid, value, callback);
8 years ago
} else if (key === 'tagWhitelist') {
return updateTagWhitelist(cid, value, callback);
}
async.waterfall([
function (next) {
db.setObjectField('category:' + cid, key, value, next);
},
function (next) {
if (key === 'order') {
updateOrder(cid, value, next);
} else if (key === 'description') {
Categories.parseDescription(cid, value, next);
} else {
next();
}
},
], callback);
}
function updateParent(cid, newParent, callback) {
10 years ago
if (parseInt(cid, 10) === parseInt(newParent, 10)) {
return callback(new Error('[[error:cant-set-self-as-parent]]'));
}
async.waterfall([
function (next) {
Categories.getCategoryField(cid, 'parentCid', next);
},
function (oldParent, next) {
async.series([
function (next) {
oldParent = parseInt(oldParent, 10) || 0;
db.sortedSetRemove('cid:' + oldParent + ':children', cid, next);
},
function (next) {
newParent = parseInt(newParent, 10) || 0;
db.sortedSetAdd('cid:' + newParent + ':children', cid, cid, next);
},
function (next) {
db.setObjectField('category:' + cid, 'parentCid', newParent, next);
},
], next);
},
], function (err) {
callback(err);
});
}
8 years ago
function updateTagWhitelist(cid, tags, callback) {
tags = tags.split(',');
tags = tags.map(function (tag) {
return utils.cleanUpTag(tag, meta.config.maximumTagLength);
}).filter(Boolean);
async.waterfall([
function (next) {
db.delete('cid:' + cid + ':tag:whitelist', next);
},
function (next) {
var scores = tags.map(function (tag, index) {
return index;
});
db.sortedSetAdd('cid:' + cid + ':tag:whitelist', scores, tags, next);
},
8 years ago
], callback);
}
function updateOrder(cid, order, callback) {
async.waterfall([
function (next) {
Categories.getCategoryField(cid, 'parentCid', next);
},
function (parentCid, next) {
async.parallel([
function (next) {
db.sortedSetAdd('categories:cid', order, cid, next);
},
function (next) {
parentCid = parseInt(parentCid, 10) || 0;
db.sortedSetAdd('cid:' + parentCid + ':children', order, cid, next);
},
], next);
},
], function (err) {
callback(err);
});
}
Categories.parseDescription = function (cid, description, callback) {
async.waterfall([
function (next) {
plugins.fireHook('filter:parse.raw', description, next);
},
function (parsedDescription, next) {
Categories.setCategoryField(cid, 'descriptionParsed', parsedDescription, next);
},
], callback);
9 years ago
};
};