Skip to content

Node.js based forum software built for the modern web

fixed #3359

Julian Lam474ff3ed358a
diff --git a/public/src/app.js b/public/src/app.js
index 3254c7a51f..a14a403da5 100644
--- a/public/src/app.js
+++ b/public/src/app.js
@@ -293,7 +293,10 @@ app.cacheBuster = null;
}
if (!chat.modalExists(touid)) {
- chat.createModal(username, touid, loadAndCenter);
+ chat.createModal({
+ username: username,
+ touid: touid
+ }, loadAndCenter);
} else {
loadAndCenter(chat.getModal(touid));
}
diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js
index b27b932eec..9ccc434abf 100644
--- a/public/src/modules/chat.js
+++ b/public/src/modules/chat.js
@@ -11,6 +11,9 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
chatsListEl = $('#chat-list'),
dropdownEl;
+ // Sync open chats between all user socket sessions
+ module.sync();
+
chatsToggleEl.on('click', function() {
if (chatsToggleEl.parent().hasClass('open')) {
return;
@@ -92,7 +95,10 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
});
}
} else {
- module.createModal(username, data.withUid, function(modal) {
+ module.createModal({
+ username: username,
+ touid: data.withUid
+ }, function(modal) {
module.toggleNew(modal.attr('UUID'), true);
if (!isSelf) {
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);
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) {
@@ -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) {
translator.translate(chatTpl, function (chatTpl) {
@@ -171,8 +213,8 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
uuid = utils.generateUUID(),
dragged = false;
- chatModal.attr('id', 'chat-modal-' + touid);
- chatModal.attr('touid', touid);
+ chatModal.attr('id', 'chat-modal-' + data.touid);
+ chatModal.attr('touid', data.touid);
chatModal.attr('intervalId', 0);
chatModal.attr('UUID', uuid);
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() {
module.close(chatModal);
@@ -218,7 +260,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
components.get('chat/input').val(text);
});
- ajaxify.go('chats/' + utils.slugify(username));
+ ajaxify.go('chats/' + utils.slugify(data.username));
module.close(chatModal);
}
@@ -243,7 +285,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
chatModal.on('mousemove keypress click', function() {
if (newMessage) {
- socket.emit('modules.chats.markRead', touid);
+ socket.emit('modules.chats.markRead', data.touid);
newMessage = false;
}
});
@@ -262,25 +304,31 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
checkStatus(chatModal);
});
- module.canMessage(touid, function(err) {
+ module.canMessage(data.touid, function(err) {
if (err) {
// Disable the text input
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'), {
- title: username,
- touid: touid,
+ title: data.username,
+ touid: data.touid,
icon: 'fa-comment',
state: ''
});
+ if (!data.silent) {
+ socket.emit('modules.chats.open', data);
+ }
+
$(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();
};
- module.close = function(chatModal) {
+ module.close = function(chatModal, silent) {
clearInterval(chatModal.attr('intervalId'));
chatModal.attr('intervalId', 0);
chatModal.remove();
@@ -297,6 +345,10 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
taskbar.discard('chat', chatModal.attr('UUID'));
Chats.notifyTyping(chatModal.attr('touid'), false);
+ if (!silent) {
+ socket.emit('modules.chats.close', chatModal.attr('touid'));
+ }
+
if (chatModal.attr('data-mobile')) {
module.disableMobileBehaviour(chatModal);
}
@@ -422,13 +474,30 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
});
};
- module.toggleNew = function(uuid, state) {
- taskbar.toggleNew(uuid, state);
- };
+ module.toggleNew = taskbar.toggleNew;
module.canMessage = function(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;
});
diff --git a/public/src/modules/taskbar.js b/public/src/modules/taskbar.js
index f1757fe553..58d3629c83 100644
--- a/public/src/modules/taskbar.js
+++ b/public/src/modules/taskbar.js
@@ -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) {
var btnEl = taskbar.tasklist.find('[data-module="' + module + '"][data-uuid="' + uuid + '"]');
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 + '"]');
btnEl.toggleClass('new', state);
+
+ if (!silent) {
+ $(window).trigger('action:taskbar.toggleNew', uuid);
+ }
};
taskbar.updateActive = function(uuid) {
@@ -119,6 +132,8 @@ define('taskbar', function() {
update();
data.element = taskbarEl;
+
+ taskbarEl.data(data);
$(window).trigger('action:taskbar.pushed', data);
}
diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js
index fbdf2b0100..95380a4b49 100644
--- a/src/socket.io/modules.js
+++ b/src/socket.io/modules.js
@@ -4,7 +4,10 @@ var meta = require('../meta'),
Messaging = require('../messaging'),
utils = require('../../public/src/utils'),
+ async = require('async'),
+
server = require('./'),
+ rooms = require('./rooms'),
SocketModules = {
chats: {},
@@ -94,6 +97,37 @@ SocketModules.chats.getRecentChats = function(socket, data, 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 */
SocketModules.sounds.getSounds = function(socket, data, callback) {
diff --git a/src/socket.io/rooms.js b/src/socket.io/rooms.js
index e392abf3f9..c32f9a5dc9 100644
--- a/src/socket.io/rooms.js
+++ b/src/socket.io/rooms.js
@@ -9,7 +9,8 @@
// Once they are closed switch to .clients() and async calls
-var pubsub = require('../pubsub');
+var pubsub = require('../pubsub'),
+ async = require('async');
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:leave', onSocketLeave);