| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | var RDB = require('./redis.js'), | 
					
						
							| 
									
										
										
										
											2013-05-23 13:21:38 -04:00
										 |  |  | 	utils = require('./../public/src/utils.js'), | 
					
						
							| 
									
										
										
										
											2013-06-25 14:38:17 -04:00
										 |  |  | 	schema = require('./schema.js'), | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	user = require('./user.js'), | 
					
						
							| 
									
										
										
										
											2013-05-09 07:27:55 +00:00
										 |  |  | 	topics = require('./topics.js'), | 
					
						
							| 
									
										
										
										
											2013-05-23 15:11:55 -04:00
										 |  |  | 	favourites = require('./favourites.js'), | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 	threadTools = require('./threadTools.js'), | 
					
						
							| 
									
										
										
										
											2013-07-22 12:44:50 -04:00
										 |  |  | 	postTools = require('./postTools'), | 
					
						
							| 
									
										
										
										
											2013-10-14 16:41:34 -04:00
										 |  |  | 	categories = require('./categories'), | 
					
						
							| 
									
										
										
										
											2013-05-27 14:55:03 -04:00
										 |  |  | 	feed = require('./feed.js'), | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 	async = require('async'), | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 	plugins = require('./plugins'), | 
					
						
							|  |  |  | 	reds = require('reds'), | 
					
						
							| 
									
										
										
										
											2013-08-26 16:37:22 -04:00
										 |  |  | 	postSearch = reds.createSearch('nodebbpostsearch'), | 
					
						
							| 
									
										
										
										
											2013-08-12 19:00:31 -04:00
										 |  |  | 	nconf = require('nconf'), | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 	meta = require('./meta.js'), | 
					
						
							| 
									
										
										
										
											2013-08-13 16:00:24 -04:00
										 |  |  | 	winston = require('winston'); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | 	Posts.getPostsByTid = function(tid, start, end, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 		RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) { | 
					
						
							|  |  |  | 			RDB.handle(err); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-26 13:38:05 -04:00
										 |  |  | 			if (pids.length) { | 
					
						
							| 
									
										
										
										
											2013-10-22 14:22:16 -04:00
										 |  |  | 				plugins.fireHook('filter:post.getTopic', pids, function(err, posts) { | 
					
						
							|  |  |  | 					if (!err & 0 < posts.length) { | 
					
						
							|  |  |  | 						Posts.getPostsByPids(pids, function(err, posts) { | 
					
						
							|  |  |  | 							plugins.fireHook('action:post.gotTopic', posts); | 
					
						
							|  |  |  | 							callback(posts); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						callback(posts); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				});			 | 
					
						
							| 
									
										
										
										
											2013-06-26 13:38:05 -04:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-08-01 16:11:00 -04:00
										 |  |  | 				callback([]); | 
					
						
							| 
									
										
										
										
											2013-06-26 13:38:05 -04: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 				return callback(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 13:54:21 -04:00
										 |  |  | 			postTools.parse(userData.signature, function(err, signature) { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 				post.user_banned = userData.banned || '0'; | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 				post.picture = userData.picture || require('gravatar').url('', {}, https = nconf.get('https')); | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 				post.signature = signature; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-13 19:16:48 -04:00
										 |  |  | 				for (var info in customUserInfo) { | 
					
						
							|  |  |  | 					if (customUserInfo.hasOwnProperty(info)) { | 
					
						
							|  |  |  | 						post[info] = userData[info] || customUserInfo[info]; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				plugins.fireHook('filter:posts.custom_profile_info', {profile: "", uid: post.uid}, function(err, profile_info) { | 
					
						
							|  |  |  | 					post.additional_profile_info = profile_info.profile; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (post.editor !== '') { | 
					
						
							|  |  |  | 						user.getUserFields(post.editor, ['username', 'userslug'], function(err, editorData) { | 
					
						
							|  |  |  | 							if (err) return callback(); | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-13 19:16:48 -04:00
										 |  |  | 							post.editorname = editorData.username; | 
					
						
							|  |  |  | 							post.editorslug = editorData.userslug; | 
					
						
							|  |  |  | 							callback(); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 						callback(); | 
					
						
							| 
									
										
										
										
											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-07-02 19:46:58 -04:00
										 |  |  | 	Posts.getPostSummaryByPids = function(pids, callback) { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 13:32:07 -04:00
										 |  |  | 		var posts = []; | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-15 14:34:15 -04:00
										 |  |  | 		function getPostSummary(pid, callback) { | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							|  |  |  | 					Posts.getPostFields(pid, ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'], function(postData) { | 
					
						
							|  |  |  | 						if (postData.deleted === '1') return callback(null); | 
					
						
							|  |  |  | 						else { | 
					
						
							| 
									
										
										
										
											2013-09-23 15:31:52 -04:00
										 |  |  | 							postData.relativeTime = new Date(parseInt(postData.timestamp || 0, 10)).toISOString(); | 
					
						
							| 
									
										
										
										
											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-08-24 18:19:01 -04:00
										 |  |  | 					topics.getTopicFields(postData.tid, ['slug', 'deleted'], function(err, topicData) { | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 						if (err) return callback(err); | 
					
						
							|  |  |  | 						else if (topicData.deleted === '1') return callback(null); | 
					
						
							| 
									
										
										
										
											2013-08-08 14:12:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-24 18:19:01 -04:00
										 |  |  | 						postData.topicSlug = topicData.slug; | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 						next(null, postData); | 
					
						
							| 
									
										
										
										
											2013-08-01 17:27:37 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function(postData, next) { | 
					
						
							|  |  |  | 					if (postData.content) { | 
					
						
							| 
									
										
										
										
											2013-09-22 13:54:21 -04:00
										 |  |  | 						postTools.parse(postData.content, function(err, content) { | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 							if (!err) postData.content = utils.strip_tags(content); | 
					
						
							|  |  |  | 							next(err, postData); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} else next(null, postData); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			], function(err, postData) { | 
					
						
							|  |  |  | 				if (!err) posts.push(postData); | 
					
						
							|  |  |  | 				callback(err); | 
					
						
							| 
									
										
										
										
											2013-07-15 14:34:15 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-15 14:34:15 -04:00
										 |  |  | 		async.eachSeries(pids, getPostSummary, function(err) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (!err) { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 				callback(null, posts); | 
					
						
							| 
									
										
										
										
											2013-08-08 14:30:42 -04:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 				callback(err, null); | 
					
						
							| 
									
										
										
										
											2013-07-15 14:34:15 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 13:32:07 -04:00
										 |  |  | 	Posts.filterBannedPosts = function(posts) { | 
					
						
							|  |  |  | 		return posts.filter(function(post) { | 
					
						
							|  |  |  | 			return post.user_banned === '0'; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-22 14:22:16 -04:00
										 |  |  | 	// TODO: this function is never called except from some debug route. clean up?
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 	Posts.getPostData = function(pid, callback) { | 
					
						
							|  |  |  | 		RDB.hgetall('post:' + pid, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (err === null) { | 
					
						
							| 
									
										
										
										
											2013-09-29 20:33:35 -04:00
										 |  |  | 				plugins.fireHook('filter:post.get', data, function(err, newData) { | 
					
						
							|  |  |  | 					if (!err) callback(newData); | 
					
						
							|  |  |  | 					else callback(data); | 
					
						
							| 
									
										
										
										
											2013-08-21 22:40:47 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			} else | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 				console.log(err); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-22 17:08:07 -04:00
										 |  |  | 	Posts.getPostFields = function(pid, fields, callback) { | 
					
						
							|  |  |  | 		RDB.hmgetObject('post:' + pid, fields, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (err === null) { | 
					
						
							| 
									
										
										
										
											2013-10-22 14:22:16 -04: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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				plugins.fireHook('filter:post.getFields', data, function(err, data) { | 
					
						
							|  |  |  | 					callback(data); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-07-22 17:08:07 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-09 20:03:19 -04:00
										 |  |  | 	Posts.getPostField = function(pid, field, callback) { | 
					
						
							|  |  |  | 		RDB.hget('post:' + pid, field, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-10-22 14:22:16 -04:00
										 |  |  | 			if (err === null) { | 
					
						
							|  |  |  | 				// 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.field = field; | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 				plugins.fireHook('filter:post.getField', data, function(err, data) { | 
					
						
							|  |  |  | 					callback(data); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-08-09 20:03:19 -04:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-10-22 14:22:16 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-08-09 20:03:19 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-22 14:22:16 -04:00
										 |  |  | 	Posts.setPostField = function(pid, field, value, done) { | 
					
						
							| 
									
										
										
										
											2013-08-09 20:03:19 -04:00
										 |  |  | 		RDB.hset('post:' + pid, field, value); | 
					
						
							| 
									
										
										
										
											2013-10-22 14:22:16 -04:00
										 |  |  | 		plugins.fireHook('action:post.setField', { | 
					
						
							|  |  |  | 			'pid': pid, | 
					
						
							|  |  |  | 			'field': field, | 
					
						
							|  |  |  | 			'value': value | 
					
						
							|  |  |  | 		}, done); | 
					
						
							| 
									
										
										
										
											2013-08-09 20:03:19 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 	Posts.getPostsByPids = function(pids, callback) { | 
					
						
							| 
									
										
										
										
											2013-09-28 17:44:18 -04:00
										 |  |  | 		var posts = [], | 
					
						
							|  |  |  | 			multi = RDB.multi(); | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-28 17:44:18 -04:00
										 |  |  | 		for(var x=0,numPids=pids.length;x<numPids;x++) { | 
					
						
							|  |  |  | 			multi.hgetall("post:"+pids[x]); | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		multi.exec(function (err, replies) { | 
					
						
							| 
									
										
										
										
											2013-09-28 17:44:18 -04:00
										 |  |  | 			async.map(replies, function(postData, _callback) { | 
					
						
							|  |  |  | 				if (postData) { | 
					
						
							| 
									
										
										
										
											2013-10-22 15:54:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  | 					postData.post_rep = postData.reputation; | 
					
						
							|  |  |  | 					postData['edited-class'] = postData.editor !== '' ? '' : 'none'; | 
					
						
							| 
									
										
										
										
											2013-10-22 15:54:02 -04:00
										 |  |  | 					try { | 
					
						
							|  |  |  | 						postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString(); | 
					
						
							|  |  |  | 						postData['relativeEditTime'] = postData.edited !== '0' ? (new Date(parseInt(postData.edited,10)).toISOString()) : ''; | 
					
						
							|  |  |  | 					} catch(e) { | 
					
						
							|  |  |  | 						winston.err('invalid time value'); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-28 17:44:18 -04:00
										 |  |  | 					if (postData.uploadedImages) { | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  | 						try { | 
					
						
							|  |  |  | 							postData.uploadedImages = JSON.parse(postData.uploadedImages); | 
					
						
							|  |  |  | 						} catch(err) { | 
					
						
							|  |  |  | 							postData.uploadedImages = []; | 
					
						
							|  |  |  | 							winston.err(err); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						postData.uploadedImages = []; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-27 11:47:16 -04:00
										 |  |  |                     postTools.parse(postData.content, function(err, content) { | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  |                         postData.content = content; | 
					
						
							| 
									
										
										
										
											2013-09-28 17:44:18 -04:00
										 |  |  | 						_callback(null, postData); | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  |                     }); | 
					
						
							| 
									
										
										
										
											2013-09-28 17:44:18 -04:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					_callback(null); | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-09-28 17:44:18 -04:00
										 |  |  | 			}, function(err, posts) { | 
					
						
							|  |  |  | 				if (!err) { | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  | 					return callback(null, posts); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					return callback(err, null); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 18:15:58 -04:00
										 |  |  | 	Posts.get_cid_by_pid = function(pid, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		Posts.getPostField(pid, 'tid', function(tid) { | 
					
						
							|  |  |  | 			if (tid) { | 
					
						
							| 
									
										
										
										
											2013-08-11 16:12:20 -04:00
										 |  |  | 				topics.getTopicField(tid, 'cid', function(err, cid) { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 					if (cid) { | 
					
						
							|  |  |  | 						callback(cid); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						callback(false); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-16 18:15:58 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-18 13:50:42 -04:00
										 |  |  | 	Posts.emitContentTooShortAlert = function(socket) { | 
					
						
							|  |  |  | 		socket.emit('event:alert', { | 
					
						
							| 
									
										
										
										
											2013-08-24 03:36:44 +08:00
										 |  |  | 			type: 'danger', | 
					
						
							| 
									
										
										
										
											2013-07-18 13:50:42 -04:00
										 |  |  | 			timeout: 2000, | 
					
						
							|  |  |  | 			title: 'Content too short', | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 			message: "Please enter a longer post. At least " + meta.config.minimumPostLength + " characters.", | 
					
						
							| 
									
										
										
										
											2013-07-18 13:50:42 -04:00
										 |  |  | 			alert_id: 'post_error' | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-10 16:14:50 -04:00
										 |  |  | 	Posts.emitTooManyPostsAlert = function(socket) { | 
					
						
							|  |  |  | 		socket.emit('event:alert', { | 
					
						
							|  |  |  | 			title: 'Too many posts!', | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			message: 'You can only post every ' + meta.config.postDelay / 1000 + ' seconds.', | 
					
						
							| 
									
										
										
										
											2013-08-24 03:36:44 +08:00
										 |  |  | 			type: 'danger', | 
					
						
							| 
									
										
										
										
											2013-08-10 16:14:50 -04:00
										 |  |  | 			timeout: 2000 | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-18 13:50:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:37:03 -04:00
										 |  |  | 	Posts.reply = function(tid, uid, content, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-23 12:35:45 -04:00
										 |  |  | 		if(content) { | 
					
						
							|  |  |  | 			content = content.trim(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-08-12 19:00:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 		if (!content || content.length < meta.config.minimumPostLength) { | 
					
						
							| 
									
										
										
										
											2013-08-10 16:14:50 -04:00
										 |  |  | 			callback(new Error('content-too-short'), null); | 
					
						
							| 
									
										
										
										
											2013-07-17 11:22:09 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2013-05-14 17:19:22 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-24 12:53:45 -04:00
										 |  |  | 		user.getUserField(uid, 'lastposttime', function(lastposttime) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (Date.now() - lastposttime < meta.config.postDelay) { | 
					
						
							| 
									
										
										
										
											2013-08-10 16:14:50 -04:00
										 |  |  | 				callback(new Error('too-many-posts'), null); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:32:28 -04:00
										 |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:37:03 -04:00
										 |  |  | 			Posts.create(uid, tid, content, function(postData) { | 
					
						
							| 
									
										
										
										
											2013-07-21 12:29:57 -04:00
										 |  |  | 				if (postData) { | 
					
						
							| 
									
										
										
										
											2013-08-19 11:56:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-22 13:06:31 -04:00
										 |  |  | 					topics.markUnRead(tid); | 
					
						
							| 
									
										
										
										
											2013-05-24 14:34:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-21 12:29:57 -04:00
										 |  |  | 					Posts.get_cid_by_pid(postData.pid, function(cid) { | 
					
						
							| 
									
										
										
										
											2013-06-28 11:36:41 -04:00
										 |  |  | 						RDB.del('cid:' + cid + ':read_by_uid', function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 							topics.markAsRead(tid, uid); | 
					
						
							| 
									
										
										
										
											2013-06-28 11:36:41 -04:00
										 |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:32:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 					threadTools.notify_followers(tid, uid); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:32:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-22 14:40:28 -04:00
										 |  |  | 					Posts.addUserInfoToPost(postData, function() { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						var socketData = { | 
					
						
							|  |  |  | 							posts: [postData] | 
					
						
							|  |  |  | 						}; | 
					
						
							| 
									
										
										
										
											2013-10-01 11:54:00 -04:00
										 |  |  | 						io.sockets.in('topic_' + tid).emit('event:new_post', socketData); | 
					
						
							|  |  |  | 						io.sockets.in('recent_posts').emit('event:new_post', socketData); | 
					
						
							|  |  |  | 						io.sockets.in('user/' + uid).emit('event:new_post', socketData); | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-10 16:14:50 -04:00
										 |  |  | 					callback(null, 'Reply successful'); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:32:28 -04:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-08-10 16:14:50 -04:00
										 |  |  | 					callback(new Error('reply-error'), null); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:32:28 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:37:03 -04:00
										 |  |  | 	Posts.create = function(uid, tid, content, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		if (uid === null) { | 
					
						
							| 
									
										
										
										
											2013-07-21 12:29:57 -04:00
										 |  |  | 			callback(null); | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		topics.isLocked(tid, function(locked) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			if (!locked || locked === '0') { | 
					
						
							|  |  |  | 				RDB.incr('global:next_post_id', function(err, pid) { | 
					
						
							|  |  |  | 					RDB.handle(err); | 
					
						
							| 
									
										
										
										
											2013-05-15 15:42:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-29 20:33:35 -04:00
										 |  |  | 					plugins.fireHook('filter:post.save', content, function(err, newContent) { | 
					
						
							|  |  |  | 						if (!err) content = newContent; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 						var timestamp = Date.now(), | 
					
						
							|  |  |  | 							postData = { | 
					
						
							|  |  |  | 								'pid': pid, | 
					
						
							|  |  |  | 								'uid': uid, | 
					
						
							|  |  |  | 								'tid': tid, | 
					
						
							|  |  |  | 								'content': content, | 
					
						
							|  |  |  | 								'timestamp': timestamp, | 
					
						
							|  |  |  | 								'reputation': 0, | 
					
						
							|  |  |  | 								'editor': '', | 
					
						
							|  |  |  | 								'edited': 0, | 
					
						
							|  |  |  | 								'deleted': 0, | 
					
						
							| 
									
										
										
										
											2013-08-22 14:40:28 -04:00
										 |  |  | 								'fav_button_class': '', | 
					
						
							|  |  |  | 								'fav_star_class': 'icon-star-empty', | 
					
						
							|  |  |  | 								'show_banned': 'hide', | 
					
						
							| 
									
										
										
										
											2013-09-19 15:02:35 -04:00
										 |  |  | 								'relativeTime': new Date(timestamp).toISOString(), | 
					
						
							| 
									
										
										
										
											2013-08-22 14:40:28 -04:00
										 |  |  | 								'post_rep': '0', | 
					
						
							|  |  |  | 								'edited-class': 'none', | 
					
						
							|  |  |  | 								'relativeEditTime': '' | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 							}; | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 						RDB.hmset('post:' + pid, postData); | 
					
						
							| 
									
										
										
										
											2013-05-27 14:55:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-19 11:56:40 -04:00
										 |  |  | 						topics.addPostToTopic(tid, pid); | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 						topics.increasePostCount(tid); | 
					
						
							|  |  |  | 						topics.updateTimestamp(tid, timestamp); | 
					
						
							| 
									
										
										
										
											2013-05-27 14:55:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 						RDB.incr('totalpostcount'); | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-22 21:47:09 -04:00
										 |  |  | 						topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 							RDB.handle(err); | 
					
						
							| 
									
										
										
										
											2013-07-06 22:02:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-22 21:47:09 -04:00
										 |  |  | 							var cid = topicData.cid; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-03 23:42:26 -04:00
										 |  |  | 							feed.updateTopic(tid); | 
					
						
							| 
									
										
										
										
											2013-05-15 15:42:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-09 20:03:19 -04:00
										 |  |  | 							RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid); | 
					
						
							| 
									
										
										
										
											2013-10-22 21:47:09 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 							if(topicData.pinned === '0') | 
					
						
							|  |  |  | 								RDB.zadd('categories:' + cid + ':tid', timestamp, tid); | 
					
						
							| 
									
										
										
										
											2013-06-18 10:01:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-09 16:27:16 -04:00
										 |  |  | 							RDB.scard('cid:' + cid + ':active_users', function(err, amount) { | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 								if (amount > 10) { | 
					
						
							|  |  |  | 									RDB.spop('cid:' + cid + ':active_users'); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-27 13:32:43 -04:00
										 |  |  | 								categories.addActiveUser(cid, uid); | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						user.onNewPostMade(uid, tid, pid, timestamp); | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-22 14:40:28 -04:00
										 |  |  | 						async.parallel({ | 
					
						
							|  |  |  | 							content: function(next) { | 
					
						
							| 
									
										
										
										
											2013-09-29 20:33:35 -04:00
										 |  |  | 								plugins.fireHook('filter:post.get', postData, function(err, newPostData) { | 
					
						
							|  |  |  | 									if (!err) postData = newPostData; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 13:54:21 -04:00
										 |  |  | 									postTools.parse(postData.content, function(err, content) { | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 										next(null, content); | 
					
						
							|  |  |  | 									}); | 
					
						
							| 
									
										
										
										
											2013-08-22 14:40:28 -04:00
										 |  |  | 								}); | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2013-08-22 14:40:28 -04:00
										 |  |  | 						}, function(err, results) { | 
					
						
							|  |  |  | 							postData.content = results.content; | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 							callback(postData); | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-14 21:47:45 -04:00
										 |  |  | 						plugins.fireHook('action:post.save', postData); | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 11:40:31 -04:00
										 |  |  | 						postSearch.index(content, pid); | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-07-21 12:29:57 -04:00
										 |  |  | 				callback(null); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:37:03 -04:00
										 |  |  | 	Posts.uploadPostImage = function(image, callback) { | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 		var imgur = require('./imgur'); | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 		imgur.setClientID(meta.config.imgurClientID); | 
					
						
							| 
									
										
										
										
											2013-08-15 16:50:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:37:03 -04:00
										 |  |  | 		if(!image) | 
					
						
							|  |  |  | 			return callback('invalid image', null); | 
					
						
							| 
									
										
										
										
											2013-08-30 11:40:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:37:03 -04:00
										 |  |  | 		imgur.upload(image.data, 'base64', function(err, data) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				callback('Can\'t upload image!', null); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				if(data.success) { | 
					
						
							|  |  |  | 					var img= {url:data.data.link, name:image.name}; | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:37:03 -04:00
										 |  |  | 					callback(null, img); | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:37:03 -04:00
										 |  |  | 					winston.error('Can\'t upload image, did you set imgurClientID?'); | 
					
						
							|  |  |  | 					callback("upload error", null); | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-08-30 11:40:20 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 	Posts.getPostsByUid = function(uid, start, end, callback) { | 
					
						
							|  |  |  | 		user.getPostIds(uid, start, end, function(pids) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (pids && pids.length) { | 
					
						
							| 
									
										
										
										
											2013-10-22 14:22:16 -04:00
										 |  |  | 				plugins.fireHook('filter:post.getTopic', pids, function(err, posts) { | 
					
						
							|  |  |  | 					if (!err & 0 < posts.length) { | 
					
						
							|  |  |  | 						Posts.getPostsByPids(pids, function(err, posts) { | 
					
						
							|  |  |  | 							plugins.fireHook('action:post.gotTopic', posts); | 
					
						
							|  |  |  | 							callback(posts); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						callback(posts); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			} else | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 				callback([]); | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-06-26 23:04:49 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-23 15:11:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-28 10:56:38 -04:00
										 |  |  | 	Posts.getTopicPostStats = function(socket) { | 
					
						
							|  |  |  | 		RDB.mget(['totaltopiccount', 'totalpostcount'], function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (err === null) { | 
					
						
							| 
									
										
										
										
											2013-06-28 10:56:38 -04:00
										 |  |  | 				var stats = { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 					topics: data[0] ? data[0] : 0, | 
					
						
							|  |  |  | 					posts: data[1] ? data[1] : 0 | 
					
						
							| 
									
										
										
										
											2013-06-28 10:56:38 -04:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2013-08-06 15:46:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-28 10:56:38 -04:00
										 |  |  | 				socket.emit('post.stats', stats); | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			} else | 
					
						
							| 
									
										
										
										
											2013-06-28 10:56:38 -04:00
										 |  |  | 				console.log(err); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 15:46:15 -04:00
										 |  |  | 	Posts.reIndexPids = function(pids, callback) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		function reIndex(pid, callback) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Posts.getPostField(pid, 'content', function(content) { | 
					
						
							| 
									
										
										
										
											2013-08-08 11:40:31 -04:00
										 |  |  | 				postSearch.remove(pid, function() { | 
					
						
							| 
									
										
										
										
											2013-08-06 15:46:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 					if (content && content.length) { | 
					
						
							| 
									
										
										
										
											2013-08-08 11:40:31 -04:00
										 |  |  | 						postSearch.index(content, pid); | 
					
						
							| 
									
										
										
										
											2013-08-06 15:46:15 -04:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					callback(null); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.each(pids, reIndex, function(err) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2013-08-06 15:46:15 -04:00
										 |  |  | 				callback(err, null); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				callback(null, 'Posts reindexed'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 	Posts.getFavourites = function(uid, callback) { | 
					
						
							|  |  |  | 		RDB.zrevrange('uid:' + uid + ':favourites', 0, -1, function(err, pids) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (err) | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 				return callback(err, null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Posts.getPostSummaryByPids(pids, function(err, posts) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 				if (err) | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 					return callback(err, null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				callback(null, posts); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  | }(exports)); |