| 
									
										
										
										
											2013-05-01 14:23:57 -04:00
										 |  |  | var	RDB = require('./redis.js'), | 
					
						
							| 
									
										
										
										
											2013-05-02 20:35:42 -04:00
										 |  |  | 	utils = require('./utils.js'), | 
					
						
							| 
									
										
										
										
											2013-05-03 16:43:07 +00:00
										 |  |  | 	marked = require('marked'), | 
					
						
							| 
									
										
										
										
											2013-05-06 22:05:42 +00:00
										 |  |  | 	user = require('./user.js'); | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(Posts) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 19:31:21 +00:00
										 |  |  | 	Posts.get = function(callback, tid, current_user, start, end) { | 
					
						
							| 
									
										
										
										
											2013-04-28 20:07:42 +00:00
										 |  |  | 		if (start == null) start = 0; | 
					
						
							|  |  |  | 		if (end == null) end = start + 10; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-06 19:46:50 +00:00
										 |  |  | 		var post_data, user_data, thread_data, vote_data; | 
					
						
							| 
									
										
										
										
											2013-05-06 12:04:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-06 19:46:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		//compile thread after all data is asynchronously called
 | 
					
						
							|  |  |  | 		function generateThread() { | 
					
						
							|  |  |  | 			if (!post_data ||! user_data || !thread_data || !vote_data) return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var posts = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for (var i=0, ii= post_data.pid.length; i<ii; i++) { | 
					
						
							|  |  |  | 				var uid = post_data.uid[i], | 
					
						
							|  |  |  | 					pid = post_data.pid[i]; | 
					
						
							|  |  |  | 					 | 
					
						
							| 
									
										
										
										
											2013-05-06 12:04:36 -04:00
										 |  |  | 				posts.push({ | 
					
						
							| 
									
										
										
										
											2013-05-06 19:46:50 +00:00
										 |  |  | 					'pid' : pid, | 
					
						
							|  |  |  | 					'uid' : uid, | 
					
						
							|  |  |  | 					'content' : marked(post_data.content[i] || ''), | 
					
						
							|  |  |  | 					'post_rep' : post_data.reputation[i] || 0, | 
					
						
							|  |  |  | 					'timestamp' : post_data.timestamp[i], | 
					
						
							|  |  |  | 					'relativeTime': utils.relativeTime(post_data.timestamp[i]), | 
					
						
							|  |  |  | 					'username' : user_data[uid].username || 'anonymous', | 
					
						
							|  |  |  | 					'user_rep' : user_data[uid].reputation || 0, | 
					
						
							|  |  |  | 					'gravatar' : user_data[uid].picture, | 
					
						
							|  |  |  | 					'fav_star_class' : vote_data[pid] ? 'icon-star' : 'icon-star-empty', | 
					
						
							| 
									
										
										
										
											2013-05-07 18:21:00 +00:00
										 |  |  | 					'display_moderator_tools' : uid == current_user ? 'show' : 'hide' | 
					
						
							| 
									
										
										
										
											2013-05-06 12:04:36 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			callback({ | 
					
						
							| 
									
										
										
										
											2013-05-06 19:46:50 +00:00
										 |  |  | 				'topic_name':thread_data.topic_name, | 
					
						
							| 
									
										
										
										
											2013-05-07 18:21:00 +00:00
										 |  |  | 				'category_name':thread_data.category_name, | 
					
						
							|  |  |  | 				'category_slug':thread_data.category_slug, | 
					
						
							| 
									
										
										
										
											2013-05-06 19:46:50 +00:00
										 |  |  | 				'locked': parseInt(thread_data.locked) || 0, | 
					
						
							| 
									
										
										
										
											2013-05-06 12:04:36 -04:00
										 |  |  | 				'topic_id': tid, | 
					
						
							| 
									
										
										
										
											2013-05-06 19:46:50 +00:00
										 |  |  | 				'posts': posts | 
					
						
							| 
									
										
										
										
											2013-05-01 21:52:40 +00:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-06 19:46:50 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// get all data for thread in asynchronous fashion
 | 
					
						
							|  |  |  | 		RDB.lrange('tid:' + tid + ':posts', start, end, function(pids) { | 
					
						
							|  |  |  | 			var content = [], uid = [], timestamp = [], pid = [], post_rep = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			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');		 | 
					
						
							|  |  |  | 				pid.push(pids[i]); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Posts.getFavouritesByPostIDs(pids, current_user, function(fav_data) { | 
					
						
							|  |  |  | 				vote_data = fav_data; | 
					
						
							|  |  |  | 				generateThread(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			RDB.multi() | 
					
						
							|  |  |  | 				.mget(content) | 
					
						
							|  |  |  | 				.mget(uid) | 
					
						
							|  |  |  | 				.mget(timestamp) | 
					
						
							|  |  |  | 				.mget(post_rep) | 
					
						
							|  |  |  | 				.get('tid:' + tid + ':title') | 
					
						
							|  |  |  | 				.get('tid:' + tid + ':locked') | 
					
						
							| 
									
										
										
										
											2013-05-07 18:21:00 +00:00
										 |  |  | 				.get('tid:' + tid + ':category_name') | 
					
						
							|  |  |  | 				.get('tid:' + tid + ':category_slug') | 
					
						
							| 
									
										
										
										
											2013-05-06 19:46:50 +00:00
										 |  |  | 				.exec(function(err, replies) { | 
					
						
							|  |  |  | 					post_data = { | 
					
						
							|  |  |  | 						pid: pids, | 
					
						
							|  |  |  | 						content: replies[0], | 
					
						
							|  |  |  | 						uid: replies[1], | 
					
						
							|  |  |  | 						timestamp: replies[2], | 
					
						
							|  |  |  | 						reputation: replies[3] | 
					
						
							|  |  |  | 					}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					thread_data = { | 
					
						
							|  |  |  | 						topic_name: replies[4], | 
					
						
							| 
									
										
										
										
											2013-05-07 18:21:00 +00:00
										 |  |  | 						locked: replies[5], | 
					
						
							|  |  |  | 						category_name: replies[6], | 
					
						
							|  |  |  | 						category_slug: replies[7] | 
					
						
							| 
									
										
										
										
											2013-05-06 19:46:50 +00:00
										 |  |  | 					}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					user.getMultipleUserFields(post_data.uid, ['username','reputation','picture'], function(user_details){ | 
					
						
							|  |  |  | 						user_data = user_details; | 
					
						
							|  |  |  | 						generateThread(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2013-04-28 20:07:42 +00:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 12:54:27 -04:00
										 |  |  | 	Posts.reply = function(socket, tid, uid, content) { | 
					
						
							| 
									
										
										
										
											2013-05-03 13:08:16 -04:00
										 |  |  | 		Posts.create(uid, tid, content, function(pid) { | 
					
						
							| 
									
										
										
										
											2013-05-06 15:30:47 -04:00
										 |  |  | 			if (pid > 0) { | 
					
						
							|  |  |  | 				RDB.rpush('tid:' + tid + ':posts', pid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				socket.emit('event:alert', { | 
					
						
							|  |  |  | 					title: 'Reply Successful', | 
					
						
							|  |  |  | 					message: 'You have successfully replied. Click here to view your reply.', | 
					
						
							|  |  |  | 					type: 'notify', | 
					
						
							|  |  |  | 					timeout: 2000 | 
					
						
							| 
									
										
										
										
											2013-05-03 21:19:28 +00:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-05-06 15:30:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 21:19:28 +00:00
										 |  |  | 			 | 
					
						
							| 
									
										
										
										
											2013-05-06 15:30:47 -04:00
										 |  |  | 				user.getUserFields(uid, ['username','reputation','picture'], function(data){ | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					var timestamp = new Date().getTime(); | 
					
						
							| 
									
										
										
										
											2013-05-07 17:27:38 +00:00
										 |  |  | 					 | 
					
						
							|  |  |  | 					io.sockets.in('topic_' + tid).emit('event:new_post', { | 
					
						
							| 
									
										
										
										
											2013-05-06 15:30:47 -04:00
										 |  |  | 						'posts' : [ | 
					
						
							|  |  |  | 							{ | 
					
						
							|  |  |  | 								'pid' : pid, | 
					
						
							|  |  |  | 								'content' : marked(content || ''), | 
					
						
							|  |  |  | 								'uid' : uid, | 
					
						
							|  |  |  | 								'username' : data.username || 'anonymous', | 
					
						
							|  |  |  | 								'user_rep' : data.reputation || 0, | 
					
						
							|  |  |  | 								'post_rep' : 0, | 
					
						
							|  |  |  | 								'gravatar' : data.picture, | 
					
						
							|  |  |  | 								'timestamp' : timestamp, | 
					
						
							|  |  |  | 								'relativeTime': utils.relativeTime(timestamp), | 
					
						
							|  |  |  | 								'fav_star_class' :'icon-star-empty'  | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						] | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} 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-01 20:58:36 +00:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 13:08:16 -04:00
										 |  |  | 	Posts.create = function(uid, tid, content, callback) { | 
					
						
							| 
									
										
										
										
											2013-04-30 12:40:16 -04:00
										 |  |  | 		if (uid === null) return; | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-06 15:30:47 -04:00
										 |  |  | 		RDB.get('tid:' + tid + ':locked', function(locked) { | 
					
						
							|  |  |  | 			if (!locked || locked === '0') { | 
					
						
							|  |  |  | 				RDB.incr('global:next_post_id', function(pid) { | 
					
						
							|  |  |  | 					// 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); | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					RDB.incr('tid:' + tid + ':postcount'); | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					// User Details - move this out later
 | 
					
						
							|  |  |  | 					RDB.lpush('uid:' + uid + ':posts', pid); | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					RDB.db.hincrby(uid, 'postcount', 1); | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					if (callback)  | 
					
						
							|  |  |  | 						callback(pid); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				callback(-1); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 19:31:21 +00:00
										 |  |  | 	Posts.favourite = function(io, pid, room_id, uid) { | 
					
						
							|  |  |  | 		RDB.get('pid:' + pid + ':uid', function(uid_of_poster) { | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 			Posts.hasFavourited(pid, uid, function(hasFavourited) { | 
					
						
							|  |  |  | 				if (hasFavourited == false) { | 
					
						
							|  |  |  | 					RDB.sadd('pid:' + pid + ':users_favourited', uid); | 
					
						
							| 
									
										
										
										
											2013-05-05 19:13:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					RDB.db.hincrby(String(uid_of_poster), 'reputation', 1); | 
					
						
							|  |  |  | 					 | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 					RDB.incr('pid:' + pid + ':rep'); | 
					
						
							| 
									
										
										
										
											2013-05-03 19:31:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					if (room_id) { | 
					
						
							|  |  |  | 						io.sockets.in(room_id).emit('event:rep_up', {uid: uid_of_poster, pid: pid}); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 19:31:21 +00:00
										 |  |  | 	Posts.unfavourite = function(io, pid, room_id, uid) { | 
					
						
							|  |  |  | 		RDB.get('pid:' + pid + ':uid', function(uid_of_poster) { | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 			Posts.hasFavourited(pid, uid, function(hasFavourited) { | 
					
						
							|  |  |  | 				if (hasFavourited == true) { | 
					
						
							|  |  |  | 					RDB.srem('pid:' + pid + ':users_favourited', uid); | 
					
						
							| 
									
										
										
										
											2013-05-05 19:13:08 -04:00
										 |  |  | 					RDB.db.hincrby(String(uid_of_poster), 'reputation', -1); | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 					RDB.decr('pid:' + pid + ':rep'); | 
					
						
							| 
									
										
										
										
											2013-05-03 19:31:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					if (room_id) { | 
					
						
							|  |  |  | 						io.sockets.in(room_id).emit('event:rep_down', {uid: uid_of_poster, pid: pid}); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Posts.hasFavourited = function(pid, uid, callback) { | 
					
						
							|  |  |  | 		RDB.sismember('pid:' + pid + ':users_favourited', uid, function(hasFavourited) { | 
					
						
							|  |  |  | 			callback(hasFavourited); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-06 19:46:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Posts.getFavouritesByPostIDs = function(pids, uid, callback) { | 
					
						
							|  |  |  | 		var loaded = 0; | 
					
						
							|  |  |  | 		var data = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (var i=0, ii=pids.length; i<ii; i++) { | 
					
						
							|  |  |  | 			(function(post_id) { | 
					
						
							|  |  |  | 				Posts.hasFavourited(post_id, uid, function(hasFavourited){ | 
					
						
							|  |  |  | 					data[post_id] = hasFavourited; | 
					
						
							|  |  |  | 					loaded ++; | 
					
						
							|  |  |  | 					if (loaded == pids.length) callback(data); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}(pids[i])) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | }(exports)); |