diff --git a/public/language/en_GB/unread.json b/public/language/en_GB/unread.json index 8bd7a20711..0af9a2cdf6 100644 --- a/public/language/en_GB/unread.json +++ b/public/language/en_GB/unread.json @@ -5,5 +5,6 @@ "mark_as_read": "Mark as Read", "selected": "Selected", "all": "All", + "all_categories": "All categories", "topics_marked_as_read.success": "Topics marked as read!" } \ No newline at end of file diff --git a/public/src/client/unread.js b/public/src/client/unread.js index acb66d4c2f..bd6c67f279 100644 --- a/public/src/client/unread.js +++ b/public/src/client/unread.js @@ -68,8 +68,6 @@ define('forum/unread', ['forum/recent', 'topicSelect', 'forum/infinitescroll', ' }); }); - socket.emit('categories.getWatchedCategories', onCategoriesLoaded); - topicSelect.init(); if ($("body").height() <= $(window).height() && $('[component="category"]').children().length >= 20) { @@ -86,9 +84,11 @@ define('forum/unread', ['forum/recent', 'topicSelect', 'forum/infinitescroll', ' if(direction < 0 || !$('[component="category"]').length) { return; } - + var params = utils.params(); + var cid = params.cid; infinitescroll.loadMore('topics.loadMoreUnreadTopics', { - after: $('[component="category"]').attr('data-nextstart') + after: $('[component="category"]').attr('data-nextstart'), + cid: cid }, function(data, done) { if (data.topics && data.topics.length) { recent.onTopicsLoaded('unread', data.topics, true, done); @@ -118,31 +118,6 @@ define('forum/unread', ['forum/recent', 'topicSelect', 'forum/infinitescroll', ' } } - function onCategoriesLoaded(err, categories) { - createCategoryLinks(categories); - } - - function createCategoryLinks(categories) { - for (var i=0; i'); - - if (category.icon) { - link.append(' ' + category.name); - } else { - link.append(category.name); - } - - $('') - .append(link) - .appendTo($('.markread .dropdown-menu')); - } return Unread; }); diff --git a/src/controllers/categories.js b/src/controllers/categories.js index 53ad658966..fe44af90a8 100644 --- a/src/controllers/categories.js +++ b/src/controllers/categories.js @@ -68,28 +68,6 @@ categoriesController.popular = function(req, res, next) { }); }; -categoriesController.unread = function(req, res, next) { - var stop = (parseInt(meta.config.topicsPerList, 10) || 20) - 1; - topics.getUnreadTopics(req.uid, 0, stop, function (err, data) { - if (err) { - return next(err); - } - - data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[unread:title]]'}]); - res.render('unread', data); - }); -}; - -categoriesController.unreadTotal = function(req, res, next) { - topics.getTotalUnread(req.uid, function (err, data) { - if (err) { - return next(err); - } - - res.json(data); - }); -}; - categoriesController.list = function(req, res, next) { async.parallel({ header: function (next) { diff --git a/src/controllers/index.js b/src/controllers/index.js index 7c56b55df6..25e0b7f1ee 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -18,6 +18,7 @@ var Controllers = { posts: require('./posts'), topics: require('./topics'), categories: require('./categories'), + unread: require('./unread'), tags: require('./tags'), search: require('./search'), users: require('./users'), diff --git a/src/controllers/unread.js b/src/controllers/unread.js new file mode 100644 index 0000000000..0354ea7310 --- /dev/null +++ b/src/controllers/unread.js @@ -0,0 +1,69 @@ + +'use strict'; + +var async = require('async'), + + meta = require('../meta'), + categories = require('../categories'), + user = require('../user'), + topics = require('../topics'), + helpers = require('./helpers'); + +var unreadController = {}; + + +unreadController.unread = function(req, res, next) { + var stop = (parseInt(meta.config.topicsPerList, 10) || 20) - 1; + var results; + var cid = req.query.cid; + + async.waterfall([ + function(next) { + async.parallel({ + watchedCategories: function(next) { + user.getWatchedCategories(req.uid, next); + }, + unreadTopics: function(next) { + topics.getUnreadTopics(cid, req.uid, 0, stop, next); + } + }, next); + }, + function(_results, next) { + results = _results; + categories.getMultipleCategoryFields(results.watchedCategories, ['cid', 'name', 'slug', 'icon', 'link'], next); + }, + function(categories, next) { + categories = categories.filter(function(category) { + return category && !category.link; + }); + categories.forEach(function(category) { + category.selected = parseInt(category.cid, 10) === parseInt(cid, 10); + if (category.selected) { + results.unreadTopics.selectedCategory = category; + } + }); + results.unreadTopics.categories = categories; + next(null, results.unreadTopics); + } + ], function(err, data) { + if (err) { + return next(err); + } + + data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[unread:title]]'}]); + res.render('unread', data); + }); +}; + + +unreadController.unreadTotal = function(req, res, next) { + topics.getTotalUnread(req.uid, function (err, data) { + if (err) { + return next(err); + } + + res.json(data); + }); +}; + +module.exports = unreadController; \ No newline at end of file diff --git a/src/routes/api.js b/src/routes/api.js index d3a366dc5d..dec27ea2d4 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -18,6 +18,7 @@ module.exports = function(app, middleware, controllers) { router.get('/post/:pid', controllers.posts.getPost); router.get('/categories/:cid/moderators', getModerators); router.get('/recent/posts/:term?', getRecentPosts); + router.get('/unread/total', middleware.authenticate, controllers.unread.unreadTotal); var multipart = require('connect-multiparty'); var multipartMiddleware = multipart(); diff --git a/src/routes/index.js b/src/routes/index.js index f741cdaa6b..1c5b046b9f 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -49,8 +49,7 @@ function categoryRoutes(app, middleware, controllers) { setupPageRoute(app, '/categories', middleware, [], controllers.categories.list); setupPageRoute(app, '/popular/:term?', middleware, [], controllers.categories.popular); setupPageRoute(app, '/recent', middleware, [], controllers.categories.recent); - setupPageRoute(app, '/unread', middleware, [middleware.authenticate], controllers.categories.unread); - app.get('/api/unread/total', middleware.authenticate, controllers.categories.unreadTotal); + setupPageRoute(app, '/unread', middleware, [middleware.authenticate], controllers.unread.unread); setupPageRoute(app, '/category/:category_id/:slug/:topic_index', middleware, [], controllers.categories.get); setupPageRoute(app, '/category/:category_id/:slug?', middleware, [], controllers.categories.get); diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index 5f4cd80eb3..1de512d62e 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -140,24 +140,12 @@ SocketTopics.markAllRead = function(socket, data, callback) { }; SocketTopics.markCategoryTopicsRead = function(socket, cid, callback) { - topics.getUnreadTids(socket.uid, 0, -1, function(err, tids) { + topics.getUnreadTids(cid, socket.uid, 0, -1, function(err, tids) { if (err) { return callback(err); } - topics.getTopicsFields(tids, ['tid', 'cid'], function(err, topicData) { - if (err) { - return callback(err); - } - - tids = topicData.filter(function(topic) { - return topic && parseInt(topic.cid, 10) === parseInt(cid, 10); - }).map(function(topic) { - return topic.tid; - }); - - SocketTopics.markAsRead(socket, tids, callback); - }); + SocketTopics.markAsRead(socket, tids, callback); }); }; @@ -501,7 +489,7 @@ SocketTopics.loadMoreUnreadTopics = function(socket, data, callback) { var start = parseInt(data.after, 10), stop = start + 9; - topics.getUnreadTopics(socket.uid, start, stop, callback); + topics.getUnreadTopics(data.cid, socket.uid, start, stop, callback); }; SocketTopics.loadMoreFromSet = function(socket, data, callback) { diff --git a/src/topics/unread.js b/src/topics/unread.js index 53f804f9fc..b0252d02ce 100644 --- a/src/topics/unread.js +++ b/src/topics/unread.js @@ -12,15 +12,15 @@ var async = require('async'), module.exports = function(Topics) { - var unreadCutoff = 86400000; + var unreadCutoff = 86400000 * 2; Topics.getTotalUnread = function(uid, callback) { - Topics.getUnreadTids(uid, 0, 20, function(err, tids) { + Topics.getUnreadTids(0, uid, 0, 20, function(err, tids) { callback(err, tids ? tids.length : 0); }); }; - Topics.getUnreadTopics = function(uid, start, stop, callback) { + Topics.getUnreadTopics = function(cid, uid, start, stop, callback) { var unreadTopics = { showSelect: true, @@ -30,7 +30,7 @@ module.exports = function(Topics) { async.waterfall([ function(next) { - Topics.getUnreadTids(uid, start, stop, next); + Topics.getUnreadTids(cid, uid, start, stop, next); }, function(tids, next) { if (!tids.length) { @@ -50,7 +50,7 @@ module.exports = function(Topics) { ], callback); }; - Topics.getUnreadTids = function(uid, start, stop, callback) { + Topics.getUnreadTids = function(cid, uid, start, stop, callback) { uid = parseInt(uid, 10); if (uid === 0) { return callback(null, []); @@ -91,7 +91,7 @@ module.exports = function(Topics) { tids = tids.slice(0, 100); - filterTopics(uid, tids, results.ignoredCids, function(err, tids) { + filterTopics(uid, tids, cid, results.ignoredCids, function(err, tids) { if (err) { return callback(err); } @@ -107,7 +107,7 @@ module.exports = function(Topics) { }); }; - function filterTopics(uid, tids, ignoredCids, callback) { + function filterTopics(uid, tids, cid, ignoredCids, callback) { if (!Array.isArray(ignoredCids) || !tids.length) { return callback(null, tids); } @@ -121,7 +121,7 @@ module.exports = function(Topics) { }, function(topics, next) { tids = topics.filter(function(topic) { - return topic && topic.cid && ignoredCids.indexOf(topic.cid.toString()) === -1; + return topic && topic.cid && ignoredCids.indexOf(topic.cid.toString()) === -1 && (!cid || parseInt(cid, 10) === parseInt(topic.cid, 10)); }).map(function(topic) { return topic.tid; });