feat: allow custom req.query.filter on /unread /recent

v1.18.x
Barış Soner Uşaklı 4 years ago
parent a8e18fdbe6
commit de824007d9

@ -30,27 +30,21 @@ helpers.noScriptErrors = async function (req, res, error, httpStatus) {
});
};
helpers.validFilters = { '': true, new: true, watched: true, unreplied: true };
helpers.terms = {
daily: 'day',
weekly: 'week',
monthly: 'month',
};
helpers.buildQueryString = function (cid, filter, term) {
const qs = {};
if (cid) {
qs.cid = cid;
}
if (filter) {
qs.filter = filter;
}
if (term) {
qs.term = term;
helpers.buildQueryString = function (query, key, value) {
const queryObj = _.clone(query);
if (value) {
queryObj[key] = value;
} else {
delete queryObj[key];
}
return Object.keys(qs).length ? '?' + querystring.stringify(qs) : '';
delete queryObj._;
return Object.keys(queryObj).length ? '?' + querystring.stringify(queryObj) : '';
};
helpers.addLinkTags = function (params) {
@ -69,25 +63,25 @@ helpers.addLinkTags = function (params) {
helpers.buildFilters = function (url, filter, query) {
return [{
name: '[[unread:all-topics]]',
url: url + helpers.buildQueryString(query.cid, '', query.term),
url: url + helpers.buildQueryString(query, 'filter', ''),
selected: filter === '',
filter: '',
icon: 'fa-book',
}, {
name: '[[unread:new-topics]]',
url: url + helpers.buildQueryString(query.cid, 'new', query.term),
url: url + helpers.buildQueryString(query, 'filter', 'new'),
selected: filter === 'new',
filter: 'new',
icon: 'fa-clock-o',
}, {
name: '[[unread:watched-topics]]',
url: url + helpers.buildQueryString(query.cid, 'watched', query.term),
url: url + helpers.buildQueryString(query, 'filter', 'watched'),
selected: filter === 'watched',
filter: 'watched',
icon: 'fa-bell-o',
}, {
name: '[[unread:unreplied-topics]]',
url: url + helpers.buildQueryString(query.cid, 'unreplied', query.term),
url: url + helpers.buildQueryString(query, 'filter', 'unreplied'),
selected: filter === 'unreplied',
filter: 'unreplied',
icon: 'fa-reply',
@ -97,22 +91,22 @@ helpers.buildFilters = function (url, filter, query) {
helpers.buildTerms = function (url, term, query) {
return [{
name: '[[recent:alltime]]',
url: url + helpers.buildQueryString(query.cid, query.filter, ''),
url: url + helpers.buildQueryString(query, 'term', ''),
selected: term === 'alltime',
term: 'alltime',
}, {
name: '[[recent:day]]',
url: url + helpers.buildQueryString(query.cid, query.filter, 'daily'),
url: url + helpers.buildQueryString(query, 'term', 'daily'),
selected: term === 'day',
term: 'day',
}, {
name: '[[recent:week]]',
url: url + helpers.buildQueryString(query.cid, query.filter, 'weekly'),
url: url + helpers.buildQueryString(query, 'term', 'weekly'),
selected: term === 'week',
term: 'week',
}, {
name: '[[recent:month]]',
url: url + helpers.buildQueryString(query.cid, query.filter, 'monthly'),
url: url + helpers.buildQueryString(query, 'term', 'monthly'),
selected: term === 'month',
term: 'month',
}];

@ -27,7 +27,7 @@ recentController.getData = async function (req, url, sort) {
const cid = req.query.cid;
const filter = req.query.filter || '';
if (!helpers.validFilters[filter] || (!term && req.query.term)) {
if (!term && req.query.term) {
return null;
}
term = term || 'alltime';
@ -64,7 +64,7 @@ recentController.getData = async function (req, url, sort) {
data.showSelect = isPrivileged;
data.showTopicTools = isPrivileged;
data.categories = categoryData.categories;
data.allCategoriesUrl = url + helpers.buildQueryString('', filter, '');
data.allCategoriesUrl = url + helpers.buildQueryString(req.query, 'cid', '');
data.selectedCategory = categoryData.selectedCategory || null;
data.selectedCids = categoryData.selectedCids;
data['feeds:disableRSS'] = meta.config['feeds:disableRSS'] || 0;
@ -79,7 +79,7 @@ recentController.getData = async function (req, url, sort) {
data.terms = helpers.buildTerms(url, term, req.query);
data.selectedTerm = data.terms.find(term => term && term.selected);
var pageCount = Math.max(1, Math.ceil(data.topicCount / settings.topicsPerPage));
const pageCount = Math.max(1, Math.ceil(data.topicCount / settings.topicsPerPage));
data.pagination = pagination.create(page, pageCount, req.query);
helpers.addLinkTags({ url: url, res: req.res, tags: data.pagination.rel });

@ -14,14 +14,10 @@ const helpers = require('./helpers');
const unreadController = module.exports;
unreadController.get = async function (req, res, next) {
unreadController.get = async function (req, res) {
const cid = req.query.cid;
const filter = req.query.filter || '';
const filterData = await plugins.fireHook('filter:unread.getValidFilters', { filters: { ...helpers.validFilters } });
if (!filterData.filters[filter]) {
return next();
}
const [watchedCategories, userSettings, isPrivileged] = await Promise.all([
getWatchedCategories(req.uid, cid, filter),
user.getSettings(req.uid),
@ -52,7 +48,7 @@ unreadController.get = async function (req, res, next) {
data.showSelect = true;
data.showTopicTools = isPrivileged;
data.categories = watchedCategories.categories;
data.allCategoriesUrl = 'unread' + helpers.buildQueryString('', filter, '');
data.allCategoriesUrl = 'unread' + helpers.buildQueryString(req.query, 'cid', '');
data.selectedCategory = watchedCategories.selectedCategory;
data.selectedCids = watchedCategories.selectedCids;
if (req.originalUrl.startsWith(nconf.get('relative_path') + '/api/unread') || req.originalUrl.startsWith(nconf.get('relative_path') + '/unread')) {
@ -81,10 +77,6 @@ async function getWatchedCategories(uid, cid, filter) {
unreadController.unreadTotal = async function (req, res, next) {
const filter = req.query.filter || '';
try {
const data = await plugins.fireHook('filter:unread.getValidFilters', { filters: { ...helpers.validFilters } });
if (!data.filters[filter]) {
return next();
}
const unreadCount = await topics.getTotalUnread(req.uid, filter);
res.json(unreadCount);
} catch (err) {

@ -78,6 +78,7 @@ module.exports = function (Topics) {
tidsByFilter: data.tidsByFilter,
cid: params.cid,
filter: params.filter,
query: params.query || {},
});
return result;
};

Loading…
Cancel
Save