From 78c65aee0518c23b3a393ebd353b6c4f1ed9a7c8 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sun, 15 Mar 2015 01:12:13 -0400 Subject: [PATCH] even more search changes --- src/categories/topics.js | 50 ++++++++++-------------- src/database/mongo/main.js | 4 +- src/postTools.js | 21 ++++++++-- src/posts/create.js | 8 +++- src/posts/delete.js | 79 ++++++++++++++++---------------------- src/threadTools.js | 2 +- 6 files changed, 81 insertions(+), 83 deletions(-) diff --git a/src/categories/topics.js b/src/categories/topics.js index 50dba67bbd..805ab30c4a 100644 --- a/src/categories/topics.js +++ b/src/categories/topics.js @@ -74,37 +74,29 @@ module.exports = function(Categories) { }); }; - Categories.onNewPostMade = function(postData, callback) { - topics.getTopicFields(postData.tid, ['cid', 'pinned'], function(err, topicData) { - if (err) { - return callback(err); - } - - if (!topicData || !topicData.cid) { - return callback(); - } - - var cid = topicData.cid; + Categories.onNewPostMade = function(cid, pinned, postData, callback) { + if (!cid || !postData) { + return callback(); + } - async.parallel([ - function(next) { - db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, postData.pid, next); - }, - function(next) { - db.incrObjectField('category:' + cid, 'post_count', next); - }, - function(next) { - if (parseInt(topicData.pinned, 10) === 1) { - next(); - } else { - db.sortedSetAdd('cid:' + cid + ':tids', postData.timestamp, postData.tid, next); - } - }, - function(next) { - db.sortedSetIncrBy('cid:' + cid + ':tids:posts', 1, postData.tid, next); + async.parallel([ + function(next) { + db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, postData.pid, next); + }, + function(next) { + db.incrObjectField('category:' + cid, 'post_count', next); + }, + function(next) { + if (parseInt(pinned, 10) === 1) { + next(); + } else { + db.sortedSetAdd('cid:' + cid + ':tids', postData.timestamp, postData.tid, next); } - ], callback); - }); + }, + function(next) { + db.sortedSetIncrBy('cid:' + cid + ':tids:posts', 1, postData.tid, next); + } + ], callback); }; }; diff --git a/src/database/mongo/main.js b/src/database/mongo/main.js index 6302d76fb2..913696aa9c 100644 --- a/src/database/mongo/main.js +++ b/src/database/mongo/main.js @@ -12,8 +12,8 @@ module.exports = function(db, module) { key: key }; for(var field in data) { - if (data.hasOwnProperty(field)) { - setData[field] = data[field]; + if (data.hasOwnProperty(field) && data[field]) { + setData[field] = data[field].toString(); } } diff --git a/src/postTools.js b/src/postTools.js index 3e9712699e..647d796841 100644 --- a/src/postTools.js +++ b/src/postTools.js @@ -19,7 +19,7 @@ var winston = require('winston'), var cache = LRU({ max: 1048576, - length: function (n) { return n.length }, + length: function (n) { return n.length; }, maxAge: 1000 * 60 * 60 }); @@ -63,22 +63,32 @@ var cache = LRU({ }, topic: function(next) { var tid = postData.tid; - posts.isMain(data.pid, function(err, isMainPost) { + async.parallel({ + cid: function(next) { + topics.getTopicField(tid, 'cid', next); + }, + isMain: function(next) { + posts.isMain(data.pid, next); + } + }, function(err, results) { if (err) { return next(err); } options.tags = options.tags || []; - if (!isMainPost) { + if (!results.isMain) { return next(null, { tid: tid, + cid: results.cid, isMainPost: false }); } var topicData = { tid: tid, + cid: results.cid, + uid: postData.uid, mainPid: data.pid, title: title, slug: tid + '/' + utils.slugify(title) @@ -98,8 +108,10 @@ var cache = LRU({ topics.getTopicTagsObjects(tid, function(err, tags) { next(err, { tid: tid, + cid: results.cid, + uid: postData.uid, title: validator.escape(title), - isMainPost: isMainPost, + isMainPost: results.isMain, tags: tags }); }); @@ -114,6 +126,7 @@ var cache = LRU({ if (err) { return callback(err); } + postData.cid = results.topic.cid; results.content = results.postData.content; plugins.fireHook('action:post.edit', postData); diff --git a/src/posts/create.js b/src/posts/create.js index a235b2f3f0..1bce6e69c7 100644 --- a/src/posts/create.js +++ b/src/posts/create.js @@ -68,7 +68,13 @@ module.exports = function(Posts) { topics.onNewPostMade(postData, next); }, function(next) { - categories.onNewPostMade(postData, next); + topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) { + if (err) { + return next(err); + } + postData.cid = topicData.cid; + categories.onNewPostMade(topicData.cid, topicData.pinned, postData, next); + }); }, function(next) { db.sortedSetAdd('posts:pid', timestamp, postData.pid, next); diff --git a/src/posts/delete.js b/src/posts/delete.js index bd130ade46..f47ff28e88 100644 --- a/src/posts/delete.js +++ b/src/posts/delete.js @@ -9,16 +9,19 @@ var async = require('async'), module.exports = function(Posts) { Posts.delete = function(pid, callback) { - Posts.setPostField(pid, 'deleted', 1, function(err) { - if (err) { - return callback(err); - } - - Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'timestamp'], function(err, postData) { - if (err) { - return callback(err); - } - + var postData; + async.waterfall([ + function(next) { + Posts.setPostField(pid, 'deleted', 1, next); + }, + function(next) { + Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'timestamp'], next); + }, + function(_post, next) { + postData = _post; + topics.getTopicField(_post.tid, 'cid', next); + }, + function(cid, next) { plugins.fireHook('action:post.delete', pid); async.parallel([ @@ -26,7 +29,7 @@ module.exports = function(Posts) { updateTopicTimestamp(postData.tid, next); }, function(next) { - removeFromCategoryRecentPosts(pid, postData.tid, next); + db.sortedSetRemove('cid:' + cid + ':pids', pid, next); }, function(next) { Posts.dismissFlag(pid, next); @@ -34,21 +37,25 @@ module.exports = function(Posts) { ], function(err) { callback(err, postData); }); - }); - }); + } + ], callback); }; Posts.restore = function(pid, callback) { - Posts.setPostField(pid, 'deleted', 0, function(err) { - if (err) { - return callback(err); - } - - Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content', 'timestamp'], function(err, postData) { - if (err) { - return callback(err); - } - + var postData; + async.waterfall([ + function(next) { + Posts.setPostField(pid, 'deleted', 0, next); + }, + function(next) { + Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content', 'timestamp'], next); + }, + function(_post, next) { + postData = _post; + topics.getTopicField(_post.tid, 'cid', next); + }, + function(cid, next) { + postData.cid = cid; plugins.fireHook('action:post.restore', postData); async.parallel([ @@ -56,13 +63,13 @@ module.exports = function(Posts) { updateTopicTimestamp(postData.tid, next); }, function(next) { - addToCategoryRecentPosts(pid, postData.tid, postData.timestamp, next); + db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, pid, next); } ], function(err) { callback(err, postData); }); - }); - }); + } + ], callback); }; function updateTopicTimestamp(tid, callback) { @@ -84,26 +91,6 @@ module.exports = function(Posts) { }); } - function removeFromCategoryRecentPosts(pid, tid, callback) { - topics.getTopicField(tid, 'cid', function(err, cid) { - if (err) { - return callback(err); - } - - db.sortedSetRemove('cid:' + cid + ':pids', pid, callback); - }); - } - - function addToCategoryRecentPosts(pid, tid, timestamp, callback) { - topics.getTopicField(tid, 'cid', function(err, cid) { - if (err) { - return callback(err); - } - - db.sortedSetAdd('cid:' + cid + ':pids', timestamp, pid, callback); - }); - } - Posts.purge = function(pid, callback) { Posts.exists(pid, function(err, exists) { if (err || !exists) { diff --git a/src/threadTools.js b/src/threadTools.js index 109a4215ce..76ea84c9df 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -21,7 +21,7 @@ var async = require('async'), }; function toggleDelete(tid, uid, isDelete, callback) { - topics.getTopicFields(tid, ['tid', 'cid', 'deleted', 'title', 'mainPid'], function(err, topicData) { + topics.getTopicFields(tid, ['tid', 'cid', 'uid', 'deleted', 'title', 'mainPid'], function(err, topicData) { if (err) { return callback(err); }