| 
									
										
										
										
											2017-02-18 01:56:23 -07:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2014-03-02 15:14:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'); | 
					
						
							|  |  |  | 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'); | 
					
						
							| 
									
										
										
										
											2017-04-08 20:22:21 -06:00
										 |  |  | var utils = require('../utils'); | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | var topicsController = module.exports; | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | topicsController.get = function (req, res, callback) { | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 	var tid = req.params.topic_id; | 
					
						
							|  |  |  | 	var currentPage = parseInt(req.query.page, 10) || 1; | 
					
						
							|  |  |  | 	var pageCount = 1; | 
					
						
							|  |  |  | 	var userPrivileges; | 
					
						
							| 
									
										
										
										
											2016-03-18 10:33:10 +02:00
										 |  |  | 	var settings; | 
					
						
							| 
									
										
										
										
											2017-06-22 12:44:37 -04:00
										 |  |  | 	var rssToken; | 
					
						
							| 
									
										
										
										
											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({ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02: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
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02: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
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				topic: function (next) { | 
					
						
							| 
									
										
										
										
											2016-01-21 20:51:07 +02:00
										 |  |  | 					topics.getTopicData(tid, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-06-22 12:44:37 -04:00
										 |  |  | 				rssToken: function (next) { | 
					
						
							|  |  |  | 					user.auth.getFeedToken(req.uid, 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; | 
					
						
							| 
									
										
										
										
											2017-06-22 12:44:37 -04:00
										 |  |  | 			rssToken = results.rssToken; | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-19 19:35:10 -04:00
										 |  |  | 			if (!userPrivileges['topics:read'] || (parseInt(results.topic.deleted, 10) && !userPrivileges.view_deleted)) { | 
					
						
							| 
									
										
										
										
											2014-11-16 00:09:43 -05:00
										 |  |  | 				return helpers.notAllowed(req, res); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-14 13:03:33 -04:00
										 |  |  | 			if (!res.locals.isAPI && (!req.params.slug || results.topic.slug !== tid + '/' + req.params.slug) && (results.topic.slug && results.topic.slug !== tid + '/')) { | 
					
						
							| 
									
										
										
										
											2016-03-22 19:54:27 -05:00
										 |  |  | 				var url = '/topic/' + results.topic.slug; | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				if (req.params.post_index) { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:42:29 +02:00
										 |  |  | 					url += '/' + req.params.post_index; | 
					
						
							| 
									
										
										
										
											2016-03-10 22:49:57 -05:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-09-14 20:57:51 -05:00
										 |  |  | 				if (currentPage > 1) { | 
					
						
							|  |  |  | 					url += '?page=' + currentPage; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-03-10 22:49:57 -05:00
										 |  |  | 				return helpers.redirect(res, url); | 
					
						
							| 
									
										
										
										
											2015-04-08 21:16:06 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 10:33:10 +02:00
										 |  |  | 			settings = results.settings; | 
					
						
							| 
									
										
										
										
											2014-11-04 14:54:30 -05:00
										 |  |  | 			var postCount = parseInt(results.topic.postcount, 10); | 
					
						
							| 
									
										
										
										
											2016-11-21 13:49:36 +03:00
										 |  |  | 			pageCount = Math.max(1, Math.ceil(postCount / settings.postsPerPage)); | 
					
						
							| 
									
										
										
										
											2017-04-03 13:45:43 -03:00
										 |  |  | 			results.topic.postcount = postCount; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-24 22:47:31 +02:00
										 |  |  | 			var set = 'tid:' + tid + ':posts'; | 
					
						
							|  |  |  | 			var reverse = false; | 
					
						
							| 
									
										
										
										
											2014-09-03 12:49:34 -04:00
										 |  |  | 			// `sort` qs has priority over user setting
 | 
					
						
							| 
									
										
										
										
											2016-07-21 23:53:43 +03:00
										 |  |  | 			var sort = req.query.sort || settings.topicPostSort; | 
					
						
							| 
									
										
										
										
											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'; | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2016-07-17 15:47:58 -05:00
										 |  |  | 				if (req.params.post_index !== 0) { | 
					
						
							|  |  |  | 					currentPage = 1; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-08-16 21:33:42 -04:00
										 |  |  | 				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) { | 
					
						
							| 
									
										
										
										
											2016-11-26 01:18:57 +03:00
										 |  |  | 					index = Math.max(0, postCount - (req.params.post_index || postCount) + 2); | 
					
						
							| 
									
										
										
										
											2015-02-07 00:52:53 -05:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2016-11-26 01:18:57 +03:00
										 |  |  | 					index = Math.max(0, req.params.post_index) || 0; | 
					
						
							| 
									
										
										
										
											2015-02-07 00:52:53 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 01:51:11 -07:00
										 |  |  | 			var start = ((currentPage - 1) * settings.postsPerPage) + postIndex; | 
					
						
							| 
									
										
										
										
											2016-01-21 20:51:07 +02:00
										 |  |  | 			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-03-21 17:49:44 +02:00
										 |  |  | 			topics.modifyPostsByPrivilege(topicData, userPrivileges); | 
					
						
							| 
									
										
										
										
											2015-10-06 18:36:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07: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) { | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 			buildBreadcrumbs(data.topicData, next); | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 		function (topicData) { | 
					
						
							|  |  |  | 			topicData.privileges = userPrivileges; | 
					
						
							|  |  |  | 			topicData.topicStaleDays = parseInt(meta.config.topicStaleDays, 10) || 60; | 
					
						
							|  |  |  | 			topicData['reputation:disabled'] = parseInt(meta.config['reputation:disabled'], 10) === 1; | 
					
						
							|  |  |  | 			topicData['downvote:disabled'] = parseInt(meta.config['downvote:disabled'], 10) === 1; | 
					
						
							|  |  |  | 			topicData['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1; | 
					
						
							|  |  |  | 			topicData.bookmarkThreshold = parseInt(meta.config.bookmarkThreshold, 10) || 5; | 
					
						
							|  |  |  | 			topicData.postEditDuration = parseInt(meta.config.postEditDuration, 10) || 0; | 
					
						
							|  |  |  | 			topicData.postDeleteDuration = parseInt(meta.config.postDeleteDuration, 10) || 0; | 
					
						
							|  |  |  | 			topicData.scrollToMyPost = settings.scrollToMyPost; | 
					
						
							|  |  |  | 			topicData.rssFeedUrl = nconf.get('relative_path') + '/topic/' + topicData.tid + '.rss'; | 
					
						
							|  |  |  | 			if (req.uid) { | 
					
						
							|  |  |  | 				topicData.rssFeedUrl += '?uid=' + req.uid + '&token=' + rssToken; | 
					
						
							| 
									
										
										
										
											2014-03-21 15:40:37 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-06-22 19:03:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			addTags(topicData, req, res); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 			topicData.postIndex = req.params.post_index; | 
					
						
							|  |  |  | 			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); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-03-21 15:40:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04: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(); | 
					
						
							| 
									
										
										
										
											2014-03-21 15:40:37 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04: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); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-03-23 17:17:56 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 			res.render('topic', topicData); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-03-10 18:28:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | function buildBreadcrumbs(topicData, callback) { | 
					
						
							|  |  |  | 	var breadcrumbs = [ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			text: topicData.category.name, | 
					
						
							|  |  |  | 			url: nconf.get('relative_path') + '/category/' + topicData.category.slug, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			text: topicData.title, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	]; | 
					
						
							| 
									
										
										
										
											2014-09-22 16:08:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			helpers.buildCategoryBreadcrumbs(topicData.category.parentCid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (crumbs, next) { | 
					
						
							|  |  |  | 			topicData.breadcrumbs = crumbs.concat(breadcrumbs); | 
					
						
							| 
									
										
										
										
											2014-03-02 22:10:57 -05:00
										 |  |  | 			next(null, topicData); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function addTags(topicData, req, res) { | 
					
						
							|  |  |  | 	function findPost(index) { | 
					
						
							|  |  |  | 		for (var i = 0; i < topicData.posts.length; i += 1) { | 
					
						
							|  |  |  | 			if (parseInt(topicData.posts[i].index, 10) === parseInt(index, 10)) { | 
					
						
							|  |  |  | 				return topicData.posts[i]; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	var description = ''; | 
					
						
							|  |  |  | 	var postAtIndex = findPost(Math.max(0, req.params.post_index - 1)); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 	if (postAtIndex && postAtIndex.content) { | 
					
						
							| 
									
										
										
										
											2017-10-13 21:02:41 -06:00
										 |  |  | 		description = utils.stripHTMLTags(utils.decodeHTMLEntities(postAtIndex.content)); | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 	if (description.length > 255) { | 
					
						
							|  |  |  | 		description = description.substr(0, 255) + '...'; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-09 18:22:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 	var ogImageUrl = ''; | 
					
						
							|  |  |  | 	if (topicData.thumb) { | 
					
						
							|  |  |  | 		ogImageUrl = topicData.thumb; | 
					
						
							| 
									
										
										
										
											2017-10-10 11:26:37 -04:00
										 |  |  | 	} else if (topicData.category.backgroundImage && (!postAtIndex || !postAtIndex.index)) { | 
					
						
							|  |  |  | 		ogImageUrl = topicData.category.backgroundImage; | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 	} else if (postAtIndex && postAtIndex.user && postAtIndex.user.picture) { | 
					
						
							|  |  |  | 		ogImageUrl = postAtIndex.user.picture; | 
					
						
							|  |  |  | 	} else if (meta.config['og:image']) { | 
					
						
							|  |  |  | 		ogImageUrl = meta.config['og:image']; | 
					
						
							|  |  |  | 	} else if (meta.config['brand:logo']) { | 
					
						
							|  |  |  | 		ogImageUrl = meta.config['brand:logo']; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ogImageUrl = '/logo.png'; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-11-05 11:53:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 	if (typeof ogImageUrl === 'string' && ogImageUrl.indexOf('http') === -1) { | 
					
						
							|  |  |  | 		ogImageUrl = nconf.get('url') + ogImageUrl; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	description = description.replace(/\n/g, ' '); | 
					
						
							|  |  |  | 	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: 'og:image', | 
					
						
							|  |  |  | 			content: ogImageUrl, | 
					
						
							|  |  |  | 			noEscape: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			property: 'og:image:url', | 
					
						
							|  |  |  | 			content: ogImageUrl, | 
					
						
							|  |  |  | 			noEscape: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			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 : '', | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 11:23:47 -05:00
										 |  |  | 	topicData.posts.forEach(function (postData) { | 
					
						
							| 
									
										
										
										
											2017-12-08 11:46:05 -05:00
										 |  |  | 		var regex = /src\s*=\s*"(.+?)"/g; | 
					
						
							| 
									
										
										
										
											2017-12-08 11:23:47 -05:00
										 |  |  | 		var match = regex.exec(postData.content); | 
					
						
							| 
									
										
										
										
											2017-12-08 11:46:05 -05:00
										 |  |  | 		while (match !== null) { | 
					
						
							| 
									
										
										
										
											2017-12-08 11:23:47 -05:00
										 |  |  | 			var image = match[1]; | 
					
						
							| 
									
										
										
										
											2017-12-08 11:46:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 11:23:47 -05:00
										 |  |  | 			if (image.startsWith(nconf.get('url') + '/plugins')) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-12-08 11:37:57 -05:00
										 |  |  | 			if (!image.startsWith('http')) { | 
					
						
							|  |  |  | 				image = nconf.get('url') + image; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-12-08 11:46:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 11:27:55 -05:00
										 |  |  | 			res.locals.metaTags.push({ | 
					
						
							| 
									
										
										
										
											2017-12-08 11:23:47 -05:00
										 |  |  | 				property: 'og:image', | 
					
						
							|  |  |  | 				content: image, | 
					
						
							|  |  |  | 				noEscape: true, | 
					
						
							| 
									
										
										
										
											2017-12-08 11:27:55 -05:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 			res.locals.metaTags.push({ | 
					
						
							|  |  |  | 				property: 'og:image:url', | 
					
						
							|  |  |  | 				content: image, | 
					
						
							|  |  |  | 				noEscape: true, | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-12-08 11:46:05 -05:00
										 |  |  | 			match = regex.exec(postData.content); | 
					
						
							| 
									
										
										
										
											2017-12-08 11:23:47 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 	res.locals.linkTags = [ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			rel: 'alternate', | 
					
						
							|  |  |  | 			type: 'application/rss+xml', | 
					
						
							|  |  |  | 			href: topicData.rssFeedUrl, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-09-22 10:26:03 -04:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			rel: 'canonical', | 
					
						
							|  |  |  | 			href: nconf.get('url') + '/topic/' + topicData.slug, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-06-22 13:04:24 -04:00
										 |  |  | 	]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (topicData.category) { | 
					
						
							|  |  |  | 		res.locals.linkTags.push({ | 
					
						
							|  |  |  | 			rel: 'up', | 
					
						
							|  |  |  | 			href: nconf.get('url') + '/category/' + topicData.category.slug, | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | topicsController.teaser = function (req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-04-27 16:41:40 -04:00
										 |  |  | 	var tid = req.params.topic_id; | 
					
						
							| 
									
										
										
										
											2014-09-22 21:54:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!utils.isNumber(tid)) { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 		return next(); | 
					
						
							| 
									
										
										
										
											2014-09-22 21:54:28 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 23:40:58 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		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
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		function (canRead, next) { | 
					
						
							| 
									
										
										
										
											2015-03-31 23:40:58 -04:00
										 |  |  | 			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); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		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
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			posts.getPostSummaryByPids([pid], req.uid, { stripTags: false }, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-05-25 16:40:03 -04:00
										 |  |  | 		function (posts) { | 
					
						
							| 
									
										
										
										
											2017-05-27 00:47:04 -04:00
										 |  |  | 			if (!posts.length) { | 
					
						
							| 
									
										
										
										
											2017-05-25 16:40:03 -04:00
										 |  |  | 				return res.status(404).json('not-found'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			res.json(posts[0]); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], next); | 
					
						
							| 
									
										
										
										
											2014-04-27 16:41:40 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | topicsController.pagination = function (req, res, callback) { | 
					
						
							| 
									
										
										
										
											2016-05-09 23:39:00 +03:00
										 |  |  | 	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), | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		topic: async.apply(topics.getTopicData, tid), | 
					
						
							| 
									
										
										
										
											2016-05-09 23:39:00 +03:00
										 |  |  | 	}, 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); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		paginationData.rel.forEach(function (rel) { | 
					
						
							| 
									
										
										
										
											2016-05-09 23:39:00 +03:00
										 |  |  | 			rel.href = nconf.get('url') + '/topic/' + results.topic.slug + rel.href; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		res.json(paginationData); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; |