Skip to content

Node.js based forum software built for the modern web

closes #742, closes #741

Baris Soner Usaklia4ae9c70dfec
diff --git a/src/categories.js b/src/categories.js
index 788c8b9427..8a6726f3bd 100644
--- a/src/categories.js
+++ b/src/categories.js
@@ -334,7 +334,7 @@ var db = require('./database.js'),
Categories.isUserActiveIn = function(cid, uid, callback) {
- db.getListRange('uid:' + uid + ':posts', 0, -1, function(err, pids) {
+ db.getSortedSetRange('uid:' + uid + ':posts', 0, -1, function(err, pids) {
if (err) {
return callback(err, null);
}
diff --git a/src/postTools.js b/src/postTools.js
index 2fa9d45485..0f8dd73f89 100644
--- a/src/postTools.js
+++ b/src/postTools.js
@@ -17,12 +17,12 @@ var winston = require('winston'),
(function(PostTools) {
PostTools.isMain = function(pid, tid, callback) {
- db.getListRange('tid:' + tid + ':posts', 0, 0, function(err, pids) {
+ db.getSortedSetRange('tid:' + tid + ':posts', 0, 0, function(err, pids) {
if(err) {
return callback(err);
}
- callback(null, pids[0] === pid);
+ callback(null, parseInt(pids[0], 10) === parseInt(pid, 10));
});
}
diff --git a/src/posts.js b/src/posts.js
index 7b15c9d4ec..6eda559aa5 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -96,7 +96,7 @@ var db = require('./database'),
};
Posts.getPostsByTid = function(tid, start, end, callback) {
- db.getListRange('tid:' + tid + ':posts', start, end, function(err, pids) {
+ db.getSortedSetRange('tid:' + tid + ':posts', start, end, function(err, pids) {
if(err) {
return callback(err);
}
@@ -325,7 +325,7 @@ var db = require('./database'),
postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString();
postData.relativeEditTime = parseInt(postData.edited, 10) !== 0 ? (new Date(parseInt(postData.edited, 10)).toISOString()) : '';
} catch(e) {
- winston.err('invalid time value');
+ require('winston').err('invalid time value');
}
postTools.parse(postData.content, function(err, content) {
diff --git a/src/threadTools.js b/src/threadTools.js
index b69c42fa7c..6d4388fcb0 100644
--- a/src/threadTools.js
+++ b/src/threadTools.js
@@ -292,7 +292,10 @@ var winston = require('winston'),
}
ThreadTools.getLatestUndeletedPid = function(tid, callback) {
- db.getListRange('tid:' + tid + ':posts', 0, -1, function(err, pids) {
+ db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, function(err, pids) {
+ if(err) {
+ return callback(err);
+ }
if (pids.length === 0) {
return callback(new Error('no-undeleted-pids-found'));
}
diff --git a/src/topics.js b/src/topics.js
index cb141ccdf4..85747dd4af 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -3,6 +3,7 @@ var async = require('async'),
nconf = require('nconf'),
validator = require('validator'),
S = require('string'),
+ winston = require('winston'),
db = require('./database'),
posts = require('./posts'),
@@ -53,7 +54,7 @@ var async = require('async'),
}
db.searchIndex('topic', title, tid);
- user.addTopicIdToUser(uid, tid);
+ user.addTopicIdToUser(uid, tid, timestamp);
// in future it may be possible to add topics to several categories, so leaving the door open here.
db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid);
@@ -111,6 +112,7 @@ var async = require('async'),
}
Topics.reply(tid, uid, content, function(err, postData) {
+
if(err) {
return callback(err, null);
} else if(!postData) {
@@ -259,26 +261,26 @@ var async = require('async'),
return callback(new Error('Topic doesn\'t exist'));
}
- posts.getPostField(pid, 'tid', function(err, oldTid) {
+ posts.getPostFields(pid, ['tid', 'timestamp'], function(err, postData) {
if(err) {
return callback(err);
}
- if(!oldTid) {
+ if(!postData) {
return callback(new Error('Post doesn\'t exist'));
}
- Topics.removePostFromTopic(oldTid, pid, function(err) {
+ Topics.removePostFromTopic(postData.tid, pid, function(err) {
if(err) {
return callback(err);
}
- Topics.decreasePostCount(oldTid);
+ Topics.decreasePostCount(postData.tid);
posts.setPostField(pid, 'tid', tid);
Topics.increasePostCount(tid);
- Topics.addPostToTopic(tid, pid, callback);
+ Topics.addPostToTopic(tid, pid, postData.timestamp, callback);
});
});
});
@@ -1041,19 +1043,19 @@ var async = require('async'),
Topics.onNewPostMade = function(tid, pid, timestamp, callback) {
Topics.increasePostCount(tid);
Topics.updateTimestamp(tid, timestamp);
- Topics.addPostToTopic(tid, pid, callback);
+ Topics.addPostToTopic(tid, pid, timestamp, callback);
}
- Topics.addPostToTopic = function(tid, pid, callback) {
- db.listAppend('tid:' + tid + ':posts', pid, callback);
+ Topics.addPostToTopic = function(tid, pid, timestamp, callback) {
+ db.sortedSetAdd('tid:' + tid + ':posts', timestamp, pid, callback);
}
Topics.removePostFromTopic = function(tid, pid, callback) {
- db.listRemoveAll('tid:' + tid + ':posts', pid, callback);
+ db.sortedSetRemove('tid:' + tid + ':posts', pid, callback);
}
Topics.getPids = function(tid, callback) {
- db.getListRange('tid:' + tid + ':posts', 0, -1, callback);
+ db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, callback);
}
Topics.getUids = function(tid, callback) {
diff --git a/src/upgrade.js b/src/upgrade.js
index de2ac7aa80..36f887ec30 100644
--- a/src/upgrade.js
+++ b/src/upgrade.js
@@ -6,6 +6,7 @@ var db = require('./database'),
User = require('./user'),
Topics = require('./topics'),
+ Posts = require('./posts'),
Utils = require('../public/src/utils'),
Upgrade = {},
@@ -14,7 +15,7 @@ var db = require('./database'),
Upgrade.check = function(callback) {
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
- var latestSchema = new Date(2014, 0, 5, 14, 5).getTime();
+ var latestSchema = new Date(2014, 0, 7).getTime();
db.get('schemaDate', function(err, value) {
if (parseInt(value, 10) >= latestSchema) {
@@ -250,6 +251,132 @@ Upgrade.upgrade = function(callback) {
winston.info('[2014/1/5] Re-slugify usernames (again) skipped');
next();
}
+ },
+ function(next) {
+ function upgradeUserPostsTopics(next) {
+
+ function upgradeUser(uid, next) {
+
+ function upgradeUserPosts(next) {
+
+ function addPostToUser(pid) {
+ Posts.getPostField(pid, 'timestamp', function(err, timestamp) {
+ db.sortedSetAdd('uid:' + uid + ':posts', timestamp, pid);
+ });
+ }
+
+ db.getListRange('uid:' + uid + ':posts', 0, -1, function(err, pids) {
+ if(err) {
+ return next(err);
+ }
+
+ if(!pids || !pids.length) {
+ return next();
+ }
+
+ db.delete('uid:' + uid + ':posts', function(err) {
+ for(var i = 0; i< pids.length; ++i) {
+ addPostToUser(pids[i]);
+ }
+ next();
+ });
+ });
+ }
+
+ function upgradeUserTopics(next) {
+
+ function addTopicToUser(tid) {
+ Topics.getTopicField(tid, 'timestamp', function(err, timestamp) {
+ db.sortedSetAdd('uid:' + uid + ':topics', timestamp, tid);
+ });
+ }
+
+ db.getListRange('uid:' + uid + ':topics', 0, -1, function(err, tids) {
+ if(err) {
+ return next(err);
+ }
+
+ if(!tids || !tids.length) {
+ return next();
+ }
+
+ db.delete('uid:' + uid + ':topics', function(err) {
+ for(var i = 0; i< tids.length; ++i) {
+ addTopicToUser(tids[i]);
+ }
+ next();
+ });
+ });
+ }
+
+ async.series([upgradeUserPosts, upgradeUserTopics], function(err, result) {
+ next(err);
+ });
+ }
+
+
+ db.getSortedSetRange('users:joindate', 0, -1, function(err, uids) {
+ if(err) {
+ return next(err);
+ }
+
+ async.each(uids, upgradeUser, function(err, result) {
+ next(err);
+ });
+ });
+ }
+
+ function upgradeTopicPosts(next) {
+ function upgradeTopic(tid, next) {
+ function addPostToTopic(pid) {
+ Posts.getPostField(pid, 'timestamp', function(err, timestamp) {
+ db.sortedSetAdd('tid:' + tid + ':posts', timestamp, pid);
+ });
+ }
+
+ db.getListRange('tid:' + tid + ':posts', 0, -1, function(err, pids) {
+ if(err) {
+ return next(err);
+ }
+
+ if(!pids || !pids.length) {
+ return next();
+ }
+
+ db.delete('tid:' + tid + ':posts', function(err) {
+ for(var i = 0; i< pids.length; ++i) {
+ addPostToTopic(pids[i]);
+ }
+ next();
+ });
+ });
+ }
+
+ db.getSetMembers('topics:tid', function(err, tids) {
+ async.each(tids, upgradeTopic, function(err, results) {
+ next(err);
+ });
+ });
+ }
+
+ thisSchemaDate = new Date(2014, 0, 7).getTime();
+ if (schemaDate < thisSchemaDate) {
+ updatesMade = true;
+
+ async.series([upgradeUserPostsTopics, upgradeTopicPosts], function(err, results) {
+ if(err) {
+ winston.err('Error upgrading '+ err.message);
+ return next(err);
+ }
+
+ winston.info('[2014/1/7] Updated topic and user posts to sorted set');
+ next();
+ });
+
+ } else {
+ winston.info('[2014/1/7] Update to topic and user posts to sorted set skipped');
+ next();
+ }
}
// Add new schema updates here
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 17!!!
diff --git a/src/user.js b/src/user.js
index 5914107add..2cabdf8180 100644
--- a/src/user.js
+++ b/src/user.js
@@ -454,7 +454,7 @@ var bcrypt = require('bcrypt'),
};
User.onNewPostMade = function(uid, tid, pid, timestamp) {
- User.addPostIdToUser(uid, pid);
+ User.addPostIdToUser(uid, pid, timestamp);
User.incrementUserFieldBy(uid, 'postcount', 1, function(err, newpostcount) {
db.sortedSetAdd('users:postcount', newpostcount, uid);
@@ -463,16 +463,16 @@ var bcrypt = require('bcrypt'),
User.setUserField(uid, 'lastposttime', timestamp);
};
- User.addPostIdToUser = function(uid, pid) {
- db.listPrepend('uid:' + uid + ':posts', pid);
+ User.addPostIdToUser = function(uid, pid, timestamp) {
+ db.sortedSetAdd('uid:' + uid + ':posts', timestamp, pid);
};
- User.addTopicIdToUser = function(uid, tid) {
- db.listPrepend('uid:' + uid + ':topics', tid);
+ User.addTopicIdToUser = function(uid, tid, timestamp) {
+ db.sortedSetAdd('uid:' + uid + ':topics', timestamp, tid);
};
User.getPostIds = function(uid, start, stop, callback) {
- db.getListRange('uid:' + uid + ':posts', start, stop, function(err, pids) {
+ db.getSortedSetRevRange('uid:' + uid + ':posts', start, stop, function(err, pids) {
if(err) {
return callback(err);
}
diff --git a/src/websockets.js b/src/websockets.js
index 38aa771277..b908f459a4 100644
--- a/src/websockets.js
+++ b/src/websockets.js
@@ -905,8 +905,8 @@ websockets.init = function(io) {
callback({
titleEditable: isMain
});
- })
- })
+ });
+ });
});
socket.on('api:post.privileges', function(pid) {