From 5cfe379841faa7b933d97f67d32f4242b8256725 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 3 Nov 2016 18:02:10 +0300 Subject: [PATCH] fix recent for inf scroll --- public/src/client/recent.js | 4 +- src/controllers/recent.js | 61 +++------------------- src/socket.io/topics/infinitescroll.js | 16 +++++- src/topics/recent.js | 72 ++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 58 deletions(-) diff --git a/public/src/client/recent.js b/public/src/client/recent.js index 346eaaaaa0..0fbf49c937 100644 --- a/public/src/client/recent.js +++ b/public/src/client/recent.js @@ -131,8 +131,10 @@ define('forum/recent', ['forum/infinitescroll', 'components'], function (infinit return; } - infinitescroll.loadMore('topics.loadMoreFromSet', { + infinitescroll.loadMore('topics.loadMoreRecentTopics', { after: $('[component="category"]').attr('data-nextstart'), + cid: utils.params().cid, + filter: ajaxify.data.selectedFilter.filter, set: $('[component="category"]').attr('data-set') ? $('[component="category"]').attr('data-set') : 'topics:recent' }, function (data, done) { if (data.topics && data.topics.length) { diff --git a/src/controllers/recent.js b/src/controllers/recent.js index 5e5ec06605..2da6e7d09c 100644 --- a/src/controllers/recent.js +++ b/src/controllers/recent.js @@ -5,8 +5,6 @@ var async = require('async'); var nconf = require('nconf'); var validator = require('validator'); -var db = require('../database'); -var privileges = require('../privileges'); var user = require('../user'); var topics = require('../topics'); var meta = require('../meta'); @@ -19,9 +17,7 @@ var validFilter = {'': true, 'new': true, 'watched': true}; recentController.get = function (req, res, next) { var page = parseInt(req.query.page, 10) || 1; - var pageCount = 1; var stop = 0; - var topicCount = 0; var settings; var cid = req.query.cid; var filter = req.params.filter || ''; @@ -37,9 +33,6 @@ recentController.get = function (req, res, next) { settings: function (next) { user.getSettings(req.uid, next); }, - tids: function (next) { - db.getSortedSetRevRange(cid ? 'cid:' + cid + ':tids' : 'topics:recent', 0, 199, next); - }, watchedCategories: function (next) { helpers.getWatchedCategories(req.uid, cid, next); } @@ -48,25 +41,17 @@ recentController.get = function (req, res, next) { function (results, next) { settings = results.settings; categoryData = results.watchedCategories; - filterTids(results.tids, req.uid, categoryData.categories, filter, next); - }, - function (tids, next) { + var start = Math.max(0, (page - 1) * settings.topicsPerPage); stop = start + settings.topicsPerPage - 1; - topicCount = tids.length; - pageCount = Math.max(1, Math.ceil(topicCount / settings.topicsPerPage)); - tids = tids.slice(start, stop + 1); - - topics.getTopicsByTids(tids, req.uid, next); + topics.getRecentTopics(cid, req.uid, start, stop, filter, next); } - ], function (err, topics) { + ], function (err, data) { if (err) { return next(err); } - var data = {}; - data.topics = topics; data.categories = categoryData.categories; data.selectedCategory = categoryData.selectedCategory; data.nextStart = stop + 1; @@ -95,50 +80,16 @@ recentController.get = function (req, res, next) { return filter && filter.selected; }); + var pageCount = Math.max(1, Math.ceil(data.topicCount / settings.topicsPerPage)); data.pagination = pagination.create(page, pageCount, req.query); + if (req.path.startsWith('/api/recent') || req.path.startsWith('/recent')) { data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[recent:title]]'}]); } + data.querystring = cid ? ('?cid=' + validator.escape(String(cid))) : ''; res.render('recent', data); }); }; -function filterTids(tids, uid, watchedCategories, filter, callback) { - async.waterfall([ - function (next) { - if (filter === 'watched') { - topics.filterWatchedTids(tids, uid, next); - } else if (filter === 'new') { - topics.filterNewTids(tids, uid, next); - } else { - topics.filterNotIgnoredTids(tids, uid, next); - } - }, - function (tids, next) { - privileges.topics.filterTids('read', tids, uid, next); - }, - function (tids, next) { - topics.getTopicsFields(tids, ['tid', 'cid'], next); - }, - function (topicData, next) { - var watchedCids = watchedCategories.map(function (category) { - return category && parseInt(category.cid, 10); - }); - - tids = topicData.filter(function (topic, index) { - if (topic) { - var topicCid = parseInt(topic.cid, 10); - return watchedCids.indexOf(topicCid) !== -1; - } else { - return false; - } - }).map(function (topic) { - return topic.tid; - }); - next(null, tids); - } - ], callback); -} - module.exports = recentController; \ No newline at end of file diff --git a/src/socket.io/topics/infinitescroll.js b/src/socket.io/topics/infinitescroll.js index 45c354620a..1c1b05f555 100644 --- a/src/socket.io/topics/infinitescroll.js +++ b/src/socket.io/topics/infinitescroll.js @@ -99,13 +99,25 @@ module.exports = function (SocketTopics) { topics.getUnreadTopics(data.cid, socket.uid, start, stop, data.filter, callback); }; + SocketTopics.loadMoreRecentTopics = function (socket, data, callback) { + if (!data || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0) { + return callback(new Error('[[error:invalid-data]]')); + } + + var start = parseInt(data.after, 10); + var stop = start + 9; + + topics.getRecentTopics(data.cid, socket.uid, start, stop, data.filter, callback); + }; + + SocketTopics.loadMoreFromSet = function (socket, data, callback) { if (!data || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0 || !data.set) { return callback(new Error('[[error:invalid-data]]')); } - var start = parseInt(data.after, 10), - stop = start + 9; + var start = parseInt(data.after, 10); + var stop = start + 9; topics.getTopicsFromSet(data.set, socket.uid, start, stop, callback); }; diff --git a/src/topics/recent.js b/src/topics/recent.js index 653ea372a7..421bb0cd71 100644 --- a/src/topics/recent.js +++ b/src/topics/recent.js @@ -5,6 +5,8 @@ var async = require('async'); var db = require('../database'); var plugins = require('../plugins'); +var privileges = require('../privileges'); +var user = require('../user'); module.exports = function (Topics) { var terms = { @@ -14,6 +16,76 @@ module.exports = function (Topics) { year: 31104000000 }; + Topics.getRecentTopics = function (cid, uid, start, stop, filter, callback) { + var recentTopics = { + nextStart : 0, + topics: [] + }; + + async.waterfall([ + function (next) { + db.getSortedSetRevRange(cid ? 'cid:' + cid + ':tids' : 'topics:recent', 0, 199, next); + }, + function (tids, next) { + filterTids(tids, uid, filter, next); + }, + function (tids, next) { + recentTopics.topicCount = tids.length; + tids = tids.slice(start, stop + 1); + Topics.getTopicsByTids(tids, uid, next); + }, + function (topicData, next) { + recentTopics.topics = topicData; + recentTopics.nextStart = stop + 1; + next(null, recentTopics); + } + ], callback); + }; + + + function filterTids(tids, uid, filter, callback) { + async.waterfall([ + function (next) { + if (filter === 'watched') { + Topics.filterWatchedTids(tids, uid, next); + } else if (filter === 'new') { + Topics.filterNewTids(tids, uid, next); + } else { + Topics.filterNotIgnoredTids(tids, uid, next); + } + }, + function (tids, next) { + privileges.topics.filterTids('read', tids, uid, next); + }, + function (tids, next) { + async.parallel({ + ignoredCids: function (next) { + if (filter === 'watched') { + return next(null, []); + } + user.getIgnoredCategories(uid, next); + }, + topicData: function (next) { + Topics.getTopicsFields(tids, ['tid', 'cid'], next); + } + }, next); + }, + function (results, next) { + tids = results.topicData.filter(function (topic) { + if (topic) { + return results.ignoredCids.indexOf(topic.cid.toString()) === -1; + } else { + return false; + } + }).map(function (topic) { + return topic.tid; + }); + next(null, tids); + } + ], callback); + } + + Topics.getLatestTopics = function (uid, start, stop, term, callback) { async.waterfall([ function (next) {