| 
									
										
										
										
											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
										 |  |  | 	marked = require('marked'), | 
					
						
							|  |  |  | 	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-05-27 14:55:03 -04:00
										 |  |  | 	feed = require('./feed.js'), | 
					
						
							| 
									
										
										
										
											2013-05-16 12:49:39 -04:00
										 |  |  | 	async = require('async'); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 00:45:04 -04:00
										 |  |  | marked.setOptions({ | 
					
						
							|  |  |  | 	breaks: true | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-07-02 16:24:13 -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-07-02 16:24:13 -04:00
										 |  |  | 				Posts.getPostsByPids(pids, function(posts) { | 
					
						
							| 
									
										
										
										
											2013-06-26 13:38:05 -04:00
										 |  |  | 					callback(posts); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				callback({ | 
					
						
							|  |  |  | 					error: 'no-posts' | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	Posts.addUserInfoToPost = function(post, callback) { | 
					
						
							|  |  |  | 		user.getUserFields(post.uid, ['username', 'userslug', 'reputation', 'picture', 'signature'], function(userData) { | 
					
						
							| 
									
										
										
										
											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-16 15:22:59 -04:00
										 |  |  | 			post.picture = userData.picture || require('gravatar').url('', {}, https=global.nconf.get('https')); | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 			post.signature = marked(userData.signature || ''); | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-13 12:27:37 -04:00
										 |  |  | 			if(post.editor !== '') { | 
					
						
							|  |  |  | 				user.getUserFields(post.editor, ['username', 'userslug'], function(editorData) { | 
					
						
							|  |  |  | 					post.editorname = editorData.username; | 
					
						
							|  |  |  | 					post.editorslug = editorData.userslug;	 | 
					
						
							|  |  |  | 					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) { | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		var returnData = []; | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-07-15 14:34:15 -04:00
										 |  |  | 		function getPostSummary(pid, callback) { | 
					
						
							|  |  |  | 			Posts.getPostFields(pid, ['pid', 'content', 'uid', 'timestamp', 'deleted'], function(postData) { | 
					
						
							|  |  |  | 				Posts.addUserInfoToPost(postData, function() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if(postData.deleted !== '1')	{ | 
					
						
							|  |  |  | 						returnData.push(postData); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					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-07-15 14:34:15 -04:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		async.eachSeries(pids, getPostSummary, function(err) { | 
					
						
							|  |  |  | 			if(!err) { | 
					
						
							|  |  |  | 				callback(returnData); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 	Posts.getPostData = function(pid, callback) { | 
					
						
							|  |  |  | 		RDB.hgetall('post:' + pid, function(err, data) { | 
					
						
							|  |  |  | 			if(err === null)  | 
					
						
							|  |  |  | 				callback(data); | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 	Posts.getPostFields = function(uid, fields, callback) { | 
					
						
							|  |  |  | 		RDB.hmget('post:' + uid, fields, function(err, data) { | 
					
						
							|  |  |  | 			if(err === null) { | 
					
						
							|  |  |  | 				var returnData = {}; | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 				for(var i=0, ii=fields.length; i<ii; ++i) { | 
					
						
							|  |  |  | 					returnData[fields[i]] = data[i]; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				callback(returnData); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							|  |  |  | 		});		 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 	Posts.getPostsByPids = function(pids, callback) { | 
					
						
							|  |  |  | 		var posts = [],  | 
					
						
							|  |  |  | 			loaded = 0; | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		for(var i=0, ii=pids.length; i<ii; ++i) { | 
					
						
							|  |  |  | 			(function(index, pid) { | 
					
						
							|  |  |  | 				Posts.getPostData(pid, function(postData) { | 
					
						
							| 
									
										
										
										
											2013-07-03 12:14:20 -04:00
										 |  |  | 				 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 					if(postData) { | 
					
						
							| 
									
										
										
										
											2013-07-03 12:14:20 -04:00
										 |  |  | 						postData.relativeTime = utils.relativeTime(postData.timestamp);	 | 
					
						
							|  |  |  | 						postData.post_rep = postData.reputation; | 
					
						
							|  |  |  | 						postData['edited-class'] = postData.editor !== '' ? '' : 'none'; | 
					
						
							|  |  |  | 						postData['relativeEditTime'] = postData.edited !== '0' ? utils.relativeTime(postData.edited) : ''; | 
					
						
							| 
									
										
										
										
											2013-07-13 12:36:51 -04:00
										 |  |  | 						postData.content = marked(postData.content || ''); | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 						posts[index] = postData; | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 					 | 
					
						
							|  |  |  | 					++loaded; | 
					
						
							|  |  |  | 					if(loaded === pids.length) | 
					
						
							|  |  |  | 						callback(posts); | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 			}(i, pids[i])); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-05-16 18:15:58 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 	Posts.getPostField = function(pid, field, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 		RDB.hget('post:' + pid, field, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 			if(err === null) | 
					
						
							|  |  |  | 				callback(data); | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-05-16 18:15:58 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-03 13:08:32 -04:00
										 |  |  | 	Posts.setPostField = function(pid, field, value) { | 
					
						
							|  |  |  | 		RDB.hset('post:' + pid, field, value); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 	Posts.getPostFields = function(pid, fields, callback) { | 
					
						
							|  |  |  | 		RDB.hmget('post:' + pid, fields, function(err, data) { | 
					
						
							|  |  |  | 			if(err === null) { | 
					
						
							|  |  |  | 				var returnData = {}; | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 				for(var i=0, ii=fields.length; i<ii; ++i) { | 
					
						
							|  |  |  | 					returnData[fields[i]] = data[i]; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				callback(returnData); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							|  |  |  | 		});		 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -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-07-02 19:02:23 -04:00
										 |  |  | 				topics.getTopicField(tid, 'cid', function(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
										 |  |  | 
 | 
					
						
							|  |  |  | 	Posts.reply = function(socket, tid, uid, content) { | 
					
						
							| 
									
										
										
										
											2013-05-14 17:19:22 -04:00
										 |  |  | 		if (uid < 1) { | 
					
						
							|  |  |  | 			socket.emit('event:alert', { | 
					
						
							|  |  |  | 				title: 'Reply Unsuccessful', | 
					
						
							|  |  |  | 				message: 'You don't seem to be logged in, so you cannot reply.', | 
					
						
							|  |  |  | 				type: 'error', | 
					
						
							|  |  |  | 				timeout: 2000 | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 10:59:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-24 12:53:45 -04:00
										 |  |  | 		user.getUserField(uid, 'lastposttime', function(lastposttime) { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-19 21:29:50 -04:00
										 |  |  | 			if(Date.now() - lastposttime < config.post_delay) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				socket.emit('event:alert', { | 
					
						
							| 
									
										
										
										
											2013-05-24 12:32:28 -04:00
										 |  |  | 					title: 'Too many posts!', | 
					
						
							|  |  |  | 					message: 'You can only post every '+ (config.post_delay / 1000) + ' seconds.', | 
					
						
							|  |  |  | 					type: 'error', | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					timeout: 2000 | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:32:28 -04:00
										 |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-24 12:32:28 -04:00
										 |  |  | 			Posts.create(uid, tid, content, function(pid) { | 
					
						
							|  |  |  | 				if (pid > 0) { | 
					
						
							|  |  |  | 					RDB.rpush('tid:' + tid + ':posts', pid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 					RDB.del('tid:' + tid + ':read_by_uid');  | 
					
						
							| 
									
										
										
										
											2013-05-24 14:34:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-24 12:32:28 -04:00
										 |  |  | 					Posts.get_cid_by_pid(pid, function(cid) { | 
					
						
							| 
									
										
										
										
											2013-06-28 11:36:41 -04:00
										 |  |  | 						RDB.del('cid:' + cid + ':read_by_uid', function(err, data) { | 
					
						
							|  |  |  | 							topics.markAsRead(tid, uid);	 | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:32:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 					Posts.getTopicPostStats(socket); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// Send notifications to users who are following this topic
 | 
					
						
							|  |  |  | 					threadTools.notify_followers(tid, uid); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:32:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 					 | 
					
						
							| 
									
										
										
										
											2013-05-24 12:32:28 -04:00
										 |  |  | 					socket.emit('event:alert', { | 
					
						
							|  |  |  | 						title: 'Reply Successful', | 
					
						
							|  |  |  | 						message: 'You have successfully replied. Click here to view your reply.', | 
					
						
							|  |  |  | 						type: 'notify', | 
					
						
							|  |  |  | 						timeout: 2000 | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-06 20:39:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-03 13:40:55 -04:00
										 |  |  | 					var timestamp = Date.now(); | 
					
						
							|  |  |  | 					var socketData = { | 
					
						
							|  |  |  | 						'posts' : [ | 
					
						
							|  |  |  | 							{ | 
					
						
							|  |  |  | 								'pid' : pid, | 
					
						
							|  |  |  | 								'content' : marked(content || ''), | 
					
						
							|  |  |  | 								'uid' : uid, | 
					
						
							|  |  |  | 								'post_rep' : 0, | 
					
						
							|  |  |  | 								'timestamp' : timestamp, | 
					
						
							|  |  |  | 								'relativeTime': utils.relativeTime(timestamp), | 
					
						
							|  |  |  | 								'fav_star_class' :'icon-star-empty', | 
					
						
							|  |  |  | 								'edited-class': 'none', | 
					
						
							|  |  |  | 								'editor': '', | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						] | 
					
						
							|  |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2013-06-27 21:11:04 -04:00
										 |  |  | 						 | 
					
						
							| 
									
										
										
										
											2013-07-03 13:40:55 -04:00
										 |  |  | 					posts.addUserInfoToPost(socketData['posts'][0], function() { | 
					
						
							| 
									
										
										
										
											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-07-03 13:40:55 -04:00
										 |  |  | 					});					 | 
					
						
							|  |  |  | 				 | 
					
						
							| 
									
										
										
										
											2013-05-24 12:32:28 -04:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					socket.emit('event:alert', { | 
					
						
							|  |  |  | 						title: 'Reply Unsuccessful', | 
					
						
							|  |  |  | 						message: 'Your reply could not be posted at this time. Please try again later.', | 
					
						
							|  |  |  | 						type: 'notify', | 
					
						
							|  |  |  | 						timeout: 2000 | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Posts.create = function(uid, tid, content, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		if (uid === null) { | 
					
						
							|  |  |  | 			callback(-1); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -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-24 12:32:28 -04:00
										 |  |  | 					 | 
					
						
							| 
									
										
										
										
											2013-06-19 21:29:50 -04:00
										 |  |  | 					var timestamp = Date.now(); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 					RDB.hmset('post:' + pid, { | 
					
						
							|  |  |  | 						'pid': pid, | 
					
						
							|  |  |  | 						'uid': uid, | 
					
						
							|  |  |  | 						'tid': tid, | 
					
						
							|  |  |  | 						'content': content, | 
					
						
							|  |  |  | 						'timestamp': timestamp, | 
					
						
							|  |  |  | 						'reputation': 0, | 
					
						
							|  |  |  | 						'editor': '', | 
					
						
							|  |  |  | 						'edited': 0, | 
					
						
							|  |  |  | 						'deleted': 0 | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-06-28 10:56:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 					topics.increasePostCount(tid); | 
					
						
							|  |  |  | 					topics.setTopicField(tid, 'lastposttime', timestamp); | 
					
						
							|  |  |  | 					topics.addToRecent(tid, timestamp); | 
					
						
							| 
									
										
										
										
											2013-05-15 15:42:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 					RDB.incr('totalpostcount'); | 
					
						
							|  |  |  | 						 | 
					
						
							| 
									
										
										
										
											2013-07-02 19:02:23 -04:00
										 |  |  | 					topics.getTopicField(tid, 'cid', function(cid) { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 						RDB.handle(err); | 
					
						
							| 
									
										
										
										
											2013-05-27 14:55:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 						feed.updateTopic(tid, cid); | 
					
						
							| 
									
										
										
										
											2013-05-27 14:55:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-06 22:02:24 -04:00
										 |  |  | 						RDB.zadd('categories:recent_posts:cid:' + cid, Date.now(), pid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -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'); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2013-05-15 15:42:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 							RDB.sadd('cid:' + cid + ':active_users', uid); | 
					
						
							| 
									
										
										
										
											2013-05-15 15:42:24 -04:00
										 |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-05-14 17:06:48 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 					 | 
					
						
							| 
									
										
										
										
											2013-07-06 22:02:24 -04:00
										 |  |  | 					 | 
					
						
							|  |  |  | 					 | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 					user.onNewPostMade(uid, tid, pid, timestamp);					 | 
					
						
							| 
									
										
										
										
											2013-06-18 10:01:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					if (callback)  | 
					
						
							|  |  |  | 						callback(pid); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				callback(-1); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-06-26 23:04:49 -04:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 	Posts.getPostsByUid = function(uid, start, end, callback) { | 
					
						
							| 
									
										
										
										
											2013-06-26 23:04:49 -04:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		user.getPostIds(uid, start, end, function(pids) { | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			if(pids && pids.length) { | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 				Posts.getPostsByPids(pids, function(posts) { | 
					
						
							|  |  |  | 					callback(posts); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-06-26 23:04:49 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 			else | 
					
						
							|  |  |  | 				callback([]); | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							|  |  |  | 					posts: data[1]?data[1]:0				 | 
					
						
							| 
									
										
										
										
											2013-06-28 10:56:38 -04:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 				socket.emit('post.stats', stats); | 
					
						
							|  |  |  | 			}				 | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | }(exports)); |