diff --git a/package.json b/package.json index f76f262134..73e81c4b63 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "nconf": "~0.6.7", "nodebb-plugin-dbsearch": "0.0.13", "nodebb-plugin-markdown": "~0.5.0", - "nodebb-plugin-mentions": "~0.5.0", + "nodebb-plugin-mentions": "~0.6.0", "nodebb-plugin-soundpack-default": "~0.1.1", "nodebb-theme-lavender": "~0.0.74", "nodebb-theme-vanilla": "~0.0.111", diff --git a/src/controllers/index.js b/src/controllers/index.js index 8f1eb2ff2f..7d7ea579ac 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -98,6 +98,8 @@ Controllers.search = function(req, res, next) { return res.redirect('/404'); } + req.params.term = req.params.term.replace(/"/g, '/"'); + search.search(req.params.term, uid, function(err, results) { if (err) { return next(err); diff --git a/src/controllers/topics.js b/src/controllers/topics.js index e482efd412..bfc2b4e396 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -16,6 +16,7 @@ var topicsController = {}, topicsController.get = function(req, res, next) { var tid = req.params.topic_id, page = req.query.page || 1, + sort = req.query.sort, uid = req.user ? req.user.uid : 0, userPrivileges; @@ -45,7 +46,15 @@ topicsController.get = function(req, res, next) { var set = 'tid:' + tid + ':posts', reverse = false; - if (settings.topicPostSort === 'newest_to_oldest') { + // `sort` qs has priority over user setting + if (sort === 'oldest_to_newest') { + reverse = false; + } else if (sort === 'newest_to_oldest') { + reverse = true; + } else if (sort === 'most_votes') { + reverse = true; + set = 'tid:' + tid + ':posts:votes'; + } else if (settings.topicPostSort === 'newest_to_oldest') { reverse = true; } else if (settings.topicPostSort === 'most_votes') { reverse = true; diff --git a/src/posts.js b/src/posts.js index 10d299b4b3..57a97e71ca 100644 --- a/src/posts.js +++ b/src/posts.js @@ -25,6 +25,7 @@ var async = require('async'), websockets = require('./socket.io'); (function(Posts) { + require('./posts/recent')(Posts); require('./posts/delete')(Posts); Posts.create = function(data, callback) { @@ -160,68 +161,6 @@ var async = require('async'), }); }; - Posts.getRecentPosts = function(uid, start, stop, term, callback) { - var terms = { - day: 86400000, - week: 604800000, - month: 2592000000 - }; - - var since = terms.day; - if (terms[term]) { - since = terms[term]; - } - - var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1; - - db.getSortedSetRevRangeByScore('posts:pid', start, count, Infinity, Date.now() - since, function(err, pids) { - if(err) { - return callback(err); - } - - 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); - }); - }); - }; - - Posts.getRecentPosterUids = function(start, end, callback) { - db.getSortedSetRevRange('posts:pid', start, end, function(err, pids) { - if (err) { - 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) { - if (err) { - return callback(err); - } - - postData = postData.map(function(post) { - return post && post.uid; - }).filter(function(value, index, array) { - return value && array.indexOf(value) === index; - }); - - callback(null, postData); - }); - }); - }; - Posts.getUserInfoForPosts = function(uids, callback) { async.parallel({ groups: function(next) { @@ -532,10 +471,20 @@ var async = require('async'), } Posts.getPidIndex = function(pid, uid, callback) { - callback = callback || function() {}; + // Making uid optional + if ((!uid && !callback) || typeof uid === 'function') { + callback = uid || function() {}; + uid = undefined; + } + async.parallel({ settings: function(next) { - user.getSettings(uid, next); + if (uid) { + user.getSettings(uid, next); + } else { + // No uid specified, so return empty object so that the check below will assume regular topic post sorting + next(null, {}); + } }, tid: function(next) { Posts.getPostField(pid, 'tid', next); diff --git a/src/posts/recent.js b/src/posts/recent.js new file mode 100644 index 0000000000..7f3b43314e --- /dev/null +++ b/src/posts/recent.js @@ -0,0 +1,70 @@ +'use strict'; + +var db = require('../database'), + privileges = require('../privileges'); + + +module.exports = function(Posts) { + var terms = { + day: 86400000, + week: 604800000, + month: 2592000000 + }; + + Posts.getRecentPosts = function(uid, start, stop, term, callback) { + var since = terms.day; + if (terms[term]) { + since = terms[term]; + } + + var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1; + + db.getSortedSetRevRangeByScore('posts:pid', start, count, Infinity, Date.now() - since, function(err, pids) { + if (err) { + return callback(err); + } + + 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); + }); + }); + }; + + Posts.getRecentPosterUids = function(start, end, callback) { + db.getSortedSetRevRange('posts:pid', start, end, function(err, pids) { + if (err) { + 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) { + if (err) { + return callback(err); + } + + postData = postData.map(function(post) { + return post && post.uid; + }).filter(function(value, index, array) { + return value && array.indexOf(value) === index; + }); + + callback(null, postData); + }); + }); + }; + +}; diff --git a/src/sitemap.js b/src/sitemap.js index 5db793a128..288d75bb13 100644 --- a/src/sitemap.js +++ b/src/sitemap.js @@ -49,7 +49,7 @@ var path = require('path'), }, function(next) { var topicUrls = []; - topics.getTopicsFromSet(0, 'topics:recent', 0, -1, function(err, data) { + topics.getTopicsFromSet(0, 'topics:recent', 0, 49, function(err, data) { if (err) { return next(err); } diff --git a/src/topics/popular.js b/src/topics/popular.js index 167bf1cb83..2dadb9fee6 100644 --- a/src/topics/popular.js +++ b/src/topics/popular.js @@ -7,15 +7,15 @@ var async = require('async'), module.exports = function(Topics) { + var terms = { + daily: 'day', + weekly: 'week', + monthly: 'month', + yearly: 'year' + }; Topics.getPopular = function(term, uid, count, callback) { count = parseInt(count, 10) || 20; - var terms = { - daily: 'day', - weekly: 'week', - monthly: 'month', - yearly: 'year' - }; if (term === 'alltime') { return getAllTimePopular(uid, count, callback); diff --git a/src/topics/recent.js b/src/topics/recent.js index 99ff00107b..8a3d8011e8 100644 --- a/src/topics/recent.js +++ b/src/topics/recent.js @@ -5,6 +5,12 @@ var db = require('./../database'); module.exports = function(Topics) { + var terms = { + day: 86400000, + week: 604800000, + month: 2592000000, + year: 31104000000 + }; Topics.getLatestTopics = function(uid, start, end, term, callback) { Topics.getLatestTids(start, end, term, function(err, tids) { @@ -17,15 +23,8 @@ module.exports = function(Topics) { }; Topics.getLatestTids = function(start, end, term, callback) { - var terms = { - day: 86400000, - week: 604800000, - month: 2592000000, - year: 31104000000 - }; - var since = terms.day; - if(terms[term]) { + if (terms[term]) { since = terms[term]; } diff --git a/src/user/settings.js b/src/user/settings.js index 2cf9fba084..8f24f54a05 100644 --- a/src/user/settings.js +++ b/src/user/settings.js @@ -28,8 +28,8 @@ module.exports = function(User) { settings.openOutgoingLinksInNewTab = settings.openOutgoingLinksInNewTab ? parseInt(settings.openOutgoingLinksInNewTab, 10) !== 0 : false; settings.dailyDigestFreq = settings.dailyDigestFreq || 'off'; settings.usePagination = settings.usePagination ? parseInt(settings.usePagination, 10) === 1 : parseInt(meta.config.usePagination, 10) === 1; - settings.topicsPerPage = settings.topicsPerPage ? parseInt(settings.topicsPerPage, 10) : parseInt(meta.config.topicsPerPage, 10) || 20; - settings.postsPerPage = settings.postsPerPage ? parseInt(settings.postsPerPage, 10) : parseInt(meta.config.postsPerPage, 10) || 10; + settings.topicsPerPage = Math.min(settings.topicsPerPage ? parseInt(settings.topicsPerPage, 10) : parseInt(meta.config.topicsPerPage, 10) || 20, 20); + settings.postsPerPage = Math.min(settings.postsPerPage ? parseInt(settings.postsPerPage, 10) : parseInt(meta.config.postsPerPage, 10) || 10, 20); settings.notificationSounds = settings.notificationSounds ? parseInt(settings.notificationSounds, 10) === 1 : true; settings.language = settings.language || meta.config.defaultLang || 'en_GB'; settings.topicPostSort = settings.topicPostSort || meta.config.topicPostSort || 'oldest_to_newest'; @@ -79,8 +79,8 @@ module.exports = function(User) { openOutgoingLinksInNewTab: data.openOutgoingLinksInNewTab, dailyDigestFreq: data.dailyDigestFreq || 'off', usePagination: data.usePagination, - topicsPerPage: data.topicsPerPage, - postsPerPage: data.postsPerPage, + topicsPerPage: Math.min(data.topicsPerPage, 20), + postsPerPage: Math.min(data.postsPerPage, 20), notificationSounds: data.notificationSounds, language: data.language || meta.config.defaultLang, followTopicsOnCreate: data.followTopicsOnCreate,