2018-10-05 14:00:18 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-08-12 14:49:40 -04:00
|
|
|
const user = require('../../user');
|
|
|
|
|
const categories = require('../../categories');
|
|
|
|
|
const accountHelpers = require('./helpers');
|
2021-01-06 22:00:56 -05:00
|
|
|
const helpers = require('../helpers');
|
2021-02-07 15:09:52 -05:00
|
|
|
const pagination = require('../../pagination');
|
|
|
|
|
const meta = require('../../meta');
|
2018-10-05 14:00:18 -03:00
|
|
|
|
2019-08-12 14:49:40 -04:00
|
|
|
const categoriesController = module.exports;
|
2018-10-05 14:00:18 -03:00
|
|
|
|
2019-08-12 14:49:40 -04:00
|
|
|
categoriesController.get = async function (req, res, next) {
|
|
|
|
|
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
|
|
|
|
|
if (!userData) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
2021-02-07 15:09:52 -05:00
|
|
|
const [states, allCategoriesData] = await Promise.all([
|
2019-08-12 14:49:40 -04:00
|
|
|
user.getCategoryWatchState(userData.uid),
|
2019-09-25 21:24:07 -04:00
|
|
|
categories.buildForSelect(userData.uid, 'find', ['descriptionParsed', 'depth', 'slug']),
|
2019-08-12 14:49:40 -04:00
|
|
|
]);
|
2018-10-05 14:00:18 -03:00
|
|
|
|
2021-02-07 15:09:52 -05:00
|
|
|
const pageCount = Math.max(1, Math.ceil(allCategoriesData.length / meta.config.categoriesPerPage));
|
|
|
|
|
const page = Math.min(parseInt(req.query.page, 10) || 1, pageCount);
|
|
|
|
|
const start = Math.max(0, (page - 1) * meta.config.categoriesPerPage);
|
|
|
|
|
const stop = start + meta.config.categoriesPerPage - 1;
|
|
|
|
|
const categoriesData = allCategoriesData.slice(start, stop + 1);
|
|
|
|
|
|
|
|
|
|
|
2019-08-12 14:49:40 -04:00
|
|
|
categoriesData.forEach(function (category) {
|
|
|
|
|
if (category) {
|
|
|
|
|
category.isIgnored = states[category.cid] === categories.watchStates.ignoring;
|
|
|
|
|
category.isWatched = states[category.cid] === categories.watchStates.watching;
|
|
|
|
|
category.isNotWatched = states[category.cid] === categories.watchStates.notwatching;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
userData.categories = categoriesData;
|
2021-02-03 23:59:08 -07:00
|
|
|
userData.title = `[[pages:account/watched_categories, ${userData.username}]]`;
|
2021-01-06 22:00:56 -05:00
|
|
|
userData.breadcrumbs = helpers.buildBreadcrumbs([
|
2021-02-03 23:59:08 -07:00
|
|
|
{ text: userData.username, url: `/user/${userData.userslug}` },
|
2021-01-06 22:00:56 -05:00
|
|
|
{ text: '[[pages:categories]]' },
|
|
|
|
|
]);
|
2021-02-07 15:09:52 -05:00
|
|
|
userData.pagination = pagination.create(page, pageCount, req.query);
|
2019-08-12 14:49:40 -04:00
|
|
|
res.render('account/categories', userData);
|
2018-10-05 14:00:18 -03:00
|
|
|
};
|