v1.18.x
Julian Lam 10 years ago
parent 87111ee6be
commit 474ff3ed35

@ -293,7 +293,10 @@ app.cacheBuster = null;
} }
if (!chat.modalExists(touid)) { if (!chat.modalExists(touid)) {
chat.createModal(username, touid, loadAndCenter); chat.createModal({
username: username,
touid: touid
}, loadAndCenter);
} else { } else {
loadAndCenter(chat.getModal(touid)); loadAndCenter(chat.getModal(touid));
} }

@ -11,6 +11,9 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
chatsListEl = $('#chat-list'), chatsListEl = $('#chat-list'),
dropdownEl; dropdownEl;
// Sync open chats between all user socket sessions
module.sync();
chatsToggleEl.on('click', function() { chatsToggleEl.on('click', function() {
if (chatsToggleEl.parent().hasClass('open')) { if (chatsToggleEl.parent().hasClass('open')) {
return; return;
@ -92,7 +95,10 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
}); });
} }
} else { } else {
module.createModal(username, data.withUid, function(modal) { module.createModal({
username: username,
touid: data.withUid
}, function(modal) {
module.toggleNew(modal.attr('UUID'), true); module.toggleNew(modal.attr('UUID'), true);
if (!isSelf) { if (!isSelf) {
app.alternatingTitle('[[modules:chat.user_has_messaged_you, ' + username + ']]'); app.alternatingTitle('[[modules:chat.user_has_messaged_you, ' + username + ']]');
@ -125,6 +131,42 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
var modal = module.getModal(data.uid); var modal = module.getModal(data.uid);
app.updateUserStatus(modal.find('[component="user/status"]'), data.status); app.updateUserStatus(modal.find('[component="user/status"]'), data.status);
}); });
socket.on('query:chats.sync', function(data, callback) {
var chats = Array.prototype.map.call(taskbar.get('chat'), function(chatObj) {
return {
username: chatObj.options.title,
uid: chatObj.options.touid,
new: chatObj.element.hasClass('new')
}
});
callback(chats);
});
socket.on('event:chats.open', function(data) {
data.silent = true;
module.createModal(data);
});
socket.on('event:chats.close', function(uid) {
module.close(module.getModal(uid), true);
});
socket.on('event:chats.toggleNew', function(data) {
var uuid = module.getModal(data.uid).attr('UUID');
module.toggleNew(uuid, data.state, true);
});
$(window).on('action:taskbar.toggleNew', function(ev, uuid) {
var modal = $('.chat-modal[uuid="' + uuid + '"]'),
touid = modal.attr('touid');
socket.emit('modules.chats.toggleNew', {
uid: touid,
state: false
});
});
}; };
module.bringModalToTop = function(chatModal) { module.bringModalToTop = function(chatModal) {
@ -163,7 +205,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
}); });
} }
module.createModal = function(username, touid, callback) { module.createModal = function(data, callback) {
templates.parse('chat', {}, function(chatTpl) { templates.parse('chat', {}, function(chatTpl) {
translator.translate(chatTpl, function (chatTpl) { translator.translate(chatTpl, function (chatTpl) {
@ -171,8 +213,8 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
uuid = utils.generateUUID(), uuid = utils.generateUUID(),
dragged = false; dragged = false;
chatModal.attr('id', 'chat-modal-' + touid); chatModal.attr('id', 'chat-modal-' + data.touid);
chatModal.attr('touid', touid); chatModal.attr('touid', data.touid);
chatModal.attr('intervalId', 0); chatModal.attr('intervalId', 0);
chatModal.attr('UUID', uuid); chatModal.attr('UUID', uuid);
chatModal.css('position', 'fixed'); chatModal.css('position', 'fixed');
@ -206,7 +248,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
}); });
}); });
chatModal.find('#chat-with-name').html(username); chatModal.find('#chat-with-name').html(data.username);
chatModal.find('#chat-close-btn').on('click', function() { chatModal.find('#chat-close-btn').on('click', function() {
module.close(chatModal); module.close(chatModal);
@ -218,7 +260,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
components.get('chat/input').val(text); components.get('chat/input').val(text);
}); });
ajaxify.go('chats/' + utils.slugify(username)); ajaxify.go('chats/' + utils.slugify(data.username));
module.close(chatModal); module.close(chatModal);
} }
@ -243,7 +285,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
chatModal.on('mousemove keypress click', function() { chatModal.on('mousemove keypress click', function() {
if (newMessage) { if (newMessage) {
socket.emit('modules.chats.markRead', touid); socket.emit('modules.chats.markRead', data.touid);
newMessage = false; newMessage = false;
} }
}); });
@ -262,25 +304,31 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
checkStatus(chatModal); checkStatus(chatModal);
}); });
module.canMessage(touid, function(err) { module.canMessage(data.touid, function(err) {
if (err) { if (err) {
// Disable the text input // Disable the text input
chatModal.find('input[type="text"]').attr('disabled', true); chatModal.find('input[type="text"]').attr('disabled', true);
} }
}); });
chatModal.find('.user-typing .text').translateText('[[modules:chat.user_typing, ' + username + ']]'); chatModal.find('.user-typing .text').translateText('[[modules:chat.user_typing, ' + data.username + ']]');
taskbar.push('chat', chatModal.attr('UUID'), { taskbar.push('chat', chatModal.attr('UUID'), {
title: username, title: data.username,
touid: touid, touid: data.touid,
icon: 'fa-comment', icon: 'fa-comment',
state: '' state: ''
}); });
if (!data.silent) {
socket.emit('modules.chats.open', data);
}
$(window).trigger('action:chat.loaded', chatModal); $(window).trigger('action:chat.loaded', chatModal);
callback(chatModal); if (typeof callback === 'function') {
callback(chatModal);
}
}); });
}); });
}; };
@ -289,7 +337,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
chatModal.find('#chat-message-input').focus(); chatModal.find('#chat-message-input').focus();
}; };
module.close = function(chatModal) { module.close = function(chatModal, silent) {
clearInterval(chatModal.attr('intervalId')); clearInterval(chatModal.attr('intervalId'));
chatModal.attr('intervalId', 0); chatModal.attr('intervalId', 0);
chatModal.remove(); chatModal.remove();
@ -297,6 +345,10 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
taskbar.discard('chat', chatModal.attr('UUID')); taskbar.discard('chat', chatModal.attr('UUID'));
Chats.notifyTyping(chatModal.attr('touid'), false); Chats.notifyTyping(chatModal.attr('touid'), false);
if (!silent) {
socket.emit('modules.chats.close', chatModal.attr('touid'));
}
if (chatModal.attr('data-mobile')) { if (chatModal.attr('data-mobile')) {
module.disableMobileBehaviour(chatModal); module.disableMobileBehaviour(chatModal);
} }
@ -422,13 +474,30 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
}); });
}; };
module.toggleNew = function(uuid, state) { module.toggleNew = taskbar.toggleNew;
taskbar.toggleNew(uuid, state);
};
module.canMessage = function(toUid, callback) { module.canMessage = function(toUid, callback) {
socket.emit('modules.chats.canMessage', toUid, callback); socket.emit('modules.chats.canMessage', toUid, callback);
}; };
module.sync = function() {
socket.emit('modules.chats.sync', function(err, users) {
for(var x=0,numUsers=users.length,user;x<numUsers;x++) {
user = users[x];
if (!module.modalExists(user.uid)) {
module.createModal({
username: user.username,
touid: user.uid,
silent: true
}, function(modal) {
if (user.new) {
module.toggleNew(modal.attr('UUID'), true, true);
}
});
}
}
});
};
return module; return module;
}); });

@ -60,14 +60,27 @@ define('taskbar', function() {
} }
}; };
taskbar.get = function(module) {
var items = $('[data-module="' + module + '"]').map(function(idx, el) {
return $(el).data();
});
return items;
};
taskbar.minimize = function(module, uuid) { taskbar.minimize = function(module, uuid) {
var btnEl = taskbar.tasklist.find('[data-module="' + module + '"][data-uuid="' + uuid + '"]'); var btnEl = taskbar.tasklist.find('[data-module="' + module + '"][data-uuid="' + uuid + '"]');
btnEl.removeClass('active'); btnEl.removeClass('active');
}; };
taskbar.toggleNew = function(uuid, state) { taskbar.toggleNew = function(uuid, state, silent) {
console.log('TOGGLING');
var btnEl = taskbar.tasklist.find('[data-uuid="' + uuid + '"]'); var btnEl = taskbar.tasklist.find('[data-uuid="' + uuid + '"]');
btnEl.toggleClass('new', state); btnEl.toggleClass('new', state);
if (!silent) {
$(window).trigger('action:taskbar.toggleNew', uuid);
}
}; };
taskbar.updateActive = function(uuid) { taskbar.updateActive = function(uuid) {
@ -119,6 +132,8 @@ define('taskbar', function() {
update(); update();
data.element = taskbarEl; data.element = taskbarEl;
taskbarEl.data(data);
$(window).trigger('action:taskbar.pushed', data); $(window).trigger('action:taskbar.pushed', data);
} }

@ -4,7 +4,10 @@ var meta = require('../meta'),
Messaging = require('../messaging'), Messaging = require('../messaging'),
utils = require('../../public/src/utils'), utils = require('../../public/src/utils'),
async = require('async'),
server = require('./'), server = require('./'),
rooms = require('./rooms'),
SocketModules = { SocketModules = {
chats: {}, chats: {},
@ -94,6 +97,37 @@ SocketModules.chats.getRecentChats = function(socket, data, callback) {
Messaging.getRecentChats(socket.uid, start, stop, callback); Messaging.getRecentChats(socket.uid, start, stop, callback);
}; };
SocketModules.chats.sync = function(socket, data, callback) {
var chats = [],
uids = [],
socketIds = rooms.clients('uid_' + socket.uid);
rooms.broadcast(socket, 'uid_' + socket.uid, 'query:chats.sync', {}, function(err, sessionData) {
sessionData.forEach(function(data) {
data.forEach(function(chat) {
if (uids.indexOf(chat.uid) === -1) {
chats.push(chat);
uids.push(chat.uid);
}
});
});
callback(err, chats);
});
};
SocketModules.chats.open = function(socket, data, callback) {
rooms.broadcast(socket, 'uid_' + socket.uid, 'event:chats.open', data);
};
SocketModules.chats.close = function(socket, data, callback) {
rooms.broadcast(socket, 'uid_' + socket.uid, 'event:chats.close', data);
};
SocketModules.chats.toggleNew = function(socket, data, callback) {
rooms.broadcast(socket, 'uid_' + socket.uid, 'event:chats.toggleNew', data);
};
/* Sounds */ /* Sounds */
SocketModules.sounds.getSounds = function(socket, data, callback) { SocketModules.sounds.getSounds = function(socket, data, callback) {

@ -9,7 +9,8 @@
// Once they are closed switch to .clients() and async calls // Once they are closed switch to .clients() and async calls
var pubsub = require('../pubsub'); var pubsub = require('../pubsub'),
async = require('async');
var rooms = {}; var rooms = {};
@ -32,6 +33,29 @@ rooms.leaveAll = function(socket, roomsToLeave) {
}); });
}; };
rooms.broadcast = function(socket, room, msg, data, callback) {
var io = require('./'),
socketIds = rooms.clients(room);
callback = callback || function() {};
async.map(socketIds, function(id, next) {
var timeout;
if (socket.id === id) {
return setImmediate(next, null, []);
}
timeout = setTimeout(function() {
next(null, []);
}, 500);
io.server.sockets.connected[id].emit(msg, data || {}, function(chats) {
clearTimeout(timeout);
next(null, chats);
});
}, callback);
};
pubsub.on('socket:join', onSocketJoin); pubsub.on('socket:join', onSocketJoin);
pubsub.on('socket:leave', onSocketLeave); pubsub.on('socket:leave', onSocketLeave);

Loading…
Cancel
Save