Skip to content

Node.js based forum software built for the modern web

Add an index for going from a post to its replies

Ben Lubarfae28ed3ccb4
diff --git a/src/posts.js b/src/posts.js
index 975846859b..0abba7e5a7 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -260,4 +260,29 @@ var plugins = require('./plugins');
});
};
+ Posts.countReplies = function(pid, callback) {
+ if (Array.isArray(pid)) {
+ db.sortedSetsCard(pid.map(function (id) {
+ return 'pid:' + id + ':replies';
+ }), callback);
+ } else {
+ db.sortedSetCard('pid:' + pid + ':replies', callback);
+ }
+ };
+
+ Posts.getReplyPids = function(pid, callback) {
+ db.getSortedSetRange('pid:' + pid + ':replies', 0, -1, callback);
+ };
+
+ Posts.getReplyPosts = function(pid, uid, callback) {
+ async.waterfall([
+ function (next) {
+ Posts.getReplyPids(pid, next);
+ },
+ function (pids, next) {
+ Posts.getPostsByPids(pids, uid, next);
+ }
+ ], callback);
+ };
+
}(exports));
diff --git a/src/posts/create.js b/src/posts/create.js
index 1054c6d694..1c24280f25 100644
--- a/src/posts/create.js
+++ b/src/posts/create.js
@@ -82,6 +82,12 @@ module.exports = function (Posts) {
function (next) {
db.sortedSetAdd('posts:pid', timestamp, postData.pid, next);
},
+ function (next) {
+ if (!postData.toPid) {
+ return next(null);
+ }
+ db.sortedSetAdd('pid:' + postData.toPid + ':replies', timestamp, postData.pid, next);
+ },
function (next) {
db.incrObjectField('global', 'postCount', next);
}
diff --git a/src/posts/delete.js b/src/posts/delete.js
index bdaae4a70c..a6b042e768 100644
--- a/src/posts/delete.js
+++ b/src/posts/delete.js
@@ -137,6 +137,17 @@ module.exports = function (Posts) {
function (next) {
deletePostFromUsersVotes(pid, next);
},
+ function(next) {
+ Posts.getPostField(pid, 'toPid', function (err, toPid) {
+ if (err) {
+ return next(err);
+ }
+ if (!parseInt(toPid, 10)) {
+ return next(null);
+ }
+ db.sortedSetRemove('pid:' + toPid + ':replies', pid, next);
+ });
+ },
function (next) {
db.sortedSetsRemove(['posts:pid', 'posts:flagged'], pid, next);
},
diff --git a/src/upgrade.js b/src/upgrade.js
index 03c776dce3..d3547aa280 100644
--- a/src/upgrade.js
+++ b/src/upgrade.js
@@ -10,7 +10,7 @@ var db = require('./database'),
schemaDate, thisSchemaDate,
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
- latestSchema = Date.UTC(2016, 9, 8);
+ latestSchema = Date.UTC(2016, 9, 14);
Upgrade.check = function (callback) {
db.get('schemaDate', function (err, value) {
@@ -907,6 +907,41 @@ Upgrade.upgrade = function (callback) {
winston.info('[2016/10/8] favourite -> bookmark refactor - skipped!');
next();
}
+ },
+ function(next) {
+ thisSchemaDate = Date.UTC(2016, 9, 14);
+
+ if (schemaDate < thisSchemaDate) {
+ updatesMade = true;
+ winston.info('[2016/10/14] Creating sorted sets for post replies');
+
+ var posts = require('./posts');
+ var batch = require('./batch');
+ batch.processSortedSet('posts:pid', function(ids, next) {
+ posts.getPostsFields(ids, ['pid', 'toPid', 'timestamp'], function(err, data) {
+ if (err) {
+ return next(err);
+ }
+
+ async.each(data, function(postData, next) {
+ if (!parseInt(post.toPid, 10)) {
+ return next(null);
+ }
+ db.sortedSetAdd('pid:' + postData.toPid + ':replies', postData.timestamp, postData.pid, next);
+ }, next);
+ });
+ }, function(err) {
+ if (err) {
+ return next(err);
+ }
+
+ winston.info('[2016/10/14] Creating sorted sets for post replies - done');
+ Upgrade.update(thisSchemaDate, next);
+ });
+ } else {
+ winston.info('[2016/10/14] Creating sorted sets for post replies - skipped!');
+ next();
+ }
}
// Add new schema updates here
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 24!!!