Node.js based forum software built for the modern web
closes #3611
barisusakli501bfbfc47bd
diff --git a/public/src/modules/search.js b/public/src/modules/search.jsindex 81b98e9ab3..f88f62b066 100644--- a/public/src/modules/search.js+++ b/public/src/modules/search.js@@ -1,5 +1,5 @@"use strict";-/* globals socket, ajaxify, app, define */+/* globals socket, ajaxify, app, define, config */define('search', ['navigator', 'translator'], function(nav, translator) {@@ -77,7 +77,7 @@ define('search', ['navigator', 'translator'], function(nav, translator) {term: term}, function(err, pids) {if (err) {- return callback(err);+ return app.alertError(err.message);}if (Array.isArray(pids)) {@@ -126,8 +126,13 @@ define('search', ['navigator', 'translator'], function(nav, translator) {if (Search.current.results.length > 0) {topicSearchEl.find('.count').html((index+1) + ' / ' + Search.current.results.length);topicSearchEl.find('.prev, .next').removeAttr('disabled');- socket.emit('posts.getPidIndex', Search.current.results[index], function(err, postIndex) {- nav.scrollToPost(postIndex-1, true); // why -1? Ask @barisusakli+ var data = {+ pid: Search.current.results[index],+ tid: Search.current.tid,+ topicPostSort: config.topicPostSort+ };+ socket.emit('posts.getPidIndex', data, function(err, postIndex) {+ nav.scrollToPost(postIndex, true);});} else {translator.translate('[[search:no-matches]]', function(text) {diff --git a/src/posts.js b/src/posts.jsindex 82be31fea0..24d3371c23 100644--- a/src/posts.js+++ b/src/posts.js@@ -168,25 +168,13 @@ var async = require('async'),});};- Posts.getPidIndex = function(pid, uid, callback) {- async.parallel({- settings: function(next) {- user.getSettings(uid, next);- },- tid: function(next) {- Posts.getPostField(pid, 'tid', next);- }- }, function(err, results) {- if(err) {- return callback(err);+ Posts.getPidIndex = function(pid, tid, topicPostSort, callback) {+ var set = topicPostSort === 'most_votes' ? 'tid:' + tid + ':posts:votes' : 'tid:' + tid + ':posts';+ db.sortedSetRank(set, pid, function(err, index) {+ if (!utils.isNumber(index)) {+ return callback(err, 0);}- var set = results.settings.topicPostSort === 'most_votes' ? 'tid:' + results.tid + ':posts:votes' : 'tid:' + results.tid + ':posts';- db.sortedSetRank(set, pid, function(err, index) {- if (!utils.isNumber(index)) {- return callback(err, 1);- }- callback(err, parseInt(index, 10) + 2);- });+ callback(err, parseInt(index, 10) + 1);});};diff --git a/src/socket.io/posts.js b/src/socket.io/posts.jsindex e302dda0ff..d6af25a71a 100644--- a/src/socket.io/posts.js+++ b/src/socket.io/posts.js@@ -579,7 +579,7 @@ function loadMorePosts(set, uid, data, callback) {}SocketPosts.getRecentPosts = function(socket, data, callback) {- if(!data || !data.count) {+ if (!data || !data.count) {return callback(new Error('[[error:invalid-data]]'));}@@ -590,8 +590,11 @@ SocketPosts.getCategory = function(socket, pid, callback) {posts.getCidByPid(pid, callback);};-SocketPosts.getPidIndex = function(socket, pid, callback) {- posts.getPidIndex(pid, socket.uid, callback);+SocketPosts.getPidIndex = function(socket, data, callback) {+ if (!data) {+ return callback(new Error('[[error:invalid-data]]'));+ }+ posts.getPidIndex(data.pid, data.tid, data.topicPostSort, callback);};module.exports = SocketPosts;diff --git a/src/topics/create.js b/src/topics/create.jsindex c4ba5a2911..995ddc1550 100644--- a/src/topics/create.js+++ b/src/topics/create.js@@ -131,7 +131,10 @@ module.exports = function(Topics) {Topics.create({uid: data.uid, title: data.title, cid: data.cid, thumb: data.thumb, tags: data.tags, timestamp: data.timestamp}, next);},function(tid, next) {- Topics.reply({uid: data.uid, tid: tid, handle: data.handle, content: data.content, timestamp: data.timestamp, req: data.req}, next);+ posts.create({uid: data.uid, tid: tid, handle: data.handle, content: data.content, timestamp: data.timestamp, ip: data.req ? data.req.ip : null}, next);+ },+ function(postData, next) {+ onNewPost(postData, data, next);},function(postData, next) {async.parallel({@@ -163,6 +166,7 @@ module.exports = function(Topics) {data.topicData = data.topicData[0];data.topicData.unreplied = 1;data.topicData.mainPost = data.postData;+ data.postData.index = 0;plugins.fireHook('action:topic.post', data.topicData);@@ -232,8 +236,40 @@ module.exports = function(Topics) {function(next) {posts.create({uid: uid, tid: tid, handle: data.handle, content: content, toPid: data.toPid, timestamp: data.timestamp, ip: data.req ? data.req.ip : null}, next);},- function(data, next) {- postData = data;+ function(_postData, next) {+ postData = _postData;+ onNewPost(postData, data, next);+ },+ function(postData, next) {+ user.getSettings(uid, next);+ },+ function(settings, next) {+ if (settings.followTopicsOnReply) {+ Topics.follow(postData.tid, uid);+ }++ posts.getPidIndex(postData.pid, postData.tid, settings.topicPostSort, next);+ },+ function(postIndex, next) {+ postData.index = postIndex;++ if (parseInt(uid, 10)) {+ Topics.notifyFollowers(postData, uid);+ user.setUserField(uid, 'lastonline', Date.now());+ }++ plugins.fireHook('action:topic.reply', postData);++ next(null, postData);+ }+ ], callback);+ };++ function onNewPost(postData, data, callback) {+ var tid = postData.tid;+ var uid = postData.uid;+ async.waterfall([+ function(next) {Topics.markAsUnreadForAll(tid, next);},function(next) {@@ -247,12 +283,6 @@ module.exports = function(Topics) {topicInfo: function(next) {Topics.getTopicFields(tid, ['tid', 'title', 'slug', 'cid', 'postcount'], next);},- settings: function(next) {- user.getSettings(uid, next);- },- postIndex: function(next) {- posts.getPidIndex(postData.pid, uid, next);- },content: function(next) {posts.parsePost(postData, next);}@@ -267,31 +297,18 @@ module.exports = function(Topics) {postData.user.username = validator.escape(data.handle);}- if (results.settings.followTopicsOnReply) {- Topics.follow(postData.tid, uid);- }- postData.index = results.postIndex - 1;postData.favourited = false;postData.votes = 0;postData.display_moderator_tools = true;postData.display_move_tools = true;postData.selfPost = false;postData.relativeTime = utils.toISOString(postData.timestamp);-- if (parseInt(uid, 10) && data.req) {- Topics.notifyFollowers(postData, uid);- user.setUserField(uid, 'lastonline', Date.now());- }-- if (postData.index > 0) {- plugins.fireHook('action:topic.reply', postData);- }-postData.topic.title = validator.escape(postData.topic.title);+next(null, postData);}], callback);- };+ }function checkTitleLength(title, callback) {if (!title || title.length < parseInt(meta.config.minimumTitleLength, 10)) {diff --git a/src/topics/follow.js b/src/topics/follow.jsindex 1b0ce4d3dd..16d54f04fb 100644--- a/src/topics/follow.js+++ b/src/topics/follow.js@@ -46,6 +46,9 @@ module.exports = function(Topics) {Topics.follow = function(tid, uid, callback) {callback = callback || function() {};+ if (!parseInt(uid, 10)) {+ return callback();+ }async.waterfall([function (next) {Topics.exists(tid, next);