| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | var db = require('./database'), | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 	utils = require('./../public/src/utils'), | 
					
						
							|  |  |  | 	user = require('./user'), | 
					
						
							|  |  |  | 	topics = require('./topics'), | 
					
						
							|  |  |  | 	categories = require('./categories'), | 
					
						
							|  |  |  | 	favourites = require('./favourites'), | 
					
						
							|  |  |  | 	threadTools = require('./threadTools'), | 
					
						
							| 
									
										
										
										
											2013-07-22 12:44:50 -04:00
										 |  |  | 	postTools = require('./postTools'), | 
					
						
							| 
									
										
										
										
											2013-10-14 16:41:34 -04:00
										 |  |  | 	categories = require('./categories'), | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 	feed = require('./feed'), | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 	plugins = require('./plugins'), | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 	meta = require('./meta'), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	async = require('async'), | 
					
						
							| 
									
										
										
										
											2013-08-12 19:00:31 -04:00
										 |  |  | 	nconf = require('nconf'), | 
					
						
							| 
									
										
										
										
											2013-11-22 08:57:54 -05:00
										 |  |  | 	validator = require('validator'), | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 	Posts.create = function(uid, tid, content, callback) { | 
					
						
							|  |  |  | 		if (uid === null) { | 
					
						
							| 
									
										
										
										
											2013-12-06 15:23:48 -05:00
										 |  |  | 			return callback(new Error('invalid-user'), null); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		topics.isLocked(tid, function(err, locked) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err, null); | 
					
						
							|  |  |  | 			} else if(locked) { | 
					
						
							| 
									
										
										
										
											2013-12-06 15:23:48 -05:00
										 |  |  | 				return callback(new Error('topic-locked'), null); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 			db.incrObjectField('global', 'nextPid', function(err, pid) { | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return callback(err, null); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				plugins.fireHook('filter:post.save', content, function(err, newContent) { | 
					
						
							|  |  |  | 					if(err) { | 
					
						
							|  |  |  | 						return callback(err, null); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					var timestamp = Date.now(), | 
					
						
							|  |  |  | 						postData = { | 
					
						
							|  |  |  | 							'pid': pid, | 
					
						
							|  |  |  | 							'uid': uid, | 
					
						
							|  |  |  | 							'tid': tid, | 
					
						
							| 
									
										
										
										
											2013-11-27 12:39:07 -05:00
										 |  |  | 							'content': newContent, | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 							'timestamp': timestamp, | 
					
						
							|  |  |  | 							'reputation': 0, | 
					
						
							|  |  |  | 							'editor': '', | 
					
						
							|  |  |  | 							'edited': 0, | 
					
						
							| 
									
										
										
										
											2013-11-28 15:18:19 -05:00
										 |  |  | 							'deleted': 0 | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 						}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 					db.setObject('post:' + pid, postData); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-28 15:18:19 -05:00
										 |  |  | 					postData.favourited = false; | 
					
						
							|  |  |  | 					postData.display_moderator_tools = true; | 
					
						
							|  |  |  | 					postData.relativeTime = new Date(timestamp).toISOString(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 					topics.addPostToTopic(tid, pid); | 
					
						
							|  |  |  | 					topics.increasePostCount(tid); | 
					
						
							|  |  |  | 					topics.updateTimestamp(tid, timestamp); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 					db.incrObjectField('global', 'postCount'); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						var cid = topicData.cid; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-28 15:33:26 -05:00
										 |  |  | 						feed.updateTopic(tid); | 
					
						
							|  |  |  | 						feed.updateRecent(); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 						db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 13:11:27 -05:00
										 |  |  | 						if(parseInt(topicData.pinned, 10) === 0) { | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 							db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid); | 
					
						
							| 
									
										
										
										
											2013-11-26 14:25:46 -05:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 						db.setCount('cid:' + cid + ':active_users', function(err, amount) { | 
					
						
							| 
									
										
										
										
											2013-11-28 18:55:10 -05:00
										 |  |  | 							if (amount > 15) { | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 								db.setRemoveRandom('cid:' + cid + ':active_users'); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							categories.addActiveUser(cid, uid); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					user.onNewPostMade(uid, tid, pid, timestamp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					plugins.fireHook('filter:post.get', postData, function(err, newPostData) { | 
					
						
							|  |  |  | 						if(err) { | 
					
						
							|  |  |  | 							return callback(err, null); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						postData = newPostData; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						postTools.parse(postData.content, function(err, content) { | 
					
						
							|  |  |  | 							if(err) { | 
					
						
							|  |  |  | 								return callback(err, null); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							postData.content = content; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							plugins.fireHook('action:post.save', postData); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 20:06:36 -05:00
										 |  |  | 							db.searchIndex('post', content, pid); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 							callback(null, postData); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Posts.reply = function(tid, uid, content, callback) { | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 		threadTools.privileges(tid, uid, function(err, privileges) { | 
					
						
							|  |  |  | 			if (content) { | 
					
						
							|  |  |  | 				content = content.trim(); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 			if (!content || content.length < meta.config.minimumPostLength) { | 
					
						
							|  |  |  | 				return callback(new Error('content-too-short')); | 
					
						
							|  |  |  | 			} else if (!privileges.write) { | 
					
						
							|  |  |  | 				return callback(new Error('no-privileges')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 			Posts.create(uid, tid, content, function(err, postData) { | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return callback(err, null); | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 				} else if(!postData) { | 
					
						
							|  |  |  | 					callback(new Error('reply-error'), null); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-07 18:45:26 -05:00
										 |  |  | 				Posts.getCidByPid(postData.pid, function(err, cid) { | 
					
						
							| 
									
										
										
										
											2013-12-07 18:58:27 -05:00
										 |  |  | 					if(err) { | 
					
						
							|  |  |  | 						return callback(err, null); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-07 18:45:26 -05:00
										 |  |  | 					db.delete('cid:' + cid + ':read_by_uid'); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-07 18:58:27 -05:00
										 |  |  | 				topics.markAsUnreadForAll(tid, function(err) { | 
					
						
							|  |  |  | 					if(err) { | 
					
						
							|  |  |  | 						return callback(err, null); | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-12-07 18:58:27 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					topics.markAsRead(tid, uid); | 
					
						
							|  |  |  | 					topics.pushUnreadCount(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				threadTools.notifyFollowers(tid, uid); | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 				Posts.addUserInfoToPost(postData, function(err) { | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 					if(err) { | 
					
						
							|  |  |  | 						return callback(err, null); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-12-07 18:58:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-27 11:27:20 -05:00
										 |  |  | 					callback(null, postData); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 	Posts.getPostsByTid = function(tid, start, end, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 		db.getListRange('tid:' + tid + ':posts', start, end, function(err, pids) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(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) { | 
					
						
							| 
									
										
										
										
											2013-11-29 13:12:50 -05:00
										 |  |  | 					if (!err && posts.length > 0) { | 
					
						
							| 
									
										
										
										
											2013-10-22 14:22:16 -04:00
										 |  |  | 						Posts.getPostsByPids(pids, function(err, posts) { | 
					
						
							|  |  |  | 							plugins.fireHook('action:post.gotTopic', posts); | 
					
						
							|  |  |  | 							callback(posts); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						callback(posts); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-10-28 13:34:36 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2013-12-06 21:08:21 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-09-27 11:47:16 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-01 20:07:30 -05:00
										 |  |  | 			postTools.parseSignature(userData.signature, function(err, signature) { | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 				post.username = userData.username || 'anonymous'; | 
					
						
							|  |  |  | 				post.userslug = userData.userslug || ''; | 
					
						
							|  |  |  | 				post.user_rep = userData.reputation || 0; | 
					
						
							|  |  |  | 				post.user_postcount = userData.postcount || 0; | 
					
						
							| 
									
										
										
										
											2013-12-05 13:11:27 -05:00
										 |  |  | 				post.user_banned = parseInt(userData.banned, 10) === 1; | 
					
						
							| 
									
										
										
										
											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]; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-03 14:19:11 -05:00
										 |  |  | 				plugins.fireHook('filter:posts.custom_profile_info', {profile: "", uid: post.uid, pid: post.pid}, function(err, profile_info) { | 
					
						
							| 
									
										
										
										
											2013-10-13 19:16:48 -04:00
										 |  |  | 					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-12-06 21:08:21 -05:00
										 |  |  | 	Posts.getPostSummaryByPids = function(pids, stripTags, 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) { | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 					Posts.getPostFields(pid, ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'], function(err, postData) { | 
					
						
							| 
									
										
										
										
											2013-12-05 13:11:27 -05:00
										 |  |  | 						if (parseInt(postData.deleted, 10) === 1) { | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 							return callback(null); | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											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-11-22 08:57:54 -05:00
										 |  |  | 					topics.getTopicFields(postData.tid, ['title', 'cid', 'slug', 'deleted'], function(err, topicData) { | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 						if (err) { | 
					
						
							|  |  |  | 							return callback(err); | 
					
						
							| 
									
										
										
										
											2013-12-05 13:11:27 -05:00
										 |  |  | 						} else if (parseInt(topicData.deleted, 10) === 1) { | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 							return callback(null); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						categories.getCategoryFields(topicData.cid, ['name', 'icon', 'slug'], function(err, categoryData) { | 
					
						
							|  |  |  | 							postData.categoryName = categoryData.name; | 
					
						
							|  |  |  | 							postData.categoryIcon = categoryData.icon; | 
					
						
							|  |  |  | 							postData.categorySlug = categoryData.slug; | 
					
						
							| 
									
										
										
										
											2013-11-22 08:57:54 -05:00
										 |  |  | 							postData.title = validator.sanitize(topicData.title).escape(); | 
					
						
							|  |  |  | 							postData.topicSlug = topicData.slug; | 
					
						
							|  |  |  | 							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-12-06 21:08:21 -05:00
										 |  |  | 							if(err) { | 
					
						
							|  |  |  | 								return next(err); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							if(stripTags) { | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 								postData.content = utils.strip_tags(content); | 
					
						
							| 
									
										
										
										
											2013-12-06 21:08:21 -05:00
										 |  |  | 							} else { | 
					
						
							|  |  |  | 								postData.content = content; | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2013-12-06 21:08:21 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 							next(null, postData); | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 					} else { | 
					
						
							|  |  |  | 						next(null, postData); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			], function(err, postData) { | 
					
						
							| 
									
										
										
										
											2013-12-02 13:28:46 -05:00
										 |  |  | 				if (!err) { | 
					
						
							|  |  |  | 					posts.push(postData); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-08-27 12:14:27 -04:00
										 |  |  | 				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-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) { | 
					
						
							|  |  |  | 				return callback(err, null); | 
					
						
							| 
									
										
										
										
											2013-11-03 11:53:44 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-11-15 13:39:09 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			plugins.fireHook('filter:post.get', data, function(err, newData) { | 
					
						
							|  |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return callback(err, null); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				callback(null, newData); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04: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) { | 
					
						
							|  |  |  | 				return callback(err, null); | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			plugins.fireHook('filter:post.getFields', data, function(err, data) { | 
					
						
							|  |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return callback(err, null); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				callback(null, data); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 		Posts.getPostFields(pid, [field], function(err, data) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err, null); | 
					
						
							| 
									
										
										
										
											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-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-08-09 20:03:19 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 	Posts.getPostsByPids = function(pids, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-04 16:58:06 -05:00
										 |  |  | 		var keys = []; | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 		for(var x=0, numPids=pids.length; x<numPids; x++) { | 
					
						
							| 
									
										
										
										
											2013-12-04 16:58:06 -05:00
										 |  |  | 			keys.push('post:' + pids[x]); | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 16:58:06 -05:00
										 |  |  | 		db.getObjects(keys, function(err, data) { | 
					
						
							|  |  |  | 			async.map(data, function(postData, _callback) { | 
					
						
							| 
									
										
										
										
											2013-09-28 17:44:18 -04:00
										 |  |  | 				if (postData) { | 
					
						
							| 
									
										
										
										
											2013-10-22 15:54:02 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					try { | 
					
						
							|  |  |  | 						postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString(); | 
					
						
							| 
									
										
										
										
											2013-12-05 21:29:51 -05:00
										 |  |  | 						postData.relativeEditTime = parseInt(postData.edited, 10) !== 0 ? (new Date(parseInt(postData.edited, 10)).toISOString()) : ''; | 
					
						
							| 
									
										
										
										
											2013-10-22 15:54:02 -04:00
										 |  |  | 					} catch(e) { | 
					
						
							|  |  |  | 						winston.err('invalid time value'); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 16:58:06 -05:00
										 |  |  | 					postTools.parse(postData.content, function(err, content) { | 
					
						
							|  |  |  | 						postData.content = content; | 
					
						
							| 
									
										
										
										
											2013-09-28 17:44:18 -04:00
										 |  |  | 						_callback(null, postData); | 
					
						
							| 
									
										
										
										
											2013-12-04 16:58:06 -05: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-12-04 16:58:06 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-09-21 22:47:27 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 	Posts.getCidByPid = function(pid, callback) { | 
					
						
							|  |  |  | 		Posts.getPostField(pid, 'tid', function(err, tid) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err, null); | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			topics.getTopicField(tid, 'cid', function(err, cid) { | 
					
						
							|  |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return callback(err, null); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (cid) { | 
					
						
							|  |  |  | 					callback(null, cid); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					callback(new Error('invalid-category-id'), null); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											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-11-11 12:40:15 -05:00
										 |  |  | 			message: 'You can only post every ' + meta.config.postDelay + ' 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.uploadPostImage = function(image, callback) { | 
					
						
							| 
									
										
										
										
											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-11-23 17:56:03 -05:00
										 |  |  | 		require('./imgur').upload(meta.config.imgurClientID, image.data, 'base64', function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:37:03 -04:00
										 |  |  | 			if(err) { | 
					
						
							| 
									
										
										
										
											2013-11-23 17:56:03 -05:00
										 |  |  | 				callback(err.message, null); | 
					
						
							| 
									
										
										
										
											2013-09-17 13:37:03 -04:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-11-23 17:56:03 -05:00
										 |  |  | 				callback(null, { | 
					
						
							|  |  |  | 					url: data.link, | 
					
						
							|  |  |  | 					name: image.name | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											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-12-05 21:29:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2013-12-05 21:29:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-22 14:22:16 -04:00
										 |  |  | 					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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 15:46:15 -04:00
										 |  |  | 	Posts.reIndexPids = function(pids, callback) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		function reIndex(pid, callback) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-15 14:57:50 -05:00
										 |  |  | 			Posts.getPostField(pid, 'content', function(err, content) { | 
					
						
							| 
									
										
										
										
											2013-12-05 20:06:36 -05:00
										 |  |  | 				db.searchRemove('post', pid, function() { | 
					
						
							| 
									
										
										
										
											2013-08-06 15:46:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 					if (content && content.length) { | 
					
						
							| 
									
										
										
										
											2013-12-05 20:06:36 -05:00
										 |  |  | 						db.searchIndex('post', 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) { | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 		db.getSortedSetRevRange('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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 21:08:21 -05:00
										 |  |  | 			Posts.getPostSummaryByPids(pids, false, 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)); |