| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | var db = require('./database'), | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 	utils = require('./../public/src/utils'), | 
					
						
							|  |  |  | 	user = require('./user'), | 
					
						
							|  |  |  | 	topics = require('./topics'), | 
					
						
							|  |  |  | 	favourites = require('./favourites'), | 
					
						
							| 
									
										
										
										
											2013-07-22 12:44:50 -04:00
										 |  |  | 	postTools = require('./postTools'), | 
					
						
							| 
									
										
										
										
											2013-10-14 16:41:34 -04:00
										 |  |  | 	categories = require('./categories'), | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 	plugins = require('./plugins'), | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 	meta = require('./meta'), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	async = require('async'), | 
					
						
							| 
									
										
										
										
											2013-12-31 17:01:51 -05:00
										 |  |  | 	path = require('path'), | 
					
						
							|  |  |  | 	fs = require('fs'), | 
					
						
							| 
									
										
										
										
											2013-08-12 19:00:31 -04:00
										 |  |  | 	nconf = require('nconf'), | 
					
						
							| 
									
										
										
										
											2013-11-22 08:57:54 -05:00
										 |  |  | 	validator = require('validator'), | 
					
						
							| 
									
										
										
										
											2013-12-12 12:17:58 -05:00
										 |  |  | 	winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2013-12-26 21:10:26 -05:00
										 |  |  | 	gravatar = require('gravatar'), | 
					
						
							|  |  |  | 	S = require('string'); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(Posts) { | 
					
						
							| 
									
										
										
										
											2013-10-13 19:16:48 -04:00
										 |  |  | 	var customUserInfo = {}; | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-22 17:56:13 -05:00
										 |  |  | 	Posts.create = function(data, callback) { | 
					
						
							|  |  |  | 		var uid = data.uid, | 
					
						
							|  |  |  | 			tid = data.tid, | 
					
						
							|  |  |  | 			content = data.content, | 
					
						
							|  |  |  | 			toPid = data.toPid; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 		if (uid === null) { | 
					
						
							| 
									
										
										
										
											2014-03-01 19:15:18 -05:00
										 |  |  | 			return callback(new Error('invalid-user')); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 19:15:18 -05:00
										 |  |  | 		var timestamp = Date.now(), | 
					
						
							|  |  |  | 			postData; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-03 19:32:52 -05:00
										 |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				db.incrObjectField('global', 'nextPid', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(pid, next) { | 
					
						
							| 
									
										
										
										
											2014-03-01 19:15:18 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				postData = { | 
					
						
							|  |  |  | 					'pid': pid, | 
					
						
							|  |  |  | 					'uid': uid, | 
					
						
							|  |  |  | 					'tid': tid, | 
					
						
							|  |  |  | 					'content': content, | 
					
						
							|  |  |  | 					'timestamp': timestamp, | 
					
						
							|  |  |  | 					'reputation': 0, | 
					
						
							|  |  |  | 					'votes': 0, | 
					
						
							|  |  |  | 					'editor': '', | 
					
						
							|  |  |  | 					'edited': 0, | 
					
						
							|  |  |  | 					'deleted': 0 | 
					
						
							|  |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2014-01-03 19:32:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-22 17:56:13 -05:00
										 |  |  | 				if (toPid) { | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 					postData.toPid = toPid; | 
					
						
							| 
									
										
										
										
											2014-02-22 17:56:13 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 19:15:18 -05:00
										 |  |  | 				plugins.fireHook('filter:post.save', postData, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(postData, next) { | 
					
						
							|  |  |  | 				db.setObject('post:' + postData.pid, postData, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(result, next) { | 
					
						
							|  |  |  | 				db.sortedSetAdd('posts:pid', timestamp, postData.pid); | 
					
						
							| 
									
										
										
										
											2014-02-14 14:39:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 19:15:18 -05:00
										 |  |  | 				db.incrObjectField('global', 'postCount'); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 19:15:18 -05:00
										 |  |  | 				topics.onNewPostMade(tid, postData.pid, timestamp); | 
					
						
							|  |  |  | 				categories.onNewPostMade(uid, tid, postData.pid, timestamp); | 
					
						
							|  |  |  | 				user.onNewPostMade(uid, tid, postData.pid, timestamp); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-03 19:32:52 -05:00
										 |  |  | 				plugins.fireHook('filter:post.get', postData, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(postData, next) { | 
					
						
							|  |  |  | 				postTools.parse(postData.content, function(err, content) { | 
					
						
							|  |  |  | 					if(err) { | 
					
						
							| 
									
										
										
										
											2014-02-22 18:56:37 -05:00
										 |  |  | 						return next(err); | 
					
						
							| 
									
										
										
										
											2014-01-03 19:32:52 -05:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-03 19:32:52 -05:00
										 |  |  | 					plugins.fireHook('action:post.save', postData); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-28 20:47:49 -05:00
										 |  |  | 					postData.content = content; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-03 19:32:52 -05:00
										 |  |  | 					next(null, postData); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-01-03 19:32:52 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-01-23 15:46:39 -05:00
										 |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-13 15:21:24 -05:00
										 |  |  | 	Posts.getPostsByTid = function(tid, start, end, reverse, callback) { | 
					
						
							|  |  |  | 		db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange']('tid:' + tid + ':posts', start, end, function(err, pids) { | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 19:15:18 -05:00
										 |  |  | 			if(!Array.isArray(pids) || !pids.length) { | 
					
						
							| 
									
										
										
										
											2013-12-11 16:08:20 -05:00
										 |  |  | 				return callback(null, []); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 19:15:18 -05:00
										 |  |  | 			Posts.getPostsByPids(pids, function(err, posts) { | 
					
						
							| 
									
										
										
										
											2013-12-11 16:08:20 -05:00
										 |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 19:15:18 -05:00
										 |  |  | 				if(!Array.isArray(posts) || !posts.length) { | 
					
						
							| 
									
										
										
										
											2013-12-11 16:08:20 -05:00
										 |  |  | 					return callback(null, []); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 19:15:18 -05:00
										 |  |  | 				plugins.fireHook('filter:post.getPosts', {tid: tid, posts: posts}, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-12-11 16:08:20 -05:00
										 |  |  | 					if(err) { | 
					
						
							|  |  |  | 						return callback(err); | 
					
						
							| 
									
										
										
										
											2013-10-22 14:22:16 -04:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-03-01 19:15:18 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					if(!data || !Array.isArray(data.posts)) { | 
					
						
							|  |  |  | 						return callback(null, []); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					callback(null, data.posts); | 
					
						
							| 
									
										
										
										
											2013-10-28 13:34:36 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-12-11 16:08:20 -05:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-09-27 11:47:16 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-20 21:00:10 -05:00
										 |  |  | 	Posts.getPostsByPids = function(pids, callback) { | 
					
						
							|  |  |  | 		var keys = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for(var x=0, numPids=pids.length; x<numPids; ++x) { | 
					
						
							|  |  |  | 			keys.push('post:' + pids[x]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		db.getObjects(keys, function(err, data) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.map(data, function(postData, next) { | 
					
						
							|  |  |  | 				if(!postData) { | 
					
						
							|  |  |  | 					return next(null); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				postData.relativeTime = utils.toISOString(postData.timestamp); | 
					
						
							|  |  |  | 				postData.relativeEditTime = parseInt(postData.edited, 10) !== 0 ? utils.toISOString(postData.edited) : ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				postTools.parse(postData.content, function(err, content) { | 
					
						
							|  |  |  | 					if(err) { | 
					
						
							|  |  |  | 						return next(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					postData.content = content; | 
					
						
							|  |  |  | 					next(null, postData); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			}, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Posts.getPostsByUid = function(callerUid, uid, start, end, callback) { | 
					
						
							|  |  |  | 		user.getPostIds(uid, start, end, function(err, pids) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.filter(pids, function(pid, next) { | 
					
						
							|  |  |  | 				postTools.privileges(pid, callerUid, function(err, privileges) { | 
					
						
							|  |  |  | 					next(privileges.read); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, function(pids) { | 
					
						
							|  |  |  | 				if (!(pids && pids.length)) { | 
					
						
							| 
									
										
										
										
											2014-02-04 17:31:05 -05:00
										 |  |  | 					return callback(null, { posts: [], nextStart: 0}); | 
					
						
							| 
									
										
										
										
											2014-01-20 21:00:10 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-04 17:31:05 -05:00
										 |  |  | 				Posts.getPostSummaryByPids(pids, false, function(err, posts) { | 
					
						
							|  |  |  | 					if(err) { | 
					
						
							|  |  |  | 						return callback(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if(!posts || !posts.length) { | 
					
						
							|  |  |  | 						return callback(null, { posts: [], nextStart: 0}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					db.sortedSetRevRank('uid:' + uid + ':posts', posts[posts.length - 1].pid, function(err, rank) { | 
					
						
							|  |  |  | 						if(err) { | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 							return callback(err); | 
					
						
							| 
									
										
										
										
											2014-02-04 17:31:05 -05:00
										 |  |  | 						} | 
					
						
							|  |  |  | 						var userPosts = { | 
					
						
							|  |  |  | 							posts: posts, | 
					
						
							|  |  |  | 							nextStart: parseInt(rank, 10) + 1 | 
					
						
							|  |  |  | 						}; | 
					
						
							|  |  |  | 						callback(null, userPosts); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-01-20 21:00:10 -05:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-01-20 21:00:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 	Posts.addUserInfoToPost = function(post, callback) { | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 		user.getUserFields(post.uid, ['username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned'], function(err, userData) { | 
					
						
							| 
									
										
										
										
											2013-09-27 11:47:16 -04:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2013-12-06 21:08:21 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-09-27 11:47:16 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-01 20:07:30 -05:00
										 |  |  | 			postTools.parseSignature(userData.signature, function(err, signature) { | 
					
						
							| 
									
										
										
										
											2013-12-12 12:17:58 -05:00
										 |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 				post.username = userData.username || 'anonymous'; | 
					
						
							|  |  |  | 				post.userslug = userData.userslug || ''; | 
					
						
							|  |  |  | 				post.user_rep = userData.reputation || 0; | 
					
						
							|  |  |  | 				post.user_postcount = userData.postcount || 0; | 
					
						
							| 
									
										
										
										
											2013-12-05 13:11:27 -05:00
										 |  |  | 				post.user_banned = parseInt(userData.banned, 10) === 1; | 
					
						
							| 
									
										
										
										
											2014-01-25 12:05:48 -05:00
										 |  |  | 				post.picture = userData.picture || gravatar.url('', {}, true); | 
					
						
							| 
									
										
										
										
											2013-12-24 13:12:25 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				if(meta.config.disableSignatures === undefined || parseInt(meta.config.disableSignatures, 10) === 0) { | 
					
						
							|  |  |  | 					post.signature = signature; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-13 19:16:48 -04:00
										 |  |  | 				for (var info in customUserInfo) { | 
					
						
							|  |  |  | 					if (customUserInfo.hasOwnProperty(info)) { | 
					
						
							|  |  |  | 						post[info] = userData[info] || customUserInfo[info]; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-30 13:06:30 -05:00
										 |  |  | 				plugins.fireHook('filter:posts.custom_profile_info', {profile: [], uid: post.uid, pid: post.pid}, function(err, profile_info) { | 
					
						
							| 
									
										
										
										
											2013-12-12 12:17:58 -05:00
										 |  |  | 					if(err) { | 
					
						
							|  |  |  | 						return callback(err); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-01-30 13:06:30 -05:00
										 |  |  | 					post.custom_profile_info = profile_info.profile; | 
					
						
							| 
									
										
										
										
											2013-10-13 19:16:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					if (post.editor !== '') { | 
					
						
							|  |  |  | 						user.getUserFields(post.editor, ['username', 'userslug'], function(err, editorData) { | 
					
						
							| 
									
										
										
										
											2013-12-12 12:17:58 -05:00
										 |  |  | 							if (err) { | 
					
						
							| 
									
										
										
										
											2013-12-12 12:19:03 -05:00
										 |  |  | 								return callback(err); | 
					
						
							| 
									
										
										
										
											2013-12-12 12:17:58 -05:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-13 19:16:48 -04:00
										 |  |  | 							post.editorname = editorData.username; | 
					
						
							|  |  |  | 							post.editorslug = editorData.userslug; | 
					
						
							| 
									
										
										
										
											2014-01-23 15:46:39 -05:00
										 |  |  | 							callback(null,  post); | 
					
						
							| 
									
										
										
										
											2013-10-13 19:16:48 -04:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2014-01-23 15:46:39 -05:00
										 |  |  | 						callback(null, post); | 
					
						
							| 
									
										
										
										
											2013-10-13 19:16:48 -04:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-09-27 11:47:16 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-24 14:34:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 21:08:21 -05:00
										 |  |  | 	Posts.getPostSummaryByPids = function(pids, stripTags, callback) { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-15 14:34:15 -04:00
										 |  |  | 		function getPostSummary(pid, callback) { | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 					Posts.getPostFields(pid, ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'], function(err, postData) { | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 						if(err) { | 
					
						
							|  |  |  | 							return next(err); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:11:27 -05:00
										 |  |  | 						if (parseInt(postData.deleted, 10) === 1) { | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 							return callback(null); | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2014-01-18 23:18:58 -05:00
										 |  |  | 							postData.relativeTime = utils.toISOString(postData.timestamp); | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 							next(null, postData); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function(postData, next) { | 
					
						
							|  |  |  | 					Posts.addUserInfoToPost(postData, function() { | 
					
						
							|  |  |  | 						next(null, postData); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function(postData, next) { | 
					
						
							| 
									
										
										
										
											2013-11-22 08:57:54 -05:00
										 |  |  | 					topics.getTopicFields(postData.tid, ['title', 'cid', 'slug', 'deleted'], function(err, topicData) { | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 						if (err) { | 
					
						
							|  |  |  | 							return callback(err); | 
					
						
							| 
									
										
										
										
											2013-12-05 13:11:27 -05:00
										 |  |  | 						} else if (parseInt(topicData.deleted, 10) === 1) { | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 							return callback(null); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						categories.getCategoryFields(topicData.cid, ['name', 'icon', 'slug'], function(err, categoryData) { | 
					
						
							|  |  |  | 							postData.categoryName = categoryData.name; | 
					
						
							|  |  |  | 							postData.categoryIcon = categoryData.icon; | 
					
						
							|  |  |  | 							postData.categorySlug = categoryData.slug; | 
					
						
							| 
									
										
										
										
											2014-02-19 17:23:25 -05:00
										 |  |  | 							postData.title = validator.escape(topicData.title); | 
					
						
							| 
									
										
										
										
											2013-11-22 08:57:54 -05:00
										 |  |  | 							postData.topicSlug = topicData.slug; | 
					
						
							|  |  |  | 							next(null, postData); | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-08-01 17:27:37 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function(postData, next) { | 
					
						
							| 
									
										
										
										
											2014-02-22 18:56:37 -05:00
										 |  |  | 					if (!postData.content) { | 
					
						
							|  |  |  | 						return next(null, postData); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-12-06 21:08:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-22 18:56:37 -05:00
										 |  |  | 					postTools.parse(postData.content, function(err, content) { | 
					
						
							|  |  |  | 						if(err) { | 
					
						
							|  |  |  | 							return next(err); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						if(stripTags) { | 
					
						
							|  |  |  | 							var s = S(content); | 
					
						
							|  |  |  | 							postData.content = s.stripTags.apply(s, utils.getTagsExcept(['img', 'i'])).s; | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							postData.content = content; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-12-06 21:08:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 						next(null, postData); | 
					
						
							| 
									
										
										
										
											2014-02-22 18:56:37 -05:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 			], callback); | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 		async.map(pids, getPostSummary, function(err, posts) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-07-15 14:34:15 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			posts = posts.filter(function(p) { | 
					
						
							|  |  |  | 				return p; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			callback(null, posts); | 
					
						
							| 
									
										
										
										
											2013-07-15 14:34:15 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 	Posts.getPostData = function(pid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 		db.getObject('post:' + pid, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-11-15 13:39:09 -05:00
										 |  |  | 			if(err) { | 
					
						
							| 
									
										
										
										
											2014-02-08 13:44:15 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-11-03 11:53:44 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-11-15 13:39:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 13:44:15 -05:00
										 |  |  | 			plugins.fireHook('filter:post.get', data, callback); | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-12-21 19:42:07 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-22 17:08:07 -04:00
										 |  |  | 	Posts.getPostFields = function(pid, fields, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 		db.getObjectFields('post:' + pid, fields, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 			if(err) { | 
					
						
							| 
									
										
										
										
											2014-02-08 13:44:15 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-07-22 17:08:07 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// TODO: I think the plugins system needs an optional 'parameters' paramter so I don't have to do this:
 | 
					
						
							|  |  |  | 			data = data || {}; | 
					
						
							|  |  |  | 			data.pid = pid; | 
					
						
							|  |  |  | 			data.fields = fields; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 13:44:15 -05:00
										 |  |  | 			plugins.fireHook('filter:post.getFields', data, callback); | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-12-21 19:42:07 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-09 20:03:19 -04:00
										 |  |  | 	Posts.getPostField = function(pid, field, callback) { | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 		Posts.getPostFields(pid, [field], function(err, data) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							| 
									
										
										
										
											2014-02-08 13:44:15 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-10-22 14:22:16 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			callback(null, data[field]); | 
					
						
							| 
									
										
										
										
											2013-08-09 20:03:19 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-12-21 19:42:07 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-09 20:03:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-27 15:03:36 -05:00
										 |  |  | 	Posts.setPostField = function(pid, field, value, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 		db.setObjectField('post:' + pid, field, value, callback); | 
					
						
							| 
									
										
										
										
											2013-10-22 14:22:16 -04:00
										 |  |  | 		plugins.fireHook('action:post.setField', { | 
					
						
							|  |  |  | 			'pid': pid, | 
					
						
							|  |  |  | 			'field': field, | 
					
						
							|  |  |  | 			'value': value | 
					
						
							| 
									
										
										
										
											2013-11-27 15:03:36 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-12-21 19:42:07 -05:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Posts.setPostFields = function(pid, data, callback) { | 
					
						
							|  |  |  | 		db.setObject('post:' + pid, data, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-09 20:03:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 	Posts.getCidByPid = function(pid, callback) { | 
					
						
							|  |  |  | 		Posts.getPostField(pid, 'tid', function(err, tid) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							| 
									
										
										
										
											2014-02-08 13:44:15 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			topics.getTopicField(tid, 'cid', function(err, cid) { | 
					
						
							|  |  |  | 				if(err) { | 
					
						
							| 
									
										
										
										
											2014-02-08 13:44:15 -05:00
										 |  |  | 					return callback(err); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (cid) { | 
					
						
							|  |  |  | 					callback(null, cid); | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2014-02-08 13:44:15 -05:00
										 |  |  | 					callback(new Error('invalid-category-id')); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:37:03 -04:00
										 |  |  | 	Posts.uploadPostImage = function(image, callback) { | 
					
						
							| 
									
										
										
										
											2013-08-15 16:50:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 00:33:10 -05:00
										 |  |  | 		if(plugins.hasListeners('filter:uploadImage')) { | 
					
						
							| 
									
										
										
										
											2014-02-13 19:41:54 -05:00
										 |  |  | 			plugins.fireHook('filter:uploadImage', image, callback); | 
					
						
							| 
									
										
										
										
											2014-02-08 21:02:06 -05:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2013-12-31 17:01:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 22:09:28 -05:00
										 |  |  | 			if (meta.config.allowFileUploads) { | 
					
						
							| 
									
										
										
										
											2014-02-08 21:02:06 -05:00
										 |  |  | 				Posts.uploadPostFile(image, callback); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				callback(new Error('Uploads are disabled!')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-12-14 16:29:33 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-31 17:01:51 -05:00
										 |  |  | 	Posts.uploadPostFile = function(file, callback) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 00:33:10 -05:00
										 |  |  | 		if(plugins.hasListeners('filter:uploadFile')) { | 
					
						
							| 
									
										
										
										
											2014-02-13 19:41:54 -05:00
										 |  |  | 			plugins.fireHook('filter:uploadFile', file, callback); | 
					
						
							| 
									
										
										
										
											2014-02-08 22:34:50 -05:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2013-12-31 17:01:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 22:34:50 -05:00
										 |  |  | 			if(!meta.config.allowFileUploads) { | 
					
						
							|  |  |  | 				return callback(new Error('File uploads are not allowed')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-12-31 17:01:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-13 19:41:54 -05:00
										 |  |  | 			if(!file) { | 
					
						
							| 
									
										
										
										
											2014-02-08 22:34:50 -05:00
										 |  |  | 				return callback(new Error('invalid file')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-12-31 17:01:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-13 19:41:54 -05:00
										 |  |  | 			if(file.size > parseInt(meta.config.maximumFileSize, 10) * 1024) { | 
					
						
							| 
									
										
										
										
											2014-02-08 22:34:50 -05:00
										 |  |  | 				return callback(new Error('File too big')); | 
					
						
							| 
									
										
										
										
											2013-12-31 17:01:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-01-16 22:06:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 22:34:50 -05:00
										 |  |  | 			var filename = 'upload-' + utils.generateUUID() + path.extname(file.name); | 
					
						
							| 
									
										
										
										
											2014-02-13 19:41:54 -05:00
										 |  |  | 			require('./file').saveFileToLocal(filename, file.path, function(err, upload) { | 
					
						
							| 
									
										
										
										
											2014-02-08 22:34:50 -05:00
										 |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				callback(null, { | 
					
						
							| 
									
										
										
										
											2014-02-13 19:41:54 -05:00
										 |  |  | 					url: upload.url, | 
					
						
							| 
									
										
										
										
											2014-02-08 22:34:50 -05:00
										 |  |  | 					name: file.name | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-01-16 22:06:23 -05:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-02-08 22:34:50 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-12-31 17:01:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 	Posts.getFavourites = function(uid, start, end, callback) { | 
					
						
							|  |  |  | 		db.getSortedSetRevRange('uid:' + uid + ':favourites', start, end, function(err, pids) { | 
					
						
							| 
									
										
										
										
											2014-01-20 21:00:10 -05:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2014-01-20 21:00:10 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 			Posts.getPostSummaryByPids(pids, false, function(err, posts) { | 
					
						
							|  |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if(!posts || !posts.length) { | 
					
						
							|  |  |  | 					return callback(null, { posts: [], nextStart: 0}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				db.sortedSetRevRank('uid:' + uid + ':favourites', posts[posts.length - 1].pid, function(err, rank) { | 
					
						
							|  |  |  | 					if(err) { | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 						return callback(err); | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					var favourites = { | 
					
						
							|  |  |  | 						posts: posts, | 
					
						
							|  |  |  | 						nextStart: parseInt(rank, 10) + 1 | 
					
						
							|  |  |  | 					}; | 
					
						
							|  |  |  | 					callback(null, favourites); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-10 22:45:52 -05:00
										 |  |  | 	Posts.getPidPage = function(pid, uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-02-22 16:48:58 -05:00
										 |  |  | 		if(!pid) { | 
					
						
							|  |  |  | 			return callback(new Error('invalid-pid')); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-22 16:48:58 -05:00
										 |  |  | 		var index = 0; | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 				Posts.getPidIndex(pid, next); | 
					
						
							| 
									
										
										
										
											2014-02-22 16:48:58 -05:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 			function(result, next) { | 
					
						
							|  |  |  | 				index = result; | 
					
						
							| 
									
										
										
										
											2014-02-22 16:48:58 -05:00
										 |  |  | 				user.getSettings(uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(settings, next) { | 
					
						
							|  |  |  | 				next(null, Math.ceil((index + 1) / settings.postsPerPage)); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2014-02-17 20:57:12 -05:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Posts.getPidIndex = function(pid, callback) { | 
					
						
							|  |  |  | 		Posts.getPostField(pid, 'tid', function(err, tid) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			db.sortedSetRank('tid:' + tid + ':posts', pid, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-01-25 19:19:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  | }(exports)); |