diff --git a/public/src/modules/composer.js b/public/src/modules/composer.js index 9c1bbf351c..45b8c2b974 100644 --- a/public/src/modules/composer.js +++ b/public/src/modules/composer.js @@ -736,7 +736,7 @@ define(['taskbar'], function(taskbar) { }, done); } else if (parseInt(postData.tid, 10) > 0) { socket.emit('posts.reply', { - topic_id: postData.tid, + tid: postData.tid, content: bodyEl.val(), toPid: postData.toPid }, done); diff --git a/src/admin/categories.js b/src/admin/categories.js index 333a22cf70..0522837a05 100644 --- a/src/admin/categories.js +++ b/src/admin/categories.js @@ -13,10 +13,12 @@ var db = require('./../database'), for (var key in category) { db.setObjectField('category:' + cid, key, category[key]); - if (key == 'name') { + if (key === 'name') { // reset slugs if name is updated var slug = cid + '/' + utils.slugify(category[key]); db.setObjectField('category:' + cid, 'slug', slug); + } else if (key === 'order') { + db.sortedSetAdd('categories:cid', category[key], cid); } } diff --git a/src/categories.js b/src/categories.js index 85315e103a..da9f84a8af 100644 --- a/src/categories.js +++ b/src/categories.js @@ -18,11 +18,10 @@ var db = require('./database'), Categories.create = function(data, callback) { db.incrObjectField('global', 'nextCid', function(err, cid) { if (err) { - return callback(err, null); + return callback(err); } var slug = cid + '/' + utils.slugify(data.name); - db.listAppend('categories:cid', cid); var category = { cid: cid, @@ -36,14 +35,20 @@ var db = require('./database'), topic_count: 0, disabled: 0, order: data.order, - link: "", + link: '', numRecentReplies: 2, class: 'col-md-3 col-xs-6', imageClass: 'default' }; - db.setObject('category:' + cid, category, function(err, data) { - callback(err, category); + db.setObject('category:' + cid, category, function(err) { + if(err) { + return callback(err); + } + + db.sortedSetAdd('categories:cid', data.order, cid); + + callback(null, category); }); }); }; @@ -132,6 +137,10 @@ var db = require('./database'), return callback(err); } + if (parseInt(topicCount, 10) === 0) { + return callback(null, 1); + } + user.getSettings(uid, function(err, settings) { if(err) { return callback(err); @@ -142,8 +151,8 @@ var db = require('./database'), }); }; - Categories.getAllCategories = function(current_user, callback) { - db.getListRange('categories:cid', 0, -1, function(err, cids) { + Categories.getAllCategories = function(uid, callback) { + db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { if(err) { return callback(err); } @@ -152,7 +161,7 @@ var db = require('./database'), return callback(null, {categories : []}); } - Categories.getCategories(cids, current_user, callback); + Categories.getCategories(cids, uid, callback); }); }; @@ -311,14 +320,11 @@ var db = require('./database'), async.map(cids, getCategory, function(err, categories) { if (err) { - winston.err(err); - return callback(err, null); + return callback(err); } categories = categories.filter(function(category) { return !!category; - }).sort(function(a, b) { - return parseInt(a.order, 10) - parseInt(b.order, 10); }); callback(null, { diff --git a/src/categoryTools.js b/src/categoryTools.js index 8dd2f6664e..454d87128b 100644 --- a/src/categoryTools.js +++ b/src/categoryTools.js @@ -2,9 +2,14 @@ var Groups = require('./groups'), User = require('./user'), async = require('async'), + db = require('./database'), CategoryTools = {}; +CategoryTools.exists = function(cid, callback) { + db.isSortedSetMember('categories:cid', cid, callback); +}; + CategoryTools.privileges = function(cid, uid, callback) { async.parallel({ "+r": function(next) { diff --git a/src/posts.js b/src/posts.js index fa3703a585..1ad0750d16 100644 --- a/src/posts.js +++ b/src/posts.js @@ -87,7 +87,7 @@ var db = require('./database'), function(postData, next) { postTools.parse(postData.content, function(err, content) { if(err) { - return next(err, null); + return next(err); } postData.content = content; @@ -303,24 +303,24 @@ var db = require('./database'), }); }, function(postData, next) { - if (postData.content) { - postTools.parse(postData.content, function(err, content) { - if(err) { - return next(err); - } + if (!postData.content) { + return next(null, postData); + } - if(stripTags) { - var s = S(content); - postData.content = s.stripTags.apply(s, utils.getTagsExcept(['img', 'i'])).s; - } else { - postData.content = content; - } + postTools.parse(postData.content, function(err, content) { + if(err) { + return next(err); + } + + if(stripTags) { + var s = S(content); + postData.content = s.stripTags.apply(s, utils.getTagsExcept(['img', 'i'])).s; + } else { + postData.content = content; + } - next(null, postData); - }); - } else { next(null, postData); - } + }); } ], callback); } diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 2b54551554..c1b4dc9aaf 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -25,7 +25,7 @@ SocketPosts.reply = function(socket, data, callback) { return callback(new Error('not-logged-in')); } - if(!data || !data.topic_id || !data.content) { + if(!data || !data.tid || !data.content) { return callback(new Error('invalid data')); } diff --git a/src/threadTools.js b/src/threadTools.js index f99f5f7ef6..b9fde04bc5 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -18,15 +18,7 @@ var winston = require('winston'), (function(ThreadTools) { ThreadTools.exists = function(tid, callback) { - - db.isSortedSetMember('topics:tid', tid, function(err, ismember) { - - if (err) { - callback(false); - } - - callback(ismember); - }); + db.isSortedSetMember('topics:tid', tid, callback); } ThreadTools.privileges = function(tid, uid, callback) { diff --git a/src/topics.js b/src/topics.js index b7e22871a5..51a7008ef9 100644 --- a/src/topics.js +++ b/src/topics.js @@ -106,6 +106,12 @@ var async = require('async'), async.waterfall([ function(next) { + categoryTools.exists(cid, next); + }, + function(categoryExists, next) { + if(!categoryExists) { + return next(new Error('category doesn\'t exist')) + } categoryTools.privileges(cid, uid, next); }, function(privileges, next) { @@ -144,7 +150,7 @@ var async = require('async'), }; Topics.reply = function(data, callback) { - var tid = data.topic_id, + var tid = data.tid, uid = data.uid, toPid = data.toPid, content = data.content, @@ -153,6 +159,12 @@ var async = require('async'), async.waterfall([ function(next) { + threadTools.exists(tid, next); + }, + function(topicExists, next) { + if (!topicExists) { + return next(new Error('topic doesn\'t exist')); + } threadTools.privileges(tid, uid, next); }, function(privilegesData, next) { @@ -265,9 +277,9 @@ var async = require('async'), }; Topics.movePostToTopic = function(pid, tid, callback) { - threadTools.exists(tid, function(exists) { - if(!exists) { - return callback(new Error('Topic doesn\'t exist')); + threadTools.exists(tid, function(err, exists) { + if(err || !exists) { + return callback(err || new Error('Topic doesn\'t exist')); } posts.getPostFields(pid, ['deleted', 'tid', 'timestamp'], function(err, postData) { @@ -426,7 +438,9 @@ var async = require('async'), if(err) { return callback(err); } - + if(!parseInt(postCount, 10)) { + return callback(null, 1); + } user.getSettings(uid, function(err, settings) { if(err) { return callback(err); @@ -762,9 +776,9 @@ var async = require('async'), }; Topics.getTopicWithPosts = function(tid, current_user, start, end, quiet, callback) { - threadTools.exists(tid, function(exists) { - if (!exists) { - return callback(new Error('Topic tid \'' + tid + '\' not found')); + threadTools.exists(tid, function(err, exists) { + if (err || !exists) { + return callback(err || new Error('Topic tid \'' + tid + '\' not found')); } // "quiet" is used for things like RSS feed updating, HTML parsing for non-js users, etc diff --git a/src/upgrade.js b/src/upgrade.js index 41076ef259..26a3900644 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -7,6 +7,7 @@ var db = require('./database'), User = require('./user'), Topics = require('./topics'), Posts = require('./posts'), + Categories = require('./categories'), Groups = require('./groups'), Meta = require('./meta'), Plugins = require('./plugins'), @@ -19,7 +20,7 @@ var db = require('./database'), Upgrade.check = function(callback) { // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema - var latestSchema = new Date(2014, 1, 20, 20, 25).getTime(); + var latestSchema = new Date(2014, 1, 22).getTime(); db.get('schemaDate', function(err, value) { if (parseInt(value, 10) >= latestSchema) { @@ -691,7 +692,7 @@ Upgrade.upgrade = function(callback) { if (schemaDate < thisSchemaDate) { updatesMade = true; - + db.setObjectField('widgets:home.tpl', 'motd', JSON.stringify([ { "widget": "html", @@ -717,9 +718,9 @@ Upgrade.upgrade = function(callback) { if (schemaDate < thisSchemaDate) { updatesMade = true; - + var container = '
{title}
{body}
'; - + db.setObjectField('widgets:category.tpl', 'sidebar', JSON.stringify([ { "widget": "recentreplies", @@ -756,7 +757,7 @@ Upgrade.upgrade = function(callback) { if (schemaDate < thisSchemaDate) { updatesMade = true; - + db.setObjectField('widgets:home.tpl', 'footer', JSON.stringify([ { "widget": "forumstats", @@ -778,7 +779,7 @@ Upgrade.upgrade = function(callback) { updatesMade = true; var container = '
{title}
{body}
'; - + db.setObjectField('widgets:home.tpl', 'sidebar', JSON.stringify([ { "widget": "html", @@ -813,6 +814,59 @@ Upgrade.upgrade = function(callback) { winston.info('[2014/2/20] Activating NodeBB Essential Widgets - skipped'); next(); } + }, + function(next) { + thisSchemaDate = new Date(2014, 1, 22).getTime(); + + if (schemaDate < thisSchemaDate) { + updatesMade = true; + + db.exists('categories:cid', function(err, exists) { + if(err) { + return next(err); + } + if(!exists) { + winston.info('[2014/2/22] Added categories to sorted set - skipped'); + return next(); + } + + db.getListRange('categories:cid', 0, -1, function(err, cids) { + if(err) { + return next(err); + } + + if(!Array.isArray(cids)) { + winston.info('[2014/2/22] Add categories to sorted set - skipped (cant find any cids)'); + return next(); + } + + db.rename('categories:cid', 'categories:cid:old', function(err) { + if(err) { + return next(err); + } + + async.each(cids, function(cid, next) { + Categories.getCategoryField(cid, 'order', function(err, order) { + if(err) { + return next(err); + } + db.sortedSetAdd('categories:cid', order, cid, next); + }); + }, function(err) { + if(err) { + return next(err); + } + winston.info('[2014/2/22] Added categories to sorted set'); + db.delete('categories:cid:old', next); + }); + }); + }); + }); + + } else { + winston.info('[2014/2/22] Added categories to sorted set - skipped'); + next(); + } } // Add new schema updates here // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 17!!!