Node.js based forum software built for the modern web
#1992
barisusaklica90afd54478
diff --git a/public/src/forum/category.js b/public/src/forum/category.jsindex 81c082ba85..d62719fb9d 100644--- a/public/src/forum/category.js+++ b/public/src/forum/category.js@@ -35,7 +35,7 @@ define('forum/category', ['composer', 'forum/pagination', 'forum/infinitescroll'enableInfiniteLoadingOrPagination();if (!config.usePagination) {- navigator.init('#topics-container > .category-item', ajaxify.variables.get('topic_count'), undefined, Category.toTop, Category.toBottom);+ navigator.init('#topics-container > .category-item', ajaxify.variables.get('topic_count'), Category.toTop, Category.toBottom, Category.navigatorCallback);}$('#topics-container').on('click', '.topic-title', function() {@@ -60,6 +60,10 @@ define('forum/category', ['composer', 'forum/pagination', 'forum/infinitescroll'});};+ Category.navigatorCallback = function(element, elementCount) {+ return parseInt(element.attr('data-index'), 10) + 1;+ }+$(window).on('action:popstate', function(ev, data) {if(data.url.indexOf('category/') === 0) {var bookmark = localStorage.getItem('category:bookmark');@@ -285,8 +289,7 @@ define('forum/category', ['composer', 'forum/pagination', 'forum/infinitescroll'return;}- infinitescroll.calculateAfter(direction, '#topics-container .category-item[data-tid]', config.topicsPerPage, function(after, offset, el) {-+ infinitescroll.calculateAfter(direction, '#topics-container .category-item[data-tid]', config.topicsPerPage, false, function(after, offset, el) {loadTopicsAfter(after, function() {if (direction < 0 && el) {Category.scrollToTopic(el.attr('data-tid'), null, 0, offset);diff --git a/public/src/forum/infinitescroll.js b/public/src/forum/infinitescroll.jsindex 053635f312..870f555a8c 100644--- a/public/src/forum/infinitescroll.js+++ b/public/src/forum/infinitescroll.js@@ -55,13 +55,16 @@ define('forum/infinitescroll', function() {});};- scroll.calculateAfter = function(direction, selector, count, callback) {+ scroll.calculateAfter = function(direction, selector, count, reverse, callback) {var after = 0,offset = 0,el = direction > 0 ? $(selector).last() : $(selector).first();+ var count = reverse ? -count : count;+ var increment = reverse ? -1 : 1;+if (direction > 0) {- after = parseInt(el.attr('data-index'), 10) + 1;+ after = parseInt(el.attr('data-index'), 10) + increment;} else {after = parseInt(el.attr('data-index'), 10);if (isNaN(after)) {diff --git a/public/src/forum/topic.js b/public/src/forum/topic.jsindex 0300d58121..61ad6f4837 100644--- a/public/src/forum/topic.js+++ b/public/src/forum/topic.js@@ -63,7 +63,7 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadThandleBookmark(tid);- navigator.init('.posts > .post-row', postCount, Topic.navigatorCallback, Topic.toTop, Topic.toBottom);+ navigator.init('.posts > .post-row', postCount, Topic.toTop, Topic.toBottom, Topic.navigatorCallback, Topic.calculateIndex);socket.on('event:new_post', onNewPost);socket.on('event:new_notification', onNewNotification);@@ -80,8 +80,11 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT};Topic.toBottom = function() {- socket.emit('topics.lastPostIndex', ajaxify.variables.get('topic_id'), function(err, index) {- navigator.scrollBottom(index);+ socket.emit('topics.postcount', ajaxify.variables.get('topic_id'), function(err, postCount) {+ if (config.topicPostSort !== 'oldest_to_newest') {+ postCount = 1;+ }+ navigator.scrollBottom(postCount);});};@@ -201,8 +204,23 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT}}- Topic.navigatorCallback = function(element) {+ Topic.calculateIndex = function(index, elementCount) {+ if (index !== 1 && config.topicPostSort !== 'oldest_to_newest') {+ return elementCount - index + 2;+ }+ return index;+ };++ Topic.navigatorCallback = function(element, elementCount) {var postIndex = parseInt(element.attr('data-index'), 10);+ var index = postIndex + 1;+ if (config.topicPostSort !== 'oldest_to_newest') {+ if (postIndex === 0) {+ index = 1;+ } else {+ index = Math.max(elementCount - postIndex + 1, 1);+ }+ }var currentBookmark = localStorage.getItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark');@@ -230,6 +248,7 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadTcurrentUrl = newUrl;}}+ return index;};function onNewPostPagination(data) {@@ -247,8 +266,9 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT}function createNewPosts(data, callback) {+ callback = callback || function() {};if(!data || (data.posts && !data.posts.length)) {- return;+ return callback(false);}function removeAlreadyAddedPosts() {@@ -297,7 +317,7 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadTremoveAlreadyAddedPosts();if(!data.posts.length) {- return;+ return callback(false);}findInsertionPoint();@@ -320,9 +340,7 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT$(window).trigger('action:posts.loaded');onNewPostsLoaded(html, data.posts);- if (typeof callback === 'function') {- callback();- }+ callback(true);});}@@ -373,7 +391,9 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadTreturn;}- infinitescroll.calculateAfter(direction, '#post-container .post-row[data-index!="0"]', config.postsPerPage, function(after, offset, el) {+ var reverse = config.topicPostSort === 'newest_to_oldest' || config.topicPostSort === 'most_votes';++ infinitescroll.calculateAfter(direction, '#post-container .post-row[data-index!="0"]', config.postsPerPage, reverse, function(after, offset, el) {loadPostsAfter(after, function() {if (direction < 0 && el) {navigator.scrollToPost(el.attr('data-index'), false, 0, offset);@@ -401,9 +421,11 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadTindicatorEl.fadeOut();if (data && data.posts && data.posts.length) {- createNewPosts(data, function() {+ createNewPosts(data, function(postsCreated) {done();- callback();+ if (postsCreated) {+ callback();+ }});hidePostToolsForDeletedPosts();} else {diff --git a/public/src/modules/navigator.js b/public/src/modules/navigator.jsindex 88e3934128..3392a7bd83 100644--- a/public/src/modules/navigator.js+++ b/public/src/modules/navigator.js@@ -11,8 +11,8 @@ define('navigator', ['forum/pagination'], function(pagination) {var count = 0;navigator.scrollActive = false;- navigator.init = function(selector, count, callback, toTop, toBottom) {-+ navigator.init = function(selector, count, toTop, toBottom, callback, calculateIndex) {+ index = 1;navigator.selector = selector;navigator.callback = callback;toTop = toTop || function() {};@@ -36,7 +36,13 @@ define('navigator', ['forum/pagination'], function(pagination) {input.val('');return;}- var url = generateUrl(input.val());++ var index = parseInt(input.val(), 10);+ if (typeof calculateIndex === 'function') {+ index = calculateIndex(index, count);+ }++ var url = generateUrl(index);input.val('');$('.pagination-block .dropdown-toggle').trigger('click');ajaxify.go(url);@@ -76,12 +82,9 @@ define('navigator', ['forum/pagination'], function(pagination) {var el = $(this);if (elementInView(el)) {- index = parseInt(el.attr('data-index'), 10) + 1;-- navigator.updateTextAndProgressBar();-if (typeof navigator.callback === 'function') {- navigator.callback(el);+ index = navigator.callback(el, count);+ navigator.updateTextAndProgressBar();}return false;@@ -147,7 +150,7 @@ define('navigator', ['forum/pagination'], function(pagination) {return scrollToPid(postIndex, highlight, duration, offset);}- if(config.usePagination) {+ if (config.usePagination) {if (window.location.search.indexOf('page') !== -1) {navigator.update();return;@@ -162,12 +165,19 @@ define('navigator', ['forum/pagination'], function(pagination) {} else {scrollToPid(postIndex, highlight, duration, offset);}+ } else {+ navigator.scrollActive = false;}};function scrollToPid(postIndex, highlight, duration, offset) {var scrollTo = $('#post_anchor_' + postIndex);+ if (!scrollTo) {+ navigator.scrollActive = false;+ return;+ }+var done = false;function animateScroll() {$('html, body').animate({@@ -195,7 +205,7 @@ define('navigator', ['forum/pagination'], function(pagination) {}}- if ($('#post-container').length && scrollTo.length) {+ if ($('#post-container').length) {animateScroll();}}diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.jsindex efd84a7e19..2fdd5306bf 100644--- a/src/categories/recentreplies.js+++ b/src/categories/recentreplies.js@@ -20,11 +20,11 @@ module.exports = function(Categories) {return callback(err, []);}- posts.getPostSummaryByPids(pids, {stripTags: true}, callback);+ posts.getPostSummaryByPids(pids, uid, {stripTags: true}, callback);});};- Categories.getRecentTopicReplies = function(categoryData, callback) {+ Categories.getRecentTopicReplies = function(categoryData, uid, callback) {async.map(categoryData, getRecentTopicPids, function(err, results) {var pids = _.flatten(results);@@ -32,7 +32,7 @@ module.exports = function(Categories) {return !!pid && array.indexOf(pid) === index;});- posts.getPostSummaryByPids(pids, {stripTags: true}, function(err, posts) {+ posts.getPostSummaryByPids(pids, uid, {stripTags: true}, function(err, posts) {if (err) {return callback(err);}diff --git a/src/controllers/groups.js b/src/controllers/groups.jsindex a7295bdd7e..9185e6e1aa 100644--- a/src/controllers/groups.js+++ b/src/controllers/groups.js@@ -17,6 +17,7 @@ groupsController.list = function(req, res) {};groupsController.details = function(req, res) {+ var uid = req.user ? parseInt(req.user.uid, 10) : 0;async.parallel({group: function(next) {groups.get(req.params.name, {@@ -24,7 +25,7 @@ groupsController.details = function(req, res) {}, next);},posts: function(next) {- groups.getLatestMemberPosts(req.params.name, 10, next);+ groups.getLatestMemberPosts(req.params.name, 10, uid, next);}}, function(err, results) {if (!err) {diff --git a/src/controllers/index.js b/src/controllers/index.jsindex 8a6ca21459..7a03171ef2 100644--- a/src/controllers/index.js+++ b/src/controllers/index.js@@ -69,7 +69,7 @@ Controllers.home = function(req, res, next) {return next(err);}- categories.getRecentTopicReplies(categoryData, function(err) {+ categories.getRecentTopicReplies(categoryData, uid, function(err) {next(err, categoryData);});});diff --git a/src/controllers/topics.js b/src/controllers/topics.jsindex 0b20eb7af6..4bd80e7032 100644--- a/src/controllers/topics.js+++ b/src/controllers/topics.js@@ -30,20 +30,18 @@ topicsController.get = function(req, res, next) {userPrivileges = privileges;- user.getSettings(uid, next);+ async.parallel({+ postCount: function(next) {+ topics.getPostCount(tid, next);+ },+ settings: function(next) {+ user.getSettings(uid, next);+ }+ }, next);},- function (settings, next) {- var postIndex = 0;- if (!settings.usePagination) {- postIndex = Math.max((req.params.post_index || 1) - (settings.postsPerPage - 1), 0);- } else if (!req.query.page) {- var index = Math.max(parseInt((req.params.post_index || 0), 10), 0);- page = Math.ceil((index + 1) / settings.postsPerPage);- }-- var start = (page - 1) * settings.postsPerPage + postIndex,- end = start + settings.postsPerPage - 1;-+ function (results, next) {+ var settings = results.settings;+ var postCount = parseInt(results.postCount, 10) + 1;var set = 'tid:' + tid + ':posts',reverse = false;@@ -54,6 +52,24 @@ topicsController.get = function(req, res, next) {set = 'tid:' + tid + ':posts:votes';}+ var postIndex = 0;+ if (!settings.usePagination) {+ if (reverse) {+ if (!req.params.post_index || parseInt(req.params.post_index, 10) === 1) {+ req.params.post_index = 0;+ }+ postIndex = Math.max(postCount - (req.params.post_index || postCount) - (settings.postsPerPage - 1), 0);+ } else {+ postIndex = Math.max((req.params.post_index || 1) - (settings.postsPerPage + 1), 0);+ }+ } else if (!req.query.page) {+ var index = Math.max(parseInt(req.params.post_index, 10), 0);+ page = Math.ceil((index + 1) / settings.postsPerPage);+ }++ var start = (page - 1) * settings.postsPerPage + postIndex,+ end = start + settings.postsPerPage - 1;+topics.getTopicWithPosts(tid, set, uid, start, end, reverse, function (err, topicData) {if (topicData) {if (topicData.deleted && !userPrivileges.view_deleted) {@@ -191,6 +207,7 @@ topicsController.get = function(req, res, next) {topicsController.teaser = function(req, res, next) {var tid = req.params.topic_id;+ var uid = req.user ? parseInt(req.user.uid, 10) : 0;topics.getLatestUndeletedPid(tid, function(err, pid) {if (err) {return next(err);@@ -200,7 +217,7 @@ topicsController.teaser = function(req, res, next) {return res.json(404, 'not-found');}- posts.getPostSummaryByPids([pid], {stripTags: false}, function(err, posts) {+ posts.getPostSummaryByPids([pid], uid, {stripTags: false}, function(err, posts) {if (err) {return next(err);}diff --git a/src/groups.js b/src/groups.jsindex b47b069b8a..219fae8e45 100644--- a/src/groups.js+++ b/src/groups.js@@ -418,7 +418,7 @@});};- Groups.getLatestMemberPosts = function(groupName, max, callback) {+ Groups.getLatestMemberPosts = function(groupName, max, uid, callback) {Groups.get(groupName, {}, function(err, groupObj) {if (err || parseInt(groupObj.memberCount, 10) === 0) {return callback(null, []);@@ -433,7 +433,7 @@return callback(err);}- posts.getPostSummaryByPids(pids, {stripTags: false}, callback);+ posts.getPostSummaryByPids(pids, uid, {stripTags: false}, callback);});});};diff --git a/src/posts.js b/src/posts.jsindex 1392ac0372..419b7bd9ab 100644--- a/src/posts.js+++ b/src/posts.js@@ -186,7 +186,7 @@ var async = require('async'),if (err) {return callback(err);}- Posts.getPostSummaryByPids(pids, {stripTags: true}, callback);+ Posts.getPostSummaryByPids(pids, uid, {stripTags: true}, callback);});});};@@ -270,7 +270,7 @@ var async = require('async'),});};- Posts.getPostSummaryByPids = function(pids, options, callback) {+ Posts.getPostSummaryByPids = function(pids, uid, options, callback) {options.stripTags = options.hasOwnProperty('stripTags') ? options.stripTags : false;options.parse = options.hasOwnProperty('parse') ? options.parse : true;@@ -319,13 +319,7 @@ var async = require('async'),});},indices: function(next) {- var pids = [], keys = [];- for (var i=0; i<posts.length; ++i) {- pids.push(posts[i].pid);- keys.push('tid:' + posts[i].tid + ':posts');- }-- db.sortedSetsRanks(keys, pids, next);+ Posts.getPostIndices(posts, uid, next);}}, function(err, results) {function toObject(key, data) {@@ -345,7 +339,7 @@ var async = require('async'),results.categories = toObject('cid', results.topicsAndCategories.categories);for (var i=0; i<posts.length; ++i) {- posts[i].index = utils.isNumber(results.indices[i]) ? parseInt(results.indices[i], 10) + 2 : 1;+ posts[i].index = utils.isNumber(results.indices[i]) ? parseInt(results.indices[i], 10) + 1 : 1;}posts = posts.filter(function(post) {@@ -486,7 +480,7 @@ var async = require('async'),if (err) {return callback(err);}- getPostsFromSet('uid:' + uid + ':posts', pids, callback);+ getPostsFromSet('uid:' + uid + ':posts', pids, callerUid, callback);});});};@@ -497,16 +491,16 @@ var async = require('async'),return callback(err);}- getPostsFromSet('uid:' + uid + ':favourites', pids, callback);+ getPostsFromSet('uid:' + uid + ':favourites', pids, uid, callback);});};- function getPostsFromSet(set, pids, callback) {+ function getPostsFromSet(set, pids, uid, callback) {if (!Array.isArray(pids) || !pids.length) {return callback(null, {posts: [], nextStart: 0});}- Posts.getPostSummaryByPids(pids, {stripTags: false}, function(err, posts) {+ Posts.getPostSummaryByPids(pids, uid, {stripTags: false}, function(err, posts) {if (err) {return callback(err);}@@ -566,6 +560,38 @@ var async = require('async'),});};+ Posts.getPostIndices = function(posts, uid, callback) {+ user.getSettings(uid, function(err, settings) {+ if (err) {+ return callback(err);+ }+ var byVotes = settings.topicPostSort === 'most_votes';+ var sets = posts.map(function(post) {+ if (byVotes) {+ return 'tid:' + post.tid + ':posts:votes';+ } else {+ return 'tid:' + post.tid + ':posts';+ }+ });++ var pids = posts.map(function(post) {+ return post.pid;+ });++ db.sortedSetsRanks(sets, pids, function(err, indices) {+ if (err) {+ return callback(err);+ }++ for (var i=0; i<indices.length; ++i) {+ indices[i] = utils.isNumber(indices[i]) ? parseInt(indices[i], 10) + 1 : 0;+ }++ callback(null, indices);+ });+ });+ };+Posts.isOwner = function(pid, uid, callback) {uid = parseInt(uid, 10);if (Array.isArray(pid)) {diff --git a/src/search.js b/src/search.jsindex 808958ad73..63fa50c69b 100644--- a/src/search.js+++ b/src/search.js@@ -50,7 +50,7 @@ search.search = function(term, uid, callback) {return callback(err);}- posts.getPostSummaryByPids(pids, {stripTags: true, parse: false}, function(err, posts) {+ posts.getPostSummaryByPids(pids, uid, {stripTags: true, parse: false}, function(err, posts) {if (err) {return callback(err);}diff --git a/src/socket.io/topics.js b/src/socket.io/topics.jsindex 20d780923b..576c368f34 100644--- a/src/socket.io/topics.js+++ b/src/socket.io/topics.js@@ -70,10 +70,6 @@ SocketTopics.postcount = function(socket, tid, callback) {topics.getTopicField(tid, 'postcount', callback);};-SocketTopics.lastPostIndex = function(socket, tid, callback) {- db.sortedSetCard('tid:' + tid + ':posts', callback);-};-SocketTopics.increaseViewCount = function(socket, tid) {topics.increaseViewCount(tid);};@@ -421,6 +417,9 @@ SocketTopics.loadMore = function(socket, data, callback) {},privileges: function(next) {privileges.topics.get(data.tid, socket.uid, next);+ },+ postCount: function(next) {+ topics.getPostCount(data.tid, next);}}, function(err, results) {if (err) {@@ -431,19 +430,23 @@ SocketTopics.loadMore = function(socket, data, callback) {return callback(new Error('[[error:no-privileges]]'));}- var start = Math.max(parseInt(data.after, 10) - 1, 0),- end = start + results.settings.postsPerPage - 1;-var set = 'tid:' + data.tid + ':posts',- reverse = false;+ reverse = false,+ start = Math.max(parseInt(data.after, 10) - 1, 0);if (results.settings.topicPostSort === 'newest_to_oldest') {reverse = true;+ data.after = results.postCount - data.after;+ start = Math.max(parseInt(data.after, 10), 0);} else if (results.settings.topicPostSort === 'most_votes') {reverse = true;+ data.after = results.postCount - data.after;+ start = Math.max(parseInt(data.after, 10), 0);set = 'tid:' + data.tid + ':posts:votes';}+ var end = start + results.settings.postsPerPage - 1;+async.parallel({posts: function(next) {topics.getTopicPosts(data.tid, set, start, end, socket.uid, reverse, next);diff --git a/src/topics.js b/src/topics.jsindex 34fe471163..8ef34f8975 100644--- a/src/topics.js+++ b/src/topics.js@@ -199,7 +199,7 @@ var async = require('async'),privileges.categories.isAdminOrMod(cids, uid, next);},teasers: function(next) {- Topics.getTeasers(tids, next);+ Topics.getTeasers(tids, uid, next);},tags: function(next) {Topics.getTopicsTagsObjects(tids, next);@@ -256,7 +256,7 @@ var async = require('async'),}async.parallel({- posts: function(next) {+ posts: function(next) {posts.getPidsFromSet(set, start, end, reverse, function(err, pids) {if (err) {return next(err);@@ -270,11 +270,7 @@ var async = require('async'),if (err) {return next(err);}- start = parseInt(start, 10);- for(var i=0; i<posts.length; ++i) {- posts[i].index = start + i;- }- posts[0].index = 0;+Topics.addPostData(posts, uid, next);});});@@ -332,7 +328,7 @@ var async = require('async'),});};- Topics.getTeasers = function(tids, callback) {+ Topics.getTeasers = function(tids, uid, callback) {if(!Array.isArray(tids)) {return callback(null, []);}@@ -350,51 +346,41 @@ var async = require('async'),return 'post:' + pid;});- async.parallel({- indices: function(next) {- var sets = tids.map(function(tid) {- return 'tid:' + tid + ':posts';- });- db.sortedSetsRanks(sets, pids, next);- },- posts: function(next) {- db.getObjectsFields(postKeys, ['pid', 'uid', 'timestamp'], next);- }- }, function(err, results) {+ db.getObjectsFields(postKeys, ['pid', 'uid', 'timestamp', 'tid'], function(err, postData) {if (err) {return callback(err);}- var indices = results.indices.map(function(index) {- if (!utils.isNumber(index)) {- return 1;- }- return parseInt(index, 10) + 2;- });-- var uids = results.posts.map(function(post) {+ var uids = postData.map(function(post) {return post.uid;}).filter(function(uid, index, array) {return array.indexOf(uid) === index;});- user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], function(err, userData) {+ async.parallel({+ users: function(next) {+ user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);+ },+ indices: function(next) {+ posts.getPostIndices(postData, uid, next);+ }+ }, function(err, results) {if (err) {return callback(err);}var users = {};- userData.forEach(function(user) {+ results.users.forEach(function(user) {users[user.uid] = user;});- results.posts.forEach(function(post, index) {+ postData.forEach(function(post, index) {post.user = users[post.uid];- post.index = indices[index];+ post.index = results.indices[index] + 1;post.timestamp = utils.toISOString(post.timestamp);});- callback(err, results.posts);+ callback(null, postData);});});});diff --git a/src/topics/posts.js b/src/topics/posts.jsindex 98934a2f33..9170cd4142 100644--- a/src/topics/posts.js+++ b/src/topics/posts.js@@ -32,11 +32,6 @@ module.exports = function(Topics) {return callback(null, []);}- start = parseInt(start, 10);- for(var i=0; i<postData.length; ++i) {- postData[i].index = start + i + 1;- }-Topics.addPostData(postData, uid, callback);});};@@ -96,6 +91,9 @@ module.exports = function(Topics) {},privileges: function(next) {privileges.posts.get(pids, uid, next);+ },+ indices: function(next) {+ posts.getPostIndices(postData, uid, next);}}, function(err, results) {if(err) {@@ -103,6 +101,7 @@ module.exports = function(Topics) {}for (var i = 0; i < postData.length; ++i) {+ postData[i].index = results.indices[i];postData[i].deleted = parseInt(postData[i].deleted, 10) === 1;postData[i].user = results.userData[postData[i].uid];postData[i].editor = postData[i].editor ? results.editors[postData[i].editor] : null;