Node.js based forum software built for the modern web
user selectable pagination
Baris Soner Usakli242927d702b5
diff --git a/public/src/forum/accountsettings.js b/public/src/forum/accountsettings.jsindex db21bb4efa..ea906036af 100644--- a/public/src/forum/accountsettings.js+++ b/public/src/forum/accountsettings.js@@ -7,7 +7,10 @@ define(['forum/accountheader'], function(header) {$('#submitBtn').on('click', function() {var settings = {- showemail: $('#showemailCheckBox').is(':checked') ? 1 : 0+ showemail: $('#showemailCheckBox').is(':checked') ? 1 : 0,+ usePagination: $('#usePaginationCheckBox').is(':checked') ? 1 : 0,+ topicsPerPage: $('#topicsPerPage').val(),+ postsPerPage: $('#postsPerPage').val()};socket.emit('user.saveSettings', settings, function(err) {diff --git a/public/templates/accountsettings.tpl b/public/templates/accountsettings.tplindex 3e8f592d5e..b6c3e62c96 100644--- a/public/templates/accountsettings.tpl+++ b/public/templates/accountsettings.tpl@@ -6,16 +6,31 @@<div class="row"><div class="col-md-6">- <h4>privacy</h4>- <div class="checkbox">- <label>- <input id="showemailCheckBox" type="checkbox" {showemail}> [[user:show_email]]- </label>- </div>+ <div class="panel panel-default">+ <div class="panel-body">+ <h4>privacy</h4>+ <div class="checkbox">+ <label>+ <input id="showemailCheckBox" type="checkbox" <!-- IF settings.showemail -->checked<!-- ENDIF settings.showemail --> > [[user:show_email]]+ </label>+ </div>+ </div>+ </div></div><div class="col-md-6">+ <div class="panel panel-default">+ <div class="panel-body">+ <div class="checkbox">+ <label>+ <input id="usePaginationCheckBox" type="checkbox" <!-- IF settings.usePagination -->checked<!-- ENDIF settings.usePagination -->> <strong>Paginate topics and posts instead of using infinite scroll.</strong>+ </label>+ </div>+ <strong>Topics per Page</strong><br /> <input id="topicsPerPage" type="text" class="form-control" value="{settings.topicsPerPage}"><br />+ <strong>Posts per Page</strong><br /> <input id="postsPerPage" type="text" class="form-control" value="{settings.postsPerPage}"><br />+ </div>+ </div></div></div><div class="form-actions">diff --git a/src/categories.js b/src/categories.jsindex 1714b6a9aa..b998701048 100644--- a/src/categories.js+++ b/src/categories.js@@ -77,7 +77,7 @@ var db = require('./database'),}function getPageCount(next) {- Categories.getPageCount(category_id, next);+ Categories.getPageCount(category_id, current_user, next);}async.parallel({@@ -146,16 +146,19 @@ var db = require('./database'),db.getSortedSetRevRange('categories:' + cid + ':tid', start, stop, callback);};- Categories.getPageCount = function(cid, callback) {+ Categories.getPageCount = function(cid, uid, callback) {db.sortedSetCard('categories:' + cid + ':tid', function(err, topicCount) {if(err) {return callback(err);}- var topicsPerPage = parseInt(meta.config.topicsPerPage, 10);- topicsPerPage = topicsPerPage ? topicsPerPage : 20;+ user.getSettings(uid, function(err, settings) {+ if(err) {+ return callback(err);+ }- callback(null, Math.ceil(parseInt(topicCount, 10) / topicsPerPage));+ callback(null, Math.ceil(parseInt(topicCount, 10) / settings.topicsPerPage));+ });});};diff --git a/src/routes/api.js b/src/routes/api.jsindex bb27b2c199..4b9c0d318f 100644--- a/src/routes/api.js+++ b/src/routes/api.js@@ -52,7 +52,22 @@ var path = require('path'),config.defaultLang = meta.config.defaultLang || 'en_GB';config.environment = process.env.NODE_ENV;- res.json(200, config);+ if (!req.user) {+ return res.json(200, config);+ }++ if(req.user) {+ user.getSettings(req.user.uid, function(err, settings) {+ if(err) {+ return next(err);+ }++ config.usePagination = settings.usePagination;+ config.topicsPerPage = settings.topicsPerPage;+ config.postsPerPage = settings.postsPerPage;+ res.json(200, config);+ });+ }});app.get('/home', function (req, res) {@@ -189,33 +204,38 @@ var path = require('path'),return res.send(404);}- var postsPerPage = parseInt(meta.config.postsPerPage ? meta.config.postsPerPage : 20, 10);- var start = (page - 1) * postsPerPage;- var end = start + postsPerPage - 1;+ user.getSettings(uid, function(err, settings) {+ if(err) {+ return next(err);+ }- ThreadTools.privileges(req.params.id, uid, function(err, privileges) {- if (privileges.read) {- topics.getTopicWithPosts(req.params.id, uid, start, end, false, function (err, data) {- if(err) {- return next(err);- }+ var start = (page - 1) * settings.postsPerPage;+ var end = start + settings.postsPerPage - 1;- if(page > data.pageCount) {- return res.send(404);- }+ ThreadTools.privileges(req.params.id, uid, function(err, privileges) {+ if (privileges.read) {+ topics.getTopicWithPosts(req.params.id, uid, start, end, false, function (err, data) {+ if(err) {+ return next(err);+ }- data.currentPage = page;- data.privileges = privileges;+ if(page > data.pageCount) {+ return res.send(404);+ }- if (parseInt(data.deleted, 10) === 1 && parseInt(data.expose_tools, 10) === 0) {- return res.json(404, {});- }+ data.currentPage = page;+ data.privileges = privileges;- res.json(data);- });- } else {- res.send(403);- }+ if (parseInt(data.deleted, 10) === 1 && parseInt(data.expose_tools, 10) === 0) {+ return res.json(404, {});+ }++ res.json(data);+ });+ } else {+ res.send(403);+ }+ });});});@@ -230,30 +250,34 @@ var path = require('path'),return res.send(404);}- var topicsPerPage = parseInt(meta.config.topicsPerPage ? meta.config.topicsPerPage : 20, 10);- var start = (page - 1) * topicsPerPage;- var end = start + topicsPerPage - 1;+ user.getSettings(uid, function(err, settings) {+ if(err) {+ return next(err);+ }- // Category Whitelisting- categoryTools.privileges(req.params.id, uid, function(err, privileges) {- if (!err && privileges.read) {- categories.getCategoryById(req.params.id, start, end, uid, function (err, data) {- if(err) {- return next(err);- }+ var start = (page - 1) * settings.topicsPerPage;+ var end = start + settings.topicsPerPage - 1;- data.currentPage = page;- data.privileges = privileges;+ categoryTools.privileges(req.params.id, uid, function(err, privileges) {+ if (!err && privileges.read) {+ categories.getCategoryById(req.params.id, start, end, uid, function (err, data) {+ if(err) {+ return next(err);+ }- if (data && parseInt(data.disabled, 10) === 0) {- res.json(data);- } else {- next();- }- }, req.params.id, uid);- } else {- res.send(403);- }+ data.currentPage = page;+ data.privileges = privileges;++ if (data && parseInt(data.disabled, 10) === 0) {+ res.json(data);+ } else {+ next();+ }+ }, req.params.id, uid);+ } else {+ res.send(403);+ }+ });});});diff --git a/src/routes/user.js b/src/routes/user.jsindex 61dc5a26af..07a6947e68 100644--- a/src/routes/user.js+++ b/src/routes/user.js@@ -270,40 +270,43 @@ var fs = require('fs'),var callerUID = req.user ? req.user.uid : '0';user.getUidByUserslug(req.params.userslug, function (err, uid) {+ if (err) {+ return next(err);+ }+if (!uid) {- res.json(404, {+ return res.json(404, {error: 'User not found!'});- return;}if (uid != callerUID || callerUID == '0') {- res.json(403, {+ return res.json(403, {error: 'Not allowed!'});- return;}-- user.getUserFields(uid, ['username', 'userslug', 'showemail'], function (err, userData) {- if (err)+ user.getUserFields(uid, ['username', 'userslug'], function (err, userData) {+ if (err) {return next(err);+ }- if (userData) {- if (userData.showemail && parseInt(userData.showemail, 10) === 1) {- userData.showemail = "checked";- } else {- userData.showemail = "";+ if(!userData) {+ return res.json(404, {+ error: 'User not found!'+ });+ }++ user.getSettings(uid, function(err, settings) {+ if(err) {+ return next(err);}userData.theirid = uid;userData.yourid = callerUID;+ userData.settings = settings;res.json(userData);- } else {- res.json(404, {- error: 'User not found!'- });- }+ });});});});@@ -313,17 +316,15 @@ var fs = require('fs'),user.getUidByUserslug(req.params.userslug, function (err, uid) {if (!uid) {- res.json(404, {+ return res.json(404, {error: 'User not found!'});- return;}if (uid != callerUID || callerUID == '0') {- res.json(403, {+ return res.json(403, {error: 'Not allowed!'});- return;}user.getUserFields(uid, ['username', 'userslug'], function (err, userData) {@@ -331,24 +332,24 @@ var fs = require('fs'),return next(err);}- if (userData) {- posts.getFavourites(uid, 0, 9, function (err, favourites) {- if (err) {- return next(err);- }-- userData.theirid = uid;- userData.yourid = callerUID;- userData.posts = favourites.posts;- userData.nextStart = favourites.nextStart;-- res.json(userData);- });- } else {- res.json(404, {+ if (!userData) {+ return res.json(404, {error: 'User not found!'});}++ posts.getFavourites(uid, 0, 9, function (err, favourites) {+ if (err) {+ return next(err);+ }++ userData.theirid = uid;+ userData.yourid = callerUID;+ userData.posts = favourites.posts;+ userData.nextStart = favourites.nextStart;++ res.json(userData);+ });});});});@@ -358,10 +359,9 @@ var fs = require('fs'),user.getUidByUserslug(req.params.userslug, function (err, uid) {if (!uid) {- res.json(404, {+ return res.json(404, {error: 'User not found!'});- return;}user.getUserFields(uid, ['username', 'userslug'], function (err, userData) {@@ -369,24 +369,24 @@ var fs = require('fs'),return next(err);}- if (userData) {- posts.getPostsByUid(callerUID, uid, 0, 19, function (err, userPosts) {- if (err) {- return next(err);- }- userData.uid = uid;- userData.theirid = uid;- userData.yourid = callerUID;- userData.posts = userPosts.posts;- userData.nextStart = userPosts.nextStart;-- res.json(userData);- });- } else {- res.json(404, {+ if (!userData) {+ return res.json(404, {error: 'User not found!'});}++ posts.getPostsByUid(callerUID, uid, 0, 19, function (err, userPosts) {+ if (err) {+ return next(err);+ }+ userData.uid = uid;+ userData.theirid = uid;+ userData.yourid = callerUID;+ userData.posts = userPosts.posts;+ userData.nextStart = userPosts.nextStart;++ res.json(userData);+ });});});});diff --git a/src/socket.io/categories.js b/src/socket.io/categories.jsindex ddea33d2f3..2e08d5bb54 100644--- a/src/socket.io/categories.js+++ b/src/socket.io/categories.js@@ -1,5 +1,6 @@var categories = require('../categories'),meta = require('./../meta'),+ user = require('./../user'),SocketCategories = {};@@ -16,16 +17,17 @@ SocketCategories.loadMore = function(socket, data, callback) {return callback(new Error('invalid data'));}- var topicsPerPage = parseInt(meta.config.topicsPerPage, 10) || 20;+ user.getSettings(socket.uid, function(err, settings) {- var start = parseInt(data.after, 10),- end = start + topicsPerPage - 1;+ var start = parseInt(data.after, 10),+ end = start + settings.topicsPerPage - 1;- categories.getCategoryTopics(data.cid, start, end, socket.uid, callback);+ categories.getCategoryTopics(data.cid, start, end, socket.uid, callback);+ });};SocketCategories.getPageCount = function(socket, cid, callback) {- categories.getPageCount(cid, callback);-}+ categories.getPageCount(cid, socket.uid, callback);+};module.exports = SocketCategories;\ No newline at end of filediff --git a/src/socket.io/topics.js b/src/socket.io/topics.jsindex 381cbc13ea..d78968c512 100644--- a/src/socket.io/topics.js+++ b/src/socket.io/topics.js@@ -1,6 +1,7 @@var topics = require('../topics'),threadTools = require('../threadTools'),index = require('./index'),+ user = require('../user'),async = require('async'),@@ -233,21 +234,21 @@ SocketTopics.loadMore = function(socket, data, callback) {return callback(new Error('invalid data'));}- var postsPerPage = parseInt(meta.config.postsPerPage, 10);- postsPerPage = postsPerPage ? postsPerPage : 20;+ user.getSettings(socket.uid, function(err, settings) {- var start = parseInt(data.after, 10),- end = start + postsPerPage - 1;-- async.parallel({- posts: function(next) {- topics.getTopicPosts(data.tid, start, end, socket.uid, next);- },- privileges: function(next) {- threadTools.privileges(data.tid, socket.uid, next);- }- }, function(err, results) {- callback(err, results);+ var start = parseInt(data.after, 10),+ end = start + settings.postsPerPage - 1;++ async.parallel({+ posts: function(next) {+ topics.getTopicPosts(data.tid, start, end, socket.uid, next);+ },+ privileges: function(next) {+ threadTools.privileges(data.tid, socket.uid, next);+ }+ }, function(err, results) {+ callback(err, results);+ });});};@@ -286,7 +287,7 @@ SocketTopics.loadMoreFromSet = function(socket, data, callback) {SocketTopics.getPageCount = function(socket, tid, callback) {- topics.getPageCount(tid, callback);+ topics.getPageCount(tid, socket.uid, callback);};module.exports = SocketTopics;\ No newline at end of filediff --git a/src/socket.io/user.js b/src/socket.io/user.jsindex f560151512..c9a9bed657 100644--- a/src/socket.io/user.js+++ b/src/socket.io/user.js@@ -135,9 +135,7 @@ SocketUser.unfollow = function(socket, data, callback) {SocketUser.saveSettings = function(socket, data, callback) {if (socket.uid && data) {- user.setUserFields(socket.uid, {- showemail: data.showemail- }, callback);+ user.saveSettings(socket.uid, data, callback);}};diff --git a/src/topics.js b/src/topics.jsindex 79a2423252..9c4fb78010 100644--- a/src/topics.js+++ b/src/topics.js@@ -391,16 +391,19 @@ var async = require('async'),});}- Topics.getPageCount = function(tid, callback) {+ Topics.getPageCount = function(tid, uid, callback) {db.sortedSetCard('tid:' + tid + ':posts', function(err, postCount) {if(err) {return callback(err);}- var postsPerPage = parseInt(meta.config.postsPerPage, 10);- postsPerPage = postsPerPage ? postsPerPage : 20;+ user.getSettings(uid, function(err, settings) {+ if(err) {+ return callback(err);+ }- callback(null, Math.ceil(parseInt(postCount, 10) / postsPerPage));+ callback(null, Math.ceil(parseInt(postCount, 10) / settings.postsPerPage));+ });});}@@ -808,7 +811,7 @@ var async = require('async'),}function getPageCount(next) {- Topics.getPageCount(tid, next);+ Topics.getPageCount(tid, current_user, next);}async.parallel([getTopicData, getTopicPosts, getPrivileges, getCategoryData, getPageCount], function(err, results) {diff --git a/src/user.js b/src/user.jsindex 846f6f5486..26d0a19f98 100644--- a/src/user.js+++ b/src/user.js@@ -184,6 +184,47 @@ var bcrypt = require('bcryptjs'),});};+ User.getSettings = function(uid, callback) {+ function sendDefaultSettings() {+ callback(null, {+ showemail: false,+ usePagination: parseInt(meta.config.usePagination, 10) !== 0,+ topicsPerPage: parseInt(meta.config.topicsPerPage, 10) || 20,+ postsPerPage: parseInt(meta.config.postsPerPage, 10) || 10+ });+ }++ if(!parseInt(uid, 10)) {+ return sendDefaultSettings();+ }++ db.getObject('user:' + uid + ':settings', function(err, settings) {+ if(err) {+ return callback(err);+ }++ if(!settings) {+ return sendDefaultSettings();+ }++ settings.showemail = parseInt(settings.showemail, 10) !== 0;+ settings.usePagination = parseInt(settings.usePagination, 10) !== 0;+ settings.topicsPerPage = parseInt(settings.topicsPerPage, 10);+ settings.postsPerPage = parseInt(settings.postsPerPage, 10);++ callback(null, settings);+ });+ }++ User.saveSettings = function(uid, data, callback) {+ db.setObject('user:' + uid + ':settings', {+ showemail: data.showemail || 0,+ usePagination: data.usePagination || 0,+ topicsPerPage: data.topicsPerPage || 20,+ postsPerPage: data.postsPerPage || 10+ }, callback);+ }+User.updateProfile = function(uid, data, callback) {var fields = ['username', 'email', 'fullname', 'website', 'location', 'birthday', 'signature'];