From 28359665187b0a3b9ec6226dca1234ebdbd725a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 19 Jan 2022 21:48:49 -0500 Subject: [PATCH] fix: urls used when recent/unread/popular/top is used as the homepage if recent is used as the homepage then changing the selected category or filters was redirecting the user to forum.com/recent?cid=1 instead of staying at forum.com/?cid=1 also fix unread page mark read button showing the selected category --- src/controllers/recent.js | 26 +++++++++++++++----------- src/controllers/unread.js | 23 ++++++++++++++--------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/controllers/recent.js b/src/controllers/recent.js index 7715fbb89c..344ec3b3eb 100644 --- a/src/controllers/recent.js +++ b/src/controllers/recent.js @@ -12,6 +12,7 @@ const pagination = require('../pagination'); const privileges = require('../privileges'); const recentController = module.exports; +const relative_path = nconf.get('relative_path'); recentController.get = async function (req, res, next) { const data = await recentController.getData(req, 'recent', 'recent'); @@ -56,33 +57,36 @@ recentController.getData = async function (req, url, sort) { query: req.query, }); + const isDisplayedAsHome = !(req.originalUrl.startsWith(`${relative_path}/api/${url}`) || req.originalUrl.startsWith(`${relative_path}/${url}`)); + const baseUrl = isDisplayedAsHome ? '' : url; + + if (isDisplayedAsHome) { + data.title = meta.config.homePageTitle || '[[pages:home]]'; + } else { + data.title = `[[pages:${url}]]`; + data.breadcrumbs = helpers.buildBreadcrumbs([{ text: `[[${url}:title]]` }]); + } + data.canPost = canPost; data.showSelect = isPrivileged; data.showTopicTools = isPrivileged; - data.allCategoriesUrl = url + helpers.buildQueryString(req.query, 'cid', ''); + data.allCategoriesUrl = baseUrl + helpers.buildQueryString(req.query, 'cid', ''); data.selectedCategory = categoryData.selectedCategory; data.selectedCids = categoryData.selectedCids; data['feeds:disableRSS'] = meta.config['feeds:disableRSS'] || 0; - data.rssFeedUrl = `${nconf.get('relative_path')}/${url}.rss`; + data.rssFeedUrl = `${relative_path}/${url}.rss`; if (req.loggedIn) { data.rssFeedUrl += `?uid=${req.uid}&token=${rssToken}`; } - data.title = meta.config.homePageTitle || '[[pages:home]]'; - data.filters = helpers.buildFilters(url, filter, req.query); + data.filters = helpers.buildFilters(baseUrl, filter, req.query); data.selectedFilter = data.filters.find(filter => filter && filter.selected); - data.terms = helpers.buildTerms(url, term, req.query); + data.terms = helpers.buildTerms(baseUrl, term, req.query); data.selectedTerm = data.terms.find(term => term && term.selected); 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 }); - - if (req.originalUrl.startsWith(`${nconf.get('relative_path')}/api/${url}`) || req.originalUrl.startsWith(`${nconf.get('relative_path')}/${url}`)) { - data.title = `[[pages:${url}]]`; - data.breadcrumbs = helpers.buildBreadcrumbs([{ text: `[[${url}:title]]` }]); - } - return data; }; diff --git a/src/controllers/unread.js b/src/controllers/unread.js index 26a8c5675c..38cda453a0 100644 --- a/src/controllers/unread.js +++ b/src/controllers/unread.js @@ -11,6 +11,7 @@ const topics = require('../topics'); const helpers = require('./helpers'); const unreadController = module.exports; +const relative_path = nconf.get('relative_path'); unreadController.get = async function (req, res) { const { cid } = req.query; @@ -34,7 +35,16 @@ unreadController.get = async function (req, res) { query: req.query, }); - data.title = meta.config.homePageTitle || '[[pages:home]]'; + const isDisplayedAsHome = !(req.originalUrl.startsWith(`${relative_path}/api/unread`) || req.originalUrl.startsWith(`${relative_path}/unread`)); + const baseUrl = isDisplayedAsHome ? '' : 'unread'; + + if (isDisplayedAsHome) { + data.title = meta.config.homePageTitle || '[[pages:home]]'; + } else { + data.title = '[[pages:unread]]'; + data.breadcrumbs = helpers.buildBreadcrumbs([{ text: '[[unread:title]]' }]); + } + data.pageCount = Math.max(1, Math.ceil(data.topicCount / userSettings.topicsPerPage)); data.pagination = pagination.create(page, data.pageCount, req.query); helpers.addLinkTags({ url: 'unread', res: req.res, tags: data.pagination.rel }); @@ -45,18 +55,13 @@ unreadController.get = async function (req, res) { } data.showSelect = true; data.showTopicTools = isPrivileged; - data.allCategoriesUrl = `unread${helpers.buildQueryString(req.query, 'cid', '')}`; + data.allCategoriesUrl = `${baseUrl}${helpers.buildQueryString(req.query, 'cid', '')}`; data.selectedCategory = categoryData.selectedCategory; data.selectedCids = categoryData.selectedCids; data.selectCategoryLabel = '[[unread:mark_as_read]]'; data.selectCategoryIcon = 'fa-inbox'; - if (req.originalUrl.startsWith(`${nconf.get('relative_path')}/api/unread`) || req.originalUrl.startsWith(`${nconf.get('relative_path')}/unread`)) { - data.title = '[[pages:unread]]'; - data.breadcrumbs = helpers.buildBreadcrumbs([{ text: '[[unread:title]]' }]); - } - - data.filters = helpers.buildFilters('unread', filter, req.query); - + data.showCategorySelectLabel = true; + data.filters = helpers.buildFilters(baseUrl, filter, req.query); data.selectedFilter = data.filters.find(filter => filter && filter.selected); res.render('unread', data);