2014-03-02 15:14:38 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
2016-02-18 18:32:08 +02:00
|
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
|
var S = require('string');
|
|
|
|
|
var nconf = require('nconf');
|
|
|
|
|
var validator = require('validator');
|
|
|
|
|
|
|
|
|
|
var user = require('../user');
|
|
|
|
|
var meta = require('../meta');
|
|
|
|
|
var topics = require('../topics');
|
|
|
|
|
var posts = require('../posts');
|
|
|
|
|
var privileges = require('../privileges');
|
|
|
|
|
var plugins = require('../plugins');
|
|
|
|
|
var helpers = require('./helpers');
|
|
|
|
|
var pagination = require('../pagination');
|
|
|
|
|
var utils = require('../../public/src/utils');
|
|
|
|
|
|
|
|
|
|
var topicsController = {};
|
2014-02-27 14:56:05 -05:00
|
|
|
|
2015-08-28 14:31:35 -04:00
|
|
|
topicsController.get = function(req, res, callback) {
|
2016-02-18 18:32:08 +02:00
|
|
|
var tid = req.params.topic_id;
|
|
|
|
|
var sort = req.query.sort;
|
|
|
|
|
var currentPage = parseInt(req.query.page, 10) || 1;
|
|
|
|
|
var pageCount = 1;
|
|
|
|
|
var userPrivileges;
|
2014-02-27 14:56:05 -05:00
|
|
|
|
2015-05-04 13:16:31 -04:00
|
|
|
if ((req.params.post_index && !utils.isNumber(req.params.post_index)) || !utils.isNumber(tid)) {
|
2015-08-28 14:31:35 -04:00
|
|
|
return callback();
|
2014-09-24 15:42:45 -04:00
|
|
|
}
|
|
|
|
|
|
2014-02-27 14:56:05 -05:00
|
|
|
async.waterfall([
|
2014-06-02 17:31:09 -04:00
|
|
|
function (next) {
|
2014-08-16 21:33:42 -04:00
|
|
|
async.parallel({
|
2014-09-18 17:22:20 -04:00
|
|
|
privileges: function(next) {
|
2015-04-01 00:23:57 -04:00
|
|
|
privileges.topics.get(tid, req.uid, next);
|
2014-09-18 17:22:20 -04:00
|
|
|
},
|
2014-08-16 21:33:42 -04:00
|
|
|
settings: function(next) {
|
2015-04-01 00:23:57 -04:00
|
|
|
user.getSettings(req.uid, next);
|
2014-09-24 15:42:45 -04:00
|
|
|
},
|
2014-11-04 14:54:30 -05:00
|
|
|
topic: function(next) {
|
2016-01-21 20:51:07 +02:00
|
|
|
topics.getTopicData(tid, next);
|
2014-08-16 21:33:42 -04:00
|
|
|
}
|
|
|
|
|
}, next);
|
2014-06-02 20:41:03 -04:00
|
|
|
},
|
2014-08-16 21:33:42 -04:00
|
|
|
function (results, next) {
|
2016-01-21 20:51:07 +02:00
|
|
|
if (!results.topic) {
|
2015-09-17 13:37:04 -04:00
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-18 17:22:20 -04:00
|
|
|
userPrivileges = results.privileges;
|
|
|
|
|
|
2014-12-01 20:28:36 -05:00
|
|
|
if (!userPrivileges.read || (parseInt(results.topic.deleted, 10) && !userPrivileges.view_deleted)) {
|
2014-11-16 00:09:43 -05:00
|
|
|
return helpers.notAllowed(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-18 14:47:04 -04:00
|
|
|
if ((!req.params.slug || results.topic.slug !== tid + '/' + req.params.slug) && (results.topic.slug && results.topic.slug !== tid + '/')) {
|
2015-04-08 21:16:06 -04:00
|
|
|
return helpers.redirect(res, '/topic/' + encodeURI(results.topic.slug));
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-01 17:55:39 -04:00
|
|
|
var settings = results.settings;
|
2014-11-04 14:54:30 -05:00
|
|
|
var postCount = parseInt(results.topic.postcount, 10);
|
2015-10-07 16:13:37 -04:00
|
|
|
pageCount = Math.max(1, Math.ceil((postCount - 1) / settings.postsPerPage));
|
2014-11-01 17:55:39 -04:00
|
|
|
|
2015-03-09 18:22:44 -04:00
|
|
|
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 : ''));
|
2014-10-30 23:14:33 -04:00
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:13:37 -04:00
|
|
|
if (settings.usePagination && (currentPage < 1 || currentPage > pageCount)) {
|
2015-08-28 14:31:35 -04:00
|
|
|
return callback();
|
2014-11-01 17:55:39 -04:00
|
|
|
}
|
|
|
|
|
|
2014-06-06 22:12:14 -04:00
|
|
|
var set = 'tid:' + tid + ':posts',
|
|
|
|
|
reverse = false;
|
|
|
|
|
|
2014-09-03 12:49:34 -04:00
|
|
|
// `sort` qs has priority over user setting
|
2015-10-27 17:03:14 -04:00
|
|
|
if (sort === 'newest_to_oldest') {
|
2014-09-03 12:49:34 -04:00
|
|
|
reverse = true;
|
|
|
|
|
} else if (sort === 'most_votes') {
|
|
|
|
|
reverse = true;
|
|
|
|
|
set = 'tid:' + tid + ':posts:votes';
|
|
|
|
|
} else if (settings.topicPostSort === 'newest_to_oldest') {
|
2014-06-06 22:12:14 -04:00
|
|
|
reverse = true;
|
|
|
|
|
} else if (settings.topicPostSort === 'most_votes') {
|
|
|
|
|
reverse = true;
|
|
|
|
|
set = 'tid:' + tid + ':posts:votes';
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-22 20:19:43 -04:00
|
|
|
var postIndex = 0;
|
2015-04-24 10:59:00 -04:00
|
|
|
|
2014-09-06 04:09:13 -04:00
|
|
|
req.params.post_index = parseInt(req.params.post_index, 10) || 0;
|
2015-02-07 00:52:53 -05:00
|
|
|
if (reverse && req.params.post_index === 1) {
|
|
|
|
|
req.params.post_index = 0;
|
|
|
|
|
}
|
2014-08-16 21:33:42 -04:00
|
|
|
if (!settings.usePagination) {
|
|
|
|
|
if (reverse) {
|
2015-07-06 13:01:26 -04:00
|
|
|
postIndex = Math.max(0, postCount - (req.params.post_index || postCount) - Math.ceil(settings.postsPerPage / 2));
|
2014-08-16 21:33:42 -04:00
|
|
|
} else {
|
2015-07-06 13:01:26 -04:00
|
|
|
postIndex = Math.max(0, (req.params.post_index || 1) - Math.ceil(settings.postsPerPage / 2));
|
2014-08-16 21:33:42 -04:00
|
|
|
}
|
|
|
|
|
} else if (!req.query.page) {
|
2015-09-22 20:19:43 -04:00
|
|
|
var index;
|
2015-02-07 00:52:53 -05:00
|
|
|
if (reverse) {
|
|
|
|
|
index = Math.max(0, postCount - (req.params.post_index || postCount));
|
|
|
|
|
} else {
|
|
|
|
|
index = Math.max(0, req.params.post_index - 1) || 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:13:37 -04:00
|
|
|
currentPage = Math.max(1, Math.ceil(index / settings.postsPerPage));
|
2014-08-16 21:33:42 -04:00
|
|
|
}
|
|
|
|
|
|
2016-01-21 20:51:07 +02:00
|
|
|
var start = (currentPage - 1) * settings.postsPerPage + postIndex;
|
|
|
|
|
var stop = start + settings.postsPerPage - 1;
|
2014-08-16 21:33:42 -04:00
|
|
|
|
2016-01-21 20:51:07 +02:00
|
|
|
topics.getTopicWithPosts(results.topic, set, req.uid, start, stop, reverse, next);
|
|
|
|
|
},
|
|
|
|
|
function (topicData, next) {
|
|
|
|
|
if (topicData.category.disabled) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2015-11-17 09:10:20 +01:00
|
|
|
|
2016-01-27 20:36:40 +02:00
|
|
|
topics.modifyPostsByPrivilege(topicData.posts, userPrivileges);
|
2015-10-06 18:36:03 -04:00
|
|
|
|
2016-01-21 20:51:07 +02:00
|
|
|
plugins.fireHook('filter:controllers.topic.get', {topicData: topicData, uid: req.uid}, next);
|
2014-02-27 14:56:05 -05:00
|
|
|
},
|
2016-01-21 10:47:40 +02:00
|
|
|
function (data, next) {
|
2015-11-05 12:34:39 -05:00
|
|
|
|
2014-12-11 22:55:00 -05:00
|
|
|
var breadcrumbs = [
|
|
|
|
|
{
|
2016-01-21 10:47:40 +02:00
|
|
|
text: data.topicData.category.name,
|
|
|
|
|
url: nconf.get('relative_path') + '/category/' + data.topicData.category.slug
|
2014-12-11 22:55:00 -05:00
|
|
|
},
|
|
|
|
|
{
|
2016-02-18 18:32:08 +02:00
|
|
|
text: validator.escape(data.topicData.title)
|
2014-12-11 22:55:00 -05:00
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
2016-01-21 10:47:40 +02:00
|
|
|
helpers.buildCategoryBreadcrumbs(data.topicData.category.parentCid, function(err, crumbs) {
|
2014-12-11 22:55:00 -05:00
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
2016-01-21 10:47:40 +02:00
|
|
|
data.topicData.breadcrumbs = crumbs.concat(breadcrumbs);
|
|
|
|
|
next(null, data.topicData);
|
2014-12-11 22:55:00 -05:00
|
|
|
});
|
|
|
|
|
},
|
2014-02-27 14:56:05 -05:00
|
|
|
function (topicData, next) {
|
2015-09-22 20:19:43 -04:00
|
|
|
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));
|
2014-02-27 14:56:05 -05:00
|
|
|
|
2015-09-22 20:19:43 -04:00
|
|
|
if (postAtIndex && postAtIndex.content) {
|
2015-09-23 01:17:31 -04:00
|
|
|
description = S(postAtIndex.content).decodeHTMLEntities().stripTags().s;
|
2014-03-21 15:40:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (description.length > 255) {
|
|
|
|
|
description = description.substr(0, 255) + '...';
|
|
|
|
|
}
|
2014-02-27 14:56:05 -05:00
|
|
|
|
2014-03-24 20:43:24 -04:00
|
|
|
var ogImageUrl = '';
|
|
|
|
|
if (topicData.thumb) {
|
|
|
|
|
ogImageUrl = topicData.thumb;
|
2015-09-22 20:19:43 -04:00
|
|
|
} else if (postAtIndex && postAtIndex.user && postAtIndex.user.picture){
|
|
|
|
|
ogImageUrl = postAtIndex.user.picture;
|
|
|
|
|
} else if (meta.config['brand:logo']) {
|
2014-03-24 20:43:24 -04:00
|
|
|
ogImageUrl = meta.config['brand:logo'];
|
|
|
|
|
} else {
|
|
|
|
|
ogImageUrl = '/logo.png';
|
2014-02-27 14:56:05 -05:00
|
|
|
}
|
|
|
|
|
|
2015-11-17 15:49:01 -05:00
|
|
|
if (typeof ogImageUrl === 'string' && ogImageUrl.indexOf('http') === -1) {
|
2014-03-24 20:43:24 -04:00
|
|
|
ogImageUrl = nconf.get('url') + ogImageUrl;
|
2014-03-23 17:17:56 -04:00
|
|
|
}
|
|
|
|
|
|
2014-04-30 13:42:49 -04:00
|
|
|
description = description.replace(/\n/g, ' ');
|
|
|
|
|
|
2014-03-02 15:14:38 -05:00
|
|
|
res.locals.metaTags = [
|
|
|
|
|
{
|
|
|
|
|
name: "title",
|
|
|
|
|
content: topicData.title
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "description",
|
|
|
|
|
content: description
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: 'og:title',
|
2016-02-18 18:32:08 +02:00
|
|
|
content: topicData.title
|
2014-03-02 15:14:38 -05:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: 'og:description',
|
|
|
|
|
content: description
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: "og:type",
|
|
|
|
|
content: 'article'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: "og:url",
|
2015-11-11 14:20:43 -05:00
|
|
|
content: nconf.get('url') + '/topic/' + topicData.slug + (req.params.post_index ? ('/' + req.params.post_index) : ''),
|
|
|
|
|
noEscape: true
|
2014-03-02 15:14:38 -05:00
|
|
|
},
|
|
|
|
|
{
|
2014-03-24 20:43:24 -04:00
|
|
|
property: 'og:image',
|
2015-11-11 14:20:43 -05:00
|
|
|
content: ogImageUrl,
|
|
|
|
|
noEscape: true
|
2014-03-02 15:14:38 -05:00
|
|
|
},
|
|
|
|
|
{
|
2014-03-24 20:43:24 -04:00
|
|
|
property: "og:image:url",
|
2015-11-11 14:20:43 -05:00
|
|
|
content: ogImageUrl,
|
|
|
|
|
noEscape: true
|
2014-03-02 15:14:38 -05:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: "article:published_time",
|
|
|
|
|
content: utils.toISOString(topicData.timestamp)
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: 'article:modified_time',
|
2014-03-21 17:48:32 -04:00
|
|
|
content: utils.toISOString(topicData.lastposttime)
|
2014-03-02 15:14:38 -05:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
property: 'article:section',
|
2014-09-21 11:29:09 -04:00
|
|
|
content: topicData.category ? topicData.category.name : ''
|
2014-03-02 15:14:38 -05:00
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
res.locals.linkTags = [
|
|
|
|
|
{
|
|
|
|
|
rel: 'alternate',
|
|
|
|
|
type: 'application/rss+xml',
|
|
|
|
|
href: nconf.get('url') + '/topic/' + tid + '.rss'
|
|
|
|
|
}
|
|
|
|
|
];
|
2014-03-10 18:28:34 -04:00
|
|
|
|
2014-09-22 16:08:40 -04:00
|
|
|
if (topicData.category) {
|
|
|
|
|
res.locals.linkTags.push({
|
|
|
|
|
rel: 'up',
|
|
|
|
|
href: nconf.get('url') + '/category/' + topicData.category.slug
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-02 22:10:57 -05:00
|
|
|
next(null, topicData);
|
2014-02-27 14:56:05 -05:00
|
|
|
}
|
2014-03-02 22:54:03 -05:00
|
|
|
], function (err, data) {
|
2014-02-27 14:56:05 -05:00
|
|
|
if (err) {
|
2015-08-28 14:31:35 -04:00
|
|
|
return callback(err);
|
2014-02-27 14:56:05 -05:00
|
|
|
}
|
|
|
|
|
|
2014-05-15 10:38:02 -04:00
|
|
|
data.privileges = userPrivileges;
|
2016-02-10 12:12:13 +02:00
|
|
|
data.topicStaleDays = parseInt(meta.config.topicStaleDays, 10) || 60;
|
2014-08-30 15:19:18 -04:00
|
|
|
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;
|
2015-03-31 23:40:58 -04:00
|
|
|
data.rssFeedUrl = nconf.get('relative_path') + '/topic/' + data.tid + '.rss';
|
2015-10-07 16:13:37 -04:00
|
|
|
data.pagination = pagination.create(currentPage, pageCount);
|
2015-01-28 13:46:07 -05:00
|
|
|
data.pagination.rel.forEach(function(rel) {
|
2015-09-29 20:30:42 -04:00
|
|
|
rel.href = nconf.get('url') + '/topic/' + data.slug + rel.href;
|
2015-01-28 13:46:07 -05:00
|
|
|
res.locals.linkTags.push(rel);
|
|
|
|
|
});
|
2014-09-18 17:22:20 -04:00
|
|
|
|
2016-01-03 14:37:37 +02:00
|
|
|
req.session.tids_viewed = req.session.tids_viewed || {};
|
|
|
|
|
if (!req.session.tids_viewed[tid] || req.session.tids_viewed[tid] < Date.now() - 3600000) {
|
|
|
|
|
topics.increaseViewCount(tid);
|
|
|
|
|
req.session.tids_viewed[tid] = Date.now();
|
|
|
|
|
}
|
2015-03-09 18:22:44 -04:00
|
|
|
|
2015-11-05 11:53:17 -05:00
|
|
|
if (req.uid) {
|
|
|
|
|
topics.markAsRead([tid], req.uid, function(err, markedRead) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
if (markedRead) {
|
|
|
|
|
topics.pushUnreadCount(req.uid);
|
|
|
|
|
topics.markTopicNotificationsRead(tid, req.uid);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-08 11:58:01 +01:00
|
|
|
plugins.fireHook('filter:topic.build', {req: req, res: res, templateData: data}, function(err, data) {
|
2015-02-18 15:26:05 +01:00
|
|
|
if (err) {
|
2015-08-28 14:31:35 -04:00
|
|
|
return callback(err);
|
2015-02-08 11:58:01 +01:00
|
|
|
}
|
|
|
|
|
res.render('topic', data.templateData);
|
|
|
|
|
});
|
2014-02-27 14:56:05 -05:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-27 16:41:40 -04:00
|
|
|
topicsController.teaser = function(req, res, next) {
|
|
|
|
|
var tid = req.params.topic_id;
|
2014-09-22 21:54:28 -04:00
|
|
|
|
|
|
|
|
if (!utils.isNumber(tid)) {
|
|
|
|
|
return next(new Error('[[error:invalid-tid]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 23:40:58 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
function(next) {
|
2015-04-01 00:23:57 -04:00
|
|
|
privileges.topics.can('read', tid, req.uid, next);
|
2015-03-31 23:40:58 -04:00
|
|
|
},
|
|
|
|
|
function(canRead, next) {
|
|
|
|
|
if (!canRead) {
|
|
|
|
|
return res.status(403).json('[[error:no-privileges]]');
|
2014-04-27 16:41:40 -04:00
|
|
|
}
|
2015-03-31 23:40:58 -04:00
|
|
|
topics.getLatestUndeletedPid(tid, next);
|
|
|
|
|
},
|
|
|
|
|
function(pid, next) {
|
2014-09-22 21:54:28 -04:00
|
|
|
if (!pid) {
|
2014-10-04 18:47:56 -04:00
|
|
|
return res.status(404).json('not-found');
|
2014-04-27 16:41:40 -04:00
|
|
|
}
|
2015-04-01 00:23:57 -04:00
|
|
|
posts.getPostSummaryByPids([pid], req.uid, {stripTags: false}, next);
|
2015-03-31 23:40:58 -04:00
|
|
|
}
|
|
|
|
|
], function(err, posts) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
2014-04-27 16:41:40 -04:00
|
|
|
|
2015-03-31 23:40:58 -04:00
|
|
|
if (!Array.isArray(posts) || !posts.length) {
|
|
|
|
|
return res.status(404).json('not-found');
|
|
|
|
|
}
|
|
|
|
|
res.json(posts[0]);
|
2014-04-27 16:41:40 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-20 16:06:17 -04:00
|
|
|
|
2014-04-10 20:31:57 +01:00
|
|
|
module.exports = topicsController;
|