mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-16 18:56:15 +01:00
categories update test
This commit is contained in:
@@ -21,24 +21,30 @@ module.exports = function (Categories) {
|
||||
};
|
||||
|
||||
function updateCategory(cid, modifiedFields, callback) {
|
||||
Categories.exists(cid, function (err, exists) {
|
||||
if (err || !exists) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
|
||||
if (modifiedFields.hasOwnProperty('name')) {
|
||||
translator.translate(modifiedFields.name, function (translated) {
|
||||
modifiedFields.slug = cid + '/' + utils.slugify(translated);
|
||||
});
|
||||
}
|
||||
|
||||
plugins.fireHook('filter:category.update', {category: modifiedFields}, function (err, categoryData) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
var category;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Categories.exists(cid, next);
|
||||
},
|
||||
function (exists, next) {
|
||||
if (!exists) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
var category = categoryData.category;
|
||||
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;
|
||||
var fields = Object.keys(category);
|
||||
// move parent to front, so its updated first
|
||||
var parentCidIndex = fields.indexOf('parentCid');
|
||||
@@ -48,15 +54,13 @@ module.exports = function (Categories) {
|
||||
|
||||
async.eachSeries(fields, function (key, next) {
|
||||
updateCategoryField(cid, key, category[key], next);
|
||||
}, function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
plugins.fireHook('action:category.update', {cid: cid, modified: category});
|
||||
callback();
|
||||
});
|
||||
});
|
||||
});
|
||||
}, next);
|
||||
},
|
||||
function (next) {
|
||||
plugins.fireHook('action:category.update', {cid: cid, modified: category});
|
||||
next();
|
||||
}
|
||||
], callback);
|
||||
}
|
||||
|
||||
function updateCategoryField(cid, key, value, callback) {
|
||||
@@ -64,73 +68,80 @@ module.exports = function (Categories) {
|
||||
return updateParent(cid, value, callback);
|
||||
}
|
||||
|
||||
db.setObjectField('category:' + cid, key, value, function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
if (key === 'order') {
|
||||
updateOrder(cid, value, callback);
|
||||
} else if (key === 'description') {
|
||||
Categories.parseDescription(cid, value, callback);
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
], callback);
|
||||
}
|
||||
|
||||
function updateParent(cid, newParent, callback) {
|
||||
if (parseInt(cid, 10) === parseInt(newParent, 10)) {
|
||||
return callback(new Error('[[error:cant-set-self-as-parent]]'));
|
||||
}
|
||||
Categories.getCategoryField(cid, 'parentCid', function (err, oldParent) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
], function (err) {
|
||||
callback(err);
|
||||
});
|
||||
], function (err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
function updateOrder(cid, order, callback) {
|
||||
Categories.getCategoryField(cid, 'parentCid', function (err, parentCid) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
], callback);
|
||||
], function (err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
Categories.parseDescription = function (cid, description, callback) {
|
||||
plugins.fireHook('filter:parse.raw', description, function (err, parsedDescription) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
plugins.fireHook('filter:parse.raw', description, next);
|
||||
},
|
||||
function (parsedDescription, next) {
|
||||
Categories.setCategoryField(cid, 'descriptionParsed', parsedDescription, next);
|
||||
}
|
||||
Categories.setCategoryField(cid, 'descriptionParsed', parsedDescription, callback);
|
||||
});
|
||||
], callback);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user