Node.js based forum software built for the modern web
recent replies part 2
psychobunny448673b14127
diff --git a/public/templates/category.tpl b/public/templates/category.tplindex 8064da84d7..ecec8a2688 100644--- a/public/templates/category.tpl+++ b/public/templates/category.tpl@@ -48,7 +48,7 @@<div class="block-header">Recent Replies</div>- <div class="block-content">+ <div class="block-content" id="category_recent_replies"></div></div>@@ -82,6 +82,7 @@<script type="text/javascript">(function() {+ var cid = '{category_id}';var room = 'category_' + '{category_id}';app.enter_room(room);@@ -129,9 +130,10 @@- //socket.emit('api:topics.getRecentReplies', tid);- socket.on('api:topics.getRecentReplies', function(replies) {+ socket.emit('api:categories.getRecentReplies', cid);+ socket.on('api:categories.getRecentReplies', function(replies) {console.log(replies);+ jQuery('#category_recent_replies')});})();diff --git a/src/categories.js b/src/categories.jsindex 0b7f6f10b7..3d8014aaec 100644--- a/src/categories.js+++ b/src/categories.js@@ -259,7 +259,11 @@ var RDB = require('./redis.js'),});}-+ Categories.getRecentReplies = function(cid, callback) {+ RDB.zrange('categories:recent_posts:cid:' + cid, 0, -1, function(err, replies) {+ callback(replies);+ });+ }Categories.getCategories = function(cids, callback, current_user) {if (cids.length === 0) {diff --git a/src/posts.js b/src/posts.jsindex aa9a7a7f81..6b93fda730 100644--- a/src/posts.js+++ b/src/posts.js@@ -23,74 +23,87 @@ marked.setOptions({}topics.markAsRead(tid, current_user);++ Posts.getPostsByPids(pids, current_user, function(posts) {+ callback(posts);+ })+- 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) {- favourites.getFavouritesByPostIDs(pids, current_user, function(fav_data) {- next(null, fav_data);- }); // to be moved- }+ // todo, getPostsByPids has duplicated stuff, have that call this fn.+ Posts.getPostSummaryByPids = function(pids, callback) {+ var content = [], uid = [], timestamp = [];- 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]);- }+ };++ Posts.getPostsByPids = function(pids, current_user, callback) {+ var content = [], uid = [], timestamp = [], 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');+ }+++ function getFavouritesData(next) {+ favourites.getFavouritesByPostIDs(pids, current_user, function(fav_data) {+ next(null, fav_data);+ }); // to be moved+ }++ 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- });+ user.getMultipleUserFields(post_data.uid, ['username','reputation','picture', 'signature'], function(user_details) {+ next(null, {+ users: user_details,+ posts: post_data});- // above, to be deprecated});- }-- 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+ // above, to be deprecated});- });+ }+ 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+ });});}@@ -136,7 +149,10 @@ marked.setOptions({RDB.del('cid:' + cid + ':read_by_uid');});- RDB.zadd('topics:recent_posts:tid:' + tid, (new Date()).getTime(), pid);+ topics.get_cid_by_tid(tid, function(cid) {+ RDB.zadd('categories:recent_posts:cid:' + cid, (new Date()).getTime(), pid);+ });+// Re-add the poster, so he/she does not get an "unread" flag on this topictopics.markAsRead(tid, uid);diff --git a/src/redis.js b/src/redis.jsindex e20413acb4..935e8afe06 100644--- a/src/redis.js+++ b/src/redis.js@@ -37,4 +37,29 @@});};+ /*+ * A lot of redis calls come back like this:+ * [key, value, key, value, key, value]+ * this is a simple utility fn to turn this into an object.+ */+ RedisDB.exports.objectify = function(arr) {+ var obj = {};+ for (var i = 0; i < arr.length; i += 2) {+ obj[arr[i]] = arr[i+1];+ }+ return obj;+ };++ /*+ * Similar to .objectify, this utility function splits the data array into two arrays+ */+ RedisDB.exports.splitify = function(arr) {+ var arr1 = [], arr2 = [];+ for (var i = 0; i < arr.length; i += 2) {+ arr1.push(arr[i]);+ arr2.push(arr[i+1]);+ }+ return [arr1,arr2];+ };+}(module));\ No newline at end of filediff --git a/src/topics.js b/src/topics.jsindex bf40fc8931..ec60397afb 100644--- a/src/topics.js+++ b/src/topics.js@@ -273,11 +273,6 @@ 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.jsindex 0a3c15d826..be73f9c00d 100644--- a/src/websockets.js+++ b/src/websockets.js@@ -263,10 +263,9 @@ 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('api:categories.getRecentReplies', function(tid) {+ categories.getRecentReplies(tid, function(replies) {+ socket.emit('api:categories.getRecentReplies', replies);});});