| 
									
										
										
										
											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-03-02 15:14:38 -05: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'), | 
					
						
							| 
									
										
										
										
											2014-03-02 15:14:38 -05: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, | 
					
						
							|  |  |  | 		page = req.query.page || 1, | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							| 
									
										
										
										
											2014-06-02 17:31:09 -04:00
										 |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2014-06-02 20:41:03 -04:00
										 |  |  | 			privileges.topics.get(tid, uid, next); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2014-06-02 20:41:03 -04:00
										 |  |  | 		function (privileges, next) { | 
					
						
							|  |  |  | 			if (!privileges.read) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-06-02 17:31:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-02 20:41:03 -04:00
										 |  |  | 			userPrivileges = privileges; | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-02 20:41:03 -04:00
										 |  |  | 			user.getSettings(uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (settings, next) { | 
					
						
							|  |  |  | 			var postIndex = 0; | 
					
						
							|  |  |  | 			if (!settings.usePagination) { | 
					
						
							|  |  |  | 				postIndex = Math.max((req.params.post_index || 1) - (settings.postsPerPage - 1), 0); | 
					
						
							|  |  |  | 			} else if (!req.query.page) { | 
					
						
							|  |  |  | 				var index = Math.max(parseInt((req.params.post_index || 0), 10), 0); | 
					
						
							|  |  |  | 				page = Math.ceil((index + 1) / settings.postsPerPage); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var start = (page - 1) * settings.postsPerPage + postIndex, | 
					
						
							|  |  |  | 				end = start + settings.postsPerPage - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 			var set = 'tid:' + tid + ':posts', | 
					
						
							|  |  |  | 				reverse = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (settings.topicPostSort === 'newest_to_oldest') { | 
					
						
							|  |  |  | 				reverse = true; | 
					
						
							|  |  |  | 			} else if (settings.topicPostSort === 'most_votes') { | 
					
						
							|  |  |  | 				reverse = true; | 
					
						
							|  |  |  | 				set = 'tid:' + tid + ':posts:votes'; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			topics.getTopicWithPosts(tid, set, uid, start, end, reverse, function (err, topicData) { | 
					
						
							| 
									
										
										
										
											2014-06-02 20:41:03 -04:00
										 |  |  | 				if (topicData) { | 
					
						
							|  |  |  | 					if (parseInt(topicData.deleted, 10) === 1 && !userPrivileges.view_deleted) { | 
					
						
							|  |  |  | 						return next(new Error('[[error:no-topic]]')); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-06-02 20:41:03 -04:00
										 |  |  | 					topicData.currentPage = page; | 
					
						
							| 
									
										
										
										
											2014-06-06 23:09:05 -04:00
										 |  |  | 					if(page > 1) { | 
					
						
							|  |  |  | 						topicData.posts.splice(0, 1); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-06-02 20:41:03 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				next(err, 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-03-21 15:40:37 -04:00
										 |  |  | 			if(topicData.posts.length) { | 
					
						
							| 
									
										
										
										
											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-02-27 14:56:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-24 20:43:24 -04:00
										 |  |  | 			var ogImageUrl = ''; | 
					
						
							|  |  |  | 			if (topicData.thumb) { | 
					
						
							|  |  |  | 				ogImageUrl = topicData.thumb; | 
					
						
							|  |  |  | 			} else if(topicData.posts.length && topicData.posts[0].user && topicData.posts[0].user.picture){ | 
					
						
							|  |  |  | 				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', | 
					
						
							|  |  |  | 					content: topicData.title | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					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', | 
					
						
							|  |  |  | 					content: topicData.category.name | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			res.locals.linkTags = [ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					rel: 'alternate', | 
					
						
							|  |  |  | 					type: 'application/rss+xml', | 
					
						
							|  |  |  | 					href: nconf.get('url') + '/topic/' + tid + '.rss' | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					rel: 'up', | 
					
						
							|  |  |  | 					href: nconf.get('url') + '/category/' + topicData.category.slug | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			]; | 
					
						
							| 
									
										
										
										
											2014-03-10 18:28:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-05-15 20:49:47 -04:00
										 |  |  | 			if (err.message === '[[error:no-privileges]]') { | 
					
						
							| 
									
										
										
										
											2014-03-14 19:07:50 -04:00
										 |  |  | 				return res.locals.isAPI ? res.json(403, err.message) : res.redirect('403'); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2014-03-14 19:07:50 -04:00
										 |  |  | 				return res.locals.isAPI ? res.json(404, 'not-found') : res.redirect('404'); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 		data.privileges = userPrivileges; | 
					
						
							| 
									
										
										
										
											2014-03-02 22:54:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 		var topic_url = tid + (req.params.slug ? '/' + req.params.slug : ''); | 
					
						
							|  |  |  | 		var queryString = qs.stringify(req.query); | 
					
						
							|  |  |  | 		if(queryString.length) { | 
					
						
							|  |  |  | 			topic_url += '?' + queryString; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Paginator for noscript
 | 
					
						
							| 
									
										
										
										
											2014-03-02 22:54:03 -05:00
										 |  |  | 		data.pages = []; | 
					
						
							| 
									
										
										
										
											2014-03-14 19:07:50 -04:00
										 |  |  | 		for(var x=1; x<=data.pageCount; x++) { | 
					
						
							| 
									
										
										
										
											2014-03-02 22:54:03 -05:00
										 |  |  | 			data.pages.push({ | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 				page: x, | 
					
						
							|  |  |  | 				active: x === parseInt(page, 10) | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 	topics.getLatestUndeletedPid(tid, function(err, pid) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return next(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!pid) { | 
					
						
							|  |  |  | 			return res.json(404, 'not-found'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		posts.getPostSummaryByPids([pid], false, function(err, posts) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!Array.isArray(posts) || !posts.length) { | 
					
						
							|  |  |  | 				return res.json(404, 'not-found'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			res.json(posts[0]); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | module.exports = topicsController; |