This commit is contained in:
barisusakli
2014-10-25 19:19:47 -04:00
parent ca9cc6b32f
commit 4c90c9a759
4 changed files with 55 additions and 92 deletions

View File

@@ -18,9 +18,7 @@ var posts = require('../posts'),
nconf = require('nconf'),
SocketModules = {
composer: {
replyHash: {}
},
composer: {},
chats: {},
notifications: {},
sounds: {},
@@ -29,27 +27,6 @@ var posts = require('../posts'),
/* Posts Composer */
var stopTracking = function(replyObj) {
if (isLast(replyObj.uid, replyObj.tid)) {
server.in('topic_' + replyObj.tid).emit('event:topic.toggleReply', {uid: replyObj.uid, isReplying: false});
}
clearInterval(replyObj.timer);
delete SocketModules.composer.replyHash[replyObj.uuid];
},
isLast = function(uid, tid) {
return _.filter(SocketModules.composer.replyHash, function(replyObj, uuid) {
if (
parseInt(replyObj.tid, 10) === parseInt(tid, 10) &&
parseInt(replyObj.uid, 10) === parseInt(uid, 10)
) {
return true;
} else {
return false;
}
}).length === 1;
};
SocketModules.composer.push = function(socket, pid, callback) {
posts.getPostFields(pid, ['content', 'tid'], function(err, postData) {
if(err || (!postData && !postData.content)) {
@@ -115,48 +92,12 @@ SocketModules.composer.renderHelp = function(socket, data, callback) {
});
};
SocketModules.composer.register = function(socket, data) {
var now = Date.now();
server.in('topic_' + data.tid).emit('event:topic.toggleReply', {uid: data.uid, isReplying: true});
data.socket = socket;
data.lastPing = now;
data.lastAnswer = now;
data.timer = setInterval(function() {
if (data.lastPing === data.lastAnswer) {
// Ping the socket to see if the composer is still active
data.lastPing = Date.now();
socket.emit('event:composer.ping', data.uuid);
} else {
stopTracking(data);
}
}, 1000*5); // Every 5 seconds...
SocketModules.composer.replyHash[data.uuid] = data;
SocketModules.composer.notifyTyping = function(socket, data) {
server.in('topic_' + data.tid).emit('event:topic.notifyTyping', data);
};
SocketModules.composer.unregister = function(socket, uuid) {
var replyObj = SocketModules.composer.replyHash[uuid];
if (uuid && replyObj) {
stopTracking(replyObj);
}
};
SocketModules.composer.pingActive = function(socket, uuid) {
var data = SocketModules.composer.replyHash[uuid];
if (data) {
data.lastAnswer = data.lastPing;
}
};
SocketModules.composer.getUsersByTid = function(socket, tid, callback) {
// Return uids with active composers
callback(null, _.filter(SocketModules.composer.replyHash, function(replyObj, uuid) {
return parseInt(replyObj.tid, 10) === parseInt(tid, 10);
}).map(function(replyObj) {
return replyObj.uid;
}));
SocketModules.composer.stopNotifyTyping = function(socket, data) {
server.in('topic_' + data.tid).emit('event:topic.stopNotifyTyping', data);
};
/* Chat */