This commit is contained in:
barisusakli
2016-04-18 15:44:07 +03:00
parent 35ac64db7f
commit ec33a57f77
3 changed files with 98 additions and 53 deletions

View File

@@ -6,5 +6,8 @@
"selected": "Selected", "selected": "Selected",
"all": "All", "all": "All",
"all_categories": "All categories", "all_categories": "All categories",
"topics_marked_as_read.success": "Topics marked as read!" "topics_marked_as_read.success": "Topics marked as read!",
"all-topics": "All Topics",
"new-topics": "New Topics",
"watched-topics": "Watched Topics"
} }

View File

@@ -5,14 +5,14 @@ var async = require('async');
var meta = require('../meta'); var meta = require('../meta');
var categories = require('../categories'); var categories = require('../categories');
var privileges = require('../privileges'); var privileges = require('../privileges');
var user = require('../user') var user = require('../user');
var topics = require('../topics'); var topics = require('../topics');
var helpers = require('./helpers'); var helpers = require('./helpers');
var plugins = require('../plugins'); var plugins = require('../plugins');
var unreadController = {}; var unreadController = {};
var validFilter = {'': true, 'new': true}; var validFilter = {'': true, 'new': true, 'watched': true};
unreadController.get = function(req, res, next) { unreadController.get = function(req, res, next) {
var stop = (parseInt(meta.config.topicsPerList, 10) || 20) - 1; var stop = (parseInt(meta.config.topicsPerList, 10) || 20) - 1;
@@ -57,6 +57,23 @@ unreadController.get = function(req, res, next) {
results.unreadTopics.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[unread:title]]'}]); results.unreadTopics.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[unread:title]]'}]);
results.unreadTopics.title = '[[pages:unread]]'; results.unreadTopics.title = '[[pages:unread]]';
results.unreadTopics.filters = [{
name: '[[unread:all-topics]]',
url: 'unread',
selected: filter === ''
}, {
name: '[[unread:new-topics]]',
url: 'unread/new',
selected: filter === 'new'
}, {
name: '[[unread:watched-topics]]',
url: 'unread/watched',
selected: filter === 'watched'
}];
results.unreadTopics.selectedFilter = results.unreadTopics.filters.filter(function(filter) {
return filter && filter.selected;
})[0];
plugins.fireHook('filter:unread.build', {req: req, res: res, templateData: results.unreadTopics}, next); plugins.fireHook('filter:unread.build', {req: req, res: res, templateData: results.unreadTopics}, next);
} }

View File

@@ -31,7 +31,6 @@ module.exports = function(Topics) {
filter = ''; filter = '';
} }
var unreadTopics = { var unreadTopics = {
showSelect: true, showSelect: true,
nextStart : 0, nextStart : 0,
@@ -77,57 +76,71 @@ module.exports = function(Topics) {
var cutoff = Topics.unreadCutoff(); var cutoff = Topics.unreadCutoff();
async.parallel({ var ignoredCids;
ignoredCids: function(next) {
user.getIgnoredCategories(uid, next); async.waterfall([
function (next) {
async.parallel({
ignoredCids: function(next) {
if (filter === 'watched') {
return next(null, []);
}
user.getIgnoredCategories(uid, next);
},
recentTids: function(next) {
db.getSortedSetRevRangeByScoreWithScores('topics:recent', 0, -1, '+inf', cutoff, next);
},
userScores: function(next) {
db.getSortedSetRevRangeByScoreWithScores('uid:' + uid + ':tids_read', 0, -1, '+inf', cutoff, next);
},
tids_unread: function(next) {
db.getSortedSetRevRangeWithScores('uid:' + uid + ':tids_unread', 0, -1, next);
}
}, next);
}, },
recentTids: function(next) { function (results, next) {
db.getSortedSetRevRangeByScoreWithScores('topics:recent', 0, -1, '+inf', cutoff, next); if (results.recentTids && !results.recentTids.length && !results.tids_unread.length) {
}, return callback(null, []);
userScores: function(next) {
db.getSortedSetRevRangeByScoreWithScores('uid:' + uid + ':tids_read', 0, -1, '+inf', cutoff, next);
},
tids_unread: function(next) {
db.getSortedSetRevRangeWithScores('uid:' + uid + ':tids_unread', 0, -1, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
if (results.recentTids && !results.recentTids.length && !results.tids_unread.length) {
return callback(null, []);
}
var userRead = {};
results.userScores.forEach(function(userItem) {
userRead[userItem.value] = userItem.score;
});
results.recentTids = results.recentTids.concat(results.tids_unread);
results.recentTids.sort(function(a, b) {
return b.score - a.score;
});
var tids = results.recentTids.filter(function(recentTopic) {
switch (filter) {
default:
return !userRead[recentTopic.value] || recentTopic.score > userRead[recentTopic.value];
case 'new':
return !userRead[recentTopic.value];
} }
}).map(function(topic) {
return topic.value;
}).filter(function(tid, index, array) {
return array.indexOf(tid) === index;
});
tids = tids.slice(0, 100); ignoredCids = results.ignoredCids;
filterTopics(uid, tids, cid, results.ignoredCids, function(err, tids) { var userRead = {};
if (err) { results.userScores.forEach(function(userItem) {
return callback(err); userRead[userItem.value] = userItem.score;
});
results.recentTids = results.recentTids.concat(results.tids_unread);
results.recentTids.sort(function(a, b) {
return b.score - a.score;
});
var tids = results.recentTids.filter(function(recentTopic) {
switch (filter) {
case 'new':
return !userRead[recentTopic.value];
default:
return !userRead[recentTopic.value] || recentTopic.score > userRead[recentTopic.value];
}
}).map(function(topic) {
return topic.value;
}).filter(function(tid, index, array) {
return array.indexOf(tid) === index;
});
if (filter === 'watched') {
filterWatchedTids(uid, tids, next);
} else {
next(null, tids);
} }
},
function (tids, next) {
tids = tids.slice(0, 100);
filterTopics(uid, tids, cid, ignoredCids, next);
},
function (tids, next) {
if (stop === -1) { if (stop === -1) {
tids = tids.slice(start); tids = tids.slice(start);
@@ -135,11 +148,23 @@ module.exports = function(Topics) {
tids = tids.slice(start, stop + 1); tids = tids.slice(start, stop + 1);
} }
callback(null, tids); next(null, tids);
}); }
}); ], callback);
}; };
function filterWatchedTids(uid, tids, callback) {
db.sortedSetScores('uid:' + uid + ':followed_tids', tids, function(err, scores) {
if (err) {
return callback(err);
}
tids = tids.filter(function(tid, index) {
return tid && !!scores[index];
});
callback(null, tids);
});
}
function filterTopics(uid, tids, cid, ignoredCids, callback) { function filterTopics(uid, tids, cid, ignoredCids, callback) {
if (!Array.isArray(ignoredCids) || !tids.length) { if (!Array.isArray(ignoredCids) || !tids.length) {
return callback(null, tids); return callback(null, tids);