Skip to content

MUC support #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
},
"dependencies": {
"hyperapp": "^1.2.10",
"strophe.js": "^1.3.4"
"strophe.js": "^1.3.4",
"strophejs-plugin-muc": "^1.1.0"
},
"files": [
"dist/",
Expand Down
53 changes: 51 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author Anders Evenrud <[email protected]>
* @licence Simplified BSD License
*/
import 'strophejs-plugin-muc';
import {$pres, Strophe} from 'strophe.js';
import {createConnectionWindow} from './connection-window.js';
import {createChatWindow} from './chat-window.js';
Expand Down Expand Up @@ -112,13 +113,47 @@ const createApplication = (core, proc) => {
id,
self: connection.jid,
title: username,
user: from
target: from
});
}

return chatWindow;
};

const findOrCreateMucWindow = name => {
let chatWindow = findChatWindow(name);
if (!chatWindow) {
const id = 'StropheJSChatWindow_' + name;

chatWindow = createChatWindow(core, proc, win, bus, {
id,
self: connection.jid,
title: name,
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);
}
};

const createJoinRoomDialog = () => {
core.make('osjs/dialog', 'prompt', {
title: 'Room name',
message: 'Enter room name',
parent: win,
value: `room@conference.${proc.settings.username.split('@')[1]}`
}, (btn, value) => {
if (btn === 'ok' && value) {
findOrCreateMucWindow(value);
}
});
};

const onReceiveMessage = msg => {
const from = msg.getAttribute('from');
const isTyping = msg.getElementsByTagName('cha:composing').length > 0;
Expand Down Expand Up @@ -165,9 +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
25 changes: 19 additions & 6 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 @@ -126,21 +126,20 @@ export const createChatWindow = (core, proc, parent, bus, options) => {
return;
}

const msg = createMessage(options.self, options.user, value);

actions.sendMessage(msg);
const msg = createMessage(options.self, options.target, value);
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
2 changes: 2 additions & 0 deletions src/main-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const createFileMenu = (state, actions) => ([
{label: 'Connection Options', onclick: () => actions.menuOptions()},
{label: 'Connect', disabled: state.connected, onclick: () => actions.menuConnect()},
{label: 'Disconnect', disabled: !state.connected, onclick: () => actions.menuDisconnect()},
{label: 'Join Room', disabled: !state.connected, onclick: () => actions.menuJoinRoom()},
{label: 'Quit', onclick: () => actions.menuQuit()}
]);

Expand Down Expand Up @@ -107,6 +108,7 @@ export const createMainWindow = (core, proc, bus) => {
menuOptions: () => () => bus.emit('open-connection-window'),
menuConnect: () => () => bus.emit('connect'),
menuDisconnect: () => () => bus.emit('disconnect'),
menuJoinRoom: () => () => bus.emit('open-room-join-dialog'),
menuQuit: () => () => proc.destroy(),
menuFile: ev => (state, actions) => {
core.make('osjs/contextmenu').show({
Expand Down
1 change: 1 addition & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const getMessageText = msg => {
.join('\n');
}

console.warn(msg);
return '';
};

Expand Down