Skip to content

Commit 37eb0fc

Browse files
committed
Updated chat window MUC functionality
1 parent 3a80f53 commit 37eb0fc

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/app.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ const createApplication = (core, proc) => {
132132
target: name,
133133
muc: true
134134
});
135+
136+
const onRoomMessage = (...args) => chatWindow.emit('strophejs/room:message', ...args);
137+
const onRoomPresence = (...args) => chatWindow.emit('strophejs/room:precense', ...args);
138+
const onRoomRoster = (...args) => chatWindow.emit('strophejs/room:roster', ...args);
139+
140+
connection.muc.join(name, null, onRoomMessage, onRoomPresence, onRoomRoster);
135141
}
136142
};
137143

@@ -140,10 +146,9 @@ const createApplication = (core, proc) => {
140146
title: 'Room name',
141147
message: 'Enter room name',
142148
parent: win,
143-
value: `conference@${proc.settings.username.split('@')[1]}`
149+
value: `room@conference.${proc.settings.username.split('@')[1]}`
144150
}, (btn, value) => {
145151
if (btn === 'ok' && value) {
146-
connection.muc.join(value);
147152
findOrCreateMucWindow(value);
148153
}
149154
});
@@ -195,10 +200,23 @@ const createApplication = (core, proc) => {
195200
}
196201
};
197202

203+
const onLeaveRoom = name => {
204+
connection.muc.leave(name);
205+
};
206+
207+
const sendMessage = (msg, target, muc) => {
208+
if (muc) {
209+
connection.muc.groupchat(target, msg);
210+
} else {
211+
connection.send(msg);
212+
}
213+
};
214+
198215
bus.on('open-room-join-dialog', () => createJoinRoomDialog());
199216
bus.on('open-connection-window', () => createConnectionWindow(core, proc, win, bus));
200217
bus.on('open-chat-window', from => findOrCreateChatWindow(from).focus());
201-
bus.on('send-message', msg => connection.send(msg));
218+
bus.on('send-message', sendMessage);
219+
bus.on('leave-room', onLeaveRoom);
202220
bus.on('receive-message', onReceiveMessage);
203221
bus.on('set-status', onSetStatus);
204222
bus.on('set-connection', onSetConnection);

src/chat-window.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const createChatWindow = (core, proc, parent, bus, options) => {
6767
const messages = (state, actions) => state.messages.map(({date, msg}) => {
6868
return h(ChatMessage, {
6969
date: format(date, 'longTime'),
70-
self: getUsername(msg.getAttribute('from')) !== getUsername(options.user),
70+
self: getUsername(msg.getAttribute('from')) !== getUsername(options.target),
7171
to: msg.getAttribute('to'),
7272
from: msg.getAttribute('from'),
7373
type: msg.getAttribute('type'),
@@ -127,20 +127,19 @@ export const createChatWindow = (core, proc, parent, bus, options) => {
127127
}
128128

129129
const msg = createMessage(options.self, options.target, value);
130-
131-
actions.sendMessage(msg);
130+
actions.sendMessage(options.muc ? value : msg);
132131
actions.addMessage({date: new Date(), msg});
133132

134133
setTimeout(() => (textarea.value = ''), 1);
135134
},
136135
setTypeStatus: typing => () => ({typing}),
137-
sendMessage: msg => () => bus.emit('send-message', msg),
136+
sendMessage: msg => () => bus.emit('send-message', msg, options.target, options.muc),
138137
addMessage: obj => state => ({messages: [...state.messages, obj]})
139138
}, view, $content);
140139

141140
let typeStatusTimeout;
142141

143-
win.on('strophejs/message', msg => {
142+
const addMessage = msg => {
144143
a.addMessage({date: new Date(), msg});
145144

146145
const container = $content.querySelector('.chat-messages');
@@ -149,7 +148,21 @@ export const createChatWindow = (core, proc, parent, bus, options) => {
149148
container.scrollTop = container.scrollHeight;
150149
}, 1);
151150
}
151+
};
152+
153+
if (options.muc) {
154+
win.on('destroy', () => bus.emit('leave-room', options.target));
155+
}
156+
157+
win.on('strophejs/room:message', addMessage);
158+
win.on('strophejs/room:presence', (...args) => {
159+
console.warn('TODO', 'strophejs/room:presence', args);
152160
});
161+
win.on('strophejs/room:roster', (...args) => {
162+
console.warn('TODO', 'strophejs/room:roster', args);
163+
});
164+
165+
win.on('strophejs/message', addMessage);
153166

154167
win.on('strophejs/started-typing', () => {
155168
clearTimeout(typeStatusTimeout);

0 commit comments

Comments
 (0)