diff --git a/public/src/forum/footer.js b/public/src/forum/footer.js index 3030bc53d4..eba06ee4cf 100644 --- a/public/src/forum/footer.js +++ b/public/src/forum/footer.js @@ -160,15 +160,11 @@ socket.on('chatMessage', function(data) { - var username = data.username; - var fromuid = data.fromuid; - var message = data.message; - var timestamp = data.timestamp; require(['chat'], function(chat) { - var chatModal = chat.createModalIfDoesntExist(username, fromuid, function(created, modal) { + var chatModal = chat.createModalIfDoesntExist(data.username, data.fromuid, function(created, modal) { if(!created) - chat.appendChatMessage(modal, message, timestamp); + chat.appendChatMessage(modal, data.message, data.timestamp); }); chatModal.show(); @@ -176,6 +172,14 @@ }); }); + socket.on('chatGoOffline', function(data) { + require(['chat'], function(chat) { + if(chat.modalOpen(data.uid)) { + var modal = chat.getModal(data.uid); + chat.appendChatMessage(modal, data.username + ' went offline\n', data.timestamp); + } + }); + }) require(['mobileMenu'], function(mobileMenu) { mobileMenu.init(); diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js index f5f9702be3..704bf16b5a 100644 --- a/public/src/modules/chat.js +++ b/public/src/modules/chat.js @@ -13,12 +13,19 @@ define(['taskbar'], function(taskbar) { chatModal.css('zIndex', topZ + 1); } + module.getModal = function(touid) { + return $('#chat-modal-' + touid); + } + + module.modalOpen = function(touid) { + return $('#chat-modal-' + touid).length !== 0; + } module.createModalIfDoesntExist = function(username, touid, callback) { - var chatModal = $('#chat-modal-'+touid); + var chatModal = $('#chat-modal-' + touid); if(!chatModal.length) { var chatModal = $('#chat-modal').clone(); - chatModal.attr('id','chat-modal-'+touid); + chatModal.attr('id','chat-modal-' + touid); var uuid = utils.generateUUID(); chatModal.attr('UUID', uuid); chatModal.appendTo($('body')); diff --git a/src/websockets.js b/src/websockets.js index 0440d9643b..c98ec013f5 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -32,7 +32,8 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), (function(io) { var users = {}, userSockets = {}, - rooms = {} + rooms = {}, + chats = {}; global.io = io; @@ -55,7 +56,14 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), io.sockets.in('global').emit('api:user.isOnline', isUserOnline(uid)); user.getUserField(uid, 'username', function(err, username) { - socket.emit('event:connect', {status: 1, username:username}); + socket.emit('event:connect', {status: 1, username:username, uid:uid}); + + if(chats[uid]) { + for(var i=0; i