From 746fa93c800f444e0ddd5fc5399050e0668cdbab Mon Sep 17 00:00:00 2001 From: psychobunny Date: Wed, 21 Aug 2013 23:34:35 +0800 Subject: [PATCH] added API call for total unread topics; moved unread notification parsing to client side also fixed a bug where the new notification icon glow would disappear on page refresh even if there were existing notifications. --- public/src/ajaxify.js | 4 +--- public/src/utils.js | 14 +++++++++----- src/meta.js | 4 ++-- src/routes/api.js | 7 +++++++ src/topics.js | 9 +++++++++ src/websockets.js | 4 ++-- 6 files changed, 30 insertions(+), 12 deletions(-) diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js index e1c2eea572..52b233691e 100644 --- a/public/src/ajaxify.js +++ b/public/src/ajaxify.js @@ -79,9 +79,7 @@ var ajaxify = {}; }, url, template); - socket.emit('api:meta.buildTitle', url, function(title) { - document.title = title; - }); + utils.refreshTitle(url); return true; } diff --git a/public/src/utils.js b/public/src/utils.js index 5305fe2305..db25a91d0a 100644 --- a/public/src/utils.js +++ b/public/src/utils.js @@ -126,12 +126,16 @@ return tags; }, - refreshTitle: function() { - var a = document.createElement('a'); + refreshTitle: function(url) { + if (!url) { + var a = document.createElement('a'); + a.href = document.location; + url = a.pathname.slice(1); + } - a.href = document.location; - socket.emit('api:meta.buildTitle', a.pathname.slice(1), function(title) { - document.title = title; + socket.emit('api:meta.buildTitle', url, function(title, numNotifications) { + document.title = (numNotifications > 0 ? '(' + numNotifications + ') ' : '') + title; + if (numNotifications > 0) document.querySelector('.notifications a i').className = 'icon-circle active'; }); } } diff --git a/src/meta.js b/src/meta.js index 4f0788efc5..e12eb4ed9a 100644 --- a/src/meta.js +++ b/src/meta.js @@ -81,9 +81,9 @@ var utils = require('./../public/src/utils.js'), var title; if (err) title = global.config.title || 'NodeBB'; - else title = (values.notifCount > 0 ? '(' + values.notifCount + ') ' : '') + (values.title ? values.title + ' | ' : '') + (global.config.title || 'NodeBB'); + else title = (values.title ? values.title + ' | ' : '') + (global.config.title || 'NodeBB'); - callback(null, title); + callback(null, title, values.notifCount); }); }, parseFragment: function(urlFragment, callback) { diff --git a/src/routes/api.js b/src/routes/api.js index 95bed93bcd..e3eee6e534 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -137,6 +137,13 @@ var user = require('./../user.js'), }); }); + app.get('/api/unread/total', function(req, res) { + var uid = (req.user) ? req.user.uid : 0; + topics.getTotalUnread(uid, function(data) { + res.json(data); + }); + }); + app.get('/api/confirm/:id', function(req, res) { user.email.confirm(req.params.id, function(data) { if (data.status === 'ok') { diff --git a/src/topics.js b/src/topics.js index 8ab3604734..cc464a6a1a 100644 --- a/src/topics.js +++ b/src/topics.js @@ -127,6 +127,15 @@ marked.setOptions({ }); } + Topics.getTotalUnread = function(uid, callback) { + RDB.zcount('topics:recent', '-inf', '+inf', function(err, count) { + if (err) count = 0; + console.log(count); + + callback(count); + }); + }; + Topics.getUnreadTopics = function(uid, start, stop, callback) { var unreadTopics = { diff --git a/src/websockets.js b/src/websockets.js index 28716421a2..542c20135f 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -739,8 +739,8 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), }); socket.on('api:meta.buildTitle', function(text, callback) { - meta.title.build(text, uid, function(err, title) { - callback(title); + meta.title.build(text, uid, function(err, title, numNotifications) { + callback(title, numNotifications); }); }); });