Files
NodeBB/src/controllers/unread.js

114 lines
3.5 KiB
JavaScript
Raw Normal View History

2015-07-13 14:47:09 -04:00
'use strict';
2016-03-10 20:28:36 -05:00
var async = require('async');
2017-11-15 14:49:04 -05:00
var nconf = require('nconf');
2016-06-09 14:29:54 +03:00
var querystring = require('querystring');
2016-05-13 14:08:50 +03:00
2017-12-06 12:06:41 -05:00
var meta = require('../meta');
2016-05-13 14:08:50 +03:00
var pagination = require('../pagination');
2016-04-18 15:44:07 +03:00
var user = require('../user');
var categories = require('../categories');
2016-03-10 20:28:36 -05:00
var topics = require('../topics');
2017-05-16 19:24:42 -04:00
var plugins = require('../plugins');
2016-03-10 20:28:36 -05:00
var helpers = require('./helpers');
2015-07-13 14:47:09 -04:00
2017-05-16 19:24:42 -04:00
var unreadController = module.exports;
2015-07-13 14:47:09 -04:00
unreadController.get = function (req, res, next) {
2016-05-13 14:08:50 +03:00
var page = parseInt(req.query.page, 10) || 1;
2015-07-13 14:47:09 -04:00
var results;
2016-09-26 18:42:01 +03:00
var cid = req.query.cid;
2018-06-18 14:37:32 -04:00
var filter = req.query.filter || '';
2016-05-13 14:08:50 +03:00
var settings;
2017-05-16 19:24:42 -04:00
2015-07-13 14:47:09 -04:00
async.waterfall([
function (next) {
fix(style): updated code to follow new eslint recommendations Squashed commit of the following: commit f9ce878b269b3568f0d649309aae1af4dcfdfeef Author: Julian Lam <julian@nodebb.org> Date: Tue Aug 13 14:30:46 2019 -0400 fix(style): updated code to follow new eslint recommendations commit 80dd370e413f22badb96ff2138e7991dfff6d836 Author: Julian Lam <julian@nodebb.org> Date: Tue Aug 13 14:14:58 2019 -0400 fix(deps): update dependency sitemap to v4 Squashed commit of the following: commit f4dd9cabb21e26fdc21f8413be822ea7c64251f8 Author: Julian Lam <julian@nodebb.org> Date: Tue Aug 13 11:33:05 2019 -0400 fix: resolved breaking changes from sitemap v4 upgrade commit 9043415ee16dcc27a8dcc2e4479d1bc5e2d1b60e Merge: e3352b272 72590b346 Author: Julian Lam <julian@nodebb.org> Date: Tue Aug 13 11:09:55 2019 -0400 Merge branch 'master' into renovate/sitemap-4.x commit e3352b272eb9400bdb00774973181397803765e4 Author: Renovate Bot <bot@renovateapp.com> Date: Mon Aug 12 07:59:05 2019 +0000 fix(deps): update dependency sitemap to v4 commit 8e3c0cdcae22acc32d352be8bb72d60e7502dbc5 Author: Renovate Bot <bot@renovateapp.com> Date: Fri Aug 9 00:49:51 2019 +0000 fix(deps): update dependency commander to v3 commit 2104449d38818f2fa4d44b3a58a0a168781acbfb Author: Renovate Bot <bot@renovateapp.com> Date: Tue Aug 13 15:00:27 2019 +0000 fix(deps): update dependency mongodb to v3.3.0 commit d2937f446a21131c070ae5d0ff33d67cfe465b8c Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Tue Aug 13 10:36:48 2019 -0400 feat: async/await admin/controllers commit 1b97e8b199f960dc24e5722702f27499ae049914 Author: Misty (Bot) <deploy@nodebb.org> Date: Tue Aug 13 09:28:39 2019 +0000 Latest translations and fallbacks commit 69a48957a2f0d23c4d194b664bda3a0431179c01 Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Mon Aug 12 21:56:09 2019 -0400 feat: async/await commit b9b2a7e593a452de4bef6d0ab6abe368a3bdb8dd Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Mon Aug 12 20:58:29 2019 -0400 feat: async/await refactor controllers/accounts commit a8d43a175974a0c8ae3dc132bf51a7ed9a4c6305 Author: Baris Usakli <barisusakli@gmail.com> Date: Mon Aug 12 14:49:40 2019 -0400 feat: async/await controllers/accounts commit 2f25aae57bf9dbe98d655276770e56bed9ec023b Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Sun Aug 11 23:09:50 2019 -0400 fix: #7831, fix pagination convert to async/await commit c9e83f2374572264855a04156278eef256b0a20c Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Sun Aug 11 00:14:35 2019 -0400 fix: remove empty line commit 30be91b26c4dd7583412c4e8d56e9c1688e48a44 Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Sun Aug 11 00:13:41 2019 -0400 fix: remove useless catchs and empty line commit 2e4a71c0b6104738f15ffbfe3246105b922fdfb3 Author: Renovate Bot <bot@renovateapp.com> Date: Sat Aug 10 06:51:50 2019 +0000 chore(deps): update dependency eslint-config-airbnb-base to v14
2019-08-13 14:33:37 -04:00
plugins.fireHook('filter:unread.getValidFilters', { filters: { ...helpers.validFilters } }, next);
2017-05-16 19:24:42 -04:00
},
function (data, _next) {
if (!data.filters[filter]) {
return next();
}
2015-07-13 14:47:09 -04:00
async.parallel({
watchedCategories: function (next) {
if (plugins.hasListeners('filter:unread.categories')) {
plugins.fireHook('filter:unread.categories', { uid: req.uid, cid: cid }, next);
} else {
2019-03-09 11:16:36 -05:00
const states = [categories.watchStates.watching];
if (filter === 'watched') {
states.push(categories.watchStates.notwatching, categories.watchStates.ignoring);
}
helpers.getCategoriesByStates(req.uid, cid, states, next);
}
2015-07-13 14:47:09 -04:00
},
settings: function (next) {
2016-05-13 14:08:50 +03:00
user.getSettings(req.uid, next);
2017-02-17 19:31:21 -07:00
},
2017-05-16 19:24:42 -04:00
}, _next);
2015-07-13 14:47:09 -04:00
},
function (_results, next) {
2015-07-13 14:47:09 -04:00
results = _results;
2016-05-13 14:08:50 +03:00
settings = results.settings;
var start = Math.max(0, (page - 1) * settings.topicsPerPage);
var stop = start + settings.topicsPerPage - 1;
2017-01-27 20:37:54 +03:00
var cutoff = req.session.unreadCutoff ? req.session.unreadCutoff : topics.unreadCutoff();
topics.getUnreadTopics({
cid: cid,
uid: req.uid,
start: start,
stop: stop,
filter: filter,
2017-02-24 12:46:40 -05:00
cutoff: cutoff,
2017-01-27 20:37:54 +03:00
}, next);
2017-02-17 19:31:21 -07:00
},
2017-05-26 14:25:19 -04:00
function (data) {
2017-12-06 11:54:08 -05:00
data.title = meta.config.homePageTitle || '[[pages:home]]';
2017-05-26 14:25:19 -04:00
data.pageCount = Math.max(1, Math.ceil(data.topicCount / settings.topicsPerPage));
data.pagination = pagination.create(page, data.pageCount, req.query);
if (settings.usePagination && (page < 1 || page > data.pageCount)) {
req.query.page = Math.max(1, Math.min(data.pageCount, page));
return helpers.redirect(res, '/unread?' + querystring.stringify(req.query));
}
data.categories = results.watchedCategories.categories;
2018-06-18 14:37:32 -04:00
data.allCategoriesUrl = 'unread' + helpers.buildQueryString('', filter, '');
2017-05-26 14:25:19 -04:00
data.selectedCategory = results.watchedCategories.selectedCategory;
2017-10-30 15:26:12 -04:00
data.selectedCids = results.watchedCategories.selectedCids;
if (req.originalUrl.startsWith(nconf.get('relative_path') + '/api/unread') || req.originalUrl.startsWith(nconf.get('relative_path') + '/unread')) {
2017-12-05 16:21:02 -05:00
data.title = '[[pages:unread]]';
2017-05-26 14:25:19 -04:00
data.breadcrumbs = helpers.buildBreadcrumbs([{ text: '[[unread:title]]' }]);
}
2018-06-18 14:37:32 -04:00
data.filters = helpers.buildFilters('unread', filter, req.query);
2017-05-26 14:25:19 -04:00
data.selectedFilter = data.filters.find(function (filter) {
return filter && filter.selected;
});
res.render('unread', data);
},
], next);
2015-07-13 14:47:09 -04:00
};
unreadController.unreadTotal = function (req, res, next) {
2018-06-18 14:37:32 -04:00
var filter = req.query.filter || '';
2017-05-26 14:25:19 -04:00
async.waterfall([
function (next) {
fix(style): updated code to follow new eslint recommendations Squashed commit of the following: commit f9ce878b269b3568f0d649309aae1af4dcfdfeef Author: Julian Lam <julian@nodebb.org> Date: Tue Aug 13 14:30:46 2019 -0400 fix(style): updated code to follow new eslint recommendations commit 80dd370e413f22badb96ff2138e7991dfff6d836 Author: Julian Lam <julian@nodebb.org> Date: Tue Aug 13 14:14:58 2019 -0400 fix(deps): update dependency sitemap to v4 Squashed commit of the following: commit f4dd9cabb21e26fdc21f8413be822ea7c64251f8 Author: Julian Lam <julian@nodebb.org> Date: Tue Aug 13 11:33:05 2019 -0400 fix: resolved breaking changes from sitemap v4 upgrade commit 9043415ee16dcc27a8dcc2e4479d1bc5e2d1b60e Merge: e3352b272 72590b346 Author: Julian Lam <julian@nodebb.org> Date: Tue Aug 13 11:09:55 2019 -0400 Merge branch 'master' into renovate/sitemap-4.x commit e3352b272eb9400bdb00774973181397803765e4 Author: Renovate Bot <bot@renovateapp.com> Date: Mon Aug 12 07:59:05 2019 +0000 fix(deps): update dependency sitemap to v4 commit 8e3c0cdcae22acc32d352be8bb72d60e7502dbc5 Author: Renovate Bot <bot@renovateapp.com> Date: Fri Aug 9 00:49:51 2019 +0000 fix(deps): update dependency commander to v3 commit 2104449d38818f2fa4d44b3a58a0a168781acbfb Author: Renovate Bot <bot@renovateapp.com> Date: Tue Aug 13 15:00:27 2019 +0000 fix(deps): update dependency mongodb to v3.3.0 commit d2937f446a21131c070ae5d0ff33d67cfe465b8c Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Tue Aug 13 10:36:48 2019 -0400 feat: async/await admin/controllers commit 1b97e8b199f960dc24e5722702f27499ae049914 Author: Misty (Bot) <deploy@nodebb.org> Date: Tue Aug 13 09:28:39 2019 +0000 Latest translations and fallbacks commit 69a48957a2f0d23c4d194b664bda3a0431179c01 Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Mon Aug 12 21:56:09 2019 -0400 feat: async/await commit b9b2a7e593a452de4bef6d0ab6abe368a3bdb8dd Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Mon Aug 12 20:58:29 2019 -0400 feat: async/await refactor controllers/accounts commit a8d43a175974a0c8ae3dc132bf51a7ed9a4c6305 Author: Baris Usakli <barisusakli@gmail.com> Date: Mon Aug 12 14:49:40 2019 -0400 feat: async/await controllers/accounts commit 2f25aae57bf9dbe98d655276770e56bed9ec023b Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Sun Aug 11 23:09:50 2019 -0400 fix: #7831, fix pagination convert to async/await commit c9e83f2374572264855a04156278eef256b0a20c Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Sun Aug 11 00:14:35 2019 -0400 fix: remove empty line commit 30be91b26c4dd7583412c4e8d56e9c1688e48a44 Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Sun Aug 11 00:13:41 2019 -0400 fix: remove useless catchs and empty line commit 2e4a71c0b6104738f15ffbfe3246105b922fdfb3 Author: Renovate Bot <bot@renovateapp.com> Date: Sat Aug 10 06:51:50 2019 +0000 chore(deps): update dependency eslint-config-airbnb-base to v14
2019-08-13 14:33:37 -04:00
plugins.fireHook('filter:unread.getValidFilters', { filters: { ...helpers.validFilters } }, next);
2017-05-26 14:25:19 -04:00
},
function (data, _next) {
2017-10-19 13:53:05 -04:00
if (!data.filters[filter]) {
2017-05-26 14:25:19 -04:00
return next();
}
topics.getTotalUnread(req.uid, filter, _next);
},
function (data) {
res.json(data);
},
], next);
2015-07-13 14:47:09 -04:00
};