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.

323 lines
8.6 KiB
JavaScript

"use strict";
11 years ago
var topicsController = {},
async = require('async'),
S = require('string'),
validator = require('validator'),
nconf = require('nconf'),
11 years ago
qs = require('querystring'),
11 years ago
user = require('../user'),
meta = require('../meta'),
topics = require('../topics'),
11 years ago
posts = require('../posts'),
privileges = require('../privileges'),
plugins = require('../plugins'),
helpers = require('./helpers'),
10 years ago
pagination = require('../pagination'),
11 years ago
utils = require('../../public/src/utils');
11 years ago
10 years ago
topicsController.get = function(req, res, callback) {
11 years ago
var tid = req.params.topic_id,
sort = req.query.sort,
userPrivileges;
11 years ago
10 years ago
if ((req.params.post_index && !utils.isNumber(req.params.post_index)) || !utils.isNumber(tid)) {
10 years ago
return callback();
11 years ago
}
11 years ago
async.waterfall([
function (next) {
async.parallel({
11 years ago
privileges: function(next) {
privileges.topics.get(tid, req.uid, next);
11 years ago
},
settings: function(next) {
user.getSettings(req.uid, next);
11 years ago
},
topic: function(next) {
topics.getTopicFields(tid, ['slug', 'postcount', 'deleted'], next);
}
}, next);
},
function (results, next) {
10 years ago
if (!results.topic.slug) {
return callback();
}
11 years ago
userPrivileges = results.privileges;
10 years ago
if (!userPrivileges.read || (parseInt(results.topic.deleted, 10) && !userPrivileges.view_deleted)) {
return helpers.notAllowed(req, res);
}
if ((!req.params.slug || results.topic.slug !== tid + '/' + req.params.slug) && (results.topic.slug && results.topic.slug !== tid + '/')) {
return helpers.redirect(res, '/topic/' + encodeURI(results.topic.slug));
}
var settings = results.settings;
var postCount = parseInt(results.topic.postcount, 10);
var pageCount = Math.max(1, Math.ceil((postCount - 1) / settings.postsPerPage));
10 years ago
var page = parseInt(req.query.page, 10) || 1;
if (utils.isNumber(req.params.post_index) && (req.params.post_index < 1 || req.params.post_index > postCount)) {
return helpers.redirect(res, '/topic/' + req.params.topic_id + '/' + req.params.slug + (req.params.post_index > postCount ? '/' + postCount : ''));
10 years ago
}
10 years ago
if (settings.usePagination && (page < 1 || page > pageCount)) {
10 years ago
return callback();
}
var set = 'tid:' + tid + ':posts',
reverse = false;
// `sort` qs has priority over user setting
if (sort === 'oldest_to_newest') {
reverse = false;
} else if (sort === 'newest_to_oldest') {
reverse = true;
} else if (sort === 'most_votes') {
reverse = true;
set = 'tid:' + tid + ':posts:votes';
} else if (settings.topicPostSort === 'newest_to_oldest') {
reverse = true;
} else if (settings.topicPostSort === 'most_votes') {
reverse = true;
set = 'tid:' + tid + ':posts:votes';
}
10 years ago
var postIndex = 0;
10 years ago
11 years ago
req.params.post_index = parseInt(req.params.post_index, 10) || 0;
if (reverse && req.params.post_index === 1) {
req.params.post_index = 0;
}
if (!settings.usePagination) {
if (reverse) {
postIndex = Math.max(0, postCount - (req.params.post_index || postCount) - Math.ceil(settings.postsPerPage / 2));
} else {
postIndex = Math.max(0, (req.params.post_index || 1) - Math.ceil(settings.postsPerPage / 2));
}
} else if (!req.query.page) {
10 years ago
var index;
if (reverse) {
index = Math.max(0, postCount - (req.params.post_index || postCount));
} else {
index = Math.max(0, req.params.post_index - 1) || 0;
}
11 years ago
page = Math.max(1, Math.ceil(index / settings.postsPerPage));
}
var start = (page - 1) * settings.postsPerPage + postIndex,
stop = start + settings.postsPerPage - 1;
topics.getTopicWithPosts(tid, set, req.uid, start, stop, reverse, function (err, topicData) {
11 years ago
if (err && err.message === '[[error:no-topic]]' && !topicData) {
10 years ago
return callback();
11 years ago
}
11 years ago
if (err && !topicData) {
return next(err);
}
11 years ago
topicData.pageCount = pageCount;
11 years ago
topicData.currentPage = page;
if (page > 1) {
11 years ago
topicData.posts.splice(0, 1);
}
plugins.fireHook('filter:controllers.topic.get', topicData, next);
11 years ago
});
},
function (topicData, next) {
var breadcrumbs = [
{
text: topicData.category.name,
url: nconf.get('relative_path') + '/category/' + topicData.category.slug
},
{
text: topicData.title,
url: nconf.get('relative_path') + '/topic/' + topicData.slug
}
];
10 years ago
helpers.buildCategoryBreadcrumbs(topicData.category.parentCid, function(err, crumbs) {
if (err) {
return next(err);
}
topicData.breadcrumbs = crumbs.concat(breadcrumbs);
next(null, topicData);
});
},
11 years ago
function (topicData, next) {
10 years ago
function findPost(index) {
for(var i=0; i<topicData.posts.length; ++i) {
if (parseInt(topicData.posts[i].index, 10) === parseInt(index, 10)) {
return topicData.posts[i];
}
}
}
var description = '';
var postAtIndex = findPost(Math.max(0, req.params.post_index - 1));
11 years ago
10 years ago
if (postAtIndex && postAtIndex.content) {
description = S(postAtIndex.content).stripTags().decodeHTMLEntities().s;
}
if (description.length > 255) {
description = description.substr(0, 255) + '...';
}
11 years ago
description = validator.escape(description);
11 years ago
var ogImageUrl = '';
if (topicData.thumb) {
ogImageUrl = topicData.thumb;
10 years ago
} else if (postAtIndex && postAtIndex.user && postAtIndex.user.picture){
ogImageUrl = postAtIndex.user.picture;
} else if (meta.config['brand:logo']) {
ogImageUrl = meta.config['brand:logo'];
} else {
ogImageUrl = '/logo.png';
11 years ago
}
if (ogImageUrl.indexOf('http') === -1) {
ogImageUrl = nconf.get('url') + ogImageUrl;
}
11 years ago
description = description.replace(/\n/g, ' ');
res.locals.metaTags = [
{
name: "title",
content: topicData.title
},
{
name: "description",
content: description
},
{
property: 'og:title',
content: topicData.title.replace(/&amp;/g, '&')
},
{
property: 'og:description',
content: description
},
{
property: "og:type",
content: 'article'
},
{
property: "og:url",
10 years ago
content: nconf.get('url') + '/topic/' + topicData.slug + (req.params.post_index ? ('/' + req.params.post_index) : '')
},
{
property: 'og:image',
content: ogImageUrl
},
{
property: "og:image:url",
content: ogImageUrl
},
{
property: "article:published_time",
content: utils.toISOString(topicData.timestamp)
},
{
property: 'article:modified_time',
content: utils.toISOString(topicData.lastposttime)
},
{
property: 'article:section',
11 years ago
content: topicData.category ? topicData.category.name : ''
}
];
res.locals.linkTags = [
{
rel: 'alternate',
type: 'application/rss+xml',
href: nconf.get('url') + '/topic/' + tid + '.rss'
11 years ago
},
{
rel: 'canonical',
href: nconf.get('url') + '/topic/' + topicData.slug
}
];
if (topicData.category) {
res.locals.linkTags.push({
rel: 'up',
href: nconf.get('url') + '/category/' + topicData.category.slug
});
}
next(null, topicData);
11 years ago
}
], function (err, data) {
11 years ago
if (err) {
10 years ago
return callback(err);
11 years ago
}
data.privileges = userPrivileges;
11 years ago
data['reputation:disabled'] = parseInt(meta.config['reputation:disabled'], 10) === 1;
data['downvote:disabled'] = parseInt(meta.config['downvote:disabled'], 10) === 1;
data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1;
data.rssFeedUrl = nconf.get('relative_path') + '/topic/' + data.tid + '.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
topics.increaseViewCount(tid);
plugins.fireHook('filter:topic.build', {req: req, res: res, templateData: data}, function(err, data) {
10 years ago
if (err) {
10 years ago
return callback(err);
}
res.render('topic', data.templateData);
});
11 years ago
});
};
11 years ago
topicsController.teaser = function(req, res, next) {
var tid = req.params.topic_id;
11 years ago
if (!utils.isNumber(tid)) {
return next(new Error('[[error:invalid-tid]]'));
}
async.waterfall([
function(next) {
privileges.topics.can('read', tid, req.uid, next);
},
function(canRead, next) {
if (!canRead) {
return res.status(403).json('[[error:no-privileges]]');
11 years ago
}
topics.getLatestUndeletedPid(tid, next);
},
function(pid, next) {
11 years ago
if (!pid) {
return res.status(404).json('not-found');
11 years ago
}
posts.getPostSummaryByPids([pid], req.uid, {stripTags: false}, next);
}
], function(err, posts) {
if (err) {
return next(err);
}
11 years ago
if (!Array.isArray(posts) || !posts.length) {
return res.status(404).json('not-found');
}
res.json(posts[0]);
11 years ago
});
};
10 years ago
module.exports = topicsController;