| 
									
										
										
										
											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'), | 
					
						
							|  |  |  | 	marked = require('marked'); | 
					
						
							| 
									
										
										
										
											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-04-28 20:07:42 +00:00
										 |  |  | 	Posts.get = function(callback, tid, start, end) { | 
					
						
							|  |  |  | 		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 = [], | 
					
						
							|  |  |  | 					timestamp = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				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'); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (pids.length > 0) { | 
					
						
							|  |  |  | 					RDB.multi() | 
					
						
							|  |  |  | 						.mget(content) | 
					
						
							|  |  |  | 						.mget(uid) | 
					
						
							|  |  |  | 						.mget(timestamp) | 
					
						
							|  |  |  | 						.exec(function(err, replies) { | 
					
						
							|  |  |  | 							content = replies[0]; | 
					
						
							|  |  |  | 							uid = replies[1]; | 
					
						
							|  |  |  | 							timestamp = replies[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							var posts = []; | 
					
						
							|  |  |  | 							for (var i=0, ii=content.length; i<ii; i++) { | 
					
						
							|  |  |  | 								posts.push({ | 
					
						
							| 
									
										
										
										
											2013-05-02 20:35:42 -04:00
										 |  |  | 									'content' : marked(content[i]), | 
					
						
							| 
									
										
										
										
											2013-05-01 21:52:40 +00:00
										 |  |  | 									'uid' : uid[i], | 
					
						
							|  |  |  | 									'timestamp' : timestamp[i], | 
					
						
							|  |  |  | 									'relativeTime': utils.relativeTime(timestamp[i]) | 
					
						
							|  |  |  | 								}); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							callback({'TOPIC_NAME':topic_name, 'TOPIC_ID': tid, 'posts': posts}); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 				} 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-01 20:58:36 +00:00
										 |  |  | 		Posts.create(uid, content, function(pid) { | 
					
						
							|  |  |  | 			RDB.rpush('tid:' + tid + ':posts', pid); | 
					
						
							| 
									
										
										
										
											2013-04-24 20:40:34 +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-04-24 20:40:34 +00:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-30 12:40:16 -04:00
										 |  |  | 	Posts.create = function(uid, content, callback) { | 
					
						
							|  |  |  | 		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()); | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			// 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); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }(exports)); |