Commit 7bf39a09 authored by JC Brand's avatar JC Brand

muc: move message handler into own method

parent 890db342
...@@ -210,7 +210,7 @@ ...@@ -210,7 +210,7 @@
"object-curly-newline": "off", "object-curly-newline": "off",
"object-curly-spacing": "off", "object-curly-spacing": "off",
"object-property-newline": [ "object-property-newline": [
"error", "off",
{ {
"allowMultiplePropertiesPerLine": true "allowMultiplePropertiesPerLine": true
} }
......
...@@ -2907,7 +2907,7 @@ ...@@ -2907,7 +2907,7 @@
done(); done();
})); }));
it("notifies user of role and affiliation changes for members not in the groupchat", it("notifies users of role and affiliation changes for members not currently in the groupchat",
mock.initConverse( mock.initConverse(
['rosterGroupsFetched'], {}, ['rosterGroupsFetched'], {},
async function (done, _converse) { async function (done, _converse) {
...@@ -2923,14 +2923,14 @@ ...@@ -2923,14 +2923,14 @@
}) })
.c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'}) .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
.c('item', { .c('item', {
'jid': 'annoyingguy@montague.lit', 'jid': 'absentguy@montague.lit',
'affiliation': 'member', 'affiliation': 'member',
'role': 'none' 'role': 'none'
}); });
_converse.connection._dataRecv(test_utils.createRequest(message)); _converse.connection._dataRecv(test_utils.createRequest(message));
await u.waitUntil(() => view.model.occupants.length > 1); await u.waitUntil(() => view.model.occupants.length > 1);
expect(view.model.occupants.length).toBe(2); expect(view.model.occupants.length).toBe(2);
expect(view.model.occupants.findWhere({'jid': 'annoyingguy@montague.lit'}).get('affiliation')).toBe('member'); expect(view.model.occupants.findWhere({'jid': 'absentguy@montague.lit'}).get('affiliation')).toBe('member');
message = $msg({ message = $msg({
from: 'lounge@montague.lit', from: 'lounge@montague.lit',
...@@ -2939,13 +2939,13 @@ ...@@ -2939,13 +2939,13 @@
}) })
.c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'}) .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
.c('item', { .c('item', {
'jid': 'annoyingguy@montague.lit', 'jid': 'absentguy@montague.lit',
'affiliation': 'none', 'affiliation': 'none',
'role': 'none' 'role': 'none'
}); });
_converse.connection._dataRecv(test_utils.createRequest(message)); _converse.connection._dataRecv(test_utils.createRequest(message));
expect(view.model.occupants.length).toBe(2); expect(view.model.occupants.length).toBe(2);
expect(view.model.occupants.findWhere({'jid': 'annoyingguy@montague.lit'}).get('affiliation')).toBe('none'); expect(view.model.occupants.findWhere({'jid': 'absentguy@montague.lit'}).get('affiliation')).toBe('none');
done(); done();
})); }));
......
...@@ -480,17 +480,41 @@ converse.plugins.add('converse-muc', { ...@@ -480,17 +480,41 @@ converse.plugins.add('converse-muc', {
return this.occupants.fetched; return this.occupants.fetched;
}, },
handleAffiliationChangedMessage (stanza) {
const item = sizzle(`x[xmlns="${Strophe.NS.MUC_USER}"] item`, stanza).pop();
if (item) {
const from = stanza.getAttribute("from");
const type = stanza.getAttribute("type");
const affiliation = item.getAttribute('affiliation');
const jid = item.getAttribute('jid');
const data = {
from, type, affiliation,
'nick': Strophe.getNodeFromJid(jid),
'states': [],
'show': type == 'unavailable' ? 'offline' : 'online',
'role': item.getAttribute('role'),
'jid': Strophe.getBareJidFromJid(jid),
'resource': Strophe.getResourceFromJid(jid)
}
const occupant = this.occupants.findOccupant({'jid': data.jid});
if (occupant) {
occupant.save(data);
} else {
this.occupants.create(data);
}
}
},
registerHandlers () { registerHandlers () {
// Register presence and message handlers for this groupchat // Register presence and message handlers for this groupchat
const room_jid = this.get('jid'); const room_jid = this.get('jid');
this.removeHandlers(); this.removeHandlers();
this.presence_handler = _converse.connection.addHandler(stanza => { this.presence_handler = _converse.connection.addHandler(
this.onPresence(stanza); stanza => (this.onPresence(stanza) || true),
return true;
},
null, 'presence', null, null, room_jid, null, 'presence', null, null, room_jid,
{'ignoreNamespaceFragment': true, 'matchBareFromJid': true} {'ignoreNamespaceFragment': true, 'matchBareFromJid': true}
); );
this.message_handler = _converse.connection.addHandler(stanza => { this.message_handler = _converse.connection.addHandler(stanza => {
if (sizzle(`message > result[xmlns="${Strophe.NS.MAM}"]`, stanza).pop()) { if (sizzle(`message > result[xmlns="${Strophe.NS.MAM}"]`, stanza).pop()) {
// MAM messages are handled in converse-mam. // MAM messages are handled in converse-mam.
...@@ -504,61 +528,27 @@ converse.plugins.add('converse-muc', { ...@@ -504,61 +528,27 @@ converse.plugins.add('converse-muc', {
}, null, 'message', 'groupchat', null, room_jid, }, null, 'message', 'groupchat', null, room_jid,
{'matchBareFromJid': true} {'matchBareFromJid': true}
); );
this.muc_notifications_handler = _converse.connection.addHandler(stanza => {
const item = sizzle(`x[xmlns="${Strophe.NS.MUC_USER}"] item`, stanza).pop();
if (item) {
const from = stanza.getAttribute("from");
const type = stanza.getAttribute("type");
const affiliation = item.getAttribute('affiliation');
const jid = item.getAttribute('jid');
const data = {
'from': from,
'nick': Strophe.getNodeFromJid(jid),
'type': type,
'states': [],
'show': type == 'unavailable' ? 'offline' : 'online',
'affiliation': affiliation,
'role': item.getAttribute('role'),
'jid': Strophe.getBareJidFromJid(jid),
'resource': Strophe.getResourceFromJid(jid)
}
const occupant = this.occupants.findOccupant({'jid': data.jid}); this.affiliation_message_handler = _converse.connection.addHandler(
stanza => (this.handleAffiliationChangedMessage(stanza) || true),
if (occupant) { Strophe.NS.MUC_USER, 'message', null, null, room_jid
occupant.save(data); );
} else {
this.occupants.create(data);
}
}
return true;
}, Strophe.NS.MUC_USER, 'message', null, null, room_jid);
}, },
removeHandlers () { removeHandlers () {
// Remove the presence and message handlers that were // Remove the presence and message handlers that were
// registered for this groupchat. // registered for this groupchat.
if (this.message_handler) { if (this.message_handler) {
if (_converse.connection) { _converse.connection && _converse.connection.deleteHandler(this.message_handler);
_converse.connection.deleteHandler(this.message_handler);
}
delete this.message_handler; delete this.message_handler;
} }
if (this.presence_handler) { if (this.presence_handler) {
if (_converse.connection) { _converse.connection && _converse.connection.deleteHandler(this.presence_handler);
_converse.connection.deleteHandler(this.presence_handler);
}
delete this.presence_handler; delete this.presence_handler;
} }
if (this.affiliation_message_handler) {
if (this.muc_notifications_handler) { _converse.connection && _converse.connection.deleteHandler(this.affiliation_message_handler);
if (_converse.connection) { delete this.affiliation_message_handler;
_converse.connection.deleteHandler(this.muc_notifications_handler);
}
delete this.muc_notifications_handler;
} }
return this; return this;
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment