| 
									
										
										
										
											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'), | 
					
						
							|  |  |  | 	user = require('./user.js'); | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(Posts) { | 
					
						
							|  |  |  | 	//data structure
 | 
					
						
							|  |  |  | 	//*global:next_post_id
 | 
					
						
							|  |  |  | 	// *pid:1:content
 | 
					
						
							|  |  |  | 	// *pid:1:uid
 | 
					
						
							|  |  |  | 	// *pid:1:timestamp
 | 
					
						
							|  |  |  | 	// ***pid:1:replies
 | 
					
						
							|  |  |  | 	// *uid:1:posts
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 19:31:21 +00:00
										 |  |  | 	Posts.get = function(callback, tid, current_user, start, end) { | 
					
						
							|  |  |  | 		console.log(current_user); | 
					
						
							| 
									
										
										
										
											2013-04-28 20:07:42 +00:00
										 |  |  | 		if (start == null) start = 0; | 
					
						
							|  |  |  | 		if (end == null) end = start + 10; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 21:52:40 +00:00
										 |  |  | 		RDB.get('tid:' + tid + ':title', function(topic_name) { //do these asynch later
 | 
					
						
							|  |  |  | 			RDB.lrange('tid:' + tid + ':posts', start, end, function(pids) { | 
					
						
							|  |  |  | 				var content = [], | 
					
						
							|  |  |  | 					uid = [], | 
					
						
							| 
									
										
										
										
											2013-05-03 16:22:40 +00:00
										 |  |  | 					timestamp = [], | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 					pid = [], | 
					
						
							|  |  |  | 					post_rep = []; | 
					
						
							| 
									
										
										
										
											2013-05-01 21:52:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				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'); | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 					post_rep.push('pid:' + pids[i] + ':rep');		 | 
					
						
							| 
									
										
										
										
											2013-05-03 16:22:40 +00:00
										 |  |  | 					pid.push(pids[i]); | 
					
						
							| 
									
										
										
										
											2013-05-01 21:52:40 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (pids.length > 0) { | 
					
						
							|  |  |  | 					RDB.multi() | 
					
						
							|  |  |  | 						.mget(content) | 
					
						
							|  |  |  | 						.mget(uid) | 
					
						
							|  |  |  | 						.mget(timestamp) | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 						.mget(post_rep) | 
					
						
							| 
									
										
										
										
											2013-05-01 21:52:40 +00:00
										 |  |  | 						.exec(function(err, replies) { | 
					
						
							|  |  |  | 							content = replies[0]; | 
					
						
							|  |  |  | 							uid = replies[1]; | 
					
						
							|  |  |  | 							timestamp = replies[2]; | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 							post_rep = replies[3]; | 
					
						
							| 
									
										
										
										
											2013-05-01 21:52:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 							user.get_user_postdetails(uid, function(user_details) { | 
					
						
							| 
									
										
										
										
											2013-05-04 04:53:43 +00:00
										 |  |  | 								user.get_gravatars_by_uids(uid, '', function(gravatars) { | 
					
						
							| 
									
										
										
										
											2013-05-03 17:09:38 +00:00
										 |  |  | 									var posts = []; | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 									var callbacks = content.length; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 17:09:38 +00:00
										 |  |  | 									for (var i=0, ii=content.length; i<ii; i++) { | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 										(function(i) { | 
					
						
							| 
									
										
										
										
											2013-05-03 19:31:21 +00:00
										 |  |  | 											Posts.hasFavourited(pid[i], current_user, function(hasFavourited) { | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 												posts.push({ | 
					
						
							|  |  |  | 													'pid' : pid[i], | 
					
						
							|  |  |  | 													'content' : marked(content[i] || ''), | 
					
						
							|  |  |  | 													'uid' : uid[i], | 
					
						
							|  |  |  | 													'username' : user_details.username[i] || 'anonymous', | 
					
						
							|  |  |  | 													'user_rep' : user_details.rep[i] || 0, | 
					
						
							|  |  |  | 													'post_rep' : post_rep[i] || 0, | 
					
						
							|  |  |  | 													'gravatar' : gravatars[i], | 
					
						
							|  |  |  | 													'timestamp' : timestamp[i], | 
					
						
							|  |  |  | 													'relativeTime': utils.relativeTime(timestamp[i]), | 
					
						
							|  |  |  | 													'fav_star_class' : hasFavourited ? 'icon-star' : 'icon-star-empty'  | 
					
						
							|  |  |  | 												}); | 
					
						
							| 
									
										
										
										
											2013-05-03 19:31:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 												callbacks--; | 
					
						
							|  |  |  | 												if (callbacks == 0) { | 
					
						
							|  |  |  | 													callback({'topic_name':topic_name, 'topic_id': tid, 'posts': posts}); | 
					
						
							|  |  |  | 												} | 
					
						
							|  |  |  | 											}); | 
					
						
							|  |  |  | 										}(i)); | 
					
						
							| 
									
										
										
										
											2013-05-03 17:09:38 +00:00
										 |  |  | 									} | 
					
						
							|  |  |  | 								}); | 
					
						
							| 
									
										
										
										
											2013-05-03 16:43:07 +00:00
										 |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2013-05-01 21:52:40 +00:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					callback({}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-04-28 20:07:42 +00:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 21:52:40 +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-01 20:58:36 +00:00
										 |  |  | 			RDB.rpush('tid:' + tid + ':posts', pid); | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 21:19:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 21:26:47 +00:00
										 |  |  | 			socket.emit('event:alert', { | 
					
						
							| 
									
										
										
										
											2013-05-01 20:58:36 +00:00
										 |  |  | 				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
										 |  |  | 
 | 
					
						
							|  |  |  | 			user.get_user_postdetails([uid], function(user_details) { | 
					
						
							|  |  |  | 				user.get_gravatars_by_uids([uid], 80, function(gravatars) { | 
					
						
							|  |  |  | 					var timestamp = new Date().getTime(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					socket.broadcast.to('topic_' + tid).emit('event:new_post', { | 
					
						
							|  |  |  | 						'posts' : [ | 
					
						
							|  |  |  | 							{ | 
					
						
							|  |  |  | 								'pid' : pid, | 
					
						
							|  |  |  | 								'content' : marked(content || ''), | 
					
						
							|  |  |  | 								'uid' : uid, | 
					
						
							|  |  |  | 								'username' : user_details.username[0] || 'anonymous', | 
					
						
							|  |  |  | 								'user_rep' : user_details.rep[0] || 0, | 
					
						
							|  |  |  | 								'post_rep' : 0, | 
					
						
							|  |  |  | 								'gravatar' : gravatars[0], | 
					
						
							|  |  |  | 								'timestamp' : timestamp, | 
					
						
							|  |  |  | 								'relativeTime': utils.relativeTime(timestamp), | 
					
						
							|  |  |  | 								'fav_star_class' :'icon-star-empty'  | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						] | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											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-04-24 20:40:34 +00:00
										 |  |  | 		RDB.incr('global:next_post_id', function(pid) { | 
					
						
							|  |  |  | 			// Posts Info
 | 
					
						
							|  |  |  | 			RDB.set('pid:' + pid + ':content', content); | 
					
						
							| 
									
										
										
										
											2013-04-30 12:40:16 -04:00
										 |  |  | 			RDB.set('pid:' + pid + ':uid', uid); | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | 			RDB.set('pid:' + pid + ':timestamp', new Date().getTime()); | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2013-05-03 13:08:16 -04:00
										 |  |  | 			RDB.incr('tid:' + tid + ':postcount'); | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | 			// User Details - move this out later
 | 
					
						
							|  |  |  | 			RDB.lpush('uid:' + uid + ':posts', pid); | 
					
						
							| 
									
										
										
										
											2013-04-25 21:55:11 +00:00
										 |  |  | 			 | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +00:00
										 |  |  | 			if (callback) callback(pid); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-03 19:31:21 +00:00
										 |  |  | 					RDB.incr('uid:' + uid_of_poster + ':rep'); | 
					
						
							| 
									
										
										
										
											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-03 19:31:21 +00:00
										 |  |  | 					RDB.decr('uid:' + uid_of_poster + ':rep'); | 
					
						
							| 
									
										
										
										
											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-04-24 20:40:34 +00:00
										 |  |  | }(exports)); |