| 
									
										
										
										
											2013-05-09 01:04:15 -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-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-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) { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 			RDB.handle(err); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-26 13:38:05 -04:00
										 |  |  | 			if (pids.length) { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 				Posts.getPostsByPids(pids, function(err, posts) { | 
					
						
							| 
									
										
										
										
											2013-06-26 13:38:05 -04:00
										 |  |  | 					callback(posts); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} 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-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) { | 
					
						
							|  |  |  | 			if(err) | 
					
						
							|  |  |  | 				return callback(); | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 			post.username = userData.username || 'anonymous'; | 
					
						
							|  |  |  | 			post.userslug = userData.userslug || ''; | 
					
						
							|  |  |  | 			post.user_rep = userData.reputation || 0; | 
					
						
							| 
									
										
										
										
											2013-07-17 10:05:29 -04:00
										 |  |  | 			post.user_postcount = userData.postcount || 0; | 
					
						
							| 
									
										
										
										
											2013-08-14 13:32:07 -04:00
										 |  |  | 			post.user_banned = userData.banned || '0'; | 
					
						
							| 
									
										
										
										
											2013-08-19 13:31:04 -04:00
										 |  |  | 			post.picture = userData.picture || require('gravatar').url('', {}, https=nconf.get('https')); | 
					
						
							| 
									
										
										
										
											2013-07-23 03:34:45 +08:00
										 |  |  | 			post.signature = postTools.markdownToHTML(userData.signature, true); | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-13 12:27:37 -04:00
										 |  |  | 			if(post.editor !== '') { | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 				user.getUserFields(post.editor, ['username', 'userslug'], function(err, editorData) { | 
					
						
							|  |  |  | 					if(err) | 
					
						
							|  |  |  | 						return callback(); | 
					
						
							| 
									
										
										
										
											2013-07-13 12:27:37 -04:00
										 |  |  | 					post.editorname = editorData.username; | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 					post.editorslug = editorData.userslug; | 
					
						
							| 
									
										
										
										
											2013-07-13 12:27:37 -04:00
										 |  |  | 					callback(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				callback(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -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-07-19 10:36:42 -04:00
										 |  |  | 			Posts.getPostFields(pid, ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'], function(postData) { | 
					
						
							| 
									
										
										
										
											2013-08-01 17:27:37 -04:00
										 |  |  | 				if(postData.deleted === '1') { | 
					
						
							| 
									
										
										
										
											2013-08-08 14:30:42 -04:00
										 |  |  | 					return callback(null); | 
					
						
							| 
									
										
										
										
											2013-08-01 17:27:37 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-07-15 14:34:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-01 17:27:37 -04:00
										 |  |  | 				Posts.addUserInfoToPost(postData, function() { | 
					
						
							| 
									
										
										
										
											2013-08-24 18:19:01 -04:00
										 |  |  | 					topics.getTopicFields(postData.tid, ['slug', 'deleted'], function(err, topicData) { | 
					
						
							|  |  |  | 						if(err) | 
					
						
							|  |  |  | 							return callback(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						if(topicData.deleted === '1') | 
					
						
							|  |  |  | 							return callback(null); | 
					
						
							| 
									
										
										
										
											2013-08-08 14:12:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						if(postData.content) | 
					
						
							|  |  |  | 							postData.content = utils.strip_tags(postTools.markdownToHTML(postData.content)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 						postData.relativeTime = utils.relativeTime(postData.timestamp); | 
					
						
							| 
									
										
										
										
											2013-08-24 18:19:01 -04:00
										 |  |  | 						postData.topicSlug = topicData.slug; | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 						posts.push(postData); | 
					
						
							| 
									
										
										
										
											2013-08-01 17:27:37 -04:00
										 |  |  | 						callback(null); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-05-24 14:34:55 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 			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-07-02 16:24:13 -04:00
										 |  |  | 	Posts.getPostData = function(pid, callback) { | 
					
						
							|  |  |  | 		RDB.hgetall('post:' + pid, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-07-19 10:59:24 -04:00
										 |  |  | 			if(err === null) { | 
					
						
							| 
									
										
										
										
											2013-08-23 15:22:26 -04:00
										 |  |  | 				plugins.fireHook('filter:post.get', data, function(data) { | 
					
						
							| 
									
										
										
										
											2013-08-21 22:40:47 -04:00
										 |  |  | 					callback(data); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-07-19 10:59:24 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 			else | 
					
						
							|  |  |  | 				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-07-02 19:46:58 -04:00
										 |  |  | 			if(err === null) { | 
					
						
							| 
									
										
										
										
											2013-07-22 17:08:07 -04:00
										 |  |  | 				callback(data); | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-22 17:08:07 -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) { | 
					
						
							|  |  |  | 			if(err === null) | 
					
						
							|  |  |  | 				callback(data); | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Posts.setPostField = function(pid, field, value) { | 
					
						
							|  |  |  | 		RDB.hset('post:' + pid, field, value); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 	Posts.getPostsByPids = function(pids, callback) { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 		var posts = []; | 
					
						
							| 
									
										
										
										
											2013-07-19 15:26:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		function iterator(pid, callback) { | 
					
						
							|  |  |  | 			Posts.getPostData(pid, function(postData) { | 
					
						
							|  |  |  | 				if(postData) { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 					postData.relativeTime = utils.relativeTime(postData.timestamp); | 
					
						
							| 
									
										
										
										
											2013-07-19 15:26:29 -04:00
										 |  |  | 					postData.post_rep = postData.reputation; | 
					
						
							|  |  |  | 					postData['edited-class'] = postData.editor !== '' ? '' : 'none'; | 
					
						
							|  |  |  | 					postData['relativeEditTime'] = postData.edited !== '0' ? utils.relativeTime(postData.edited) : ''; | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-22 12:44:50 -04:00
										 |  |  | 					postData.content = postTools.markdownToHTML(postData.content); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-21 12:29:57 -04:00
										 |  |  | 					if(postData.uploadedImages) { | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 						try { | 
					
						
							|  |  |  | 							postData.uploadedImages = JSON.parse(postData.uploadedImages); | 
					
						
							|  |  |  | 						} catch(err) { | 
					
						
							|  |  |  | 							postData.uploadedImages = []; | 
					
						
							|  |  |  | 							winston.err(err); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-07-21 12:29:57 -04:00
										 |  |  | 					} else { | 
					
						
							|  |  |  | 						postData.uploadedImages = []; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-07-19 15:26:29 -04:00
										 |  |  | 					posts.push(postData); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				callback(null); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-19 15:26:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		async.eachSeries(pids, iterator, function(err) { | 
					
						
							|  |  |  | 			if(!err) { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 				callback(null, posts); | 
					
						
							| 
									
										
										
										
											2013-07-19 15:26:29 -04:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 				callback(err, null); | 
					
						
							| 
									
										
										
										
											2013-07-19 15:26:29 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											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', { | 
					
						
							|  |  |  | 			type: 'error', | 
					
						
							|  |  |  | 			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-08-23 13:14:36 -04:00
										 |  |  | 			message: 'You can only post every '+ meta.config.postDelay/1000 + ' seconds.', | 
					
						
							| 
									
										
										
										
											2013-08-10 16:14:50 -04:00
										 |  |  | 			type: 'error', | 
					
						
							|  |  |  | 			timeout: 2000 | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-18 13:50:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-10 16:14:50 -04:00
										 |  |  | 	Posts.reply = function(tid, uid, content, images, 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-08-23 13:14:36 -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-07-21 12:29:57 -04:00
										 |  |  | 			Posts.create(uid, tid, content, images, function(postData) { | 
					
						
							|  |  |  | 				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() { | 
					
						
							|  |  |  | 						var	socketData = { posts: [postData] }; | 
					
						
							| 
									
										
										
										
											2013-06-27 21:11:04 -04:00
										 |  |  | 						io.sockets.in('topic_' + tid).emit('event:new_post', socketData); | 
					
						
							|  |  |  | 						io.sockets.in('recent_posts').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-07-21 12:29:57 -04:00
										 |  |  | 	Posts.create = function(uid, tid, content, images, 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-08-21 22:40:47 -04:00
										 |  |  | 					plugins.fireHook('filter:post.save', content, function(content) { | 
					
						
							| 
									
										
										
										
											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-23 13:14:36 -04:00
										 |  |  | 								'uploadedImages': '[]', | 
					
						
							| 
									
										
										
										
											2013-08-22 14:40:28 -04:00
										 |  |  | 								'fav_button_class': '', | 
					
						
							|  |  |  | 								'fav_star_class': 'icon-star-empty', | 
					
						
							|  |  |  | 								'show_banned': 'hide', | 
					
						
							|  |  |  | 								'relativeTime': '0 seconds', | 
					
						
							|  |  |  | 								'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-08-11 16:12:20 -04:00
										 |  |  | 						topics.getTopicField(tid, 'cid', function(err, cid) { | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 							RDB.handle(err); | 
					
						
							| 
									
										
										
										
											2013-07-06 22:02:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 							feed.updateTopic(tid, cid); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 							RDB.zadd('categories:' + cid + ':tid', timestamp, tid); | 
					
						
							| 
									
										
										
										
											2013-06-18 10:01:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-28 01:37:40 -04:00
										 |  |  | 							// this is a bit of a naive implementation, defn something to look at post-MVP
 | 
					
						
							|  |  |  | 							RDB.scard('cid:' + cid + ':active_users', function(amount) { | 
					
						
							|  |  |  | 								if (amount > 10) { | 
					
						
							|  |  |  | 									RDB.spop('cid:' + cid + ':active_users'); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								RDB.sadd('cid:' + cid + ':active_users', uid); | 
					
						
							|  |  |  | 							}); | 
					
						
							| 
									
										
										
										
											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({ | 
					
						
							|  |  |  | 							uploadedImages: function(next) { | 
					
						
							|  |  |  | 								uploadPostImages(postData, images, function(err, uploadedImages) { | 
					
						
							|  |  |  | 									if(err) { | 
					
						
							|  |  |  | 										winston.error('Uploading images failed!', err.stack); | 
					
						
							|  |  |  | 										next(null, []); | 
					
						
							|  |  |  | 									} else { | 
					
						
							|  |  |  | 										next(null, uploadedImages); | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 								}); | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							content: function(next) { | 
					
						
							| 
									
										
										
										
											2013-08-23 15:22:26 -04:00
										 |  |  | 								plugins.fireHook('filter:post.get', postData, function(postData) { | 
					
						
							| 
									
										
										
										
											2013-08-26 11:56:28 -04:00
										 |  |  | 									postData.content = postTools.markdownToHTML(postData.content, false); | 
					
						
							| 
									
										
										
										
											2013-08-23 15:22:26 -04:00
										 |  |  | 									next(null, postData.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.uploadedImages = results.uploadedImages; | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 							Posts.setPostField(pid, 'uploadedImages', JSON.stringify(postData.uploadedImages)); | 
					
						
							| 
									
										
										
										
											2013-08-22 14:40:28 -04:00
										 |  |  | 							postData.content = results.content; | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 							callback(postData); | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-23 15:22:26 -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
										 |  |  | 
 | 
					
						
							|  |  |  | 	function uploadPostImages(postData, images, callback) { | 
					
						
							|  |  |  | 		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-08-20 12:11:17 -04:00
										 |  |  | 		var uploadedImages = []; | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		function uploadImage(image, callback) { | 
					
						
							|  |  |  | 			imgur.upload(image.data, 'base64', function(err, data) { | 
					
						
							|  |  |  | 				if(err) { | 
					
						
							|  |  |  | 					callback(err); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					if(data.success) { | 
					
						
							|  |  |  | 						var img= {url:data.data.link, name:image.name}; | 
					
						
							|  |  |  | 						uploadedImages.push(img); | 
					
						
							|  |  |  | 						callback(null); | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2013-08-15 16:50:00 -04:00
										 |  |  | 						winston.error('Can\'t upload image, did you set imgurClientID?'); | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 						callback(data); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-08-03 20:54:16 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(!images) { | 
					
						
							|  |  |  | 			callback(null, uploadedImages); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			async.each(images, uploadImage, function(err) { | 
					
						
							|  |  |  | 				if(!err) { | 
					
						
							|  |  |  | 					callback(null, uploadedImages); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					console.log(err); | 
					
						
							|  |  |  | 					callback(err, null); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 	Posts.getPostsByUid = function(uid, start, end, callback) { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		user.getPostIds(uid, start, end, function(pids) { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 			if(pids && pids.length) { | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				Posts.getPostsByPids(pids, function(err, posts) { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 					callback(posts); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-06-26 23:04:49 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 			else | 
					
						
							|  |  |  | 				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) { | 
					
						
							|  |  |  | 			if(err === null) { | 
					
						
							|  |  |  | 				var stats = { | 
					
						
							| 
									
										
										
										
											2013-06-28 12:54:56 -04:00
										 |  |  | 					topics: data[0]?data[0]:0, | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 					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-08-20 12:11:17 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-28 10:56:38 -04:00
										 |  |  | 			else | 
					
						
							|  |  |  | 				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
										 |  |  | 
 | 
					
						
							|  |  |  | 					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) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				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) { | 
					
						
							|  |  |  | 			if(err) | 
					
						
							|  |  |  | 				return callback(err, null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Posts.getPostSummaryByPids(pids, function(err, posts) { | 
					
						
							|  |  |  | 				if(err) | 
					
						
							|  |  |  | 					return callback(err, null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				callback(null, posts); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | }(exports)); |