diff --git a/src/routes/api.js b/src/routes/api.js
index 18e1cfc21b..cbc09b983b 100644
--- a/src/routes/api.js
+++ b/src/routes/api.js
@@ -258,6 +258,20 @@ var path = require('path'),
});
});
+ app.get('/popular/:set?', function (req, res, next) {
+ var uid = (req.user) ? req.user.uid : 0;
+ var set = 'topics:' + req.params.set;
+ if(!req.params.set) {
+ set = 'topics:posts';
+ }
+ topics.getTopicsFromSet(uid, set, 0, 19, function(err, data) {
+ if(err) {
+ return next(err);
+ }
+ res.json(data);
+ });
+ });
+
app.get('/unread', function (req, res, next) {
var uid = (req.user) ? req.user.uid : 0;
topics.getUnreadTopics(uid, 0, 19, function (err, data) {
diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js
index c420b133ec..381cbc13ea 100644
--- a/src/socket.io/topics.js
+++ b/src/socket.io/topics.js
@@ -273,8 +273,20 @@ SocketTopics.loadMoreUnreadTopics = function(socket, data, callback) {
topics.getUnreadTopics(socket.uid, start, end, callback);
};
+SocketTopics.loadMoreFromSet = function(socket, data, callback) {
+ if(!data || !data.after || !data.set) {
+ return callback(new Error('invalid data'));
+ }
+
+ var start = parseInt(data.after, 10),
+ end = start + 9;
+
+ topics.getTopicsFromSet(socket.uid, data.set, start, end, callback);
+};
+
+
SocketTopics.getPageCount = function(socket, tid, callback) {
topics.getPageCount(tid, callback);
-}
+};
module.exports = SocketTopics;
\ No newline at end of file
diff --git a/src/topics.js b/src/topics.js
index b56b742f0b..147447adf9 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -415,6 +415,42 @@ var async = require('async'),
});
}
+ function getTopics(set, uid, tids, callback) {
+ var latestTopics = {
+ 'topics': []
+ };
+
+ if (!tids || !tids.length) {
+ return callback(null, latestTopics);
+ }
+
+ async.filter(tids, function(tid, next) {
+ threadTools.privileges(tid, uid, function(err, privileges) {
+ next(!err && privileges.read);
+ });
+ }, function(tids) {
+ Topics.getTopicsByTids(tids, 0, uid, function(err, topicData) {
+ if(err) {
+ return callback(err);
+ }
+
+ if(!topicData || !topicData.length) {
+ return callback(null, latestTopics);
+ }
+
+ db.sortedSetRevRank(set, topicData[topicData.length - 1].tid, function(err, rank) {
+ if(err) {
+ return calllback(err);
+ }
+
+ latestTopics.nextStart = parseInt(rank, 10) + 1;
+ latestTopics.topics = topicData;
+ callback(null, latestTopics);
+ });
+ });
+ });
+ }
+
Topics.getLatestTopics = function(current_user, start, end, term, callback) {
var timestamp = Date.now();
@@ -436,38 +472,17 @@ var async = require('async'),
return callback(err);
}
- var latestTopics = {
- 'no_topics_message': 'hidden',
- 'topics': []
- };
+ getTopics('topics:recent', current_user, tids, callback);
+ });
+ }
- if (!tids || !tids.length) {
- latestTopics.no_topics_message = 'show';
- return callback(null, latestTopics);
+ Topics.getTopicsFromSet = function(uid, set, start, end, callback) {
+ db.getSortedSetRevRange(set, start, end, function(err, tids) {
+ if(err) {
+ return callback(err);
}
- async.filter(tids, function(tid, next) {
- threadTools.privileges(tid, current_user, function(err, privileges) {
- next(!err && privileges.read);
- });
- }, function(tids) {
- Topics.getTopicsByTids(tids, 0, current_user, function(err, topicData) {
- if(err) {
- return callback(err);
- }
-
- if(!topicData || !topicData.length) {
- latestTopics.no_topics_message = 'show';
- return callback(null, latestTopics);
- }
-
- db.sortedSetRevRank('topics:recent', topicData[topicData.length - 1].tid, function(err, rank) {
- latestTopics.nextStart = parseInt(rank,10) + 1;
- latestTopics.topics = topicData;
- callback(null, latestTopics);
- });
- });
- });
+ getTopics(set, uid, tids, callback);
});
}
@@ -1075,15 +1090,30 @@ var async = require('async'),
}
Topics.increasePostCount = function(tid, callback) {
- db.incrObjectField('topic:' + tid, 'postcount', callback);
+ db.incrObjectField('topic:' + tid, 'postcount', function(err, value) {
+ if(err) {
+ return callback(err);
+ }
+ db.sortedSetAdd('topics:posts', value, tid, callback);
+ });
}
Topics.decreasePostCount = function(tid, callback) {
- db.decrObjectField('topic:' + tid, 'postcount', callback);
+ db.decrObjectField('topic:' + tid, 'postcount', function(err, value) {
+ if(err) {
+ return callback(err);
+ }
+ db.sortedSetAdd('topics:posts', value, tid, callback);
+ });
}
Topics.increaseViewCount = function(tid, callback) {
- db.incrObjectField('topic:' + tid, 'viewcount', callback);
+ db.incrObjectField('topic:' + tid, 'viewcount', function(err, value) {
+ if(err) {
+ return callback(err);
+ }
+ db.sortedSetAdd('topics:views', value, tid, callback);
+ });
}
Topics.isLocked = function(tid, callback) {
@@ -1143,6 +1173,8 @@ var async = require('async'),
Topics.delete = function(tid) {
Topics.setTopicField(tid, 'deleted', 1);
db.sortedSetRemove('topics:recent', tid);
+ db.sortedSetRemove('topics:posts', tid);
+ db.sortedSetRemove('topics:views', tid);
Topics.getTopicField(tid, 'cid', function(err, cid) {
feed.updateCategory(cid);
@@ -1152,8 +1184,10 @@ var async = require('async'),
Topics.restore = function(tid) {
Topics.setTopicField(tid, 'deleted', 0);
- Topics.getTopicField(tid, 'lastposttime', function(err, lastposttime) {
- db.sortedSetAdd('topics:recent', lastposttime, tid);
+ Topics.getTopicFields(tid, ['lastposttime', 'postcount', 'viewcount'], function(err, topicData) {
+ db.sortedSetAdd('topics:recent', topicData.lastposttime, tid);
+ db.sortedSetAdd('topics:posts', topicData.postcount, tid);
+ db.sortedSetAdd('topics:views', topicData.viewcount, tid);
});
Topics.getTopicField(tid, 'cid', function(err, cid) {
diff --git a/src/upgrade.js b/src/upgrade.js
index 1581ea1589..c3205b0579 100644
--- a/src/upgrade.js
+++ b/src/upgrade.js
@@ -19,7 +19,7 @@ var db = require('./database'),
Upgrade.check = function(callback) {
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
- var latestSchema = new Date(2014, 0, 30, 15, 0).getTime();
+ var latestSchema = new Date(2014, 0, 30, 16, 0).getTime();
db.get('schemaDate', function(err, value) {
if (parseInt(value, 10) >= latestSchema) {
@@ -429,6 +429,53 @@ Upgrade.upgrade = function(callback) {
winston.info('[2014/1/30] Fixing language settings -- skipped');
next();
}
+ },
+ function(next) {
+ function updateTopic(tid, next) {
+ Topics.getTopicFields(tid, ['postcount', 'viewcount'], function(err, topicData) {
+ if(err) {
+ next(err);
+ }
+
+ if(topicData) {
+ if(!topicData.postcount) {
+ topicData.postcount = 0;
+ }
+
+ if(!topicData.viewcount) {
+ topicData.viewcount = 0;
+ }
+
+ db.sortedSetAdd('topics:posts', topicData.postcount, tid);
+ db.sortedSetAdd('topics:views', topicData.viewcount, tid);
+ }
+
+ next();
+ });
+ }
+
+ thisSchemaDate = new Date(2014, 0, 30, 16, 0).getTime();
+ if (schemaDate < thisSchemaDate) {
+ updatesMade = true;
+
+
+
+ winston.info('[2014/1/30] Adding new topic sets');
+ db.getSortedSetRange('topics:recent', 0, -1, function(err, tids) {
+ if(err) {
+ return next(err);
+ }
+
+ async.each(tids, updateTopic, function(err) {
+ next(err);
+ });
+ });
+
+
+ } else {
+ winston.info('[2014/1/30] Adding new topic sets -- skipped');
+ next();
+ }
}
// Add new schema updates here
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 17!!!
diff --git a/src/webserver.js b/src/webserver.js
index 743512e4f2..5c3a5874dc 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -430,7 +430,7 @@ module.exports.server = server;
// Basic Routes (entirely client-side parsed, goal is to move the rest of the crap in this file into this one section)
(function () {
- var routes = ['login', 'register', 'account', 'recent', '403', '404', '500'],
+ var routes = ['login', 'register', 'account', 'recent', 'popular', '403', '404', '500'],
loginRequired = ['unread', 'notifications'];
async.each(routes.concat(loginRequired), function(route, next) {
@@ -868,6 +868,16 @@ module.exports.server = server;
});
+ app.get('/popular/:term?', function (req, res) {
+ app.build_header({
+ req: req,
+ res: res
+ }, function (err, header) {
+ res.send(header + app.create_route('popular/' + req.params.term, null, 'popular') + templates.footer);
+ });
+
+ });
+
app.get('/outgoing', function (req, res) {
if (!req.query.url) {
return res.redirect('/404');