Skip to content

Commit

Permalink
Updated chat window MUC functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Jan 26, 2020
1 parent 5146875 commit 11d3ada
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
24 changes: 21 additions & 3 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand All @@ -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);
}
});
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 18 additions & 5 deletions src/chat-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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');
Expand All @@ -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);
Expand Down

0 comments on commit 11d3ada

Please sign in to comment.