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.

185 lines
4.2 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'),
user = require('./../user'),
meta = require('./../meta'),
topics = require('./../topics'),
threadTools = require('./../threadTools'),
utils = require('./../../public/src/utils');
11 years ago
topicsController.get = function(req, res, next) {
var tid = req.params.topic_id,
page = req.query.page || 1,
uid = req.user ? req.user.uid : 0,
privileges;
11 years ago
async.waterfall([
function(next) {
threadTools.privileges(tid, uid, function(err, userPrivileges) {
if (err) {
return next(err);
11 years ago
}
if (!userPrivileges.read) {
return next(new Error('[[error:no-privileges]]'));
}
privileges = userPrivileges;
next();
11 years ago
});
},
function (next) {
user.getSettings(uid, function(err, settings) {
if (err) {
return next(err);
}
var start = (page - 1) * settings.postsPerPage,
end = start + settings.postsPerPage - 1;
11 years ago
topics.getTopicWithPosts(tid, uid, start, end, function (err, topicData) {
if (topicData) {
if (parseInt(topicData.deleted, 10) === 1 && parseInt(topicData.expose_tools, 10) === 0) {
return next(new Error('[[error:no-topic]]'));
11 years ago
}
topicData.currentPage = page;
11 years ago
}
next(err, topicData);
});
});
},
function (topicData, next) {
var description = '';
11 years ago
if(topicData.posts.length) {
11 years ago
description = S(topicData.posts[0].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;
} else if(topicData.posts.length && topicData.posts[0].user && topicData.posts[0].user.picture){
ogImageUrl = topicData.posts[0].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;
}
res.locals.metaTags = [
{
name: "title",
content: topicData.title
},
{
name: "description",
content: description
},
{
property: 'og:title',
content: topicData.title
},
{
property: 'og:description',
content: description
},
{
property: "og:type",
content: 'article'
},
{
property: "og:url",
content: nconf.get('url') + '/topic/' + topicData.slug
},
{
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',
content: topicData.category.name
}
];
res.locals.linkTags = [
{
rel: 'alternate',
type: 'application/rss+xml',
href: nconf.get('url') + '/topic/' + tid + '.rss'
},
{
rel: 'up',
href: nconf.get('url') + '/category/' + topicData.category.slug
}
];
next(null, topicData);
11 years ago
}
], function (err, data) {
11 years ago
if (err) {
if (err.message === 'not-enough-privileges') {
return res.locals.isAPI ? res.json(403, err.message) : res.redirect('403');
11 years ago
} else {
return res.locals.isAPI ? res.json(404, 'not-found') : res.redirect('404');
11 years ago
}
}
data.privileges = privileges;
11 years ago
var topic_url = tid + (req.params.slug ? '/' + req.params.slug : '');
var queryString = qs.stringify(req.query);
if(queryString.length) {
topic_url += '?' + queryString;
}
if (uid) {
topics.markAsRead(tid, uid, function(err) {
topics.pushUnreadCount(uid);
topics.markTopicNotificationsRead(tid, uid);
});
}
topics.increaseViewCount(tid);
11 years ago
// Paginator for noscript
data.pages = [];
for(var x=1; x<=data.pageCount; x++) {
data.pages.push({
11 years ago
page: x,
active: x === parseInt(page, 10)
});
}
res.render('topic', data);
11 years ago
});
};
module.exports = topicsController;