| 
									
										
										
										
											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'), | 
					
						
							|  |  |  | 	threadTools = require('./../threadTools'), | 
					
						
							|  |  |  | 	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, | 
					
						
							|  |  |  | 		privileges; | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function(next) { | 
					
						
							| 
									
										
										
										
											2014-03-02 22:54:03 -05:00
										 |  |  | 			threadTools.privileges(tid, ((req.user) ? req.user.uid || 0 : 0), function(err, userPrivileges) { | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 				if (!err) { | 
					
						
							| 
									
										
										
										
											2014-03-02 22:54:03 -05:00
										 |  |  | 					if (!userPrivileges.read) { | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 						next(new Error('not-enough-privileges')); | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2014-03-02 22:54:03 -05:00
										 |  |  | 						privileges = userPrivileges; | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 						next(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					next(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			user.getSettings(uid, function(err, settings) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return next(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				var start = (page - 1) * settings.topicsPerPage, | 
					
						
							|  |  |  | 					end = start + settings.topicsPerPage - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				topics.getTopicWithPosts(tid, uid, start, end, function (err, topicData) { | 
					
						
							|  |  |  | 					if (topicData) { | 
					
						
							|  |  |  | 						if (parseInt(topicData.deleted, 10) === 1 && parseInt(topicData.expose_tools, 10) === 0) { | 
					
						
							|  |  |  | 							return next(new Error('Topic deleted'), null); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					next(err, topicData); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (topicData, next) { | 
					
						
							|  |  |  | 			var lastMod = topicData.timestamp, | 
					
						
							|  |  |  | 				description = (function() { | 
					
						
							|  |  |  | 					var	content = ''; | 
					
						
							|  |  |  | 					if(topicData.posts.length) { | 
					
						
							|  |  |  | 						content = S(topicData.posts[0].content).stripTags().s; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (content.length > 255) { | 
					
						
							|  |  |  | 						content = content.substr(0, 255) + '...'; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					return validator.escape(content); | 
					
						
							|  |  |  | 				})(), | 
					
						
							|  |  |  | 				timestamp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for (var x = 0, numPosts = topicData.posts.length; x < numPosts; x++) { | 
					
						
							|  |  |  | 				timestamp = parseInt(topicData.posts[x].timestamp, 10); | 
					
						
							|  |  |  | 				if (timestamp > lastMod) { | 
					
						
							|  |  |  | 					lastMod = timestamp; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-02 15:14:38 -05:00
										 |  |  | 			var ogImageUrl = meta.config['brand:logo']; | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 			if(ogImageUrl && ogImageUrl.indexOf('http') === -1) { | 
					
						
							|  |  |  | 				ogImageUrl = nconf.get('url') + ogImageUrl; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					property: "og:image:url", | 
					
						
							|  |  |  | 					content: ogImageUrl | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					property: 'og:image', | 
					
						
							|  |  |  | 					content: topicData.posts.length?topicData.posts[0].picture:'' | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					property: "article:published_time", | 
					
						
							|  |  |  | 					content: utils.toISOString(topicData.timestamp) | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					property: 'article:modified_time', | 
					
						
							|  |  |  | 					content: utils.toISOString(lastMod) | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					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-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) { | 
					
						
							|  |  |  | 			if (err.message === 'not-enough-privileges') { | 
					
						
							|  |  |  | 				return res.redirect('403'); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				return res.redirect('404'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-02 22:54:03 -05:00
										 |  |  | 		data.privileges = privileges; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 = []; | 
					
						
							|  |  |  | 		for(var x=1;x<=data.pageCount;x++) { | 
					
						
							|  |  |  | 			data.pages.push({ | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 				page: x, | 
					
						
							|  |  |  | 				active: x === parseInt(page, 10) | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (res.locals.isAPI) { | 
					
						
							| 
									
										
										
										
											2014-03-02 22:54:03 -05:00
										 |  |  | 			res.json(data); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2014-03-02 22:54:03 -05:00
										 |  |  | 			res.render('topic', data); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:56:05 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = topicsController; |