feat: #7743 categories

This commit is contained in:
Barış Soner Uşaklı
2019-07-16 00:41:42 -04:00
parent 45223cded6
commit fcf3e0770b
13 changed files with 714 additions and 1230 deletions

View File

@@ -10,162 +10,88 @@ var plugins = require('../plugins');
var cache = require('../cache');
module.exports = function (Categories) {
Categories.update = function (modified, callback) {
Categories.update = async function (modified) {
var cids = Object.keys(modified);
async.each(cids, function (cid, next) {
updateCategory(cid, modified[cid], next);
}, function (err) {
callback(err, cids);
});
await Promise.all(cids.map(cid => updateCategory(cid, modified[cid])));
return cids;
};
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', { cid: cid, category: modifiedFields }, next);
},
function (categoryData, next) {
category = categoryData.category;
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) {
updateCategoryField(cid, key, category[key], next);
}, next);
},
function (next) {
plugins.fireHook('action:category.update', { cid: cid, modified: category });
next();
},
], callback);
}
function updateCategoryField(cid, key, value, callback) {
if (key === 'parentCid') {
return updateParent(cid, value, callback);
} else if (key === 'tagWhitelist') {
return updateTagWhitelist(cid, value, callback);
async function updateCategory(cid, modifiedFields) {
const exists = await Categories.exists(cid);
if (!exists) {
return;
}
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) {
if (parseInt(cid, 10) === parseInt(newParent, 10)) {
return callback(new Error('[[error:cant-set-self-as-parent]]'));
if (modifiedFields.hasOwnProperty('name')) {
const translated = await translator.translate(modifiedFields.name);
modifiedFields.slug = cid + '/' + utils.slugify(translated);
}
async.waterfall([
function (next) {
Categories.getChildrenCids(cid, next);
},
function (childrenCids, next) {
if (childrenCids.includes(parseInt(newParent, 10))) {
return next(new Error('[[error:cant-set-child-as-parent]]'));
}
Categories.getCategoryField(cid, 'parentCid', next);
},
function (oldParent, next) {
async.series([
function (next) {
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);
},
function (next) {
cache.del(['cid:' + oldParent + ':children', 'cid:' + newParent + ':children']);
next();
},
], next);
},
], function (err) {
callback(err);
const result = await plugins.fireHook('filter:category.update', { cid: cid, category: modifiedFields });
const category = result.category;
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]);
}
await async.eachSeries(fields, async function (key) {
await updateCategoryField(cid, key, category[key]);
});
plugins.fireHook('action:category.update', { cid: cid, modified: category });
}
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((tag, index) => index);
db.sortedSetAdd('cid:' + cid + ':tag:whitelist', scores, tags, next);
},
function (next) {
cache.del('cid:' + cid + ':tag:whitelist');
next();
},
], callback);
async function updateCategoryField(cid, key, value) {
if (key === 'parentCid') {
return await updateParent(cid, value);
} else if (key === 'tagWhitelist') {
return await updateTagWhitelist(cid, value);
}
await db.setObjectField('category:' + cid, key, value);
if (key === 'order') {
await updateOrder(cid, value);
} else if (key === 'description') {
await Categories.parseDescription(cid, value);
}
}
function updateOrder(cid, order, callback) {
async.waterfall([
function (next) {
Categories.getCategoryField(cid, 'parentCid', next);
},
function (parentCid, next) {
db.sortedSetsAdd(['categories:cid', 'cid:' + parentCid + ':children'], order, cid, function (err) {
cache.del(['categories:cid', 'cid:' + parentCid + ':children']);
next(err);
});
},
], err => callback(err));
async function updateParent(cid, newParent) {
newParent = parseInt(newParent, 10) || 0;
if (parseInt(cid, 10) === newParent) {
throw new Error('[[error:cant-set-self-as-parent]]');
}
const childrenCids = await Categories.getChildrenCids(cid);
if (childrenCids.includes(newParent)) {
throw new Error('[[error:cant-set-child-as-parent]]');
}
const oldParent = await Categories.getCategoryField(cid, 'parentCid');
await Promise.all([
db.sortedSetRemove('cid:' + oldParent + ':children', cid),
db.sortedSetAdd('cid:' + newParent + ':children', cid, cid),
db.setObjectField('category:' + cid, 'parentCid', newParent),
]);
cache.del(['cid:' + oldParent + ':children', 'cid:' + newParent + ':children']);
}
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);
async function updateTagWhitelist(cid, tags) {
tags = tags.split(',').map(tag => utils.cleanUpTag(tag, meta.config.maximumTagLength))
.filter(Boolean);
await db.delete('cid:' + cid + ':tag:whitelist');
const scores = tags.map((tag, index) => index);
await db.sortedSetAdd('cid:' + cid + ':tag:whitelist', scores, tags);
cache.del('cid:' + cid + ':tag:whitelist');
}
async function updateOrder(cid, order) {
const parentCid = await Categories.getCategoryField(cid, 'parentCid');
await db.sortedSetsAdd(['categories:cid', 'cid:' + parentCid + ':children'], order, cid);
cache.del(['categories:cid', 'cid:' + parentCid + ':children']);
}
Categories.parseDescription = async function (cid, description) {
const parsedDescription = await plugins.fireHook('filter:parse.raw', description);
await Categories.setCategoryField(cid, 'descriptionParsed', parsedDescription);
};
};