From 55cb790d3c897230f1b5cbe0668c5f374f09a802 Mon Sep 17 00:00:00 2001
From: psychobunny <psycho.bunny@hotmail.com>
Date: Fri, 24 May 2013 09:00:00 -0400
Subject: [PATCH] potential fix for duplicate chat results, started recent
 replies block

---
 public/templates/category.tpl | 8 ++++++++
 public/templates/topic.tpl    | 2 +-
 src/posts.js                  | 2 ++
 src/topics.js                 | 6 ++++++
 src/websockets.js             | 7 +++++++
 5 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/public/templates/category.tpl b/public/templates/category.tpl
index 40ee2654a9..464aa5d4e4 100644
--- a/public/templates/category.tpl
+++ b/public/templates/category.tpl
@@ -126,5 +126,13 @@
 		// jQuery('<div></div>').appendTo("#topics-container").hide().append(html).fadeIn('slow');	
 		// set_up_posts(uniqueid);
 	});
+
+
+
+	//socket.emit('api:topics.getRecentReplies', tid);
+	socket.on('api:topics.getRecentReplies', function(replies) {
+		console.log(replies);
+	});
+
 })();
 </script>
\ No newline at end of file
diff --git a/public/templates/topic.tpl b/public/templates/topic.tpl
index c2611a59ee..5d9b8f17c5 100644
--- a/public/templates/topic.tpl
+++ b/public/templates/topic.tpl
@@ -398,7 +398,7 @@
 			'event:topic_deleted', 'event:topic_restored', 'event:topic:locked',
 			'event:topic_unlocked', 'event:topic_pinned', 'event:topic_unpinned',
 			'event:topic_moved', 'event:post_edited', 'event:post_deleted', 'event:post_restored',
-			'api:posts.favourite'
+			'api:posts.favourite', 'chatMessage'
 		]);
 
 
diff --git a/src/posts.js b/src/posts.js
index 59bf61a568..e8de7135de 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -140,6 +140,8 @@ marked.setOptions({
 					RDB.del('cid:' + cid + ':read_by_uid');
 				});
 
+				RDB.zadd('topics:recent_posts:tid:' + tid, (new Date()).getTime(), pid);
+			
 				// Re-add the poster, so he/she does not get an "unread" flag on this topic
 				topics.markAsRead(tid, uid);
 				// this will duplicate once we enter the thread, which is where we should be going
diff --git a/src/topics.js b/src/topics.js
index 43dc651053..422b1e7a49 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -274,6 +274,12 @@ marked.setOptions({
 		});
 	}
 
+	Topics.getRecentReplies = function(tid, callback) {
+		RDB.zrange('topics:recent_posts:tid:' + tid, 0, -1, 'WITHSCORES', function(err, replies) {
+			callback(replies);
+		});
+	}
+
 	Topics.post = function(socket, uid, title, content, category_id) {
 		if (!category_id) throw new Error('Attempted to post without a category_id');
 		
diff --git a/src/websockets.js b/src/websockets.js
index 5db88cb5e8..0a3c15d826 100644
--- a/src/websockets.js
+++ b/src/websockets.js
@@ -263,6 +263,13 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
 			notifications.mark_read(nid, uid);
 		});
 
+		socket.on('api:topics.getRecentReplies', function(tid) {
+			console.log('test');
+			topics.getRecentReplies(tid, function(replies) {
+				socket.emit('api:topics.getRecentReplies', replies);
+			});
+		});
+
 		socket.on('sendChatMessage', function(data) {
 			var touid = data.touid;