| 
									
										
										
										
											2014-03-02 15:14:38 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | var topicsController = {}, | 
					
						
							|  |  |  | 	async = require('async'), | 
					
						
							|  |  |  | 	S = require('string'), | 
					
						
							|  |  |  | 	validator = require('validator'), | 
					
						
							| 
									
										
										
										
											2014-03-02 15:14:38 -05:00
										 |  |  | 	nconf = require('nconf'), | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 	qs = require('querystring'), | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:20 -04:00
										 |  |  | 	user = require('../user'), | 
					
						
							|  |  |  | 	meta = require('../meta'), | 
					
						
							|  |  |  | 	topics = require('../topics'), | 
					
						
							| 
									
										
										
										
											2014-04-27 16:41:40 -04:00
										 |  |  | 	posts = require('../posts'), | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 	privileges = require('../privileges'), | 
					
						
							| 
									
										
										
										
											2015-01-13 15:47:32 -05:00
										 |  |  | 	plugins = require('../plugins'), | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 	helpers = require('./helpers'), | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:20 -04:00
										 |  |  | 	utils = require('../../public/src/utils'); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | topicsController.get = function(req, res, next) { | 
					
						
							|  |  |  | 	var tid = req.params.topic_id, | 
					
						
							| 
									
										
										
										
											2014-09-06 04:29:20 -04:00
										 |  |  | 		page = 1, | 
					
						
							| 
									
										
										
										
											2014-09-03 12:49:34 -04:00
										 |  |  | 		sort = req.query.sort, | 
					
						
							| 
									
										
										
										
											2014-03-02 22:54:03 -05:00
										 |  |  | 		uid = req.user ? req.user.uid : 0, | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 		userPrivileges; | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-24 15:42:45 -04:00
										 |  |  | 	if (req.params.post_index && !utils.isNumber(req.params.post_index)) { | 
					
						
							| 
									
										
										
										
											2014-12-01 20:28:36 -05:00
										 |  |  | 		return helpers.notFound(req, res); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 					privileges.topics.get(tid, uid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2014-08-16 21:33:42 -04:00
										 |  |  | 				settings: function(next) { | 
					
						
							|  |  |  | 					user.getSettings(uid, next); | 
					
						
							| 
									
										
										
										
											2014-09-24 15:42:45 -04:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2014-11-04 14:54:30 -05:00
										 |  |  | 				topic: function(next) { | 
					
						
							| 
									
										
										
										
											2014-11-16 00:09:43 -05:00
										 |  |  | 					topics.getTopicFields(tid, ['slug', 'postcount', 'deleted'], 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) { | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:20 -04:00
										 |  |  | 			userPrivileges = results.privileges; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 20:28:36 -05:00
										 |  |  | 			if (userPrivileges.disabled || tid + '/' + req.params.slug !== results.topic.slug) { | 
					
						
							|  |  |  | 				return helpers.notFound(req, res); | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:20 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2014-11-01 17:55:39 -04:00
										 |  |  | 			var pageCount = Math.ceil((postCount - 1) / settings.postsPerPage); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-30 23:14:33 -04:00
										 |  |  | 			if (utils.isNumber(req.params.post_index)) { | 
					
						
							|  |  |  | 				var url = ''; | 
					
						
							| 
									
										
										
										
											2014-11-08 18:40:38 -05:00
										 |  |  | 				if (req.params.post_index < 1 || req.params.post_index > postCount) { | 
					
						
							|  |  |  | 					url = '/topic/' + req.params.topic_id + '/' + req.params.slug + (req.params.post_index > postCount ? '/' + postCount : ''); | 
					
						
							| 
									
										
										
										
											2014-10-30 23:14:33 -04:00
										 |  |  | 					return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-01 17:55:39 -04:00
										 |  |  | 			if (settings.usePagination && (req.query.page < 1 || req.query.page > pageCount)) { | 
					
						
							| 
									
										
										
										
											2014-12-01 20:28:36 -05:00
										 |  |  | 				return helpers.notFound(req, res); | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 			if (sort === 'oldest_to_newest') { | 
					
						
							|  |  |  | 				reverse = false; | 
					
						
							|  |  |  | 			} else if (sort === 'newest_to_oldest') { | 
					
						
							|  |  |  | 				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'; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 21:33:42 -04:00
										 |  |  | 			var postIndex = 0; | 
					
						
							| 
									
										
										
										
											2014-09-06 04:29:20 -04:00
										 |  |  | 			page = parseInt(req.query.page, 10) || 1; | 
					
						
							| 
									
										
										
										
											2014-09-06 04:09:13 -04:00
										 |  |  | 			req.params.post_index = parseInt(req.params.post_index, 10) || 0; | 
					
						
							| 
									
										
										
										
											2014-08-16 21:33:42 -04:00
										 |  |  | 			if (!settings.usePagination) { | 
					
						
							|  |  |  | 				if (reverse) { | 
					
						
							| 
									
										
										
										
											2014-09-06 04:29:20 -04:00
										 |  |  | 					if (req.params.post_index === 1) { | 
					
						
							| 
									
										
										
										
											2014-08-16 21:33:42 -04:00
										 |  |  | 						req.params.post_index = 0; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					postIndex = Math.max(postCount - (req.params.post_index || postCount) - (settings.postsPerPage - 1), 0); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					postIndex = Math.max((req.params.post_index || 1) - (settings.postsPerPage + 1), 0); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else if (!req.query.page) { | 
					
						
							| 
									
										
										
										
											2014-09-23 16:16:45 -04:00
										 |  |  | 				var index = Math.max(req.params.post_index - 1, 0) || 0; | 
					
						
							| 
									
										
										
										
											2014-09-13 18:50:35 -04:00
										 |  |  | 				page = Math.max(1, Math.ceil(index / settings.postsPerPage)); | 
					
						
							| 
									
										
										
										
											2014-08-16 21:33:42 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var start = (page - 1) * settings.postsPerPage + postIndex, | 
					
						
							|  |  |  | 				end = start + settings.postsPerPage - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 			topics.getTopicWithPosts(tid, set, uid, start, end, reverse, function (err, topicData) { | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:20 -04:00
										 |  |  | 				if (err && err.message === '[[error:no-topic]]' && !topicData) { | 
					
						
							| 
									
										
										
										
											2014-12-01 20:28:36 -05:00
										 |  |  | 					return helpers.notFound(req, res); | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:20 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-11-16 00:09:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:20 -04:00
										 |  |  | 				if (err && !topicData) { | 
					
						
							|  |  |  | 					return next(err); | 
					
						
							| 
									
										
										
										
											2014-06-02 20:41:03 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-01 17:55:39 -04:00
										 |  |  | 				topicData.pageCount = pageCount; | 
					
						
							| 
									
										
										
										
											2014-09-20 19:09:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:20 -04:00
										 |  |  | 				topicData.currentPage = page; | 
					
						
							|  |  |  | 				if(page > 1) { | 
					
						
							|  |  |  | 					topicData.posts.splice(0, 1); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-13 15:47:32 -05:00
										 |  |  | 				plugins.fireHook('filter:controllers.topic.get', topicData, function(err, topicData) { | 
					
						
							|  |  |  | 					next(null, topicData); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 		function (topicData, next) { | 
					
						
							|  |  |  | 			var breadcrumbs = [ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					text: topicData.category.name, | 
					
						
							|  |  |  | 					url: nconf.get('relative_path') + '/category/' + topicData.category.slug | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					text: topicData.title, | 
					
						
							|  |  |  | 					url: nconf.get('relative_path') + '/topic/' + topicData.slug | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			helpers.buildBreadcrumbs(topicData.category.parentCid, function(err, crumbs) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return next(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				topicData.breadcrumbs = crumbs.concat(breadcrumbs); | 
					
						
							|  |  |  | 				next(null, topicData); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 		function (topicData, next) { | 
					
						
							| 
									
										
										
										
											2014-03-21 17:48:32 -04:00
										 |  |  | 			var description = ''; | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-30 14:42:48 -04:00
										 |  |  | 			if (topicData.posts[0] && topicData.posts[0].content) { | 
					
						
							| 
									
										
										
										
											2014-03-23 16:57:04 -04:00
										 |  |  | 				description = S(topicData.posts[0].content).stripTags().decodeHTMLEntities().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-21 15:40:37 -04:00
										 |  |  | 			description = validator.escape(description); | 
					
						
							| 
									
										
										
										
											2014-09-23 23:05:41 -04:00
										 |  |  | 			description = description.replace(/'/g, '''); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-24 20:43:24 -04:00
										 |  |  | 			var ogImageUrl = ''; | 
					
						
							|  |  |  | 			if (topicData.thumb) { | 
					
						
							|  |  |  | 				ogImageUrl = topicData.thumb; | 
					
						
							| 
									
										
										
										
											2014-08-30 14:42:48 -04:00
										 |  |  | 			} else if(topicData.posts.length && topicData.posts[0] && topicData.posts[0].user && topicData.posts[0].user.picture){ | 
					
						
							| 
									
										
										
										
											2014-03-24 20:43:24 -04:00
										 |  |  | 				ogImageUrl = topicData.posts[0].user.picture; | 
					
						
							|  |  |  | 			} else if(meta.config['brand:logo']) { | 
					
						
							|  |  |  | 				ogImageUrl = meta.config['brand:logo']; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				ogImageUrl = '/logo.png'; | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-24 20:43:24 -04:00
										 |  |  | 			if (ogImageUrl.indexOf('http') === -1) { | 
					
						
							|  |  |  | 				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', | 
					
						
							| 
									
										
										
										
											2014-10-01 19:00:26 -04:00
										 |  |  | 					content: topicData.title.replace(/&/g, '&') | 
					
						
							| 
									
										
										
										
											2014-03-02 15:14:38 -05:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					property: 'og:description', | 
					
						
							|  |  |  | 					content: description | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					property: "og:type", | 
					
						
							|  |  |  | 					content: 'article' | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					property: "og:url", | 
					
						
							|  |  |  | 					content: nconf.get('url') + '/topic/' + topicData.slug | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2014-03-24 20:43:24 -04:00
										 |  |  | 					property: 'og:image', | 
					
						
							| 
									
										
										
										
											2014-03-02 15:14:38 -05:00
										 |  |  | 					content: ogImageUrl | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2014-03-24 20:43:24 -04:00
										 |  |  | 					property: "og:image:url", | 
					
						
							|  |  |  | 					content: ogImageUrl | 
					
						
							| 
									
										
										
										
											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-09-22 16:09:27 -04:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2014-06-11 12:04:22 -04:00
										 |  |  | 				{ | 
					
						
							|  |  |  | 					rel: 'canonical', | 
					
						
							|  |  |  | 					href: nconf.get('url') + '/topic/' + topicData.slug | 
					
						
							| 
									
										
										
										
											2014-03-02 15:14:38 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			]; | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:20 -04:00
										 |  |  | 			return next(err); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 		data.privileges = userPrivileges; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2014-03-02 22:54:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-21 13:59:40 -04:00
										 |  |  | 		topics.increaseViewCount(tid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-20 15:04:14 -04:00
										 |  |  | 		if (!res.locals.isAPI) { | 
					
						
							|  |  |  | 			// Paginator for noscript
 | 
					
						
							|  |  |  | 			data.pages = []; | 
					
						
							|  |  |  | 			for(var x=1; x<=data.pageCount; x++) { | 
					
						
							|  |  |  | 				data.pages.push({ | 
					
						
							|  |  |  | 					page: x, | 
					
						
							|  |  |  | 					active: x === parseInt(page, 10) | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 20:05:14 -04:00
										 |  |  | 		res.render('topic', data); | 
					
						
							| 
									
										
										
										
											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-08-16 21:33:42 -04:00
										 |  |  | 	var uid = req.user ? parseInt(req.user.uid, 10) : 0; | 
					
						
							| 
									
										
										
										
											2014-09-22 21:54:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!utils.isNumber(tid)) { | 
					
						
							|  |  |  | 		return next(new Error('[[error:invalid-tid]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	privileges.topics.can('read', tid, uid, function(err, canRead) { | 
					
						
							| 
									
										
										
										
											2014-04-27 16:41:40 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return next(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 21:54:28 -04:00
										 |  |  | 		if (!canRead) { | 
					
						
							| 
									
										
										
										
											2014-11-16 16:46:40 -05:00
										 |  |  | 			return res.status(403).json('[[error:no-privileges]]'); | 
					
						
							| 
									
										
										
										
											2014-04-27 16:41:40 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 21:54:28 -04:00
										 |  |  | 		topics.getLatestUndeletedPid(tid, function(err, pid) { | 
					
						
							| 
									
										
										
										
											2014-04-27 16:41:40 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 21:54:28 -04:00
										 |  |  | 			posts.getPostSummaryByPids([pid], uid, {stripTags: false}, function(err, posts) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return next(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (!Array.isArray(posts) || !posts.length) { | 
					
						
							| 
									
										
										
										
											2014-10-04 18:47:56 -04:00
										 |  |  | 					return res.status(404).json('not-found'); | 
					
						
							| 
									
										
										
										
											2014-09-22 21:54:28 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				res.json(posts[0]); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-04-27 16:41:40 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | module.exports = topicsController; |