Skip to content

Node.js based forum software built for the modern web

fixed categories.getRecentReplies so it only returns count posts instead of getting 10, posts are added and removed from sorted set when they are deleted restored

Baris Usakli401a30e02ccd
diff --git a/src/categories.js b/src/categories.js
index 2da43b1dd7..2cfb7f8160 100644
--- a/src/categories.js
+++ b/src/categories.js
@@ -216,24 +216,22 @@ var db = require('./database.js'),
};
Categories.getRecentReplies = function(cid, count, callback) {
- db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, (count < 10) ? 10 : count, function(err, pids) {
+ db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, count - 1, function(err, pids) {
if (err) {
winston.err(err);
- callback([]);
- return;
+ return callback([]);
}
if (pids.length === 0) {
- callback([]);
- return;
+ return callback([]);
}
posts.getPostSummaryByPids(pids, true, function(err, postData) {
- if (postData.length > count) {
- postData = postData.slice(0, count);
+ if(err) {
+ return callback(err);
}
- callback(postData);
+ callback(null, postData);
});
});
};
diff --git a/src/postTools.js b/src/postTools.js
index 506868ff95..5d1132cf8e 100644
--- a/src/postTools.js
+++ b/src/postTools.js
@@ -143,6 +143,12 @@ var db = require('./database'),
db.sortedSetAdd('users:postcount', postcount, postData.uid);
});
+ topics.getTopicField(postData.tid, 'cid', function(err, cid) {
+ if(!err) {
+ db.sortedSetRemove('categories:recent_posts:cid:' + cid, pid);
+ }
+ });
+
// Delete the thread if it is the last undeleted post
threadTools.getLatestUndeletedPid(postData.tid, function(err, pid) {
if (err && err.message === 'no-undeleted-pids-found') {
@@ -192,6 +198,12 @@ var db = require('./database'),
threadTools.getLatestUndeletedPid(postData.tid, function(err, pid) {
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
topics.updateTimestamp(postData.tid, timestamp);
+
+ topics.getTopicField(postData.tid, 'cid', function(err, cid) {
+ if(!err) {
+ db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
+ }
+ });
});
});
diff --git a/src/routes/api.js b/src/routes/api.js
index 46e0069ad3..ca0955ccbe 100644
--- a/src/routes/api.js
+++ b/src/routes/api.js
@@ -39,22 +39,29 @@ var path = require('path'),
res.json(200, config);
});
- app.get('/home', function (req, res) {
+ app.get('/home', function (req, res, next) {
var uid = (req.user) ? req.user.uid : 0;
categories.getAllCategories(uid, function (err, data) {
data.categories = data.categories.filter(function (category) {
return (!category.disabled || parseInt(category.disabled, 10) === 0);
});
- function iterator(category, callback) {
- categories.getRecentReplies(category.cid, 2, function (posts) {
+ function getRecentReplies(category, callback) {
+ categories.getRecentReplies(category.cid, 2, function (err, posts) {
+ if(err) {
+ return callback(err);
+ }
category.posts = posts;
category.post_count = posts.length > 2 ? 2 : posts.length;
callback(null);
});
}
- async.each(data.categories, iterator, function (err) {
+ async.each(data.categories, getRecentReplies, function (err) {
+ if(err) {
+ return next(err);
+ }
+
data.motd_class = (parseInt(meta.config.show_motd, 10) === 1 || meta.config.show_motd === undefined) ? '' : ' none';
data.motd_class += (meta.config.motd && meta.config.motd.length > 0 ? '' : ' default');
diff --git a/src/websockets.js b/src/websockets.js
index a355ec873f..cede0b6181 100644
--- a/src/websockets.js
+++ b/src/websockets.js
@@ -672,7 +672,7 @@ websockets.init = function(io) {
});
socket.on('api:categories.getRecentReplies', function(tid) {
- categories.getRecentReplies(tid, 4, function(replies) {
+ categories.getRecentReplies(tid, 4, function(err, replies) {
socket.emit('api:categories.getRecentReplies', replies);
});
});