Node.js based forum software built for the modern web
if user is in the topic when a new post comes in mark the topic read
Baris Soner Usakli039f02456dbe
diff --git a/public/src/forum/footer.js b/public/src/forum/footer.jsindex 1bcdac57bf..abee2958ac 100644--- a/public/src/forum/footer.js+++ b/public/src/forum/footer.js@@ -8,11 +8,30 @@ define(['notifications', 'chat'], function(Notifications, Chat) {Chat.prepareDOM();translator.prepareDOM();- function updateUnreadCount(err, count) {+ function markCurrentTopicRead(tid) {+ if(tids && tids.length > 0 && tids.indexOf(tid) !== -1) {+ socket.emit('topics.markAsRead', {tid: tid, uid: app.uid});+ return;+ }+ }++ function updateUnreadCount(err, tids) {+ var count = 0;+ if(tids && tids.length) {+ count = tids.length;+ }++ var postContainer = $('#post-container');+ if(postContainer.length) {+ markCurrentTopicRead(postContainer.attr('data-tid'));+ return;+ }+$('#unread-count').toggleClass('unread-count', count > 0);$('#unread-count').attr('data-content', count > 20 ? '20+' : count);}+socket.on('event:unread.updateCount', updateUnreadCount);socket.emit('user.getUnreadCount', updateUnreadCount);});\ No newline at end of filediff --git a/public/src/forum/topic.js b/public/src/forum/topic.jsindex 1725bc8347..ceba1026f8 100644--- a/public/src/forum/topic.js+++ b/public/src/forum/topic.js@@ -539,7 +539,6 @@ define(['composer'], function(composer) {'posts.favourite']);-socket.on('get_users_in_room', function(data) {if(data && data.room.indexOf('topic') !== -1) {diff --git a/src/socket.io/topics.js b/src/socket.io/topics.jsindex 8dd19299ed..9b1c8936cd 100644--- a/src/socket.io/topics.js+++ b/src/socket.io/topics.js@@ -71,13 +71,23 @@ SocketTopics.postcount = function(socket, tid, callback) {topics.getTopicField(tid, 'postcount', callback);};+SocketTopics.markAsRead = function(socket, data) {+ if(!data || !data.tid || !data.uid) {+ return;+ }++ topics.markAsRead(data.tid, data.uid, function(err) {+ topics.pushUnreadCount(data.uid);+ });+}+SocketTopics.markAllRead = function(socket, data, callback) {topics.markAllRead(socket.uid, function(err) {if(err) {return callback(err);}- index.server.sockets.in('uid_' + socket.uid).emit('event:unread.updateCount', null, 0);+ index.server.sockets.in('uid_' + socket.uid).emit('event:unread.updateCount', null, []);callback(null);});diff --git a/src/socket.io/user.js b/src/socket.io/user.jsindex ea608750e4..957ec2c5d5 100644--- a/src/socket.io/user.js+++ b/src/socket.io/user.js@@ -141,9 +141,7 @@ SocketUser.getOnlineAnonCount = function(socket, data, callback) {};SocketUser.getUnreadCount = function(socket, data, callback) {- topics.getUnreadTids(socket.uid, 0, 19, function(err, tids) {- callback(err, tids?tids.length:0);- });+ topics.getUnreadTids(socket.uid, 0, 19, callback);};SocketUser.getActiveUsers = function(socket, data, callback) {diff --git a/src/topics.js b/src/topics.jsindex 7b11c4f972..ba88cef81b 100644--- a/src/topics.js+++ b/src/topics.js@@ -184,8 +184,9 @@ var async = require('async'),return callback(err, null);}- Topics.markAsRead(tid, uid);- Topics.pushUnreadCount();+ Topics.markAsRead(tid, uid, function(err) {+ Topics.pushUnreadCount(null);+ });});});});@@ -633,9 +634,13 @@ var async = require('async'),uids = [uids];}+ uids = uids.filter(function(value) {+ return parseInt(value, 10) !== 0;+ });+async.each(uids, function(uid, next) {Topics.getUnreadTids(uid, 0, 19, function(err, tids) {- websockets.in('uid_' + uid).emit('event:unread.updateCount', null, tids.length);+ websockets.in('uid_' + uid).emit('event:unread.updateCount', null, tids);next();});}, function(err) {@@ -764,8 +769,9 @@ var async = require('async'),// "quiet" is used for things like RSS feed updating, HTML parsing for non-js users, etcif (!quiet) {- Topics.markAsRead(tid, current_user);- Topics.pushUnreadCount(current_user);+ Topics.markAsRead(tid, current_user, function(err) {+ Topics.pushUnreadCount(current_user);+ });Topics.increaseViewCount(tid);}@@ -916,13 +922,15 @@ var async = require('async'),return callback(err);}- if (tids && tids.length) {- for (var i = 0; i < tids.length; ++i) {- Topics.markAsRead(tids[i], uid);- }+ if(!tids || !tids.length) {+ return callback(null);}- callback(null);+ function markRead(tid, next) {+ Topics.markAsRead(tid, uid, next);+ }++ async.each(tids, markRead, callback);});}@@ -938,9 +946,13 @@ var async = require('async'),db.delete('tid:' + tid + ':read_by_uid', callback);}- Topics.markAsRead = function(tid, uid) {+ Topics.markAsRead = function(tid, uid, callback) {- db.setAdd('tid:' + tid + ':read_by_uid', uid);+ db.setAdd('tid:' + tid + ':read_by_uid', uid, function(err) {+ if(callback) {+ callback(err);+ }+ });Topics.getTopicField(tid, 'cid', function(err, cid) {