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.

340 lines
9.9 KiB
JavaScript

8 years ago
'use strict';
const nconf = require('nconf');
const winston = require('winston');
const user = require('../user');
const meta = require('../meta');
const topics = require('../topics');
const posts = require('../posts');
const privileges = require('../privileges');
const plugins = require('../plugins');
const helpers = require('./helpers');
const pagination = require('../pagination');
const utils = require('../utils');
const analytics = require('../analytics');
const topicsController = module.exports;
11 years ago
topicsController.get = async function getTopic(req, res, callback) {
const tid = req.params.topic_id;
11 years ago
if ((req.params.post_index && !utils.isNumber(req.params.post_index) && req.params.post_index !== 'unread') || !utils.isNumber(tid)) {
10 years ago
return callback();
11 years ago
}
let postIndex = parseInt(req.params.post_index, 10) || 1;
const [
userPrivileges,
settings,
topicData,
rssToken,
] = await Promise.all([
Async refactor in place (#7736) * feat: allow both callback&and await * feat: ignore async key * feat: callbackify and promisify in same file * Revert "feat: callbackify and promisify in same file" This reverts commit cea206a9b8e6d8295310074b18cc82a504487862. * feat: no need to store .callbackify * feat: change getTopics to async * feat: remove .async * fix: byScore * feat: rewrite topics/index and social with async/await * fix: rewrite topics/data.js fix issue with async.waterfall, only pass result if its not undefined * feat: add callbackify to redis/psql * feat: psql use await * fix: redis :volcano: * feat: less returns * feat: more await rewrite * fix: redis tests * feat: convert sortedSetAdd rewrite psql transaction to async/await * feat: :dog: * feat: test * feat: log client and query * feat: log bind * feat: more logs * feat: more logs * feat: check perform * feat: dont callbackify transaction * feat: remove logs * fix: main functions * feat: more logs * fix: increment * fix: rename * feat: remove cls * fix: remove console.log * feat: add deprecation message to .async usage * feat: update more dbal methods * fix: redis :voodoo: * feat: fix redis zrem, convert setObject * feat: upgrade getObject methods * fix: psql getObjectField * fix: redis tests * feat: getObjectKeys * feat: getObjectValues * feat: isObjectField * fix: add missing return * feat: delObjectField * feat: incrObjectField * fix: add missing await * feat: remove exposed helpers * feat: list methods * feat: flush/empty * feat: delete * fix: redis delete all * feat: get/set * feat: incr/rename * feat: type * feat: expire * feat: setAdd * feat: setRemove * feat: isSetMember * feat: getSetMembers * feat: setCount, setRemoveRandom * feat: zcard,zcount * feat: sortedSetRank * feat: isSortedSetMember * feat: zincrby * feat: sortedSetLex * feat: processSortedSet * fix: add mising await * feat: debug psql * fix: psql test * fix: test * fix: another test * fix: test fix * fix: psql tests * feat: remove logs * feat: user arrow func use builtin async promises * feat: topic bookmarks * feat: topic.delete * feat: topic.restore * feat: topics.purge * feat: merge * feat: suggested * feat: topics/user.js * feat: topics modules * feat: topics/follow * fix: deprecation msg * feat: fork * feat: topics/posts * feat: sorted/recent * feat: topic/teaser * feat: topics/tools * feat: topics/unread * feat: add back node versions disable deprecation notice wrap async controllers in try/catch * feat: use db directly * feat: promisify in place * fix: redis/psql * feat: deprecation message logs for psql * feat: more logs * feat: more logs * feat: logs again * feat: more logs * fix: call release * feat: restore travis, remove logs * fix: loops * feat: remove .async. usage
6 years ago
privileges.topics.get(tid, req.uid),
user.getSettings(req.uid),
topics.getTopicData(tid),
user.auth.getFeedToken(req.uid),
]);
11 years ago
let currentPage = parseInt(req.query.page, 10) || 1;
const pageCount = Math.max(1, Math.ceil((topicData && topicData.postcount) / settings.postsPerPage));
if (!topicData || userPrivileges.disabled || (settings.usePagination && (currentPage < 1 || currentPage > pageCount))) {
return callback();
}
10 years ago
if (!userPrivileges['topics:read'] || (topicData.deleted && !userPrivileges.view_deleted)) {
return helpers.notAllowed(req, res);
}
11 years ago
if (!res.locals.isAPI && (!req.params.slug || topicData.slug !== tid + '/' + req.params.slug) && (topicData.slug && topicData.slug !== tid + '/')) {
return helpers.redirect(res, '/topic/' + topicData.slug + (postIndex ? '/' + postIndex : '') + (currentPage > 1 ? '?page=' + currentPage : ''));
}
if (postIndex === 'unread') {
Async refactor in place (#7736) * feat: allow both callback&and await * feat: ignore async key * feat: callbackify and promisify in same file * Revert "feat: callbackify and promisify in same file" This reverts commit cea206a9b8e6d8295310074b18cc82a504487862. * feat: no need to store .callbackify * feat: change getTopics to async * feat: remove .async * fix: byScore * feat: rewrite topics/index and social with async/await * fix: rewrite topics/data.js fix issue with async.waterfall, only pass result if its not undefined * feat: add callbackify to redis/psql * feat: psql use await * fix: redis :volcano: * feat: less returns * feat: more await rewrite * fix: redis tests * feat: convert sortedSetAdd rewrite psql transaction to async/await * feat: :dog: * feat: test * feat: log client and query * feat: log bind * feat: more logs * feat: more logs * feat: check perform * feat: dont callbackify transaction * feat: remove logs * fix: main functions * feat: more logs * fix: increment * fix: rename * feat: remove cls * fix: remove console.log * feat: add deprecation message to .async usage * feat: update more dbal methods * fix: redis :voodoo: * feat: fix redis zrem, convert setObject * feat: upgrade getObject methods * fix: psql getObjectField * fix: redis tests * feat: getObjectKeys * feat: getObjectValues * feat: isObjectField * fix: add missing return * feat: delObjectField * feat: incrObjectField * fix: add missing await * feat: remove exposed helpers * feat: list methods * feat: flush/empty * feat: delete * fix: redis delete all * feat: get/set * feat: incr/rename * feat: type * feat: expire * feat: setAdd * feat: setRemove * feat: isSetMember * feat: getSetMembers * feat: setCount, setRemoveRandom * feat: zcard,zcount * feat: sortedSetRank * feat: isSortedSetMember * feat: zincrby * feat: sortedSetLex * feat: processSortedSet * fix: add mising await * feat: debug psql * fix: psql test * fix: test * fix: another test * fix: test fix * fix: psql tests * feat: remove logs * feat: user arrow func use builtin async promises * feat: topic bookmarks * feat: topic.delete * feat: topic.restore * feat: topics.purge * feat: merge * feat: suggested * feat: topics/user.js * feat: topics modules * feat: topics/follow * fix: deprecation msg * feat: fork * feat: topics/posts * feat: sorted/recent * feat: topic/teaser * feat: topics/tools * feat: topics/unread * feat: add back node versions disable deprecation notice wrap async controllers in try/catch * feat: use db directly * feat: promisify in place * fix: redis/psql * feat: deprecation message logs for psql * feat: more logs * feat: more logs * feat: logs again * feat: more logs * fix: call release * feat: restore travis, remove logs * fix: loops * feat: remove .async. usage
6 years ago
postIndex = await topics.getUserBookmark(tid, req.uid);
}
if (utils.isNumber(postIndex) && (postIndex < 1 || postIndex > topicData.postcount)) {
return helpers.redirect(res, '/topic/' + req.params.topic_id + '/' + req.params.slug + (postIndex > topicData.postcount ? '/' + topicData.postcount : ''));
}
postIndex = Math.max(1, postIndex);
const sort = req.query.sort || settings.topicPostSort;
const set = sort === 'most_votes' ? 'tid:' + tid + ':posts:votes' : 'tid:' + tid + ':posts';
const reverse = sort === 'newest_to_oldest' || sort === 'most_votes';
if (settings.usePagination && !req.query.page) {
currentPage = calculatePageFromIndex(postIndex, settings);
}
const { start, stop } = calculateStartStop(currentPage, postIndex, settings);
Async refactor in place (#7736) * feat: allow both callback&and await * feat: ignore async key * feat: callbackify and promisify in same file * Revert "feat: callbackify and promisify in same file" This reverts commit cea206a9b8e6d8295310074b18cc82a504487862. * feat: no need to store .callbackify * feat: change getTopics to async * feat: remove .async * fix: byScore * feat: rewrite topics/index and social with async/await * fix: rewrite topics/data.js fix issue with async.waterfall, only pass result if its not undefined * feat: add callbackify to redis/psql * feat: psql use await * fix: redis :volcano: * feat: less returns * feat: more await rewrite * fix: redis tests * feat: convert sortedSetAdd rewrite psql transaction to async/await * feat: :dog: * feat: test * feat: log client and query * feat: log bind * feat: more logs * feat: more logs * feat: check perform * feat: dont callbackify transaction * feat: remove logs * fix: main functions * feat: more logs * fix: increment * fix: rename * feat: remove cls * fix: remove console.log * feat: add deprecation message to .async usage * feat: update more dbal methods * fix: redis :voodoo: * feat: fix redis zrem, convert setObject * feat: upgrade getObject methods * fix: psql getObjectField * fix: redis tests * feat: getObjectKeys * feat: getObjectValues * feat: isObjectField * fix: add missing return * feat: delObjectField * feat: incrObjectField * fix: add missing await * feat: remove exposed helpers * feat: list methods * feat: flush/empty * feat: delete * fix: redis delete all * feat: get/set * feat: incr/rename * feat: type * feat: expire * feat: setAdd * feat: setRemove * feat: isSetMember * feat: getSetMembers * feat: setCount, setRemoveRandom * feat: zcard,zcount * feat: sortedSetRank * feat: isSortedSetMember * feat: zincrby * feat: sortedSetLex * feat: processSortedSet * fix: add mising await * feat: debug psql * fix: psql test * fix: test * fix: another test * fix: test fix * fix: psql tests * feat: remove logs * feat: user arrow func use builtin async promises * feat: topic bookmarks * feat: topic.delete * feat: topic.restore * feat: topics.purge * feat: merge * feat: suggested * feat: topics/user.js * feat: topics modules * feat: topics/follow * fix: deprecation msg * feat: fork * feat: topics/posts * feat: sorted/recent * feat: topic/teaser * feat: topics/tools * feat: topics/unread * feat: add back node versions disable deprecation notice wrap async controllers in try/catch * feat: use db directly * feat: promisify in place * fix: redis/psql * feat: deprecation message logs for psql * feat: more logs * feat: more logs * feat: logs again * feat: more logs * fix: call release * feat: restore travis, remove logs * fix: loops * feat: remove .async. usage
6 years ago
await topics.getTopicWithPosts(topicData, set, req.uid, start, stop, reverse);
topics.modifyPostsByPrivilege(topicData, userPrivileges);
Async refactor in place (#7736) * feat: allow both callback&and await * feat: ignore async key * feat: callbackify and promisify in same file * Revert "feat: callbackify and promisify in same file" This reverts commit cea206a9b8e6d8295310074b18cc82a504487862. * feat: no need to store .callbackify * feat: change getTopics to async * feat: remove .async * fix: byScore * feat: rewrite topics/index and social with async/await * fix: rewrite topics/data.js fix issue with async.waterfall, only pass result if its not undefined * feat: add callbackify to redis/psql * feat: psql use await * fix: redis :volcano: * feat: less returns * feat: more await rewrite * fix: redis tests * feat: convert sortedSetAdd rewrite psql transaction to async/await * feat: :dog: * feat: test * feat: log client and query * feat: log bind * feat: more logs * feat: more logs * feat: check perform * feat: dont callbackify transaction * feat: remove logs * fix: main functions * feat: more logs * fix: increment * fix: rename * feat: remove cls * fix: remove console.log * feat: add deprecation message to .async usage * feat: update more dbal methods * fix: redis :voodoo: * feat: fix redis zrem, convert setObject * feat: upgrade getObject methods * fix: psql getObjectField * fix: redis tests * feat: getObjectKeys * feat: getObjectValues * feat: isObjectField * fix: add missing return * feat: delObjectField * feat: incrObjectField * fix: add missing await * feat: remove exposed helpers * feat: list methods * feat: flush/empty * feat: delete * fix: redis delete all * feat: get/set * feat: incr/rename * feat: type * feat: expire * feat: setAdd * feat: setRemove * feat: isSetMember * feat: getSetMembers * feat: setCount, setRemoveRandom * feat: zcard,zcount * feat: sortedSetRank * feat: isSortedSetMember * feat: zincrby * feat: sortedSetLex * feat: processSortedSet * fix: add mising await * feat: debug psql * fix: psql test * fix: test * fix: another test * fix: test fix * fix: psql tests * feat: remove logs * feat: user arrow func use builtin async promises * feat: topic bookmarks * feat: topic.delete * feat: topic.restore * feat: topics.purge * feat: merge * feat: suggested * feat: topics/user.js * feat: topics modules * feat: topics/follow * fix: deprecation msg * feat: fork * feat: topics/posts * feat: sorted/recent * feat: topic/teaser * feat: topics/tools * feat: topics/unread * feat: add back node versions disable deprecation notice wrap async controllers in try/catch * feat: use db directly * feat: promisify in place * fix: redis/psql * feat: deprecation message logs for psql * feat: more logs * feat: more logs * feat: logs again * feat: more logs * fix: call release * feat: restore travis, remove logs * fix: loops * feat: remove .async. usage
6 years ago
const hookData = await plugins.fireHook('filter:controllers.topic.get', { topicData: topicData, uid: req.uid });
await Promise.all([
buildBreadcrumbs(hookData.topicData),
addTags(topicData, req, res),
]);
topicData.privileges = userPrivileges;
topicData.topicStaleDays = meta.config.topicStaleDays;
topicData['reputation:disabled'] = meta.config['reputation:disabled'];
topicData['downvote:disabled'] = meta.config['downvote:disabled'];
topicData['feeds:disableRSS'] = meta.config['feeds:disableRSS'];
topicData.bookmarkThreshold = meta.config.bookmarkThreshold;
topicData.postEditDuration = meta.config.postEditDuration;
topicData.postDeleteDuration = meta.config.postDeleteDuration;
topicData.scrollToMyPost = settings.scrollToMyPost;
topicData.allowMultipleBadges = meta.config.allowMultipleBadges === 1;
topicData.rssFeedUrl = nconf.get('relative_path') + '/topic/' + topicData.tid + '.rss';
if (req.loggedIn) {
topicData.rssFeedUrl += '?uid=' + req.uid + '&token=' + rssToken;
}
topicData.postIndex = postIndex;
topicData.pagination = pagination.create(currentPage, pageCount, req.query);
topicData.pagination.rel.forEach(function (rel) {
rel.href = nconf.get('url') + '/topic/' + topicData.slug + rel.href;
res.locals.linkTags.push(rel);
});
11 years ago
incrementViewCount(req, tid);
markAsRead(req, tid);
analytics.increment(['pageviews:byCid:' + topicData.category.cid]);
10 years ago
res.render('topic', topicData);
};
function calculatePageFromIndex(postIndex, settings) {
return 1 + Math.floor((postIndex - 1) / settings.postsPerPage);
}
function calculateStartStop(page, postIndex, settings) {
let startSkip = 0;
if (!settings.usePagination) {
if (postIndex !== 0) {
page = 1;
}
startSkip = Math.max(0, postIndex - Math.ceil(settings.postsPerPage / 2));
}
const start = ((page - 1) * settings.postsPerPage) + startSkip;
const stop = start + settings.postsPerPage - 1;
return { start: Math.max(0, start), stop: Math.max(0, stop) };
}
8 years ago
function incrementViewCount(req, tid) {
if (req.uid >= 0) {
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();
}
}
}
11 years ago
function markAsRead(req, tid) {
if (req.loggedIn) {
topics.markAsRead([tid], req.uid, function (err, markedRead) {
if (err) {
return winston.error(err);
}
if (markedRead) {
topics.pushUnreadCount(req.uid);
topics.markTopicNotificationsRead([tid], req.uid);
}
});
}
}
async function buildBreadcrumbs(topicData) {
const breadcrumbs = [
8 years ago
{
text: topicData.category.name,
url: nconf.get('relative_path') + '/category/' + topicData.category.slug,
},
{
text: topicData.title,
},
];
Async refactor in place (#7736) * feat: allow both callback&and await * feat: ignore async key * feat: callbackify and promisify in same file * Revert "feat: callbackify and promisify in same file" This reverts commit cea206a9b8e6d8295310074b18cc82a504487862. * feat: no need to store .callbackify * feat: change getTopics to async * feat: remove .async * fix: byScore * feat: rewrite topics/index and social with async/await * fix: rewrite topics/data.js fix issue with async.waterfall, only pass result if its not undefined * feat: add callbackify to redis/psql * feat: psql use await * fix: redis :volcano: * feat: less returns * feat: more await rewrite * fix: redis tests * feat: convert sortedSetAdd rewrite psql transaction to async/await * feat: :dog: * feat: test * feat: log client and query * feat: log bind * feat: more logs * feat: more logs * feat: check perform * feat: dont callbackify transaction * feat: remove logs * fix: main functions * feat: more logs * fix: increment * fix: rename * feat: remove cls * fix: remove console.log * feat: add deprecation message to .async usage * feat: update more dbal methods * fix: redis :voodoo: * feat: fix redis zrem, convert setObject * feat: upgrade getObject methods * fix: psql getObjectField * fix: redis tests * feat: getObjectKeys * feat: getObjectValues * feat: isObjectField * fix: add missing return * feat: delObjectField * feat: incrObjectField * fix: add missing await * feat: remove exposed helpers * feat: list methods * feat: flush/empty * feat: delete * fix: redis delete all * feat: get/set * feat: incr/rename * feat: type * feat: expire * feat: setAdd * feat: setRemove * feat: isSetMember * feat: getSetMembers * feat: setCount, setRemoveRandom * feat: zcard,zcount * feat: sortedSetRank * feat: isSortedSetMember * feat: zincrby * feat: sortedSetLex * feat: processSortedSet * fix: add mising await * feat: debug psql * fix: psql test * fix: test * fix: another test * fix: test fix * fix: psql tests * feat: remove logs * feat: user arrow func use builtin async promises * feat: topic bookmarks * feat: topic.delete * feat: topic.restore * feat: topics.purge * feat: merge * feat: suggested * feat: topics/user.js * feat: topics modules * feat: topics/follow * fix: deprecation msg * feat: fork * feat: topics/posts * feat: sorted/recent * feat: topic/teaser * feat: topics/tools * feat: topics/unread * feat: add back node versions disable deprecation notice wrap async controllers in try/catch * feat: use db directly * feat: promisify in place * fix: redis/psql * feat: deprecation message logs for psql * feat: more logs * feat: more logs * feat: logs again * feat: more logs * fix: call release * feat: restore travis, remove logs * fix: loops * feat: remove .async. usage
6 years ago
const parentCrumbs = await helpers.buildCategoryBreadcrumbs(topicData.category.parentCid);
topicData.breadcrumbs = parentCrumbs.concat(breadcrumbs);
8 years ago
}
async function addTags(topicData, req, res) {
var postAtIndex = topicData.posts.find(p => parseInt(p.index, 10) === parseInt(Math.max(0, req.params.post_index - 1), 10));
11 years ago
var description = '';
8 years ago
if (postAtIndex && postAtIndex.content) {
description = utils.stripHTMLTags(utils.decodeHTMLEntities(postAtIndex.content));
8 years ago
}
11 years ago
8 years ago
if (description.length > 255) {
description = description.substr(0, 255) + '...';
}
description = description.replace(/\n/g, ' ');
8 years ago
res.locals.metaTags = [
{
name: 'title',
content: topicData.titleRaw,
},
{
name: 'description',
content: description,
},
{
property: 'og:title',
content: topicData.titleRaw,
},
{
property: 'og:description',
content: description,
},
{
property: 'og:type',
content: 'article',
},
{
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 : '',
},
];
await addOGImageTags(res, topicData, postAtIndex);
7 years ago
8 years ago
res.locals.linkTags = [
8 years ago
{
rel: 'canonical',
href: nconf.get('url') + '/topic/' + topicData.slug,
},
8 years ago
];
if (!topicData['feeds:disableRSS']) {
res.locals.linkTags.push({
rel: 'alternate',
type: 'application/rss+xml',
href: topicData.rssFeedUrl,
});
}
8 years ago
if (topicData.category) {
res.locals.linkTags.push({
rel: 'up',
href: nconf.get('url') + '/category/' + topicData.category.slug,
});
}
}
11 years ago
async function addOGImageTags(res, topicData, postAtIndex) {
Async refactor in place (#7736) * feat: allow both callback&and await * feat: ignore async key * feat: callbackify and promisify in same file * Revert "feat: callbackify and promisify in same file" This reverts commit cea206a9b8e6d8295310074b18cc82a504487862. * feat: no need to store .callbackify * feat: change getTopics to async * feat: remove .async * fix: byScore * feat: rewrite topics/index and social with async/await * fix: rewrite topics/data.js fix issue with async.waterfall, only pass result if its not undefined * feat: add callbackify to redis/psql * feat: psql use await * fix: redis :volcano: * feat: less returns * feat: more await rewrite * fix: redis tests * feat: convert sortedSetAdd rewrite psql transaction to async/await * feat: :dog: * feat: test * feat: log client and query * feat: log bind * feat: more logs * feat: more logs * feat: check perform * feat: dont callbackify transaction * feat: remove logs * fix: main functions * feat: more logs * fix: increment * fix: rename * feat: remove cls * fix: remove console.log * feat: add deprecation message to .async usage * feat: update more dbal methods * fix: redis :voodoo: * feat: fix redis zrem, convert setObject * feat: upgrade getObject methods * fix: psql getObjectField * fix: redis tests * feat: getObjectKeys * feat: getObjectValues * feat: isObjectField * fix: add missing return * feat: delObjectField * feat: incrObjectField * fix: add missing await * feat: remove exposed helpers * feat: list methods * feat: flush/empty * feat: delete * fix: redis delete all * feat: get/set * feat: incr/rename * feat: type * feat: expire * feat: setAdd * feat: setRemove * feat: isSetMember * feat: getSetMembers * feat: setCount, setRemoveRandom * feat: zcard,zcount * feat: sortedSetRank * feat: isSortedSetMember * feat: zincrby * feat: sortedSetLex * feat: processSortedSet * fix: add mising await * feat: debug psql * fix: psql test * fix: test * fix: another test * fix: test fix * fix: psql tests * feat: remove logs * feat: user arrow func use builtin async promises * feat: topic bookmarks * feat: topic.delete * feat: topic.restore * feat: topics.purge * feat: merge * feat: suggested * feat: topics/user.js * feat: topics modules * feat: topics/follow * fix: deprecation msg * feat: fork * feat: topics/posts * feat: sorted/recent * feat: topic/teaser * feat: topics/tools * feat: topics/unread * feat: add back node versions disable deprecation notice wrap async controllers in try/catch * feat: use db directly * feat: promisify in place * fix: redis/psql * feat: deprecation message logs for psql * feat: more logs * feat: more logs * feat: logs again * feat: more logs * fix: call release * feat: restore travis, remove logs * fix: loops * feat: remove .async. usage
6 years ago
const uploads = postAtIndex ? await posts.uploads.listWithSizes(postAtIndex.pid) : [];
fix(style): updated code to follow new eslint recommendations Squashed commit of the following: commit f9ce878b269b3568f0d649309aae1af4dcfdfeef Author: Julian Lam <[email protected]> Date: Tue Aug 13 14:30:46 2019 -0400 fix(style): updated code to follow new eslint recommendations commit 80dd370e413f22badb96ff2138e7991dfff6d836 Author: Julian Lam <[email protected]> Date: Tue Aug 13 14:14:58 2019 -0400 fix(deps): update dependency sitemap to v4 Squashed commit of the following: commit f4dd9cabb21e26fdc21f8413be822ea7c64251f8 Author: Julian Lam <[email protected]> Date: Tue Aug 13 11:33:05 2019 -0400 fix: resolved breaking changes from sitemap v4 upgrade commit 9043415ee16dcc27a8dcc2e4479d1bc5e2d1b60e Merge: e3352b272 72590b346 Author: Julian Lam <[email protected]> Date: Tue Aug 13 11:09:55 2019 -0400 Merge branch 'master' into renovate/sitemap-4.x commit e3352b272eb9400bdb00774973181397803765e4 Author: Renovate Bot <[email protected]> Date: Mon Aug 12 07:59:05 2019 +0000 fix(deps): update dependency sitemap to v4 commit 8e3c0cdcae22acc32d352be8bb72d60e7502dbc5 Author: Renovate Bot <[email protected]> Date: Fri Aug 9 00:49:51 2019 +0000 fix(deps): update dependency commander to v3 commit 2104449d38818f2fa4d44b3a58a0a168781acbfb Author: Renovate Bot <[email protected]> Date: Tue Aug 13 15:00:27 2019 +0000 fix(deps): update dependency mongodb to v3.3.0 commit d2937f446a21131c070ae5d0ff33d67cfe465b8c Author: Barış Soner Uşaklı <[email protected]> Date: Tue Aug 13 10:36:48 2019 -0400 feat: async/await admin/controllers commit 1b97e8b199f960dc24e5722702f27499ae049914 Author: Misty (Bot) <[email protected]> Date: Tue Aug 13 09:28:39 2019 +0000 Latest translations and fallbacks commit 69a48957a2f0d23c4d194b664bda3a0431179c01 Author: Barış Soner Uşaklı <[email protected]> Date: Mon Aug 12 21:56:09 2019 -0400 feat: async/await commit b9b2a7e593a452de4bef6d0ab6abe368a3bdb8dd Author: Barış Soner Uşaklı <[email protected]> Date: Mon Aug 12 20:58:29 2019 -0400 feat: async/await refactor controllers/accounts commit a8d43a175974a0c8ae3dc132bf51a7ed9a4c6305 Author: Baris Usakli <[email protected]> Date: Mon Aug 12 14:49:40 2019 -0400 feat: async/await controllers/accounts commit 2f25aae57bf9dbe98d655276770e56bed9ec023b Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 23:09:50 2019 -0400 fix: #7831, fix pagination convert to async/await commit c9e83f2374572264855a04156278eef256b0a20c Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 00:14:35 2019 -0400 fix: remove empty line commit 30be91b26c4dd7583412c4e8d56e9c1688e48a44 Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 00:13:41 2019 -0400 fix: remove useless catchs and empty line commit 2e4a71c0b6104738f15ffbfe3246105b922fdfb3 Author: Renovate Bot <[email protected]> Date: Sat Aug 10 06:51:50 2019 +0000 chore(deps): update dependency eslint-config-airbnb-base to v14
6 years ago
const images = uploads.map(upload => {
upload.name = nconf.get('url') + nconf.get('upload_url') + '/files/' + upload.name;
return upload;
});
if (topicData.thumb) {
images.push(topicData.thumb);
}
if (topicData.category.backgroundImage && (!postAtIndex || !postAtIndex.index)) {
images.push(topicData.category.backgroundImage);
}
if (postAtIndex && postAtIndex.user && postAtIndex.user.picture) {
images.push(postAtIndex.user.picture);
}
images.forEach(path => addOGImageTag(res, path));
}
function addOGImageTag(res, image) {
let imageUrl;
if (typeof image === 'string' && !image.startsWith('http')) {
imageUrl = nconf.get('url') + image.replace(new RegExp('^' + nconf.get('relative_path')), '');
} else if (typeof image === 'object') {
imageUrl = image.name;
} else {
imageUrl = image;
}
res.locals.metaTags.push({
property: 'og:image',
content: imageUrl,
noEscape: true,
}, {
property: 'og:image:url',
content: imageUrl,
noEscape: true,
});
if (typeof image === 'object' && image.width && image.height) {
res.locals.metaTags.push({
property: 'og:image:width',
content: String(image.width),
}, {
property: 'og:image:height',
content: String(image.height),
});
}
}
topicsController.teaser = async function (req, res, next) {
const tid = req.params.topic_id;
11 years ago
if (!utils.isNumber(tid)) {
return next();
11 years ago
}
const canRead = await privileges.topics.can('topics:read', tid, req.uid);
if (!canRead) {
return res.status(403).json('[[error:no-privileges]]');
}
const pid = await topics.getLatestUndeletedPid(tid);
if (!pid) {
return res.status(404).json('not-found');
}
const postData = await posts.getPostSummaryByPids([pid], req.uid, { stripTags: false });
if (!postData.length) {
return res.status(404).json('not-found');
}
res.json(postData[0]);
11 years ago
};
topicsController.pagination = async function (req, res, callback) {
const tid = req.params.topic_id;
const currentPage = parseInt(req.query.page, 10) || 1;
9 years ago
if (!utils.isNumber(tid)) {
return callback();
}
const [userPrivileges, settings, topic] = await Promise.all([
privileges.topics.get(tid, req.uid),
user.getSettings(req.uid),
topics.getTopicData(tid),
]);
9 years ago
if (!topic) {
return callback();
}
9 years ago
if (!userPrivileges.read || (topic.deleted && !userPrivileges.view_deleted)) {
return helpers.notAllowed(req, res);
}
9 years ago
var postCount = topic.postcount;
var pageCount = Math.max(1, Math.ceil(postCount / settings.postsPerPage));
9 years ago
var paginationData = pagination.create(currentPage, pageCount);
paginationData.rel.forEach(function (rel) {
rel.href = nconf.get('url') + '/topic/' + topic.slug + rel.href;
9 years ago
});
res.json(paginationData);
9 years ago
};