| 
									
										
										
										
											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-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-16 12:49:39 -04:00
										 |  |  | 	config = require('../config.js'), | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 	threadTools = require('./threadTools.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-05-24 11:18:28 -04:00
										 |  |  | 	Posts.getPostsByTid = function(tid, current_user, start, end, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 		RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) { | 
					
						
							|  |  |  | 			RDB.handle(err); | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			if (pids.length === 0 ) { | 
					
						
							| 
									
										
										
										
											2013-05-24 11:18:28 -04:00
										 |  |  | 				throw new Error('Topic should never have 0 posts. tid: ' + tid); | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			topics.markAsRead(tid, current_user); | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			var content = [], uid = [], timestamp = [], pid = [], post_rep = [], editor = [], editTime = [], deleted = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for (var i=0, ii=pids.length; i<ii; i++) { | 
					
						
							|  |  |  | 				content.push('pid:' + pids[i] + ':content'); | 
					
						
							|  |  |  | 				uid.push('pid:' + pids[i] + ':uid'); | 
					
						
							|  |  |  | 				timestamp.push('pid:' + pids[i] + ':timestamp'); | 
					
						
							|  |  |  | 				post_rep.push('pid:' + pids[i] + ':rep'); | 
					
						
							|  |  |  | 				editor.push('pid:' + pids[i] + ':editor'); | 
					
						
							|  |  |  | 				editTime.push('pid:' + pids[i] + ':edited'); | 
					
						
							|  |  |  | 				deleted.push('pid:' + pids[i] + ':deleted'); | 
					
						
							|  |  |  | 				pid.push(pids[i]); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			function getFavouritesData(next) { | 
					
						
							| 
									
										
										
										
											2013-05-23 15:11:55 -04:00
										 |  |  | 				favourites.getFavouritesByPostIDs(pids, current_user, function(fav_data) { | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 					next(null, fav_data); | 
					
						
							|  |  |  | 				}); // to be moved
 | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 			function getPostData(next) { | 
					
						
							|  |  |  | 				RDB.multi() | 
					
						
							|  |  |  | 					.mget(content) | 
					
						
							|  |  |  | 					.mget(uid) | 
					
						
							|  |  |  | 					.mget(timestamp) | 
					
						
							|  |  |  | 					.mget(post_rep) | 
					
						
							|  |  |  | 					.mget(editor) | 
					
						
							|  |  |  | 					.mget(editTime) | 
					
						
							|  |  |  | 					.mget(deleted) | 
					
						
							|  |  |  | 					.exec(function(err, replies) { | 
					
						
							|  |  |  | 						post_data = { | 
					
						
							|  |  |  | 							pid: pids, | 
					
						
							|  |  |  | 							content: replies[0], | 
					
						
							|  |  |  | 							uid: replies[1], | 
					
						
							|  |  |  | 							timestamp: replies[2], | 
					
						
							|  |  |  | 							reputation: replies[3], | 
					
						
							|  |  |  | 							editor: replies[4], | 
					
						
							|  |  |  | 							editTime: replies[5], | 
					
						
							|  |  |  | 							deleted: replies[6] | 
					
						
							|  |  |  | 						}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						// below, to be deprecated
 | 
					
						
							|  |  |  | 						// Add any editors to the user_data object
 | 
					
						
							|  |  |  | 						for(var x = 0, numPosts = post_data.editor.length; x < numPosts; x++) { | 
					
						
							|  |  |  | 							if (post_data.editor[x] !== null && post_data.uid.indexOf(post_data.editor[x]) === -1) { | 
					
						
							|  |  |  | 								post_data.uid.push(post_data.editor[x]); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						user.getMultipleUserFields(post_data.uid, ['username','reputation','picture', 'signature'], function(user_details) { | 
					
						
							|  |  |  | 							next(null, { | 
					
						
							|  |  |  | 								users: user_details, | 
					
						
							|  |  |  | 								posts: post_data | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 						// above, to be deprecated
 | 
					
						
							| 
									
										
										
										
											2013-05-18 14:59:50 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-05-16 21:30:16 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			async.parallel([getFavouritesData, getPostData], function(err, results) { | 
					
						
							|  |  |  | 				callback({ | 
					
						
							|  |  |  | 					'voteData' : results[0], // to be moved
 | 
					
						
							|  |  |  | 					'userData' : results[1].users, // to be moved
 | 
					
						
							|  |  |  | 					'postData' : results[1].posts | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-05-18 14:59:50 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 21:30:16 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-16 18:15:58 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Posts.get_tid_by_pid = function(pid, callback) { | 
					
						
							|  |  |  | 		RDB.get('pid:' + pid + ':tid', function(err, tid) { | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 			if (tid && parseInt(tid) > 0) { | 
					
						
							|  |  |  | 				callback(tid); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				callback(false); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-16 18:15:58 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Posts.get_cid_by_pid = function(pid, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-21 18:12:39 -04:00
										 |  |  | 		Posts.get_tid_by_pid(pid, function(tid) { | 
					
						
							| 
									
										
										
										
											2013-05-16 18:15:58 -04:00
										 |  |  | 			if (tid) topics.get_cid_by_tid(tid, function(cid) { | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -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-09 01:04:15 -04:00
										 |  |  | 		Posts.create(uid, tid, content, function(pid) { | 
					
						
							|  |  |  | 			if (pid > 0) { | 
					
						
							|  |  |  | 				RDB.rpush('tid:' + tid + ':posts', pid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 07:27:55 +00:00
										 |  |  | 				RDB.del('tid:' + tid + ':read_by_uid'); // let everybody know there is an unread post
 | 
					
						
							| 
									
										
										
										
											2013-05-21 18:12:39 -04:00
										 |  |  | 				Posts.get_cid_by_pid(pid, function(cid) { | 
					
						
							|  |  |  | 					RDB.del('cid:' + cid + ':read_by_uid'); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-05-09 07:27:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-24 09:00:00 -04:00
										 |  |  | 				RDB.zadd('topics:recent_posts:tid:' + tid, (new Date()).getTime(), pid); | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2013-05-16 10:59:45 -04:00
										 |  |  | 				// Re-add the poster, so he/she does not get an "unread" flag on this topic
 | 
					
						
							|  |  |  | 				topics.markAsRead(tid, uid); | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 				// this will duplicate once we enter the thread, which is where we should be going
 | 
					
						
							| 
									
										
										
										
											2013-05-16 10:59:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -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-05-23 12:52:16 -04:00
										 |  |  | 				user.getUserFields(uid, ['username','reputation','picture','signature'], function(data) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					 | 
					
						
							|  |  |  | 					var timestamp = new Date().getTime(); | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					io.sockets.in('topic_' + tid).emit('event:new_post', { | 
					
						
							|  |  |  | 						'posts' : [ | 
					
						
							|  |  |  | 							{ | 
					
						
							|  |  |  | 								'pid' : pid, | 
					
						
							|  |  |  | 								'content' : marked(content || ''), | 
					
						
							|  |  |  | 								'uid' : uid, | 
					
						
							|  |  |  | 								'username' : data.username || 'anonymous', | 
					
						
							|  |  |  | 								'user_rep' : data.reputation || 0, | 
					
						
							|  |  |  | 								'post_rep' : 0, | 
					
						
							|  |  |  | 								'gravatar' : data.picture, | 
					
						
							| 
									
										
										
										
											2013-05-21 22:56:09 -04:00
										 |  |  | 								'signature' : marked(data.signature || ''), | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 								'timestamp' : timestamp, | 
					
						
							|  |  |  | 								'relativeTime': utils.relativeTime(timestamp), | 
					
						
							| 
									
										
										
										
											2013-05-12 15:59:45 -04:00
										 |  |  | 								'fav_star_class' :'icon-star-empty', | 
					
						
							|  |  |  | 								'edited-class': 'none', | 
					
						
							|  |  |  | 								'editor': '', | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -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 | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Posts.create = function(uid, tid, content, callback) { | 
					
						
							|  |  |  | 		if (uid === null) return; | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		RDB.get('tid:' + tid + ':locked', function(err, locked) { | 
					
						
							|  |  |  | 			RDB.handle(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!locked || locked === '0') { | 
					
						
							|  |  |  | 				RDB.incr('global:next_post_id', function(err, pid) { | 
					
						
							|  |  |  | 					RDB.handle(err); | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 					// Posts Info
 | 
					
						
							|  |  |  | 					RDB.set('pid:' + pid + ':content', content); | 
					
						
							|  |  |  | 					RDB.set('pid:' + pid + ':uid', uid); | 
					
						
							|  |  |  | 					RDB.set('pid:' + pid + ':timestamp', new Date().getTime()); | 
					
						
							|  |  |  | 					RDB.set('pid:' + pid + ':rep', 0); | 
					
						
							| 
									
										
										
										
											2013-05-09 10:20:25 -04:00
										 |  |  | 					RDB.set('pid:' + pid + ':tid', tid); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					 | 
					
						
							|  |  |  | 					RDB.incr('tid:' + tid + ':postcount'); | 
					
						
							| 
									
										
										
										
											2013-05-14 17:06:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-23 12:52:16 -04:00
										 |  |  | 					user.getUserFields(uid, ['username'], function(data) { | 
					
						
							| 
									
										
										
										
											2013-05-15 15:42:24 -04:00
										 |  |  | 						//add active users to this category
 | 
					
						
							|  |  |  | 						RDB.get('tid:' + tid + ':cid', function(err, cid) { | 
					
						
							|  |  |  | 							RDB.handle(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							// 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', data.username); | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-05-14 17:06:48 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 					 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					 | 
					
						
							|  |  |  | 					// User Details - move this out later
 | 
					
						
							|  |  |  | 					RDB.lpush('uid:' + uid + ':posts', pid); | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					user.incrementUserFieldBy(uid, 'postcount', 1); | 
					
						
							| 
									
										
										
										
											2013-05-09 10:20:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					if (callback)  | 
					
						
							|  |  |  | 						callback(pid); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				callback(-1); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 10:20:25 -04:00
										 |  |  | 	Posts.getRawContent = function(pid, socket) { | 
					
						
							|  |  |  | 		RDB.get('pid:' + pid + ':content', function(err, raw) { | 
					
						
							|  |  |  | 			socket.emit('api:posts.getRawPost', { post: raw }); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-23 15:11:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | }(exports)); |