| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-12 16:45:00 -04:00
										 |  |  | var async = require('async'), | 
					
						
							|  |  |  | 	path = require('path'), | 
					
						
							|  |  |  | 	fs = require('fs'), | 
					
						
							|  |  |  | 	nconf = require('nconf'), | 
					
						
							|  |  |  | 	validator = require('validator'), | 
					
						
							|  |  |  | 	winston = require('winston'), | 
					
						
							|  |  |  | 	gravatar = require('gravatar'), | 
					
						
							|  |  |  | 	S = require('string'), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	db = require('./database'), | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 	utils = require('./../public/src/utils'), | 
					
						
							|  |  |  | 	user = require('./user'), | 
					
						
							| 
									
										
										
										
											2014-06-12 16:45:00 -04:00
										 |  |  | 	groups = require('./groups'), | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 	topics = require('./topics'), | 
					
						
							|  |  |  | 	favourites = require('./favourites'), | 
					
						
							| 
									
										
										
										
											2013-07-22 12:44:50 -04:00
										 |  |  | 	postTools = require('./postTools'), | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 	privileges = require('./privileges'), | 
					
						
							| 
									
										
										
										
											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'), | 
					
						
							| 
									
										
										
										
											2014-06-12 16:45:00 -04:00
										 |  |  | 	emitter = require('./emitter'); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(Posts) { | 
					
						
							| 
									
										
										
										
											2014-06-09 12:51:49 -04:00
										 |  |  | 	require('./posts/delete')(Posts); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-04-09 22:26:23 -04:00
										 |  |  | 			return callback(new Error('[[error:invalid-uid]]')); | 
					
						
							| 
									
										
										
										
											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-17 17:38:47 -04:00
										 |  |  | 				emitter.emit('event:newpost', postData); | 
					
						
							| 
									
										
										
										
											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-06-06 22:12:14 -04:00
										 |  |  | 	Posts.getPostsByTid = function(tid, set, start, end, reverse, callback) { | 
					
						
							|  |  |  | 		db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, 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); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-05 15:38:54 -05:00
										 |  |  | 	Posts.getRecentPosts = function(uid, start, stop, term, callback) { | 
					
						
							|  |  |  | 		var terms = { | 
					
						
							|  |  |  | 			day: 86400000, | 
					
						
							|  |  |  | 			week: 604800000, | 
					
						
							|  |  |  | 			month: 2592000000 | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var since = terms.day; | 
					
						
							|  |  |  | 		if (terms[term]) { | 
					
						
							|  |  |  | 			since = terms[term]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:59:17 -04:00
										 |  |  | 		db.getSortedSetRevRangeByScore('posts:pid', start, count, Infinity, Date.now() - since, function(err, pids) { | 
					
						
							| 
									
										
										
										
											2014-03-05 15:38:54 -05:00
										 |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.filter(pids, function(pid, next) { | 
					
						
							| 
									
										
										
										
											2014-05-17 18:59:34 -04:00
										 |  |  | 				privileges.posts.can('read', pid, uid, function(err, canRead) { | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 					next(!err && canRead); | 
					
						
							| 
									
										
										
										
											2014-03-05 15:38:54 -05:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}, function(pids) { | 
					
						
							|  |  |  | 				Posts.getPostSummaryByPids(pids, true, callback); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-28 01:03:26 -04:00
										 |  |  | 	Posts.getUserInfoForPosts = function(uids, callback) { | 
					
						
							|  |  |  | 		user.getMultipleUserFields(uids, ['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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-28 01:03:26 -04:00
										 |  |  | 			async.map(userData, function(userData, next) { | 
					
						
							|  |  |  | 				var userInfo = { | 
					
						
							| 
									
										
										
										
											2014-07-03 09:42:39 -04:00
										 |  |  | 					uid: userData.uid || 0, | 
					
						
							| 
									
										
										
										
											2014-06-28 01:03:26 -04:00
										 |  |  | 					username: userData.username || '[[global:guest]]', | 
					
						
							|  |  |  | 					userslug: userData.userslug || '', | 
					
						
							|  |  |  | 					reputation: userData.reputation || 0, | 
					
						
							|  |  |  | 					postcount: userData.postcount || 0, | 
					
						
							|  |  |  | 					banned: parseInt(userData.banned, 10) === 1, | 
					
						
							|  |  |  | 					picture: userData.picture || user.createGravatarURLFromEmail('') | 
					
						
							|  |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2014-03-13 16:41:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-28 01:03:26 -04:00
										 |  |  | 				async.parallel({ | 
					
						
							|  |  |  | 					signature: function(next) { | 
					
						
							|  |  |  | 						if (parseInt(meta.config.disableSignatures, 10) === 1) { | 
					
						
							|  |  |  | 							return next(); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						postTools.parseSignature(userData.signature, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					customProfileInfo: function(next) { | 
					
						
							|  |  |  | 						plugins.fireHook('filter:posts.custom_profile_info', {profile: [], uid: userData.uid}, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					groups: function(next) { | 
					
						
							|  |  |  | 						groups.getUserGroups(userData.uid, next); | 
					
						
							| 
									
										
										
										
											2013-10-13 19:16:48 -04:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-06-28 01:03:26 -04:00
										 |  |  | 				}, function(err, results) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return next(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					userInfo.signature = results.signature; | 
					
						
							|  |  |  | 					userInfo.custom_profile_info = results.custom_profile_info; | 
					
						
							|  |  |  | 					userInfo.groups = results.groups; | 
					
						
							|  |  |  | 					next(null, userInfo); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, callback); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 13:18:12 -04:00
										 |  |  | 		function getPostSummary(post, callback) { | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 13:18:12 -04:00
										 |  |  | 			post.relativeTime = utils.toISOString(post.timestamp); | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-20 15:07:53 -04:00
										 |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				user: function(next) { | 
					
						
							| 
									
										
										
										
											2014-05-04 17:26:56 -04:00
										 |  |  | 					user.getUserFields(post.uid, ['username', 'userslug', 'picture'], next); | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2014-04-20 15:07:53 -04:00
										 |  |  | 				topicCategory: function(next) { | 
					
						
							| 
									
										
										
										
											2014-03-20 13:18:12 -04:00
										 |  |  | 					topics.getTopicFields(post.tid, ['title', 'cid', 'slug', 'deleted'], function(err, topicData) { | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 						if (err) { | 
					
						
							| 
									
										
										
										
											2014-03-20 13:18:12 -04:00
										 |  |  | 							return next(err); | 
					
						
							| 
									
										
										
										
											2013-12-05 13:11:27 -05:00
										 |  |  | 						} else if (parseInt(topicData.deleted, 10) === 1) { | 
					
						
							| 
									
										
										
										
											2014-03-20 13:18:12 -04:00
										 |  |  | 							return callback(); | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2014-03-20 13:18:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 						categories.getCategoryFields(topicData.cid, ['name', 'icon', 'slug'], function(err, categoryData) { | 
					
						
							| 
									
										
										
										
											2014-03-20 13:18:12 -04:00
										 |  |  | 							if (err) { | 
					
						
							|  |  |  | 								return next(err); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-05 14:52:32 -05:00
										 |  |  | 							topicData.title = validator.escape(topicData.title); | 
					
						
							| 
									
										
										
										
											2014-04-20 15:07:53 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 							next(null, {topic: topicData, category: categoryData}); | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-08-01 17:27:37 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2014-04-20 15:07:53 -04:00
										 |  |  | 				content: function(next) { | 
					
						
							| 
									
										
										
										
											2014-03-20 13:18:12 -04:00
										 |  |  | 					if (!post.content) { | 
					
						
							| 
									
										
										
										
											2014-04-20 15:07:53 -04:00
										 |  |  | 						return next(null, post.content); | 
					
						
							| 
									
										
										
										
											2014-02-22 18:56:37 -05:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-12-06 21:08:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-20 15:07:53 -04:00
										 |  |  | 					postTools.parse(post.content, next); | 
					
						
							| 
									
										
										
										
											2014-06-02 17:31:09 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				index: function(next) { | 
					
						
							|  |  |  | 					Posts.getPidIndex(post.pid, next); | 
					
						
							| 
									
										
										
										
											2014-04-20 15:07:53 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}, function(err, results) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-02-22 18:56:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-20 15:07:53 -04:00
										 |  |  | 				post.user = results.user; | 
					
						
							|  |  |  | 				post.topic = results.topicCategory.topic; | 
					
						
							|  |  |  | 				post.category = results.topicCategory.category; | 
					
						
							| 
									
										
										
										
											2014-06-13 15:33:22 -04:00
										 |  |  | 				post.index = results.index; | 
					
						
							| 
									
										
										
										
											2013-12-06 21:08:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-20 15:07:53 -04:00
										 |  |  | 				if (stripTags) { | 
					
						
							|  |  |  | 					var s = S(results.content); | 
					
						
							| 
									
										
										
										
											2014-04-27 17:17:07 -04:00
										 |  |  | 					post.content = s.stripTags.apply(s, utils.stripTags).s; | 
					
						
							| 
									
										
										
										
											2014-04-20 15:07:53 -04:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					post.content = results.content; | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-04-20 15:07:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-27 17:17:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-20 15:07:53 -04:00
										 |  |  | 				callback(null, post); | 
					
						
							| 
									
										
										
										
											2014-03-20 13:18:12 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 13:18:12 -04:00
										 |  |  | 		var keys = pids.map(function(pid) { | 
					
						
							|  |  |  | 			return 'post:' + pid; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		db.getObjectsFields(keys, ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'], function(err, posts) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-07-15 14:34:15 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			posts = posts.filter(function(p) { | 
					
						
							| 
									
										
										
										
											2014-04-09 15:49:36 -04:00
										 |  |  | 				return !!p && parseInt(p.deleted, 10) !== 1; | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 13:18:12 -04:00
										 |  |  | 			async.map(posts, getPostSummary, function(err, posts) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				posts = posts.filter(function(post) { | 
					
						
							|  |  |  | 					return !!post; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				return 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-28 14:59:01 -04:00
										 |  |  | 	Posts.getPostsFields = function(pids, fields, callback) { | 
					
						
							|  |  |  | 		var keys = pids.map(function(pid) { | 
					
						
							|  |  |  | 			return 'post:' + pid; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		db.getObjectsFields(keys, fields, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2014-03-13 20:24:04 -04:00
										 |  |  | 				if(err || !cid) { | 
					
						
							| 
									
										
										
										
											2014-04-09 22:26:23 -04:00
										 |  |  | 					return callback(err || new Error('[[error:invalid-cid]]')); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-03-13 20:24:04 -04:00
										 |  |  | 				callback(null, cid); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-28 14:59:01 -04:00
										 |  |  | 	Posts.getCidsByPids = function(pids, callback) { | 
					
						
							|  |  |  | 		Posts.getPostsFields(pids, ['tid'], function(err, posts) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var tids = posts.map(function(post) { | 
					
						
							|  |  |  | 				return post.tid; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			topics.getTopicsFields(tids, ['cid'], function(err, topics) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				var cids = topics.map(function(topic) { | 
					
						
							|  |  |  | 					return topic.cid; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				callback(null, cids); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-20 17:46:13 -04:00
										 |  |  | 	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) { | 
					
						
							|  |  |  | 				privileges.posts.can('read', pid, callerUid, function(err, canRead) { | 
					
						
							|  |  |  | 					next(!err && canRead); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, function(pids) { | 
					
						
							|  |  |  | 				getPostsFromSet('uid:' + uid + ':posts', pids, callback); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-05-20 17:46:13 -04:00
										 |  |  | 			getPostsFromSet('uid:' + uid + ':favourites', pids, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function getPostsFromSet(set, pids, callback) { | 
					
						
							|  |  |  | 		if (!Array.isArray(pids) || !pids.length) { | 
					
						
							|  |  |  | 			return callback(null, {posts: [], nextStart: 0}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Posts.getPostSummaryByPids(pids, false, function(err, posts) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!Array.isArray(posts) || !posts.length) { | 
					
						
							|  |  |  | 				return callback(null, {posts: [], nextStart: 0}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			db.sortedSetRevRank(set, posts[posts.length - 1].pid, function(err, rank) { | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-05-20 17:46:13 -04:00
										 |  |  | 				var data = { | 
					
						
							|  |  |  | 					posts: posts, | 
					
						
							|  |  |  | 					nextStart: parseInt(rank, 10) + 1 | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 				callback(null, data); | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-05-20 17:46:13 -04: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) { | 
					
						
							| 
									
										
										
										
											2014-04-09 22:26:23 -04:00
										 |  |  | 			return callback(new Error('[[error:invalid-pid]]')); | 
					
						
							| 
									
										
										
										
											2014-02-22 16:48:58 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											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-06-13 15:33:22 -04:00
										 |  |  | 				if (index === 1) { | 
					
						
							|  |  |  | 					return callback(null, 1); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-02-22 16:48:58 -05:00
										 |  |  | 				user.getSettings(uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(settings, next) { | 
					
						
							| 
									
										
										
										
											2014-06-13 15:33:22 -04:00
										 |  |  | 				next(null, Math.ceil((index - 1) / settings.postsPerPage)); | 
					
						
							| 
									
										
										
										
											2014-02-22 16:48:58 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		], 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); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 			db.sortedSetRank('tid:' + tid + ':posts', pid, function(err, index) { | 
					
						
							| 
									
										
										
										
											2014-06-13 15:33:22 -04:00
										 |  |  | 				if (!utils.isNumber(index)) { | 
					
						
							|  |  |  | 					return callback(err, 1); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				callback(err, parseInt(index, 10) + 2); | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-02-17 20:57:12 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-01-25 19:19:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 	Posts.isOwner = function(pid, uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-06-28 14:59:01 -04:00
										 |  |  | 		uid = parseInt(uid, 10); | 
					
						
							|  |  |  | 		if (Array.isArray(pid)) { | 
					
						
							|  |  |  | 			Posts.getPostsFields(pid, ['uid'], function(err, posts) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				posts = posts.map(function(post) { | 
					
						
							|  |  |  | 					return post && parseInt(post.uid, 10) === uid; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				callback(null, posts); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			Posts.getPostField(pid, 'uid', function(err, author) { | 
					
						
							|  |  |  | 				callback(err, parseInt(author, 10) === uid); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 18:06:59 -04:00
										 |  |  | 	Posts.isMain = function(pid, callback) { | 
					
						
							|  |  |  | 		Posts.getPostField(pid, 'tid', function(err, tid) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			topics.getTopicField(tid, 'mainPid', function(err, mainPid) { | 
					
						
							|  |  |  | 				callback(err, parseInt(pid, 10) === parseInt(mainPid, 10)); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 	Posts.updatePostVoteCount = function(pid, voteCount, callback) { | 
					
						
							|  |  |  | 		async.parallel([ | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				Posts.getPostField(pid, 'tid', function(err, tid) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return next(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					topics.getTopicField(tid, 'mainPid', function(err, mainPid) { | 
					
						
							|  |  |  | 						if (err) { | 
					
						
							|  |  |  | 							return next(err); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						if (parseInt(mainPid, 10) === parseInt(pid, 10)) { | 
					
						
							|  |  |  | 							return next(); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						db.sortedSetAdd('tid:' + tid + ':posts:votes', voteCount, pid, next); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				Posts.setPostField(pid, 'votes', voteCount, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  | }(exports)); |