updateUnreadCount

v1.18.x
barisusakli 11 years ago
parent 22577917da
commit 32257c9b2f

@ -24,6 +24,22 @@ define('forum/footer', ['notifications', 'chat'], function(Notifications, Chat)
.attr('data-content', count > 20 ? '20+' : count); .attr('data-content', count > 20 ? '20+' : count);
} }
function onNewPost(data) {
if (data && data.posts && data.posts.length) {
var post = data.posts[0];
if (parseInt(post.uid, 10) !== parseInt(app.uid, 10)) {
increaseUnreadCount();
}
}
}
function increaseUnreadCount() {
var count = parseInt($('#unread-count').attr('data-content'), 10) + 1;
updateUnreadTopicCount(null, count);
}
socket.on('event:new_post', onNewPost);
socket.on('event:unread.updateCount', updateUnreadTopicCount); socket.on('event:unread.updateCount', updateUnreadTopicCount);
socket.emit('user.getUnreadCount', updateUnreadTopicCount); socket.emit('user.getUnreadCount', updateUnreadTopicCount);

@ -51,14 +51,13 @@ SocketPosts.reply = function(socket, data, callback) {
return; return;
} }
for(var i=0; i<uids.length; ++i) { for(var i=0; i<uids.length; ++i) {
if (uids[i] !== socket.uid) { if (parseInt(uids[i], 10) !== socket.uid) {
websockets.in('uid_' + uids[i]).emit('event:new_post', result); websockets.in('uid_' + uids[i]).emit('event:new_post', result);
} }
} }
}); });
websockets.emitTopicPostStats(); websockets.emitTopicPostStats();
topics.pushUnreadCount();
}); });
}; };

@ -47,7 +47,7 @@ SocketTopics.post = function(socket, data, callback) {
return; return;
} }
for(var i=0; i<uids.length; ++i) { for(var i=0; i<uids.length; ++i) {
if (uids[i] !== socket.uid) { if (parseInt(uids[i], 10) !== socket.uid) {
websockets.in('uid_' + uids[i]).emit('event:new_post', result.postData); websockets.in('uid_' + uids[i]).emit('event:new_post', result.postData);
websockets.in('uid_' + uids[i]).emit('event:new_topic', result.topicData); websockets.in('uid_' + uids[i]).emit('event:new_topic', result.topicData);
} }
@ -55,7 +55,6 @@ SocketTopics.post = function(socket, data, callback) {
}); });
websockets.emitTopicPostStats(); websockets.emitTopicPostStats();
topics.pushUnreadCount();
}); });
}; };
@ -188,7 +187,7 @@ SocketTopics.markAsUnreadForAll = function(socket, tids, callback) {
if(err) { if(err) {
return next(err); return next(err);
} }
topics.pushUnreadCount(); topics.pushUnreadCount(socket.uid);
next(); next();
}); });
}); });

@ -156,38 +156,18 @@ module.exports = function(Topics) {
}); });
} }
Topics.pushUnreadCount = function(uids, callback) { Topics.pushUnreadCount = function(uid, callback) {
if (typeof callback === 'function') { callback = callback || function() {}:
return callback(null);
} else {
return null;
}
var websockets = require('./../socket.io'); if (!uid || parseInt(uid, 10) === 0) {
return callback();
if (!uids) {
uids = websockets.getConnectedClients();
} else if (!Array.isArray(uids)) {
uids = [uids];
} }
uids = uids.filter(function(value) {
return parseInt(value, 10) !== 0;
});
async.eachLimit(uids, 5, function(uid, next) {
Topics.getTotalUnread(uid, function(err, count) { Topics.getTotalUnread(uid, function(err, count) {
websockets.in('uid_' + uid).emit('event:unread.updateCount', null, count);
next();
});
}, function(err) {
if (err) { if (err) {
winston.error(err.message); return callback(err);
} }
require('../socket.io').in('uid_' + uid).emit('event:unread.updateCount', null, count);
if (callback) {
callback(); callback();
}
}); });
}; };

@ -135,8 +135,8 @@ var
User.updateLastOnlineTime = function(uid, callback) { User.updateLastOnlineTime = function(uid, callback) {
callback = callback || function() {}; callback = callback || function() {};
User.getUserField(uid, 'status', function(err, status) { User.getUserFields(uid, ['status', 'lastonline'], function(err, userData) {
if(err || status === 'offline') { if(err || userData.status === 'offline' || Date.now() - parseInt(userData.lastonline, 10) < 300000) {
return callback(err); return callback(err);
} }

Loading…
Cancel
Save