mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-07-05 17:48:50 +02:00
changed chat online offline messages
This commit is contained in:
@@ -54,17 +54,19 @@
|
||||
|
||||
var onlineStatus = $('.account-online-status');
|
||||
|
||||
socket.on('api:user.isOnline', function(online) {
|
||||
if(online) {
|
||||
function handleUserOnline(data) {
|
||||
if(data.online) {
|
||||
onlineStatus.find('span span').text('online');
|
||||
onlineStatus.find('i').attr('class', 'icon-circle');
|
||||
} else {
|
||||
onlineStatus.find('span span').text('offline');
|
||||
onlineStatus.find('i').attr('class', 'icon-circle-blank');
|
||||
}
|
||||
});
|
||||
|
||||
socket.emit('api:user.isOnline', theirid);
|
||||
}
|
||||
|
||||
socket.on('api:user.isOnline', handleUserOnline);
|
||||
|
||||
socket.emit('api:user.isOnline', theirid, handleUserOnline);
|
||||
|
||||
});
|
||||
|
||||
|
||||
@@ -162,25 +162,18 @@
|
||||
socket.on('chatMessage', function(data) {
|
||||
|
||||
require(['chat'], function(chat) {
|
||||
var chatModal = chat.createModalIfDoesntExist(data.username, data.fromuid, function(created, modal) {
|
||||
if(!created)
|
||||
chat.appendChatMessage(modal, data.message, data.timestamp);
|
||||
});
|
||||
var modal = null;
|
||||
if(chat.modalExists(data.fromuid)) {
|
||||
modal = chat.getModal(data.fromuid);
|
||||
chat.appendChatMessage(modal, data.message, data.timestamp);
|
||||
} else {
|
||||
modal = chat.createModal(data.username, data.fromuid);
|
||||
}
|
||||
|
||||
chatModal.show();
|
||||
chat.bringModalToTop(chatModal);
|
||||
chat.load(modal.attr('UUID'));
|
||||
});
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
@@ -288,9 +288,13 @@
|
||||
return;
|
||||
|
||||
require(['chat'], function(chat) {
|
||||
var chatModal = chat.createModalIfDoesntExist(username, touid);
|
||||
chatModal.modal();
|
||||
chat.bringModalToTop(chatModal); // I don't think this is necessary
|
||||
var chatModal;
|
||||
if(!chat.modalExists(touid)) {
|
||||
chatModal = chat.createModal(username, touid);
|
||||
} else {
|
||||
chatModal = chat.getModal(touid);
|
||||
}
|
||||
chat.load(chatModal.attr('UUID'));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -17,46 +17,74 @@ define(['taskbar'], function(taskbar) {
|
||||
return $('#chat-modal-' + touid);
|
||||
}
|
||||
|
||||
module.modalOpen = function(touid) {
|
||||
module.modalExists = function(touid) {
|
||||
return $('#chat-modal-' + touid).length !== 0;
|
||||
}
|
||||
module.createModalIfDoesntExist = function(username, touid, callback) {
|
||||
var chatModal = $('#chat-modal-' + touid);
|
||||
|
||||
if(!chatModal.length) {
|
||||
var chatModal = $('#chat-modal').clone();
|
||||
chatModal.attr('id','chat-modal-' + touid);
|
||||
var uuid = utils.generateUUID();
|
||||
chatModal.attr('UUID', uuid);
|
||||
chatModal.appendTo($('body'));
|
||||
chatModal.draggable({
|
||||
start:function(){
|
||||
module.bringModalToTop(chatModal);
|
||||
|
||||
function checkStatus(chatModal, callback) {
|
||||
socket.emit('api:user.isOnline', chatModal.touid, function(data) {
|
||||
if(data.online !== chatModal.online) {
|
||||
if(data.online) {
|
||||
module.appendChatMessage(chatModal, chatModal.username + ' has come online.\n', data.timestamp);
|
||||
} else {
|
||||
module.appendChatMessage(chatModal, chatModal.username + ' has gone offline.\n', data.timestamp);
|
||||
}
|
||||
});
|
||||
chatModal.find('#chat-with-name').html(username);
|
||||
|
||||
chatModal.find('.close').on('click', function(e) {
|
||||
chatModal.hide();
|
||||
taskbar.discard('chat', uuid);
|
||||
});
|
||||
|
||||
chatModal.on('click', function(e) {
|
||||
module.bringModalToTop(chatModal);
|
||||
});
|
||||
|
||||
addSendHandler(chatModal, touid);
|
||||
|
||||
getChatMessages(chatModal, touid, callback);
|
||||
|
||||
taskbar.push('chat', chatModal.attr('UUID'), {title:'chat with '+username});
|
||||
return chatModal;
|
||||
chatModal.online = data.online;
|
||||
}
|
||||
if(callback)
|
||||
callback(data.online);
|
||||
});
|
||||
}
|
||||
|
||||
function checkOnlineStatus(chatModal) {
|
||||
if(chatModal.intervalId === 0) {
|
||||
chatModal.intervalId = setInterval(function(){
|
||||
checkStatus(chatModal);
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
module.createModal = function(username, touid, callback) {
|
||||
|
||||
var chatModal = $('#chat-modal').clone(),
|
||||
uuid = utils.generateUUID();
|
||||
|
||||
chatModal.intervalId = 0;
|
||||
chatModal.touid = touid;
|
||||
chatModal.username = username;
|
||||
|
||||
if(callback)
|
||||
callback(false, chatModal);
|
||||
chatModal.attr('id', 'chat-modal-' + touid);
|
||||
chatModal.attr('UUID', uuid);
|
||||
chatModal.appendTo($('body'));
|
||||
chatModal.draggable({
|
||||
start:function() {
|
||||
module.bringModalToTop(chatModal);
|
||||
}
|
||||
});
|
||||
|
||||
chatModal.find('#chat-with-name').html(username);
|
||||
|
||||
taskbar.push('chat', chatModal.attr('UUID'), {title:'chat with '+username});
|
||||
chatModal.find('.close').on('click', function(e) {
|
||||
clearInterval(chatModal.intervalId);
|
||||
chatModal.intervalId = 0;
|
||||
chatModal.hide();
|
||||
taskbar.discard('chat', uuid);
|
||||
});
|
||||
|
||||
chatModal.on('click', function(e) {
|
||||
module.bringModalToTop(chatModal);
|
||||
});
|
||||
|
||||
addSendHandler(chatModal);
|
||||
|
||||
checkStatus(chatModal, function(online) {
|
||||
chatModal.online = online;
|
||||
getChatMessages(chatModal, function() {
|
||||
checkOnlineStatus(chatModal);
|
||||
});
|
||||
});
|
||||
|
||||
taskbar.push('chat', chatModal.attr('UUID'), {title:'chat with ' + username});
|
||||
return chatModal;
|
||||
}
|
||||
|
||||
@@ -64,45 +92,46 @@ define(['taskbar'], function(taskbar) {
|
||||
var chatModal = $('div[UUID="'+uuid+'"]');
|
||||
chatModal.show();
|
||||
module.bringModalToTop(chatModal);
|
||||
checkOnlineStatus(chatModal);
|
||||
}
|
||||
|
||||
module.minimize = function(uuid) {
|
||||
var chatModal = $('div[UUID="'+uuid+'"]');
|
||||
chatModal.hide();
|
||||
taskbar.minimize('chat', uuid);
|
||||
clearInterval(chatModal.intervalId);
|
||||
chatModal.intervalId = 0;
|
||||
}
|
||||
|
||||
function getChatMessages(chatModal, touid, callback) {
|
||||
socket.emit('getChatMessages', {touid:touid}, function(messages) {
|
||||
function getChatMessages(chatModal, callback) {
|
||||
socket.emit('getChatMessages', {touid:chatModal.touid}, function(messages) {
|
||||
for(var i = 0; i<messages.length; ++i) {
|
||||
module.appendChatMessage(chatModal, messages[i].content, messages[i].timestamp);
|
||||
}
|
||||
|
||||
if(callback)
|
||||
callback(true, chatModal);
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
function addSendHandler(chatModal, touid) {
|
||||
function addSendHandler(chatModal) {
|
||||
chatModal.find('#chat-message-input').off('keypress');
|
||||
chatModal.find('#chat-message-input').on('keypress', function(e) {
|
||||
if(e.which === 13) {
|
||||
sendMessage(chatModal, touid);
|
||||
sendMessage(chatModal);
|
||||
}
|
||||
});
|
||||
|
||||
chatModal.find('#chat-message-send-btn').off('click');
|
||||
chatModal.find('#chat-message-send-btn').on('click', function(e){
|
||||
sendMessage(chatModal, touid);
|
||||
sendMessage(chatModal);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
function sendMessage(chatModal, touid) {
|
||||
function sendMessage(chatModal) {
|
||||
var msg = app.strip_tags(chatModal.find('#chat-message-input').val());
|
||||
if(msg.length) {
|
||||
msg = msg +'\n';
|
||||
socket.emit('sendChatMessage', { touid:touid, message:msg});
|
||||
socket.emit('sendChatMessage', { touid:chatModal.touid, message:msg});
|
||||
chatModal.find('#chat-message-input').val('');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
</div><!-- /.modal-dialog -->
|
||||
</div><!-- /.modal -->
|
||||
|
||||
<div id="chat-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="Chat" aria-hidden="true">
|
||||
<div id="chat-modal" class="modal" tabindex="-1" role="dialog" aria-labelledby="Chat" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
|
||||
@@ -32,8 +32,7 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
|
||||
(function(io) {
|
||||
var users = {},
|
||||
userSockets = {},
|
||||
rooms = {},
|
||||
chats = {};
|
||||
rooms = {};
|
||||
|
||||
global.io = io;
|
||||
|
||||
@@ -57,13 +56,6 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
|
||||
|
||||
user.getUserField(uid, 'username', function(err, username) {
|
||||
socket.emit('event:connect', {status: 1, username:username, uid:uid});
|
||||
|
||||
if(chats[uid]) {
|
||||
for(var i=0; i<chats[uid].length; ++i) {
|
||||
io.sockets.in(chats[uid][i]).emit('chatMessage', {fromuid:uid, username:username, message: username+' came online\n', timestamp: Date.now()});
|
||||
socket.join(chats[uid][i]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -82,18 +74,6 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
|
||||
delete users[sessionID];
|
||||
if(uid) {
|
||||
io.sockets.in('global').emit('api:user.isOnline', isUserOnline(uid));
|
||||
|
||||
user.getUserField(uid, 'username', function(err, username) {
|
||||
|
||||
if(chats[uid] && chats[uid].length) {
|
||||
|
||||
for(var i=0; i<chats[uid].length; ++i) {
|
||||
|
||||
io.sockets.in(chats[uid][i]).emit('chatGoOffline', {uid:uid, username:username, timestamp:Date.now()});
|
||||
socket.leave(chats[uid][i]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,8 +249,8 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
|
||||
socket.emit('api:user.get_online_users', returnData);
|
||||
});
|
||||
|
||||
socket.on('api:user.isOnline', function(uid) {
|
||||
socket.emit('api:user.isOnline', isUserOnline(uid));
|
||||
socket.on('api:user.isOnline', function(uid, callback) {
|
||||
callback({online:isUserOnline(uid), timestamp:Date.now()});
|
||||
});
|
||||
|
||||
socket.on('api:user.changePassword', function(data, callback) {
|
||||
@@ -548,9 +528,6 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
|
||||
|
||||
var msg = utils.strip_tags(data.message);
|
||||
|
||||
var uids = [uid, touid].sort();
|
||||
var chatroom = 'chatroom_'+uids[0]+'_'+uids[1];
|
||||
|
||||
user.getUserField(uid, 'username', function(err, username) {
|
||||
var finalMessage = username + ': ' + msg,
|
||||
notifText = 'New message from <strong>' + username + '</strong>';
|
||||
@@ -570,13 +547,8 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
|
||||
numSockets = userSockets[touid].length;
|
||||
|
||||
for(var x=0; x<numSockets; ++x) {
|
||||
userSockets[touid][x].join(chatroom);
|
||||
userSockets[touid][x].emit('chatMessage', {fromuid:uid, username:username, message: finalMessage, timestamp: Date.now()});
|
||||
}
|
||||
|
||||
chats[touid] = chats[touid] || [];
|
||||
if(chats[touid].indexOf(chatroom) === -1)
|
||||
chats[touid].push(chatroom);
|
||||
}
|
||||
|
||||
if(userSockets[uid]) {
|
||||
@@ -584,13 +556,8 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
|
||||
numSockets = userSockets[uid].length;
|
||||
|
||||
for(var x=0; x<numSockets; ++x) {
|
||||
userSockets[uid][x].join(chatroom);
|
||||
userSockets[uid][x].emit('chatMessage', {fromuid:touid, username:username, message:'You : ' + msg, timestamp: Date.now()});
|
||||
}
|
||||
|
||||
chats[uid] = chats[uid] || [];
|
||||
if(chats[uid].indexOf(chatroom) === -1)
|
||||
chats[uid].push(chatroom);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user