fix recent for inf scroll

v1.18.x
barisusakli 8 years ago
parent 5d9e8beb52
commit 5cfe379841

@ -131,8 +131,10 @@ define('forum/recent', ['forum/infinitescroll', 'components'], function (infinit
return; return;
} }
infinitescroll.loadMore('topics.loadMoreFromSet', { infinitescroll.loadMore('topics.loadMoreRecentTopics', {
after: $('[component="category"]').attr('data-nextstart'), 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' set: $('[component="category"]').attr('data-set') ? $('[component="category"]').attr('data-set') : 'topics:recent'
}, function (data, done) { }, function (data, done) {
if (data.topics && data.topics.length) { if (data.topics && data.topics.length) {

@ -5,8 +5,6 @@ var async = require('async');
var nconf = require('nconf'); var nconf = require('nconf');
var validator = require('validator'); var validator = require('validator');
var db = require('../database');
var privileges = require('../privileges');
var user = require('../user'); var user = require('../user');
var topics = require('../topics'); var topics = require('../topics');
var meta = require('../meta'); var meta = require('../meta');
@ -19,9 +17,7 @@ var validFilter = {'': true, 'new': true, 'watched': true};
recentController.get = function (req, res, next) { recentController.get = function (req, res, next) {
var page = parseInt(req.query.page, 10) || 1; var page = parseInt(req.query.page, 10) || 1;
var pageCount = 1;
var stop = 0; var stop = 0;
var topicCount = 0;
var settings; var settings;
var cid = req.query.cid; var cid = req.query.cid;
var filter = req.params.filter || ''; var filter = req.params.filter || '';
@ -37,9 +33,6 @@ recentController.get = function (req, res, next) {
settings: function (next) { settings: function (next) {
user.getSettings(req.uid, next); user.getSettings(req.uid, next);
}, },
tids: function (next) {
db.getSortedSetRevRange(cid ? 'cid:' + cid + ':tids' : 'topics:recent', 0, 199, next);
},
watchedCategories: function (next) { watchedCategories: function (next) {
helpers.getWatchedCategories(req.uid, cid, next); helpers.getWatchedCategories(req.uid, cid, next);
} }
@ -48,25 +41,17 @@ recentController.get = function (req, res, next) {
function (results, next) { function (results, next) {
settings = results.settings; settings = results.settings;
categoryData = results.watchedCategories; categoryData = results.watchedCategories;
filterTids(results.tids, req.uid, categoryData.categories, filter, next);
},
function (tids, next) {
var start = Math.max(0, (page - 1) * settings.topicsPerPage); var start = Math.max(0, (page - 1) * settings.topicsPerPage);
stop = start + settings.topicsPerPage - 1; stop = start + settings.topicsPerPage - 1;
topicCount = tids.length; topics.getRecentTopics(cid, req.uid, start, stop, filter, next);
pageCount = Math.max(1, Math.ceil(topicCount / settings.topicsPerPage));
tids = tids.slice(start, stop + 1);
topics.getTopicsByTids(tids, req.uid, next);
} }
], function (err, topics) { ], function (err, data) {
if (err) { if (err) {
return next(err); return next(err);
} }
var data = {};
data.topics = topics;
data.categories = categoryData.categories; data.categories = categoryData.categories;
data.selectedCategory = categoryData.selectedCategory; data.selectedCategory = categoryData.selectedCategory;
data.nextStart = stop + 1; data.nextStart = stop + 1;
@ -95,50 +80,16 @@ recentController.get = function (req, res, next) {
return filter && filter.selected; return filter && filter.selected;
}); });
var pageCount = Math.max(1, Math.ceil(data.topicCount / settings.topicsPerPage));
data.pagination = pagination.create(page, pageCount, req.query); data.pagination = pagination.create(page, pageCount, req.query);
if (req.path.startsWith('/api/recent') || req.path.startsWith('/recent')) { if (req.path.startsWith('/api/recent') || req.path.startsWith('/recent')) {
data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[recent:title]]'}]); data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[recent:title]]'}]);
} }
data.querystring = cid ? ('?cid=' + validator.escape(String(cid))) : ''; data.querystring = cid ? ('?cid=' + validator.escape(String(cid))) : '';
res.render('recent', data); 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; module.exports = recentController;

@ -99,13 +99,25 @@ module.exports = function (SocketTopics) {
topics.getUnreadTopics(data.cid, socket.uid, start, stop, data.filter, callback); 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) { SocketTopics.loadMoreFromSet = function (socket, data, callback) {
if (!data || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0 || !data.set) { if (!data || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0 || !data.set) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
var start = parseInt(data.after, 10), var start = parseInt(data.after, 10);
stop = start + 9; var stop = start + 9;
topics.getTopicsFromSet(data.set, socket.uid, start, stop, callback); topics.getTopicsFromSet(data.set, socket.uid, start, stop, callback);
}; };

@ -5,6 +5,8 @@
var async = require('async'); var async = require('async');
var db = require('../database'); var db = require('../database');
var plugins = require('../plugins'); var plugins = require('../plugins');
var privileges = require('../privileges');
var user = require('../user');
module.exports = function (Topics) { module.exports = function (Topics) {
var terms = { var terms = {
@ -14,6 +16,76 @@ module.exports = function (Topics) {
year: 31104000000 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) { Topics.getLatestTopics = function (uid, start, stop, term, callback) {
async.waterfall([ async.waterfall([
function (next) { function (next) {

Loading…
Cancel
Save