mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-16 10:46:14 +01:00
refactor
This commit is contained in:
@@ -8,32 +8,19 @@ var db = require('../database');
|
||||
|
||||
module.exports = function (Categories) {
|
||||
Categories.getCategoryData = function (cid, callback) {
|
||||
db.getObject('category:' + cid, function (err, category) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
modifyCategory(category);
|
||||
callback(null, category);
|
||||
});
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getObject('category:' + cid, next);
|
||||
},
|
||||
function (category, next) {
|
||||
modifyCategory(category);
|
||||
next(null, category);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Categories.getCategoriesData = function (cids, callback) {
|
||||
if (!Array.isArray(cids) || !cids.length) {
|
||||
return callback(null, []);
|
||||
}
|
||||
var keys = cids.map(function (cid) {
|
||||
return 'category:' + cid;
|
||||
});
|
||||
|
||||
db.getObjects(keys, function (err, categories) {
|
||||
if (err || !Array.isArray(categories) || !categories.length) {
|
||||
return callback(err, []);
|
||||
}
|
||||
|
||||
categories.forEach(modifyCategory);
|
||||
callback(null, categories);
|
||||
});
|
||||
Categories.getCategoriesFields(cids, [], callback);
|
||||
};
|
||||
|
||||
function modifyCategory(category) {
|
||||
@@ -76,15 +63,19 @@ module.exports = function (Categories) {
|
||||
var keys = cids.map(function (cid) {
|
||||
return 'category:' + cid;
|
||||
});
|
||||
|
||||
db.getObjectsFields(keys, fields, function (err, categories) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
categories.forEach(modifyCategory);
|
||||
callback(null, categories);
|
||||
});
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (fields.length) {
|
||||
db.getObjectsFields(keys, fields, next);
|
||||
} else {
|
||||
db.getObjects(keys, next);
|
||||
}
|
||||
},
|
||||
function (categories, next) {
|
||||
categories.forEach(modifyCategory);
|
||||
next(null, categories);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Categories.getMultipleCategoryFields = function (cids, fields, callback) {
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var winston = require('winston');
|
||||
var _ = require('underscore');
|
||||
|
||||
var db = require('../database');
|
||||
@@ -31,36 +30,33 @@ module.exports = function (Categories) {
|
||||
};
|
||||
|
||||
Categories.updateRecentTid = function (cid, tid, callback) {
|
||||
async.parallel({
|
||||
count: function (next) {
|
||||
db.sortedSetCard('cid:' + cid + ':recent_tids', next);
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
count: function (next) {
|
||||
db.sortedSetCard('cid:' + cid + ':recent_tids', next);
|
||||
},
|
||||
numRecentReplies: function (next) {
|
||||
db.getObjectField('category:' + cid, 'numRecentReplies', next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
numRecentReplies: function (next) {
|
||||
db.getObjectField('category:' + cid, 'numRecentReplies', next);
|
||||
function (results, next) {
|
||||
if (results.count < results.numRecentReplies) {
|
||||
return db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, callback);
|
||||
}
|
||||
db.getSortedSetRangeWithScores('cid:' + cid + ':recent_tids', 0, results.count - results.numRecentReplies, next);
|
||||
},
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (results.count < results.numRecentReplies) {
|
||||
return db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, callback);
|
||||
}
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getSortedSetRangeWithScores('cid:' + cid + ':recent_tids', 0, results.count - results.numRecentReplies, next);
|
||||
},
|
||||
function (data, next) {
|
||||
if (!data.length) {
|
||||
return next();
|
||||
}
|
||||
db.sortedSetsRemoveRangeByScore(['cid:' + cid + ':recent_tids'], '-inf', data[data.length - 1].score, next);
|
||||
},
|
||||
function (next) {
|
||||
db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, next);
|
||||
},
|
||||
], callback);
|
||||
});
|
||||
function (data, next) {
|
||||
if (!data.length) {
|
||||
return next();
|
||||
}
|
||||
db.sortedSetsRemoveRangeByScore(['cid:' + cid + ':recent_tids'], '-inf', data[data.length - 1].score, next);
|
||||
},
|
||||
function (next) {
|
||||
db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Categories.getRecentTopicReplies = function (categoryData, uid, callback) {
|
||||
@@ -185,65 +181,60 @@ module.exports = function (Categories) {
|
||||
Categories.moveRecentReplies = function (tid, oldCid, cid, callback) {
|
||||
callback = callback || function () {};
|
||||
updatePostCount(tid, oldCid, cid);
|
||||
topics.getPids(tid, function (err, pids) {
|
||||
if (err) {
|
||||
return winston.error(err.message);
|
||||
}
|
||||
|
||||
if (!Array.isArray(pids) || !pids.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
batch.processArray(pids, function (pids, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts.getPostsFields(pids, ['timestamp'], next);
|
||||
},
|
||||
function (postData, next) {
|
||||
var timestamps = postData.map(function (post) {
|
||||
return post && post.timestamp;
|
||||
});
|
||||
|
||||
async.parallel([
|
||||
function (next) {
|
||||
db.sortedSetRemove('cid:' + oldCid + ':pids', pids, next);
|
||||
},
|
||||
function (next) {
|
||||
db.sortedSetAdd('cid:' + cid + ':pids', timestamps, pids, next);
|
||||
},
|
||||
], next);
|
||||
},
|
||||
], next);
|
||||
}, function (err) {
|
||||
if (err) {
|
||||
winston.error(err.stack);
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
topics.getPids(tid, next);
|
||||
},
|
||||
function (pids, next) {
|
||||
if (!Array.isArray(pids) || !pids.length) {
|
||||
return callback();
|
||||
}
|
||||
callback(err);
|
||||
});
|
||||
});
|
||||
|
||||
batch.processArray(pids, function (pids, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts.getPostsFields(pids, ['timestamp'], next);
|
||||
},
|
||||
function (postData, next) {
|
||||
var timestamps = postData.map(function (post) {
|
||||
return post && post.timestamp;
|
||||
});
|
||||
|
||||
async.parallel([
|
||||
function (next) {
|
||||
db.sortedSetRemove('cid:' + oldCid + ':pids', pids, next);
|
||||
},
|
||||
function (next) {
|
||||
db.sortedSetAdd('cid:' + cid + ':pids', timestamps, pids, next);
|
||||
},
|
||||
], next);
|
||||
},
|
||||
], next);
|
||||
}, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
function updatePostCount(tid, oldCid, newCid) {
|
||||
topics.getTopicField(tid, 'postcount', function (err, postCount) {
|
||||
if (err) {
|
||||
return winston.error(err.message);
|
||||
}
|
||||
if (!parseInt(postCount, 10)) {
|
||||
return;
|
||||
}
|
||||
async.parallel([
|
||||
function (next) {
|
||||
db.incrObjectFieldBy('category:' + oldCid, 'post_count', -postCount, next);
|
||||
},
|
||||
function (next) {
|
||||
db.incrObjectFieldBy('category:' + newCid, 'post_count', postCount, next);
|
||||
},
|
||||
], function (err) {
|
||||
if (err) {
|
||||
winston.error(err.message);
|
||||
function updatePostCount(tid, oldCid, newCid, callback) {
|
||||
callback = callback || function () {};
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
topics.getTopicField(tid, 'postcount', next);
|
||||
},
|
||||
function (postCount, next) {
|
||||
if (!parseInt(postCount, 10)) {
|
||||
return callback();
|
||||
}
|
||||
});
|
||||
});
|
||||
async.parallel([
|
||||
function (next) {
|
||||
db.incrObjectFieldBy('category:' + oldCid, 'post_count', -postCount, next);
|
||||
},
|
||||
function (next) {
|
||||
db.incrObjectFieldBy('category:' + newCid, 'post_count', postCount, next);
|
||||
},
|
||||
], next);
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
|
||||
var db = require('../database');
|
||||
|
||||
module.exports = function (Categories) {
|
||||
@@ -12,21 +14,22 @@ module.exports = function (Categories) {
|
||||
return 'cid:' + cid + ':read_by_uid';
|
||||
});
|
||||
|
||||
db.isMemberOfSets(keys, uid, function (err, hasRead) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.isMemberOfSets(keys, uid, next);
|
||||
},
|
||||
function (hasRead, next) {
|
||||
keys = keys.filter(function (key, index) {
|
||||
return !hasRead[index];
|
||||
});
|
||||
|
||||
keys = keys.filter(function (key, index) {
|
||||
return !hasRead[index];
|
||||
});
|
||||
if (!keys.length) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
if (!keys.length) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
db.setsAdd(keys, uid, callback);
|
||||
});
|
||||
db.setsAdd(keys, uid, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Categories.markAsUnreadForAll = function (cid, callback) {
|
||||
@@ -38,11 +41,9 @@ module.exports = function (Categories) {
|
||||
};
|
||||
|
||||
Categories.hasReadCategories = function (cids, uid, callback) {
|
||||
var sets = [];
|
||||
|
||||
for (var i = 0, ii = cids.length; i < ii; i += 1) {
|
||||
sets.push('cid:' + cids[i] + ':read_by_uid');
|
||||
}
|
||||
var sets = cids.map(function (cid) {
|
||||
return 'cid:' + cid + ':read_by_uid';
|
||||
});
|
||||
|
||||
db.isMemberOfSets(sets, uid, callback);
|
||||
};
|
||||
|
||||
@@ -15,31 +15,21 @@ module.exports = function (middleware) {
|
||||
middleware.admin = {};
|
||||
middleware.admin.isAdmin = function (req, res, next) {
|
||||
winston.warn('[middleware.admin.isAdmin] deprecation warning, no need to use this from plugins!');
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
user.isAdministrator(req.uid, next);
|
||||
},
|
||||
function (isAdmin, next) {
|
||||
if (!isAdmin) {
|
||||
return controllers.helpers.notAllowed(req, res);
|
||||
}
|
||||
next();
|
||||
},
|
||||
], next);
|
||||
middleware.isAdmin(req, res, next);
|
||||
};
|
||||
|
||||
middleware.admin.buildHeader = function (req, res, next) {
|
||||
res.locals.renderAdminHeader = true;
|
||||
|
||||
controllers.api.getConfig(req, res, function (err, config) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
res.locals.config = config;
|
||||
next();
|
||||
});
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
controllers.api.getConfig(req, res, next);
|
||||
},
|
||||
function (config, next) {
|
||||
res.locals.config = config;
|
||||
next();
|
||||
},
|
||||
], next);
|
||||
};
|
||||
|
||||
middleware.admin.renderHeader = function (req, res, data, next) {
|
||||
@@ -48,41 +38,31 @@ module.exports = function (middleware) {
|
||||
authentication: [],
|
||||
};
|
||||
|
||||
user.getUserFields(req.uid, ['username', 'userslug', 'email', 'picture', 'email:confirmed'], function (err, userData) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
userData: function (next) {
|
||||
user.getUserFields(req.uid, ['username', 'userslug', 'email', 'picture', 'email:confirmed'], next);
|
||||
},
|
||||
scripts: function (next) {
|
||||
getAdminScripts(next);
|
||||
},
|
||||
custom_header: function (next) {
|
||||
plugins.fireHook('filter:admin.header.build', custom_header, next);
|
||||
},
|
||||
config: function (next) {
|
||||
controllers.api.getConfig(req, res, next);
|
||||
},
|
||||
configs: function (next) {
|
||||
meta.configs.list(next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
var userData = results.userData;
|
||||
userData.uid = req.uid;
|
||||
userData['email:confirmed'] = parseInt(userData['email:confirmed'], 10) === 1;
|
||||
|
||||
userData.uid = req.uid;
|
||||
userData['email:confirmed'] = parseInt(userData['email:confirmed'], 10) === 1;
|
||||
|
||||
async.parallel({
|
||||
scripts: function (next) {
|
||||
plugins.fireHook('filter:admin.scripts.get', [], function (err, scripts) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
var arr = [];
|
||||
scripts.forEach(function (script) {
|
||||
arr.push({ src: script });
|
||||
});
|
||||
|
||||
next(null, arr);
|
||||
});
|
||||
},
|
||||
custom_header: function (next) {
|
||||
plugins.fireHook('filter:admin.header.build', custom_header, next);
|
||||
},
|
||||
config: function (next) {
|
||||
controllers.api.getConfig(req, res, next);
|
||||
},
|
||||
configs: function (next) {
|
||||
meta.configs.list(next);
|
||||
},
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.locals.config = results.config;
|
||||
|
||||
var acpPath = req.path.slice(1).split('/');
|
||||
@@ -111,10 +91,22 @@ module.exports = function (middleware) {
|
||||
templateValues.template[res.locals.template] = true;
|
||||
|
||||
req.app.render('admin/header', templateValues, next);
|
||||
});
|
||||
});
|
||||
},
|
||||
], next);
|
||||
};
|
||||
|
||||
function getAdminScripts(callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
plugins.fireHook('filter:admin.scripts.get', [], next);
|
||||
},
|
||||
function (scripts, next) {
|
||||
next(null, scripts.map(function (script) {
|
||||
return { src: script };
|
||||
}));
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
middleware.admin.renderFooter = function (req, res, data, next) {
|
||||
req.app.render('admin/footer', data, next);
|
||||
|
||||
Reference in New Issue
Block a user