Files
NodeBB/src/categories/update.js

168 lines
4.3 KiB
JavaScript
Raw Normal View History

2014-02-28 15:25:50 -05:00
'use strict';
2016-08-26 19:33:16 +03:00
var async = require('async');
2016-12-09 18:53:08 +03:00
2016-08-26 19:33:16 +03:00
var db = require('../database');
2016-12-09 18:53:08 +03:00
var meta = require('../meta');
var utils = require('../utils');
var translator = require('../translator');
2016-08-26 19:33:16 +03:00
var plugins = require('../plugins');
2013-05-24 11:18:41 -04:00
module.exports = function (Categories) {
Categories.update = function (modified, callback) {
2015-09-28 16:09:33 -04:00
var cids = Object.keys(modified);
async.each(cids, function (cid, next) {
2015-09-28 16:09:33 -04:00
updateCategory(cid, modified[cid], next);
}, function (err) {
2015-09-28 16:09:33 -04:00
callback(err, cids);
});
};
2015-01-18 14:31:37 -05:00
2015-09-28 16:09:33 -04:00
function updateCategory(cid, modifiedFields, callback) {
2016-11-23 13:38:20 +03:00
var category;
async.waterfall([
function (next) {
Categories.exists(cid, next);
},
function (exists, next) {
if (!exists) {
return callback();
2015-07-05 08:31:21 +03:00
}
2016-11-23 13:38:20 +03:00
if (modifiedFields.hasOwnProperty('name')) {
translator.translate(modifiedFields.name, function (translated) {
modifiedFields.slug = cid + '/' + utils.slugify(translated);
next();
});
} else {
next();
}
},
function (next) {
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:category.update', { category: modifiedFields }, next);
2016-11-23 13:38:20 +03:00
},
function (categoryData, next) {
category = categoryData.category;
2015-09-28 16:09:33 -04:00
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) {
2015-09-28 16:09:33 -04:00
updateCategoryField(cid, key, category[key], next);
2016-11-23 13:38:20 +03:00
}, next);
},
function (next) {
2017-02-18 12:30:49 -07:00
plugins.fireHook('action:category.update', { cid: cid, modified: category });
2016-11-23 13:38:20 +03:00
next();
2017-02-17 19:31:21 -07:00
},
2016-11-23 13:38:20 +03:00
], callback);
2015-09-28 16:09:33 -04:00
}
2013-05-24 11:18:41 -04:00
function updateCategoryField(cid, key, value, callback) {
if (key === 'parentCid') {
return updateParent(cid, value, callback);
2016-12-09 18:53:08 +03:00
} else if (key === 'tagWhitelist') {
return updateTagWhitelist(cid, value, callback);
}
2016-11-23 13:38:20 +03:00
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();
}
2017-02-17 19:31:21 -07:00
},
2016-11-23 13:38:20 +03:00
], callback);
}
function updateParent(cid, newParent, callback) {
2015-09-15 12:58:19 -04:00
if (parseInt(cid, 10) === parseInt(newParent, 10)) {
return callback(new Error('[[error:cant-set-self-as-parent]]'));
}
2016-11-23 13:38:20 +03:00
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);
2017-02-17 19:31:21 -07:00
},
2016-11-23 13:38:20 +03:00
], next);
2017-02-17 19:31:21 -07:00
},
2016-11-23 13:38:20 +03:00
], function (err) {
callback(err);
});
}
2016-12-09 18:53:08 +03:00
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);
2017-02-17 19:31:21 -07:00
},
2016-12-09 18:53:08 +03:00
], callback);
}
function updateOrder(cid, order, callback) {
2016-11-23 13:38:20 +03:00
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);
2017-02-17 19:31:21 -07:00
},
2016-11-23 13:38:20 +03:00
], next);
2017-02-17 19:31:21 -07:00
},
2016-11-23 13:38:20 +03:00
], function (err) {
callback(err);
});
}
Categories.parseDescription = function (cid, description, callback) {
2016-11-23 13:38:20 +03:00
async.waterfall([
function (next) {
plugins.fireHook('filter:parse.raw', description, next);
},
function (parsedDescription, next) {
Categories.setCategoryField(cid, 'descriptionParsed', parsedDescription, next);
2017-02-17 19:31:21 -07:00
},
2016-11-23 13:38:20 +03:00
], callback);
2016-03-15 15:38:16 +02:00
};
2014-04-10 20:31:57 +01:00
};