From 29d8b9c18120bda48c8a22ccb04a2ee52547c4c9 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 16 Dec 2015 10:09:00 +0200 Subject: [PATCH] more room changes app.openChat --- public/src/app.js | 13 ++---- public/src/client/chats.js | 89 +++++++++++++++++++------------------- public/src/modules/chat.js | 40 ++++++++--------- src/socket.io/modules.js | 10 ++--- 4 files changed, 73 insertions(+), 79 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index a66f0c4151..10e192ba63 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -239,11 +239,7 @@ app.cacheBuster = null; } }; - app.openChat = function (username, touid) { - if (username === app.user.username) { - return app.alertError('[[error:cant-chat-with-yourself]]'); - } - + app.openChat = function (roomId) { if (!app.user.uid) { return app.alertError('[[error:not-logged-in]]'); } @@ -255,13 +251,12 @@ app.cacheBuster = null; chat.focusInput(chatModal); } - if (!chat.modalExists(touid)) { + if (!chat.modalExists(roomId)) { chat.createModal({ - username: username, - touid: touid + roomId: roomId }, loadAndCenter); } else { - loadAndCenter(chat.getModal(touid)); + loadAndCenter(chat.getModal(roomId)); } }); }; diff --git a/public/src/client/chats.js b/public/src/client/chats.js index 9bfc6aeabe..2eb7c17380 100644 --- a/public/src/client/chats.js +++ b/public/src/client/chats.js @@ -28,8 +28,7 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', Chats.initialised = true; - if (ajaxify.data.hasOwnProperty('meta') && ajaxify.data.meta.hasOwnProperty('uid')) { - // This is an active chat, focus on the input box + if (ajaxify.data.hasOwnProperty('roomId')) { components.get('chat/input').focus(); } }; @@ -47,20 +46,20 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', Chats.switchChat($(this).attr('data-roomid')); }); - Chats.addSendHandlers(Chats.getRecipientUid(), $('.chat-input'), $('.expanded-chat button[data-action="send"]')); + Chats.addSendHandlers(ajaxify.data.roomId, $('.chat-input'), $('.expanded-chat button[data-action="send"]')); $('[data-action="pop-out"]').on('click', function() { - var username = $('.expanded-chat').attr('data-username'), - uid = Chats.getRecipientUid(), - text = components.get('chat/input').val(); + + var text = components.get('chat/input').val(); + var roomId = ajaxify.data.roomId; if (app.previousUrl && app.previousUrl.match(/chats/)) { ajaxify.go('chats', function() { - app.openChat(username, uid); + app.openChat(roomId); }, true); } else { window.history.go(-1); - app.openChat(username, uid); + app.openChat(roomId); } $(window).one('action:chat.loaded', function() { @@ -87,7 +86,7 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', } }); - Chats.addSinceHandler(Chats.getRecipientUid(), $('.expanded-chat .chat-content'), $('.expanded-chat [data-since]')); + Chats.addSinceHandler(ajaxify.data.roomId, $('.expanded-chat .chat-content'), $('.expanded-chat [data-since]')); }; Chats.addHotkeys = function() { @@ -121,6 +120,9 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', Chats.prepEdit = function(inputEl, messageId) { socket.emit('modules.chats.getRaw', { mid: messageId }, function(err, raw) { + if (err) { + return app.alertError(err.message); + } // Populate the input field with the raw message content if (inputEl.val().length === 0) { // By setting the `data-mid` attribute, I tell the chat code that I am editing a @@ -134,39 +136,40 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', Chats.delete = function(messageId) { translator.translate('[[modules:chat.delete_message_confirm]]', function(translated) { bootbox.confirm(translated, function(ok) { - if (ok) { - socket.emit('modules.chats.delete', { - messageId: messageId - }, function(err) { - if (err) { - return app.alertError(err.message); - } + if (!ok) { + return; + } - // Remove message from list - components.get('chat/message', messageId).slideUp('slow', function() { - $(this).remove(); - }); + socket.emit('modules.chats.delete', { + messageId: messageId + }, function(err) { + if (err) { + return app.alertError(err.message); + } + + components.get('chat/message', messageId).slideUp('slow', function() { + $(this).remove(); }); - } + }); }); }); }; - Chats.addSinceHandler = function(toUid, chatContentEl, sinceEl) { + Chats.addSinceHandler = function(roomId, chatContentEl, sinceEl) { sinceEl.on('click', function() { var since = $(this).attr('data-since'); sinceEl.removeClass('selected'); $(this).addClass('selected'); - Chats.loadChatSince(toUid, chatContentEl, since); + Chats.loadChatSince(roomId, chatContentEl, since); return false; }); }; - Chats.addSendHandlers = function(toUid, inputEl, sendEl) { + Chats.addSendHandlers = function(roomId, inputEl, sendEl) { inputEl.off('keypress').on('keypress', function(e) { if (e.which === 13 && !e.shiftKey) { - Chats.sendMessage(toUid, inputEl); + Chats.sendMessage(roomId, inputEl); return false; } }); @@ -177,12 +180,12 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', return; } - Chats.notifyTyping(toUid, val); + Chats.notifyTyping(roomId, val); $(this).attr('data-typing', val); }); sendEl.off('click').on('click', function() { - Chats.sendMessage(toUid, inputEl); + Chats.sendMessage(roomId, inputEl); inputEl.focus(); return false; }); @@ -192,11 +195,11 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', ajaxify.go('chats/' + roomid); }; - Chats.loadChatSince = function(toUid, chatContentEl, since) { - if (!toUid) { + Chats.loadChatSince = function(roomId, chatContentEl, since) { + if (!roomId) { return; } - socket.emit('modules.chats.get', {touid: toUid, since: since}, function(err, messages) { + socket.emit('modules.chats.get', {roomId: roomId, since: since}, function(err, messages) { if (err) { return app.alertError(err.message); } @@ -210,12 +213,9 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', Chats.addGlobalEventListeners = function() { $(window).on('resize', Chats.resizeMainWindow); $(window).on('mousemove keypress click', function() { - if (newMessage) { - var recipientUid = Chats.getRecipientUid(); - if (recipientUid) { - socket.emit('modules.chats.markRead', recipientUid); - newMessage = false; - } + if (newMessage && ajaxify.data.roomId) { + socket.emit('modules.chats.markRead', ajaxify.data.roomId); + newMessage = false; } }); }; @@ -325,14 +325,14 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', Chats.setActive(); }; - Chats.notifyTyping = function(toUid, typing) { + Chats.notifyTyping = function(roomId, typing) { socket.emit('modules.chats.user' + (typing ? 'Start' : 'Stop') + 'Typing', { - touid: toUid, + roomId: roomId, fromUid: app.user.uid }); }; - Chats.sendMessage = function(toUid, inputEl) { + Chats.sendMessage = function(roomId, inputEl) { var msg = inputEl.val(), mid = inputEl.attr('data-mid'); @@ -349,7 +349,7 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', if (!mid) { socket.emit('modules.chats.send', { - touid: toUid, + roomId: roomId, message: msg }, function(err) { if (err) { @@ -360,7 +360,7 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', } sounds.play('chat-outgoing'); - Chats.notifyTyping(toUid, false); + Chats.notifyTyping(roomId, false); }); } else { socket.emit('modules.chats.edit', { @@ -371,7 +371,7 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', return app.alertError(err.message); } - Chats.notifyTyping(toUid, false); + Chats.notifyTyping(roomId, false); }); } }; @@ -385,9 +385,8 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', }; Chats.setActive = function() { - var recipientUid = Chats.getRecipientUid(); - if (recipientUid) { - socket.emit('modules.chats.markRead', recipientUid); + if (ajaxify.data.roomId) { + socket.emit('modules.chats.markRead', ajaxify.data.roomId); $('.expanded-chat input').focus(); } $('.chats-list li').removeClass('bg-primary'); diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js index b294107955..8ccc475741 100644 --- a/public/src/modules/chat.js +++ b/public/src/modules/chat.js @@ -129,9 +129,9 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra dropdownEl.click(function() { if (!ajaxify.currentPage.match(/^chats\//)) { - app.openChat(userObj.username, userObj.uid); + app.openChat(roomObj.roomId); } else { - ajaxify.go('chats/' + utils.slugify(userObj.username)); + ajaxify.go('chats/' + roomObj.roomId); } }); }); @@ -156,12 +156,12 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra chatModal.css('zIndex', topZ + 1); }; - module.getModal = function(touid) { - return $('#chat-modal-' + touid); + module.getModal = function(roomId) { + return $('#chat-modal-' + roomId); }; - module.modalExists = function(touid) { - return $('#chat-modal-' + touid).length !== 0; + module.modalExists = function(roomId) { + return $('#chat-modal-' + roomId).length !== 0; }; function checkStatus(chatModal) { @@ -182,8 +182,8 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra uuid = utils.generateUUID(), dragged = false; - chatModal.attr('id', 'chat-modal-' + data.touid); - chatModal.attr('touid', data.touid); + chatModal.attr('id', 'chat-modal-' + data.roomId); + chatModal.attr('roomId', data.roomId); chatModal.attr('intervalId', 0); chatModal.attr('UUID', uuid); chatModal.css('position', 'fixed'); @@ -254,7 +254,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra chatModal.on('mousemove keypress click', function() { if (newMessage) { - socket.emit('modules.chats.markRead', data.touid); + socket.emit('modules.chats.markRead', data.roomId); newMessage = false; } }); @@ -270,15 +270,15 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra Chats.delete(messageId); }); - Chats.addSinceHandler(chatModal.attr('touid'), chatModal.find('.chat-content'), chatModal.find('[data-since]')); + Chats.addSinceHandler(chatModal.attr('roomId'), chatModal.find('.chat-content'), chatModal.find('[data-since]')); - Chats.addSendHandlers(chatModal.attr('touid'), chatModal.find('#chat-message-input'), chatModal.find('#chat-message-send-btn')); + Chats.addSendHandlers(chatModal.attr('roomId'), chatModal.find('#chat-message-input'), chatModal.find('#chat-message-send-btn')); - Chats.loadChatSince(chatModal.attr('touid'), chatModal.find('.chat-content'), 'recent'); + Chats.loadChatSince(chatModal.attr('roomId'), chatModal.find('.chat-content'), 'recent'); checkStatus(chatModal); - module.canMessage(data.touid, function(err) { + module.canMessage(data.roomId, function(err) { if (err) { // Disable the text input chatModal.find('input[type="text"]').attr('disabled', true); @@ -287,7 +287,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra taskbar.push('chat', chatModal.attr('UUID'), { title: data.username, - touid: data.touid, + roomId: data.roomId, icon: 'fa-comment', state: '' }); @@ -311,7 +311,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra chatModal.remove(); chatModal.data('modal', null); taskbar.discard('chat', chatModal.attr('UUID')); - Chats.notifyTyping(chatModal.attr('touid'), false); + Chats.notifyTyping(chatModal.attr('roomId'), false); if (chatModal.attr('data-mobile')) { module.disableMobileBehaviour(chatModal); @@ -334,14 +334,14 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra }; module.load = function(uuid) { - var chatModal = $('div[UUID="'+uuid+'"]'); + var chatModal = $('div[UUID="' + uuid + '"]'); chatModal.removeClass('hide'); checkStatus(chatModal); taskbar.updateActive(uuid); Chats.scrollToBottom(chatModal.find('.chat-content')); module.bringModalToTop(chatModal); module.focusInput(chatModal); - socket.emit('modules.chats.markRead', chatModal.attr('touid')); + socket.emit('modules.chats.markRead', chatModal.attr('roomId')); var env = utils.findBootstrapEnvironment(); if (env === 'xs' || env === 'sm') { @@ -380,13 +380,13 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra taskbar.minimize('chat', uuid); clearInterval(chatModal.attr('intervalId')); chatModal.attr('intervalId', 0); - Chats.notifyTyping(chatModal.attr('touid'), false); + Chats.notifyTyping(chatModal.attr('roomId'), false); }; module.toggleNew = taskbar.toggleNew; - module.canMessage = function(toUid, callback) { - socket.emit('modules.chats.canMessage', toUid, callback); + module.canMessage = function(roomId, callback) { + socket.emit('modules.chats.canMessage', roomId, callback); }; diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js index 0d1d82b34c..e1c1d78b8f 100644 --- a/src/socket.io/modules.js +++ b/src/socket.io/modules.js @@ -17,7 +17,7 @@ var meta = require('../meta'), /* Chat */ SocketModules.chats.get = function(socket, data, callback) { - if(!data) { + if(!data || !data.roomId) { return callback(new Error('[[error:invalid-data]]')); } @@ -97,14 +97,14 @@ SocketModules.chats.delete = function(socket, data, callback) { }); } -SocketModules.chats.canMessage = function(socket, toUid, callback) { - Messaging.canMessage(socket.uid, toUid, function(err, allowed) { +SocketModules.chats.canMessage = function(socket, roomId, callback) { + Messaging.canMessage(socket.uid, roomId, function(err, allowed) { callback(!allowed ? new Error('[[error:chat-restricted]]') : undefined); }); }; -SocketModules.chats.markRead = function(socket, touid, callback) { - Messaging.markRead(socket.uid, touid, function(err) { +SocketModules.chats.markRead = function(socket, roomId, callback) { + Messaging.markRead(socket.uid, roomId, function(err) { if (err) { return callback(err); }