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.

366 lines
10 KiB
JavaScript

8 years ago
'use strict';
var async = require('async');
var S = require('string');
var nconf = require('nconf');
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 = {};
11 years ago
topicsController.get = function (req, res, callback) {
var tid = req.params.topic_id;
var currentPage = parseInt(req.query.page, 10) || 1;
var pageCount = 1;
var userPrivileges;
9 years ago
var settings;
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({
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.getTopicData(tid, next);
},
}, next);
},
function (results, next) {
if (!results.topic) {
10 years ago
return callback();
}
11 years ago
userPrivileges = results.privileges;
if (!userPrivileges.read || !userPrivileges['topics:read'] || (parseInt(results.topic.deleted, 10) && !userPrivileges.view_deleted)) {
return helpers.notAllowed(req, res);
}
if (!res.locals.isAPI && (!req.params.slug || results.topic.slug !== tid + '/' + req.params.slug) && (results.topic.slug && results.topic.slug !== tid + '/')) {
var url = '/topic/' + results.topic.slug;
if (req.params.post_index) {
url += '/' + req.params.post_index;
}
if (currentPage > 1) {
url += '?page=' + currentPage;
}
return helpers.redirect(res, url);
}
9 years ago
settings = results.settings;
var postCount = parseInt(results.topic.postcount, 10);
pageCount = Math.max(1, Math.ceil(postCount / settings.postsPerPage));
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 : ''));
11 years ago
}
10 years ago
if (settings.usePagination && (currentPage < 1 || currentPage > pageCount)) {
10 years ago
return callback();
}
var set = 'tid:' + tid + ':posts';
var reverse = false;
// `sort` qs has priority over user setting
var sort = req.query.sort || settings.topicPostSort;
if (sort === 'newest_to_oldest') {
reverse = true;
} else if (sort === '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 (req.params.post_index !== 0) {
currentPage = 1;
}
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) + 2);
} else {
index = Math.max(0, req.params.post_index) || 0;
}
10 years ago
currentPage = Math.max(1, Math.ceil(index / settings.postsPerPage));
}
var start = ((currentPage - 1) * settings.postsPerPage) + postIndex;
var stop = start + settings.postsPerPage - 1;
topics.getTopicWithPosts(results.topic, set, req.uid, start, stop, reverse, next);
},
function (topicData, next) {
if (topicData.category.disabled) {
return callback();
}
9 years ago
topics.modifyPostsByPrivilege(topicData, userPrivileges);
plugins.fireHook('filter:controllers.topic.get', { topicData: topicData, uid: req.uid }, next);
11 years ago
},
function (data, next) {
var breadcrumbs = [
{
text: data.topicData.category.name,
url: nconf.get('relative_path') + '/category/' + data.topicData.category.slug,
},
{
text: data.topicData.title,
},
];
helpers.buildCategoryBreadcrumbs(data.topicData.category.parentCid, function (err, crumbs) {
if (err) {
return next(err);
}
data.topicData.breadcrumbs = crumbs.concat(breadcrumbs);
next(null, data.topicData);
});
},
11 years ago
function (topicData, next) {
10 years ago
function findPost(index) {
for (var i = 0; i < topicData.posts.length; i += 1) {
10 years ago
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).decodeHTMLEntities().stripTags().s;
}
if (description.length > 255) {
description = description.substr(0, 255) + '...';
}
11 years ago
var ogImageUrl = '';
if (topicData.thumb) {
ogImageUrl = topicData.thumb;
9 years ago
} else if (postAtIndex && postAtIndex.user && postAtIndex.user.picture) {
10 years ago
ogImageUrl = postAtIndex.user.picture;
9 years ago
} else if (meta.config['og:image']) {
ogImageUrl = meta.config['og:image'];
10 years ago
} else if (meta.config['brand:logo']) {
ogImageUrl = meta.config['brand:logo'];
} else {
ogImageUrl = '/logo.png';
11 years ago
}
9 years ago
if (typeof ogImageUrl === 'string' && ogImageUrl.indexOf('http') === -1) {
ogImageUrl = nconf.get('url') + ogImageUrl;
}
11 years ago
description = description.replace(/\n/g, ' ');
res.locals.metaTags = [
{
8 years ago
name: 'title',
content: topicData.titleRaw,
},
{
8 years ago
name: 'description',
content: description,
},
{
property: 'og:title',
content: topicData.titleRaw,
},
{
property: 'og:description',
content: description,
},
{
8 years ago
property: 'og:type',
content: 'article',
},
{
8 years ago
property: 'og:url',
content: nconf.get('url') + '/topic/' + topicData.slug + (req.params.post_index ? ('/' + req.params.post_index) : ''),
noEscape: true,
},
{
property: 'og:image',
content: ogImageUrl,
noEscape: true,
},
{
8 years ago
property: 'og:image:url',
content: ogImageUrl,
noEscape: true,
},
{
8 years ago
property: 'article:published_time',
content: utils.toISOString(topicData.timestamp),
},
{
property: 'article:modified_time',
content: utils.toISOString(topicData.lastposttime),
},
{
property: 'article:section',
content: topicData.category ? topicData.category.name : '',
},
];
res.locals.linkTags = [
{
rel: 'alternate',
type: 'application/rss+xml',
href: nconf.get('url') + '/topic/' + tid + '.rss',
},
];
if (topicData.category) {
res.locals.linkTags.push({
rel: 'up',
href: nconf.get('url') + '/category/' + topicData.category.slug,
});
}
next(null, topicData);
},
], function (err, data) {
11 years ago
if (err) {
10 years ago
return callback(err);
11 years ago
}
data.privileges = userPrivileges;
data.topicStaleDays = parseInt(meta.config.topicStaleDays, 10) || 60;
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;
9 years ago
data.bookmarkThreshold = parseInt(meta.config.bookmarkThreshold, 10) || 5;
9 years ago
data.postEditDuration = parseInt(meta.config.postEditDuration, 10) || 0;
data.postDeleteDuration = parseInt(meta.config.postDeleteDuration, 10) || 0;
9 years ago
data.scrollToMyPost = settings.scrollToMyPost;
data.rssFeedUrl = nconf.get('relative_path') + '/topic/' + data.tid + '.rss';
data.pagination = pagination.create(currentPage, pageCount, req.query);
data.pagination.rel.forEach(function (rel) {
10 years ago
rel.href = nconf.get('url') + '/topic/' + data.slug + rel.href;
10 years ago
res.locals.linkTags.push(rel);
});
11 years ago
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();
}
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);
}
});
}
9 years ago
res.render('topic', data);
11 years ago
});
};
topicsController.teaser = function (req, res, next) {
11 years ago
var tid = req.params.topic_id;
11 years ago
if (!utils.isNumber(tid)) {
return next();
11 years ago
}
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
});
};
topicsController.pagination = function (req, res, callback) {
9 years ago
var tid = req.params.topic_id;
var currentPage = parseInt(req.query.page, 10) || 1;
if (!utils.isNumber(tid)) {
return callback();
}
async.parallel({
privileges: async.apply(privileges.topics.get, tid, req.uid),
settings: async.apply(user.getSettings, req.uid),
topic: async.apply(topics.getTopicData, tid),
9 years ago
}, function (err, results) {
if (err || !results.topic) {
return callback(err);
}
if (!results.privileges.read || (parseInt(results.topic.deleted, 10) && !results.privileges.view_deleted)) {
return helpers.notAllowed(req, res);
}
var postCount = parseInt(results.topic.postcount, 10);
var pageCount = Math.max(1, Math.ceil((postCount - 1) / results.settings.postsPerPage));
var paginationData = pagination.create(currentPage, pageCount);
paginationData.rel.forEach(function (rel) {
9 years ago
rel.href = nconf.get('url') + '/topic/' + results.topic.slug + rel.href;
});
res.json(paginationData);
});
};
10 years ago
module.exports = topicsController;