From 15e118eab64d4767a551ab88e0b4772d6e0d3606 Mon Sep 17 00:00:00 2001 From: Anders Evenrud Date: Sun, 26 Jan 2020 22:44:54 +0100 Subject: [PATCH] Updated chat window MUC functionality --- src/app.js | 24 +++++++++++++++++++++--- src/chat-window.js | 23 ++++++++++++++++++----- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/app.js b/src/app.js index 6a68987..2208af3 100644 --- a/src/app.js +++ b/src/app.js @@ -132,6 +132,12 @@ const createApplication = (core, proc) => { target: name, muc: true }); + + const onRoomMessage = (...args) => chatWindow.emit('strophejs/room:message', ...args); + const onRoomPresence = (...args) => chatWindow.emit('strophejs/room:precense', ...args); + const onRoomRoster = (...args) => chatWindow.emit('strophejs/room:roster', ...args); + + connection.muc.join(name, null, onRoomMessage, onRoomPresence, onRoomRoster); } }; @@ -140,10 +146,9 @@ const createApplication = (core, proc) => { title: 'Room name', message: 'Enter room name', parent: win, - value: `conference@${proc.settings.username.split('@')[1]}` + value: `room@conference.${proc.settings.username.split('@')[1]}` }, (btn, value) => { if (btn === 'ok' && value) { - connection.muc.join(value); findOrCreateMucWindow(value); } }); @@ -195,10 +200,23 @@ const createApplication = (core, proc) => { } }; + const onLeaveRoom = name => { + connection.muc.leave(name); + }; + + const sendMessage = (msg, target, muc) => { + if (muc) { + connection.muc.groupchat(target, msg); + } else { + connection.send(msg); + } + }; + bus.on('open-room-join-dialog', () => createJoinRoomDialog()); bus.on('open-connection-window', () => createConnectionWindow(core, proc, win, bus)); bus.on('open-chat-window', from => findOrCreateChatWindow(from).focus()); - bus.on('send-message', msg => connection.send(msg)); + bus.on('send-message', sendMessage); + bus.on('leave-room', onLeaveRoom); bus.on('receive-message', onReceiveMessage); bus.on('set-status', onSetStatus); bus.on('set-connection', onSetConnection); diff --git a/src/chat-window.js b/src/chat-window.js index c6cda1e..1665fe9 100644 --- a/src/chat-window.js +++ b/src/chat-window.js @@ -67,7 +67,7 @@ export const createChatWindow = (core, proc, parent, bus, options) => { const messages = (state, actions) => state.messages.map(({date, msg}) => { return h(ChatMessage, { date: format(date, 'longTime'), - self: getUsername(msg.getAttribute('from')) !== getUsername(options.user), + self: getUsername(msg.getAttribute('from')) !== getUsername(options.target), to: msg.getAttribute('to'), from: msg.getAttribute('from'), type: msg.getAttribute('type'), @@ -127,20 +127,19 @@ export const createChatWindow = (core, proc, parent, bus, options) => { } const msg = createMessage(options.self, options.target, value); - - actions.sendMessage(msg); + actions.sendMessage(options.muc ? value : msg); actions.addMessage({date: new Date(), msg}); setTimeout(() => (textarea.value = ''), 1); }, setTypeStatus: typing => () => ({typing}), - sendMessage: msg => () => bus.emit('send-message', msg), + sendMessage: msg => () => bus.emit('send-message', msg, options.target, options.muc), addMessage: obj => state => ({messages: [...state.messages, obj]}) }, view, $content); let typeStatusTimeout; - win.on('strophejs/message', msg => { + const addMessage = msg => { a.addMessage({date: new Date(), msg}); const container = $content.querySelector('.chat-messages'); @@ -149,7 +148,21 @@ export const createChatWindow = (core, proc, parent, bus, options) => { container.scrollTop = container.scrollHeight; }, 1); } + }; + + if (options.muc) { + win.on('destroy', () => bus.emit('leave-room', options.target)); + } + + win.on('strophejs/room:message', addMessage); + win.on('strophejs/room:presence', (...args) => { + console.warn('TODO', 'strophejs/room:presence', args); }); + win.on('strophejs/room:roster', (...args) => { + console.warn('TODO', 'strophejs/room:roster', args); + }); + + win.on('strophejs/message', addMessage); win.on('strophejs/started-typing', () => { clearTimeout(typeStatusTimeout);