Skip to content

Commit

Permalink
closes #3128
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Apr 18, 2016
1 parent 35ac64d commit ec33a57
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 49 deletions.
5 changes: 4 additions & 1 deletion public/language/en_GB/unread.json
Expand Up @@ -6,5 +6,8 @@
"selected": "Selected",
"all": "All",
"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"
}
21 changes: 19 additions & 2 deletions src/controllers/unread.js
Expand Up @@ -5,14 +5,14 @@ var async = require('async');
var meta = require('../meta');
var categories = require('../categories');
var privileges = require('../privileges');
var user = require('../user')
var user = require('../user');
var topics = require('../topics');
var helpers = require('./helpers');
var plugins = require('../plugins');

var unreadController = {};

var validFilter = {'': true, 'new': true};
var validFilter = {'': true, 'new': true, 'watched': true};

unreadController.get = function(req, res, next) {
var stop = (parseInt(meta.config.topicsPerList, 10) || 20) - 1;
Expand Down Expand Up @@ -57,6 +57,23 @@ unreadController.get = function(req, res, next) {

results.unreadTopics.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[unread:title]]'}]);
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);
}
Expand Down
117 changes: 71 additions & 46 deletions src/topics/unread.js
Expand Up @@ -31,7 +31,6 @@ module.exports = function(Topics) {
filter = '';
}


var unreadTopics = {
showSelect: true,
nextStart : 0,
Expand Down Expand Up @@ -77,68 +76,94 @@ module.exports = function(Topics) {

var cutoff = Topics.unreadCutoff();

async.parallel({
ignoredCids: function(next) {
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);
var ignoredCids;

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);
},
tids_unread: function(next) {
db.getSortedSetRevRangeWithScores('uid:' + uid + ':tids_unread', 0, -1, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
function (results, next) {
if (results.recentTids && !results.recentTids.length && !results.tids_unread.length) {
return callback(null, []);
}

if (results.recentTids && !results.recentTids.length && !results.tids_unread.length) {
return callback(null, []);
}
ignoredCids = results.ignoredCids;

var userRead = {};
results.userScores.forEach(function(userItem) {
userRead[userItem.value] = userItem.score;
});
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;
});
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;
});

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];
if (filter === 'watched') {
filterWatchedTids(uid, tids, next);
} else {
next(null, tids);
}
}).map(function(topic) {
return topic.value;
}).filter(function(tid, index, array) {
return array.indexOf(tid) === index;
});
},
function (tids, next) {

tids = tids.slice(0, 100);
tids = tids.slice(0, 100);

filterTopics(uid, tids, cid, results.ignoredCids, function(err, tids) {
if (err) {
return callback(err);
}
filterTopics(uid, tids, cid, ignoredCids, next);
},
function (tids, next) {

if (stop === -1) {
tids = tids.slice(start);
} else {
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) {
if (!Array.isArray(ignoredCids) || !tids.length) {
Expand Down

0 comments on commit ec33a57

Please sign in to comment.