You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/controllers/categories.js

346 lines
8.9 KiB
JavaScript

"use strict";
var categoriesController = {},
async = require('async'),
nconf = require('nconf'),
10 years ago
validator = require('validator'),
10 years ago
db = require('../database'),
11 years ago
privileges = require('../privileges'),
11 years ago
user = require('../user'),
categories = require('../categories'),
topics = require('../topics'),
meta = require('../meta'),
10 years ago
plugins = require('../plugins'),
10 years ago
pagination = require('../pagination'),
helpers = require('./helpers'),
10 years ago
utils = require('../../public/src/utils');
categoriesController.recent = function(req, res, next) {
var stop = (parseInt(meta.config.topicsPerList, 10) || 20) - 1;
topics.getTopicsFromSet('topics:recent', req.uid, 0, stop, function(err, data) {
11 years ago
if (err) {
return next(err);
}
11 years ago
data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1;
10 years ago
data.rssFeedUrl = nconf.get('relative_path') + '/recent.rss';
10 years ago
data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[recent:title]]'}]);
res.render('recent', data);
});
};
var anonCache = {}, lastUpdateTime = 0;
categoriesController.popular = function(req, res, next) {
var terms = {
daily: 'day',
weekly: 'week',
monthly: 'month',
alltime: 'alltime'
};
var term = terms[req.params.term] || 'day';
if (!req.uid) {
if (anonCache[term] && (Date.now() - lastUpdateTime) < 60 * 60 * 1000) {
return res.render('popular', anonCache[term]);
}
}
topics.getPopular(term, req.uid, meta.config.topicsPerList, function(err, topics) {
11 years ago
if (err) {
return next(err);
}
var data = {
topics: topics,
10 years ago
'feeds:disableRSS': parseInt(meta.config['feeds:disableRSS'], 10) === 1,
10 years ago
rssFeedUrl: nconf.get('relative_path') + '/popular/' + (req.params.term || 'daily') + '.rss',
10 years ago
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[global:header.popular]]'}])
};
if (!req.uid) {
anonCache[term] = data;
lastUpdateTime = Date.now();
}
res.render('popular', data);
});
};
categoriesController.unread = function(req, res, next) {
var stop = (parseInt(meta.config.topicsPerList, 10) || 20) - 1;
topics.getUnreadTopics(req.uid, 0, stop, function (err, data) {
11 years ago
if (err) {
return next(err);
}
11 years ago
10 years ago
data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[unread:title]]'}]);
res.render('unread', data);
});
};
11 years ago
categoriesController.unreadTotal = function(req, res, next) {
topics.getTotalUnread(req.uid, function (err, data) {
if (err) {
11 years ago
return next(err);
}
res.json(data);
11 years ago
});
};
10 years ago
categoriesController.list = function(req, res, next) {
async.parallel({
header: function (next) {
res.locals.metaTags = [{
name: "title",
content: validator.escape(meta.config.title || 'NodeBB')
}, {
name: "description",
content: validator.escape(meta.config.description || '')
}, {
property: 'og:title',
content: 'Index | ' + validator.escape(meta.config.title || 'NodeBB')
}, {
property: 'og:type',
content: 'website'
}];
if(meta.config['brand:logo']) {
res.locals.metaTags.push({
property: 'og:image',
content: meta.config['brand:logo']
});
}
next(null);
},
categories: function (next) {
var categoryData;
async.waterfall([
function(next) {
categories.getCategoriesByPrivilege(req.uid, 'find', next);
},
function(_categoryData, next) {
categoryData = _categoryData;
var allCategories = [];
categoryData = categoryData.filter(function(category) {
if (!category.parent) {
allCategories.push(category);
}
if (Array.isArray(category.children) && category.children.length) {
allCategories.push.apply(allCategories, category.children);
}
return category && !category.parent;
});
categories.getRecentTopicReplies(allCategories, req.uid, next);
10 years ago
}
], function(err) {
next(err, categoryData);
10 years ago
});
}
}, function (err, data) {
if (err) {
return next(err);
}
plugins.fireHook('filter:categories.build', {req: req, res: res, templateData: data}, function(err, data) {
if (err) {
return next(err);
}
res.render('categories', data.templateData);
});
10 years ago
});
};
categoriesController.get = function(req, res, next) {
var cid = req.params.category_id,
10 years ago
page = parseInt(req.query.page, 10) || 1,
userPrivileges;
10 years ago
if (req.params.topic_index && !utils.isNumber(req.params.topic_index)) {
10 years ago
return helpers.notFound(req, res);
10 years ago
}
async.waterfall([
11 years ago
function(next) {
11 years ago
async.parallel({
exists: function(next) {
categories.exists(cid, next);
},
11 years ago
categoryData: function(next) {
10 years ago
categories.getCategoryFields(cid, ['slug', 'disabled', 'topic_count'], next);
11 years ago
},
privileges: function(next) {
privileges.categories.get(cid, req.uid, next);
11 years ago
},
userSettings: function(next) {
user.getSettings(req.uid, next);
11 years ago
}
}, next);
11 years ago
},
11 years ago
function(results, next) {
11 years ago
if (!results.exists || (results.categoryData && parseInt(results.categoryData.disabled, 10) === 1)) {
10 years ago
return helpers.notFound(req, res);
11 years ago
}
11 years ago
if (!results.privileges.read) {
return helpers.notAllowed(req, res);
}
if ((!req.params.slug || results.categoryData.slug !== cid + '/' + req.params.slug) && (results.categoryData.slug && results.categoryData.slug !== cid + '/')) {
return helpers.redirect(res, '/category/' + encodeURI(results.categoryData.slug));
}
10 years ago
var topicIndex = utils.isNumber(req.params.topic_index) ? parseInt(req.params.topic_index, 10) - 1 : 0;
var topicCount = parseInt(results.categoryData.topic_count, 10);
10 years ago
10 years ago
if (topicIndex < 0 || topicIndex > Math.max(topicCount - 1, 0)) {
return helpers.redirect(res, '/category/' + cid + '/' + req.params.slug + (topicIndex > topicCount ? '/' + topicCount : ''));
10 years ago
}
userPrivileges = results.privileges;
11 years ago
var settings = results.userSettings;
if (!settings.usePagination) {
10 years ago
topicIndex = Math.max(topicIndex - (settings.topicsPerPage - 1), 0);
11 years ago
} else if (!req.query.page) {
10 years ago
var index = Math.max(parseInt((topicIndex || 0), 10), 0);
11 years ago
page = Math.ceil((index + 1) / settings.topicsPerPage);
10 years ago
topicIndex = 0;
11 years ago
}
10 years ago
var set = 'cid:' + cid + ':tids',
reverse = false;
if (settings.categoryTopicSort === 'newest_to_oldest') {
reverse = true;
} else if (settings.categoryTopicSort === 'most_posts') {
reverse = true;
set = 'cid:' + cid + ':tids:posts';
}
11 years ago
var start = (page - 1) * settings.topicsPerPage + topicIndex,
stop = start + settings.topicsPerPage - 1;
11 years ago
next(null, {
cid: cid,
10 years ago
set: set,
reverse: reverse,
start: start,
stop: stop,
uid: req.uid
});
},
function(payload, next) {
user.getUidByUserslug(req.query.author, function(err, uid) {
payload.targetUid = uid;
10 years ago
if (uid) {
payload.set = 'cid:' + cid + ':uid:' + uid + ':tids';
}
next(err, payload);
});
},
function(payload, next) {
categories.getCategoryById(payload, next);
},
function(categoryData, next) {
10 years ago
if (categoryData.link) {
db.incrObjectField('category:' + categoryData.cid, 'timesClicked');
return res.redirect(categoryData.link);
}
var breadcrumbs = [
{
text: categoryData.name,
url: nconf.get('relative_path') + '/category/' + categoryData.slug
}
];
10 years ago
helpers.buildCategoryBreadcrumbs(categoryData.parentCid, function(err, crumbs) {
if (err) {
return next(err);
}
categoryData.breadcrumbs = crumbs.concat(breadcrumbs);
next(null, categoryData);
});
},
function(categoryData, next) {
categories.getRecentTopicReplies(categoryData.children, req.uid, function(err) {
next(err, categoryData);
});
},
function (categoryData, next) {
categoryData.privileges = userPrivileges;
categoryData.showSelect = categoryData.privileges.editable;
res.locals.metaTags = [
{
name: 'title',
content: categoryData.name
},
{
property: 'og:title',
content: categoryData.name
},
{
name: 'description',
content: categoryData.description
},
{
property: "og:type",
content: 'website'
}
];
if (categoryData.backgroundImage) {
11 years ago
res.locals.metaTags.push({
name: 'og:image',
content: categoryData.backgroundImage
});
}
res.locals.linkTags = [
{
rel: 'alternate',
type: 'application/rss+xml',
href: nconf.get('url') + '/category/' + cid + '.rss'
},
{
rel: 'up',
href: nconf.get('url')
}
];
next(null, categoryData);
}
], function (err, data) {
if (err) {
11 years ago
return next(err);
}
data.currentPage = page;
11 years ago
data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1;
10 years ago
data.rssFeedUrl = nconf.get('relative_path') + '/category/' + data.cid + '.rss';
data.pagination = pagination.create(data.currentPage, data.pageCount);
10 years ago
data.pagination.rel.forEach(function(rel) {
res.locals.linkTags.push(rel);
});
11 years ago
plugins.fireHook('filter:category.build', {req: req, res: res, templateData: data}, function(err, data) {
if (err) {
return next(err);
}
res.render('category', data.templateData);
});
});
};
11 years ago
module.exports = categoriesController;