refactor: async/await controllers/tags
parent
f15c7f12b3
commit
5362488584
@ -1,106 +1,72 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const validator = require('validator');
|
||||||
var validator = require('validator');
|
|
||||||
|
|
||||||
var user = require('../user');
|
const user = require('../user');
|
||||||
var categories = require('../categories');
|
const categories = require('../categories');
|
||||||
var topics = require('../topics');
|
const topics = require('../topics');
|
||||||
var privileges = require('../privileges');
|
const privileges = require('../privileges');
|
||||||
var pagination = require('../pagination');
|
const pagination = require('../pagination');
|
||||||
var helpers = require('./helpers');
|
const helpers = require('./helpers');
|
||||||
|
|
||||||
var tagsController = module.exports;
|
const tagsController = module.exports;
|
||||||
|
|
||||||
tagsController.getTag = function (req, res, next) {
|
tagsController.getTag = async function (req, res) {
|
||||||
var tag = validator.escape(String(req.params.tag));
|
const tag = validator.escape(String(req.params.tag));
|
||||||
var page = parseInt(req.query.page, 10) || 1;
|
const page = parseInt(req.query.page, 10) || 1;
|
||||||
|
|
||||||
var templateData = {
|
const templateData = {
|
||||||
topics: [],
|
topics: [],
|
||||||
tag: tag,
|
tag: tag,
|
||||||
breadcrumbs: helpers.buildBreadcrumbs([{ text: '[[tags:tags]]', url: '/tags' }, { text: tag }]),
|
breadcrumbs: helpers.buildBreadcrumbs([{ text: '[[tags:tags]]', url: '/tags' }, { text: tag }]),
|
||||||
title: '[[pages:tag, ' + tag + ']]',
|
title: '[[pages:tag, ' + tag + ']]',
|
||||||
};
|
};
|
||||||
var settings;
|
const settings = await user.getSettings(req.uid);
|
||||||
var topicCount = 0;
|
const start = Math.max(0, (page - 1) * settings.topicsPerPage);
|
||||||
var start;
|
const stop = start + settings.topicsPerPage - 1;
|
||||||
|
const states = [categories.watchStates.watching, categories.watchStates.notwatching, categories.watchStates.ignoring];
|
||||||
|
const [topicCount, tids, categoriesData] = await Promise.all([
|
||||||
|
topics.getTagTopicCount(req.params.tag),
|
||||||
|
topics.getTagTids(req.params.tag, start, stop),
|
||||||
|
helpers.getCategoriesByStates(req.uid, '', states),
|
||||||
|
]);
|
||||||
|
|
||||||
async.waterfall([
|
if (Array.isArray(tids) && !tids.length) {
|
||||||
function (next) {
|
return res.render('tag', templateData);
|
||||||
user.getSettings(req.uid, next);
|
}
|
||||||
},
|
|
||||||
function (_settings, next) {
|
|
||||||
settings = _settings;
|
|
||||||
start = Math.max(0, (page - 1) * settings.topicsPerPage);
|
|
||||||
var stop = start + settings.topicsPerPage - 1;
|
|
||||||
async.parallel({
|
|
||||||
topicCount: function (next) {
|
|
||||||
topics.getTagTopicCount(req.params.tag, next);
|
|
||||||
},
|
|
||||||
tids: function (next) {
|
|
||||||
topics.getTagTids(req.params.tag, start, stop, next);
|
|
||||||
},
|
|
||||||
categories: function (next) {
|
|
||||||
const states = [categories.watchStates.watching, categories.watchStates.notwatching, categories.watchStates.ignoring];
|
|
||||||
helpers.getCategoriesByStates(req.uid, '', states, next);
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
if (Array.isArray(results.tids) && !results.tids.length) {
|
|
||||||
return res.render('tag', templateData);
|
|
||||||
}
|
|
||||||
|
|
||||||
templateData.categories = results.categories.categories;
|
templateData.categories = categoriesData.categories;
|
||||||
|
|
||||||
topicCount = results.topicCount;
|
templateData.topics = await topics.getTopics(tids, req.uid);
|
||||||
topics.getTopics(results.tids, req.uid, next);
|
topics.calculateTopicIndices(templateData.topics, start);
|
||||||
|
res.locals.metaTags = [
|
||||||
|
{
|
||||||
|
name: 'title',
|
||||||
|
content: tag,
|
||||||
},
|
},
|
||||||
function (topicsData) {
|
{
|
||||||
topics.calculateTopicIndices(topicsData, start);
|
property: 'og:title',
|
||||||
res.locals.metaTags = [
|
content: tag,
|
||||||
{
|
},
|
||||||
name: 'title',
|
];
|
||||||
content: tag,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
property: 'og:title',
|
|
||||||
content: tag,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
templateData.topics = topicsData;
|
|
||||||
|
|
||||||
var pageCount = Math.max(1, Math.ceil(topicCount / settings.topicsPerPage));
|
const pageCount = Math.max(1, Math.ceil(topicCount / settings.topicsPerPage));
|
||||||
templateData.pagination = pagination.create(page, pageCount);
|
templateData.pagination = pagination.create(page, pageCount);
|
||||||
|
|
||||||
res.render('tag', templateData);
|
res.render('tag', templateData);
|
||||||
},
|
|
||||||
], next);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
tagsController.getTags = function (req, res, next) {
|
tagsController.getTags = async function (req, res) {
|
||||||
async.waterfall([
|
const [canSearch, tags] = await Promise.all([
|
||||||
function (next) {
|
privileges.global.can('search:tags', req.uid),
|
||||||
async.parallel({
|
topics.getTags(0, 99),
|
||||||
canSearch: function (next) {
|
]);
|
||||||
privileges.global.can('search:tags', req.uid, next);
|
|
||||||
},
|
res.render('tags', {
|
||||||
tags: function (next) {
|
tags: tags.filter(Boolean),
|
||||||
topics.getTags(0, 99, next);
|
displayTagSearch: canSearch,
|
||||||
},
|
nextStart: 100,
|
||||||
}, next);
|
breadcrumbs: helpers.buildBreadcrumbs([{ text: '[[tags:tags]]' }]),
|
||||||
},
|
title: '[[pages:tags]]',
|
||||||
function (results) {
|
});
|
||||||
results.tags = results.tags.filter(Boolean);
|
|
||||||
var data = {
|
|
||||||
tags: results.tags,
|
|
||||||
displayTagSearch: results.canSearch,
|
|
||||||
nextStart: 100,
|
|
||||||
breadcrumbs: helpers.buildBreadcrumbs([{ text: '[[tags:tags]]' }]),
|
|
||||||
title: '[[pages:tags]]',
|
|
||||||
};
|
|
||||||
res.render('tags', data);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue