From e93b2b6aa1241b7e806dad69169e2010dd857008 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 8 Nov 2014 16:15:24 -0500 Subject: [PATCH 01/16] removed action:app.load --- src/plugins.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/plugins.js b/src/plugins.js index f9f275c0a7..df8f02f6d6 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -129,9 +129,6 @@ var fs = require('fs'), app.render.apply(app, arguments); }; - // Deprecated as of v0.5.0, remove this hook call for NodeBB v0.6.0-1 - Plugins.fireHook('action:app.load', router, middleware, controllers); - Plugins.fireHook('static:app.load', {app: app, router: router, middleware: middleware, controllers: controllers}, function() { hotswap.replace('plugins', router); winston.info('[plugins] All plugins reloaded and rerouted'); From 3f12d363f769588322776ce0ba801777f81a89e5 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 8 Nov 2014 17:12:38 -0500 Subject: [PATCH 02/16] plugins fireHook refactor (drunk) --- src/plugins.js | 167 ++++++++++++++++++------------------------------- 1 file changed, 62 insertions(+), 105 deletions(-) diff --git a/src/plugins.js b/src/plugins.js index df8f02f6d6..bdcdb5f387 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -388,120 +388,77 @@ var fs = require('fs'), return !!(Plugins.loadedHooks[hook] && Plugins.loadedHooks[hook].length > 0); }; - Plugins.fireHook = function(hook) { - var callback = typeof arguments[arguments.length-1] === 'function' ? arguments[arguments.length-1] : null, - args = arguments.length ? Array.prototype.slice.call(arguments, 1) : []; - - if (callback) { - args.pop(); - } + Plugins.fireHook = function(hook, params, callback) { + callback = typeof callback === 'function' ? callback : function() {}; var hookList = Plugins.loadedHooks[hook]; - if (Array.isArray(hookList)) { - // if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\''); - var hookType = hook.split(':')[0]; - switch (hookType) { - case 'filter': - async.reduce(hookList, args, function(value, hookObj, next) { - if (hookObj.method) { - if (!hookObj.hasOwnProperty('callbacked') || hookObj.callbacked === true) { - // omg, after 6 months I finally realised what this does... - // It adds the callback to the arguments passed-in, since the callback - // is defined in *this* file (the async cb), and not the hooks themselves. - value = hookObj.method.apply(Plugins, value.concat(function() { - next(arguments[0], Array.prototype.slice.call(arguments, 1)); - })); - - /* - Backwards compatibility block for v0.5.0 - Remove this once NodeBB enters v0.5.0-1 - */ - if (value !== undefined && value !== callback) { - winston.warn('[plugins/' + hookObj.id + '] "callbacked" deprecated as of 0.4x. Use asynchronous method instead for hook: ' + hook); - next(null, [value]); - } - } else { - winston.warn('[plugins/' + hookObj.id + '] "callbacked" deprecated as of 0.4x. Use asynchronous method instead for hook: ' + hook); - value = hookObj.method.apply(Plugins, value); - next(null, [value]); - } - /* End backwards compatibility block */ - } else { - if (global.env === 'development') { - winston.info('[plugins] Expected method for hook \'' + hook + '\' in plugin \'' + hookObj.id + '\' not found, skipping.'); - } - next(null, [value]); - } - }, function(err, values) { - if (err) { - if (global.env === 'development') { - winston.info('[plugins] Problem executing hook: ' + hook + ' err: ' + JSON.stringify(err)); - } - } + if (!Array.isArray(hookList) || !hookList.length) { + return callback(null, params); + } - if (callback) { - callback.apply(Plugins, [err].concat(values)); - } - }); - break; - case 'action': - var deprecationWarn = []; - async.each(hookList, function(hookObj, next) { - /* - Backwards compatibility block for v0.5.0 - Remove this once NodeBB enters v0.6.0-1 - */ - if (hook === 'action:app.load') { - deprecationWarn.push(hookObj.id); - } - /* End backwards compatibility block */ + // if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\''); + var hookType = hook.split(':')[0]; + switch (hookType) { + case 'filter': + fireFilterHook(hook, hookList, params, callback); + break; + case 'action': + fireActionHook(hook, hookList, params, callback); + break; + case 'static': + fireStaticHook(hook, hookList, params, callback); + break; + default: + winston.warn('[plugins] Unknown hookType: ' + hookType + ', hook : ' + hook); + break; + } + }; - if (hookObj.method) { - hookObj.method.apply(Plugins, args); - } else { - if (global.env === 'development') { - winston.info('[plugins] Expected method \'' + hookObj.method + '\' in plugin \'' + hookObj.id + '\' not found, skipping.'); - } - } + function fireFilterHook(hook, hookList, params, callback) { + async.reduce(hookList, params, function(params, hookObj, next) { + if (typeof hookObj.method !== 'function') { + if (global.env === 'development') { + winston.warn('[plugins] Expected method for hook \'' + hook + '\' in plugin \'' + hookObj.id + '\' not found, skipping.'); + } + return next(null, params); + } - next(); - }, function() { - /* - Backwards compatibility block for v0.5.0 - Remove this once NodeBB enters v0.6.0-1 - */ - if (deprecationWarn.length) { - winston.warn('[plugins] The `action:app.load` hook is deprecated in favour of `static:app.load`, please notify the developers of the following plugins:'); - for(var x=0,numDeprec=deprecationWarn.length;x Date: Sat, 8 Nov 2014 17:38:03 -0500 Subject: [PATCH 03/16] closes #2370 --- src/views/config.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/views/config.json b/src/views/config.json index 075189a68a..70ba11b3af 100644 --- a/src/views/config.json +++ b/src/views/config.json @@ -6,13 +6,13 @@ "^users/latest": "users", "^users/sort-reputation": "users", "^users/search": "users", - "^user.*edit": "account/edit", - "^user.*following": "account/following", - "^user.*followers": "account/followers", - "^user.*settings": "account/settings", - "^user.*favourites": "account/favourites", - "^user.*posts": "account/posts", - "^user.*topics": "account/topics", + "^user/.*/edit": "account/edit", + "^user/.*/following": "account/following", + "^user/.*/followers": "account/followers", + "^user/.*/settings": "account/settings", + "^user/.*/favourites": "account/favourites", + "^user/.*/posts": "account/posts", + "^user/.*/topics": "account/topics", "^user/.*": "account/profile", "^reset/.*": "reset_code", "^tags/.*": "tag", From af4cbfb20dc18a977b1a9ce650bc9c46272018fe Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 8 Nov 2014 18:40:38 -0500 Subject: [PATCH 04/16] closes #2368 --- src/controllers/categories.js | 23 +++++++++++++++++------ src/controllers/topics.js | 7 ++----- src/middleware/middleware.js | 20 -------------------- src/routes/index.js | 2 +- 4 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/controllers/categories.js b/src/controllers/categories.js index 9952a0d8ff..812737de7f 100644 --- a/src/controllers/categories.js +++ b/src/controllers/categories.js @@ -9,7 +9,8 @@ var categoriesController = {}, categories = require('../categories'), topics = require('../topics'), meta = require('../meta'), - plugins = require('../plugins'); + plugins = require('../plugins'), + utils = require('../../public/src/utils'); // todo: This might be better placed somewhere else var apiToRegular = function(url) { @@ -44,7 +45,6 @@ categoriesController.popular = function(req, res, next) { if (uid === 0) { if (anonCache[term] && (Date.now() - lastUpdateTime) < 60 * 60 * 1000) { - console.log('returning from cache'); return res.render('popular', anonCache[term]); } } @@ -105,6 +105,10 @@ categoriesController.get = function(req, res, next) { uid = req.user ? req.user.uid : 0, userPrivileges; + if (req.params.topic_index && !utils.isNumber(req.params.topic_index)) { + return categoriesController.notFound(req, res); + } + async.waterfall([ function(next) { async.parallel({ @@ -112,7 +116,7 @@ categoriesController.get = function(req, res, next) { categories.exists(cid, next); }, categoryData: function(next) { - categories.getCategoryFields(cid, ['slug', 'disabled'], next); + categories.getCategoryFields(cid, ['slug', 'disabled', 'topic_count'], next); }, privileges: function(next) { privileges.categories.get(cid, uid, next); @@ -135,14 +139,21 @@ categoriesController.get = function(req, res, next) { return categoriesController.notAllowed(req, res); } + var topicIndex = utils.isNumber(req.params.topic_index) ? parseInt(req.params.topic_index, 10) : 1; + var topicCount = parseInt(results.categoryData.topic_count, 10) + 1; + + if (topicIndex < 1 || topicIndex > topicCount) { + var url = '/category/' + cid + '/' + req.params.slug + (topicIndex > topicCount ? '/' + topicCount : ''); + return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url); + } + userPrivileges = results.privileges; var settings = results.userSettings; - var topicIndex = 0; if (!settings.usePagination) { - topicIndex = Math.max((req.params.topic_index || 1) - (settings.topicsPerPage - 1), 0); + topicIndex = Math.max((topicIndex || 1) - (settings.topicsPerPage - 1), 0); } else if (!req.query.page) { - var index = Math.max(parseInt((req.params.topic_index || 0), 10), 0); + var index = Math.max(parseInt((topicIndex || 0), 10), 0); page = Math.ceil((index + 1) / settings.topicsPerPage); } diff --git a/src/controllers/topics.js b/src/controllers/topics.js index 8596803ca2..d2ff101636 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -60,11 +60,8 @@ topicsController.get = function(req, res, next) { if (utils.isNumber(req.params.post_index)) { var url = ''; - if (req.params.post_index > postCount) { - url = '/topic/' + req.params.topic_id + '/' + req.params.slug + '/' + postCount; - return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url); - } else if (req.params.post_index < 1) { - url = '/topic/' + req.params.topic_id + '/' + req.params.slug; + if (req.params.post_index < 1 || req.params.post_index > postCount) { + url = '/topic/' + req.params.topic_id + '/' + req.params.slug + (req.params.post_index > postCount ? '/' + postCount : ''); return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url); } } diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index 6d0b06b7dc..422805a391 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -127,26 +127,6 @@ middleware.addSlug = function(req, res, next) { next(); }; -middleware.checkTopicIndex = function(req, res, next) { - categories.getCategoryField(req.params.category_id, 'topic_count', function(err, topicCount) { - if (err) { - return next(err); - } - var topicIndex = parseInt(req.params.topic_index, 10); - topicCount = parseInt(topicCount, 10) + 1; - var url = ''; - - if (topicIndex > topicCount) { - url = '/category/' + req.params.category_id + '/' + req.params.slug + '/' + topicCount; - return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url); - } else if (topicIndex < 1) { - url = '/category/' + req.params.category_id + '/' + req.params.slug; - return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url); - } - next(); - }); -}; - middleware.prepareAPI = function(req, res, next) { res.locals.isAPI = true; next(); diff --git a/src/routes/index.js b/src/routes/index.js index 4d5f84ce48..395999507c 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -54,7 +54,7 @@ function categoryRoutes(app, middleware, controllers) { setupPageRoute(app, '/unread', middleware, [middleware.authenticate], controllers.categories.unread); app.get('/api/unread/total', middleware.authenticate, controllers.categories.unreadTotal); - setupPageRoute(app, '/category/:category_id/:slug/:topic_index', middleware, [middleware.applyCSRF, middleware.checkTopicIndex], controllers.categories.get); + setupPageRoute(app, '/category/:category_id/:slug/:topic_index', middleware, [middleware.applyCSRF], controllers.categories.get); setupPageRoute(app, '/category/:category_id/:slug?', middleware, [middleware.applyCSRF, middleware.addSlug], controllers.categories.get); } From 7823416410cb948594eb9efd5646f877beb7da74 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 8 Nov 2014 23:22:10 -0500 Subject: [PATCH 05/16] err check, assignPostsToCategory is sync --- src/categories/recentreplies.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.js index 38d2c74590..9c076fbcfc 100644 --- a/src/categories/recentreplies.js +++ b/src/categories/recentreplies.js @@ -29,6 +29,9 @@ module.exports = function(Categories) { return callback(null, []); } async.map(categoryData, getRecentTopicPids, function(err, results) { + if (err) { + return callback(err); + } var pids = _.flatten(results); @@ -41,21 +44,21 @@ module.exports = function(Categories) { return callback(err); } - async.each(categoryData, function(category, next) { - assignPostsToCategory(category, posts, next); - }, callback); + categoryData.forEach(function(category) { + assignPostsToCategory(category, posts); + }); + + callback(); }); }); }; - function assignPostsToCategory(category, posts, next) { + function assignPostsToCategory(category, posts) { category.posts = posts.filter(function(post) { return parseInt(post.category.cid, 10) === parseInt(category.cid, 10); }).sort(function(a, b) { return b.timestamp - a.timestamp; }).slice(0, parseInt(category.numRecentReplies, 10)); - - next(); } function getRecentTopicPids(category, callback) { From daebc322eb9a1911041a61d9447a844628f18dba Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 8 Nov 2014 23:36:38 -0500 Subject: [PATCH 06/16] meh --- src/categories/update.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/categories/update.js b/src/categories/update.js index 002628d723..f1cf7c02af 100644 --- a/src/categories/update.js +++ b/src/categories/update.js @@ -2,8 +2,8 @@ 'use strict'; var async = require('async'), - db = require('./../database'), - utils = require('./../../public/src/utils'); + db = require('../database'), + utils = require('../../public/src/utils'); module.exports = function(Categories) { From 8fdc03eaad6dc232b2607fab1c8ba4621a05d543 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 8 Nov 2014 23:54:21 -0500 Subject: [PATCH 07/16] categories.js refactor --- src/categories.js | 186 ++------------------------------------- src/categories/create.js | 48 ++++++++++ src/categories/topics.js | 89 +++++++++++++++++++ src/categories/unread.js | 56 ++++++++++++ 4 files changed, 201 insertions(+), 178 deletions(-) create mode 100644 src/categories/create.js create mode 100644 src/categories/topics.js create mode 100644 src/categories/unread.js diff --git a/src/categories.js b/src/categories.js index 4ccad2bb74..35b55d5a34 100644 --- a/src/categories.js +++ b/src/categories.js @@ -1,68 +1,26 @@ 'use strict'; -var db = require('./database'), - posts = require('./posts'), - utils = require('./../public/src/utils'), +var async = require('async'), + nconf = require('nconf'), + + db = require('./database'), user = require('./user'), Groups = require('./groups'), - topics = require('./topics'), plugins = require('./plugins'), - meta = require('./meta'), validator = require('validator'), - privileges = require('./privileges'), - - async = require('async'), - winston = require('winston'), - nconf = require('nconf'); + privileges = require('./privileges'); (function(Categories) { + require('./categories/create')(Categories); require('./categories/delete')(Categories); + require('./categories/topics')(Categories); + require('./categories/unread')(Categories); require('./categories/activeusers')(Categories); require('./categories/recentreplies')(Categories); require('./categories/update')(Categories); - Categories.create = function(data, callback) { - db.incrObjectField('global', 'nextCid', function(err, cid) { - if (err) { - return callback(err); - } - - var slug = cid + '/' + utils.slugify(data.name); - - var category = { - cid: cid, - name: data.name, - description: data.description, - icon: data.icon, - bgColor: data.bgColor, - color: data.color, - slug: slug, - parentCid: 0, - topic_count: 0, - post_count: 0, - disabled: 0, - order: data.order, - link: '', - numRecentReplies: 1, - class: 'col-md-3 col-xs-6', - imageClass: 'auto' - }; - - async.series([ - async.apply(db.setObject, 'category:' + cid, category), - async.apply(db.sortedSetAdd, 'categories:cid', data.order, cid) - ], function(err) { - if (err) { - return callback(err); - } - - callback(null, category); - }); - }); - }; - Categories.exists = function(cid, callback) { db.isSortedSetMember('categories:cid', cid, callback); }; @@ -112,42 +70,6 @@ var db = require('./database'), }); }; - Categories.getCategoryTopics = function(data, callback) { - var tids; - async.waterfall([ - function(next) { - Categories.getTopicIds(data.targetUid ? 'cid:' + data.cid + ':uid:' + data.targetUid + ':tids' : 'cid:' + data.cid + ':tids', data.start, data.stop, next); - }, - function(topicIds, next) { - tids = topicIds; - topics.getTopicsByTids(tids, data.uid, next); - }, - function(topics, next) { - if (!Array.isArray(topics) || !topics.length) { - return next(null, { - topics: [], - nextStart: 1 - }); - } - - var indices = {}, - i = 0; - for(i=0; i Date: Sun, 9 Nov 2014 00:01:46 -0500 Subject: [PATCH 08/16] fixed indent --- src/categories.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/categories.js b/src/categories.js index 35b55d5a34..d0d11c7981 100644 --- a/src/categories.js +++ b/src/categories.js @@ -242,7 +242,7 @@ var async = require('async'), } if (!cids.length) { - return callback(null, []); + return callback(null, []); } async.parallel({ From 420aa5e10282c041cfd09a10e3d83f9ebecf0c8e Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sun, 9 Nov 2014 00:33:26 -0500 Subject: [PATCH 09/16] fix moderators --- src/categories.js | 70 +++++++++++++----------------------- src/groups.js | 4 +++ src/privileges/categories.js | 2 +- 3 files changed, 30 insertions(+), 46 deletions(-) diff --git a/src/categories.js b/src/categories.js index d0d11c7981..46c9fe18bd 100644 --- a/src/categories.js +++ b/src/categories.js @@ -84,22 +84,19 @@ var async = require('async'), }; Categories.getPageCount = function(cid, uid, callback) { - Categories.getCategoryField(cid, 'topic_count', function(err, topicCount) { + async.parallel({ + topicCount: async.apply(Categories.getCategoryField, cid, 'topic_count'), + settings: async.apply(user.getSettings, uid) + }, function(err, results) { if (err) { return callback(err); } - if (parseInt(topicCount, 10) === 0) { + if (!parseInt(results.topicCount, 10)) { return callback(null, 1); } - user.getSettings(uid, function(err, settings) { - if (err) { - return callback(err); - } - - callback(null, Math.ceil(parseInt(topicCount, 10) / settings.topicsPerPage)); - }); + callback(null, Math.ceil(parseInt(results.topicCount, 10) / results.settings.topicsPerPage)); }); }; @@ -118,49 +115,32 @@ var async = require('async'), }; Categories.getCategoriesByPrivilege = function(uid, privilege, callback) { - db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { - if (err) { - return callback(err); - } - - if (!Array.isArray(cids) || !cids.length) { - return callback(null, []); - } - - privileges.categories.filterCids(privilege, cids, uid, function(err, cids) { - if (err) { - return callback(err); - } - - Categories.getCategories(cids, uid, function(err, categories) { - if (err) { - return callback(err); - } - - categories = categories.filter(function(category) { - return !category.disabled; - }); - - callback(null, categories); + async.waterfall([ + function(next) { + db.getSortedSetRange('categories:cid', 0, -1, next); + }, + function(cids, next) { + privileges.categories.filterCids(privilege, cids, uid, next); + }, + function(cids, next) { + Categories.getCategories(cids, uid, next); + }, + function(categories, next) { + categories = categories.filter(function(category) { + return !category.disabled; }); - }); - }); + next(null, categories); + } + ], callback); }; Categories.getModerators = function(cid, callback) { - Groups.get('cid:' + cid + ':privileges:mods', {}, function(err, groupObj) { - if (err && err === 'group-not-found') { - return callback(null, []); - } - if (err) { + Groups.getMembers('cid:' + cid + ':privileges:mods', function(err, uids) { + if (err || !Array.isArray(uids) || !uids.length) { return callback(err); } - if (!Array.isArray(groupObj) || !groupObj.members.length) { - return callback(null, []); - } - - user.getMultipleUserFields(groupObj.members, ['uid', 'username', 'userslug', 'picture'], callback); + user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], callback); }); }; diff --git a/src/groups.js b/src/groups.js index 4f007f1eec..cbdc55c32b 100644 --- a/src/groups.js +++ b/src/groups.js @@ -140,6 +140,10 @@ var async = require('async'), }); }; + Groups.getMembers = function(groupName, callback) { + db.getSetMembers('group:' + groupName + ':members', callback); + }; + Groups.search = function(query, options, callback) { if (!query) { return callback(null, []); diff --git a/src/privileges/categories.js b/src/privileges/categories.js index c957f1be6e..309404ac11 100644 --- a/src/privileges/categories.js +++ b/src/privileges/categories.js @@ -70,7 +70,7 @@ module.exports = function(privileges) { }; privileges.categories.filterCids = function(privilege, cids, uid, callback) { - if (!cids.length) { + if (!Array.isArray(cids) || !cids.length) { return callback(null, []); } From 39def60f9b38e5931eefbff05cee8f80c3d23c8d Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sun, 9 Nov 2014 00:45:18 -0500 Subject: [PATCH 10/16] return empty array --- src/categories.js | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/categories.js b/src/categories.js index 46c9fe18bd..6b5705b33f 100644 --- a/src/categories.js +++ b/src/categories.js @@ -102,12 +102,8 @@ var async = require('async'), Categories.getAllCategories = function(uid, callback) { db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { - if (err) { - return callback(err); - } - - if (!Array.isArray(cids) || !cids.length) { - return callback(null, []); + if (err || !Array.isArray(cids) || !cids.length) { + return callback(err, []); } Categories.getCategories(cids, uid, callback); @@ -137,7 +133,7 @@ var async = require('async'), Categories.getModerators = function(cid, callback) { Groups.getMembers('cid:' + cid + ':privileges:mods', function(err, uids) { if (err || !Array.isArray(uids) || !uids.length) { - return callback(err); + return callback(err, []); } user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], callback); @@ -156,12 +152,8 @@ var async = require('async'), }); db.getObjects(keys, function(err, categories) { - if (err) { - return callback(err); - } - - if (!Array.isArray(categories) || !categories.length) { - return callback(null, []); + if (err || !Array.isArray(categories) || !categories.length) { + return callback(err, []); } async.map(categories, modifyCategory, callback); From 263f479b204da93e78d357b7c0c1bbc01d0172e7 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sun, 9 Nov 2014 01:12:24 -0500 Subject: [PATCH 11/16] posts.js refactor pt1 --- src/posts.js | 412 +----------------------------------------- src/posts/category.js | 55 ++++++ src/posts/create.js | 86 +++++++++ src/posts/summary.js | 140 ++++++++++++++ src/posts/user.js | 163 +++++++++++++++++ 5 files changed, 451 insertions(+), 405 deletions(-) create mode 100644 src/posts/category.js create mode 100644 src/posts/create.js create mode 100644 src/posts/summary.js create mode 100644 src/posts/user.js diff --git a/src/posts.js b/src/posts.js index c5756610fd..ddb87d5a18 100644 --- a/src/posts.js +++ b/src/posts.js @@ -1,107 +1,25 @@ 'use strict'; var async = require('async'), - path = require('path'), - fs = require('fs'), - nconf = require('nconf'), _ = require('underscore'), - validator = require('validator'), - winston = require('winston'), - gravatar = require('gravatar'), - S = require('string'), - db = require('./database'), utils = require('../public/src/utils'), user = require('./user'), - groups = require('./groups'), topics = require('./topics'), - favourites = require('./favourites'), postTools = require('./postTools'), - privileges = require('./privileges'), - categories = require('./categories'), - plugins = require('./plugins'), - meta = require('./meta'), - emitter = require('./emitter'), - websockets = require('./socket.io'); + plugins = require('./plugins'); (function(Posts) { - require('./posts/recent')(Posts); + + require('./posts/create')(Posts); require('./posts/delete')(Posts); + require('./posts/user')(Posts); + require('./posts/category')(Posts); + require('./posts/summary')(Posts); + require('./posts/recent')(Posts); require('./posts/flags')(Posts); - Posts.create = function(data, callback) { - var uid = data.uid, - tid = data.tid, - content = data.content, - timestamp = data.timestamp || Date.now(); - - - if (uid === null) { - return callback(new Error('[[error:invalid-uid]]')); - } - - var postData; - - async.waterfall([ - function(next) { - db.incrObjectField('global', 'nextPid', next); - }, - function(pid, next) { - - postData = { - 'pid': pid, - 'uid': uid, - 'tid': tid, - 'content': content, - 'timestamp': timestamp, - 'reputation': 0, - 'votes': 0, - 'editor': '', - 'edited': 0, - 'deleted': 0 - }; - - if (data.toPid) { - postData.toPid = data.toPid; - } - - plugins.fireHook('filter:post.save', postData, next); - }, - function(postData, next) { - db.setObject('post:' + postData.pid, postData, next); - }, - function(next) { - async.parallel([ - function(next) { - user.onNewPostMade(postData, next); - }, - function(next) { - topics.onNewPostMade(postData, next); - }, - function(next) { - categories.onNewPostMade(postData, next); - }, - function(next) { - db.sortedSetAdd('posts:pid', timestamp, postData.pid, next); - }, - function(next) { - db.incrObjectField('global', 'postCount', next); - } - ], function(err) { - if (err) { - return next(err); - } - plugins.fireHook('filter:post.get', postData, next); - }); - }, - function(postData, next) { - plugins.fireHook('action:post.save', postData); - next(null, postData); - } - ], callback); - }; - Posts.exists = function(pid, callback) { db.isSortedSetMember('posts:pid', pid, callback); }; @@ -167,184 +85,6 @@ var async = require('async'), }); }; - Posts.getUserInfoForPosts = function(uids, uid, callback) { - async.parallel({ - groups: function(next) { - groups.getUserGroups(uids, next); - }, - userData: function(next) { - user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned', 'status'], next); - }, - online: function(next) { - websockets.isUsersOnline(uids, next); - } - }, function(err, results) { - if (err) { - return callback(err); - } - - var userData = results.userData; - for(var i=0; i Date: Sun, 9 Nov 2014 01:24:12 -0500 Subject: [PATCH 12/16] use getPostsFields --- src/posts/recent.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/posts/recent.js b/src/posts/recent.js index 12dab097d4..f3a0508a79 100644 --- a/src/posts/recent.js +++ b/src/posts/recent.js @@ -43,15 +43,7 @@ module.exports = function(Posts) { return callback(err); } - if (!Array.isArray(pids) || !pids.length) { - return callback(null, []); - } - - pids = pids.map(function(pid) { - return 'post:' + pid; - }); - - db.getObjectsFields(pids, ['uid'], function(err, postData) { + Posts.getPostsFields(pids, ['uid'], function(err, postData) { if (err) { return callback(err); } From c18de5cdd27a9adc58d8295dd0a7e8df64ed84ca Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sun, 9 Nov 2014 01:30:27 -0500 Subject: [PATCH 13/16] posts/recent.js refactor --- src/posts/recent.js | 56 ++++++++++++++++++----------------------- src/posts/summary.js | 6 +---- src/privileges/posts.js | 2 +- 3 files changed, 26 insertions(+), 38 deletions(-) diff --git a/src/posts/recent.js b/src/posts/recent.js index f3a0508a79..4a970a6435 100644 --- a/src/posts/recent.js +++ b/src/posts/recent.js @@ -1,6 +1,7 @@ 'use strict'; -var db = require('../database'), +var async = require('async'), + db = require('../database'), privileges = require('../privileges'); @@ -19,44 +20,35 @@ module.exports = function(Posts) { var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1; - db.getSortedSetRevRangeByScore('posts:pid', start, count, '+inf', Date.now() - since, function(err, pids) { - if (err) { - return callback(err); + async.waterfall([ + function(next) { + db.getSortedSetRevRangeByScore('posts:pid', start, count, '+inf', Date.now() - since, next); + }, + function(pids, next) { + privileges.posts.filter('read', pids, uid, next); + }, + function(pids, next) { + Posts.getPostSummaryByPids(pids, uid, {stripTags: true}, next); } - - if (!Array.isArray(pids) || !pids.length) { - return callback(null, []); - } - - privileges.posts.filter('read', pids, uid, function(err, pids) { - if (err) { - return callback(err); - } - Posts.getPostSummaryByPids(pids, uid, {stripTags: true}, callback); - }); - }); + ], callback); }; Posts.getRecentPosterUids = function(start, end, callback) { - db.getSortedSetRevRange('posts:pid', start, end, function(err, pids) { - if (err) { - return callback(err); - } - - Posts.getPostsFields(pids, ['uid'], function(err, postData) { - if (err) { - return callback(err); - } - + async.waterfall([ + function(next) { + db.getSortedSetRevRange('posts:pid', start, end, next); + }, + function(pids, next) { + Posts.getPostsFields(pids, ['uid'], next); + }, + function(postData, next) { postData = postData.map(function(post) { return post && post.uid; }).filter(function(value, index, array) { return value && array.indexOf(value) === index; }); - - callback(null, postData); - }); - }); - }; - + next(null, postData); + } + ], callback); + }; }; diff --git a/src/posts/summary.js b/src/posts/summary.js index 3e1fae403b..9b1d880176 100644 --- a/src/posts/summary.js +++ b/src/posts/summary.js @@ -24,13 +24,9 @@ module.exports = function(Posts) { return callback(null, []); } - var keys = pids.map(function(pid) { - return 'post:' + pid; - }); - var fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'].concat(options.extraFields); - db.getObjectsFields(keys, fields, function(err, posts) { + Posts.getPostsFields(pids, fields, function(err, posts) { if (err) { return callback(err); } diff --git a/src/privileges/posts.js b/src/privileges/posts.js index e5035ceb4b..ec61bf826c 100644 --- a/src/privileges/posts.js +++ b/src/privileges/posts.js @@ -72,7 +72,7 @@ module.exports = function(privileges) { }; privileges.posts.filter = function(privilege, pids, uid, callback) { - if (!pids.length) { + if (!Array.isArray(pids) || !pids.length) { return callback(null, []); } posts.getCidsByPids(pids, function(err, cids) { From aa6137b9c36adb42a388db880591c2397dbc7d5a Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 10 Nov 2014 11:46:08 -0500 Subject: [PATCH 14/16] fixed potential XSS in registration form --- public/src/client/register.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/src/client/register.js b/public/src/client/register.js index a332aa7c6f..6780faae59 100644 --- a/public/src/client/register.js +++ b/public/src/client/register.js @@ -146,7 +146,7 @@ define('forum/register', function() { }); username.on('keyup', function() { - $('#yourUsername').html(this.value.length > 0 ? this.value : 'username'); + $('#yourUsername').text(this.value.length > 0 ? this.value : 'username'); }); username.on('blur', function() { From 121fb88008a74c0d87dc54451d12529ba61fc658 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 10 Nov 2014 11:53:08 -0500 Subject: [PATCH 15/16] updating widget-essentials to 0.1.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f92d2be881..634e3e0169 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "nodebb-plugin-spam-be-gone": "^0.2.51", "nodebb-theme-lavender": "~0.1.0", "nodebb-theme-vanilla": "~0.1.0", - "nodebb-widget-essentials": "~0.1.1", + "nodebb-widget-essentials": "~0.1.2", "npm": "^2.1.4", "passport": "^0.2.1", "passport-local": "1.0.0", From 91103fb09e6e29e34651baddbd5a80d2f8c9f670 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 10 Nov 2014 12:02:03 -0500 Subject: [PATCH 16/16] updating widget-essentials to 0.2.0 ** --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 634e3e0169..9bf8ebff47 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "nodebb-plugin-spam-be-gone": "^0.2.51", "nodebb-theme-lavender": "~0.1.0", "nodebb-theme-vanilla": "~0.1.0", - "nodebb-widget-essentials": "~0.1.2", + "nodebb-widget-essentials": "~0.2.0", "npm": "^2.1.4", "passport": "^0.2.1", "passport-local": "1.0.0",