From 4e59b850736910ee98629a54369799464fa53589 Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Fri, 11 Oct 2013 14:48:14 -0400 Subject: [PATCH] recent page, for hour, day, week, month --- public/src/forum/recent.js | 23 ++++++++++++++++++++++- public/templates/config.json | 2 ++ public/templates/recent.tpl | 9 +++++++++ src/routes/api.js | 4 ++-- src/topics.js | 15 +++++++++++++-- src/websockets.js | 2 +- 6 files changed, 49 insertions(+), 6 deletions(-) diff --git a/public/src/forum/recent.js b/public/src/forum/recent.js index 5fed07107e..a29b183631 100644 --- a/public/src/forum/recent.js +++ b/public/src/forum/recent.js @@ -5,6 +5,8 @@ define(function() { Recent.newPostCount = 0; Recent.loadingMoreTopics = false; + var active = ''; + Recent.init = function() { app.enter_room('recent_posts'); @@ -13,6 +15,24 @@ define(function() { 'event:new_post' ]); + + function getActiveSection() { + var url = window.location.href, + parts = url.split('/'), + active = parts[parts.length - 1]; + return active; + } + + active = getActiveSection(); + + jQuery('.nav-pills li').removeClass('active'); + jQuery('.nav-pills li a').each(function() { + if (this.getAttribute('href').match(active)) { + jQuery(this.parentNode).addClass('active'); + return false; + } + }); + $('#new-topics-alert').on('click', function() { $(this).hide(); }); @@ -75,7 +95,8 @@ define(function() { Recent.loadMoreTopics = function() { Recent.loadingMoreTopics = true; socket.emit('api:topics.loadMoreRecentTopics', { - after: $('#topics-container').children().length + after: $('#topics-container').children().length, + term: active }, function(data) { if (data.topics && data.topics.length) { Recent.onTopicsLoaded(data.topics); diff --git a/public/templates/config.json b/public/templates/config.json index f41ecdaf19..7e0be8d93d 100644 --- a/public/templates/config.json +++ b/public/templates/config.json @@ -26,6 +26,8 @@ "^user.*favourites": "favourites", "^user/.*": "account", + "^recent/.*": "recent", + "reset/.*": "reset_code" }, "force_refresh": { diff --git a/public/templates/recent.tpl b/public/templates/recent.tpl index 8cf6127874..25f6cd1c34 100644 --- a/public/templates/recent.tpl +++ b/public/templates/recent.tpl @@ -4,6 +4,15 @@
+ + +
+
diff --git a/src/routes/api.js b/src/routes/api.js index d201a31c93..845c95e049 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -131,9 +131,9 @@ var user = require('./../user.js'), }, req.params.id, uid); }); - app.get('/recent', function (req, res) { + app.get('/recent/:term?', function (req, res) { var uid = (req.user) ? req.user.uid : 0; - topics.getLatestTopics(uid, 0, 19, function (data) { + topics.getLatestTopics(uid, 0, 19, req.params.term, function (data) { res.json(data); }); }); diff --git a/src/topics.js b/src/topics.js index 2e88ebf16c..5e16f791aa 100644 --- a/src/topics.js +++ b/src/topics.js @@ -94,11 +94,22 @@ schema = require('./schema.js'), }); } - Topics.getLatestTopics = function(current_user, start, end, callback) { + Topics.getLatestTopics = function(current_user, start, end, term, callback) { var timestamp = Date.now(); - var args = ['topics:recent', '+inf', timestamp - 86400000, 'LIMIT', start, end - start + 1]; + var terms = { + hour: 3600000, + day: 86400000, + week: 604800000, + month: 18144000000 + }; + + var since = terms['day']; + if(terms[term]) + since = terms[term]; + + var args = ['topics:recent', '+inf', timestamp - since, 'LIMIT', start, end - start + 1]; RDB.zrevrangebyscore(args, function(err, tids) { diff --git a/src/websockets.js b/src/websockets.js index 8fae538380..d88d7b4e08 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -783,7 +783,7 @@ module.exports.init = function(io) { var start = data.after, end = start + 9; - topics.getLatestTopics(uid, start, end, function(latestTopics) { + topics.getLatestTopics(uid, start, end, data.term, function(latestTopics) { callback(latestTopics); }); });