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