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

muc: move message handler into own method

parent 890db342
......@@ -210,7 +210,7 @@
"object-curly-newline": "off",
"object-curly-spacing": "off",
"object-property-newline": [
"error",
"off",
{
"allowMultiplePropertiesPerLine": true
}
......
......@@ -2907,7 +2907,7 @@
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(
['rosterGroupsFetched'], {},
async function (done, _converse) {
......@@ -2923,14 +2923,14 @@
})
.c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
.c('item', {
'jid': 'annoyingguy@montague.lit',
'jid': 'absentguy@montague.lit',
'affiliation': 'member',
'role': 'none'
});
_converse.connection._dataRecv(test_utils.createRequest(message));
await u.waitUntil(() => view.model.occupants.length > 1);
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({
from: 'lounge@montague.lit',
......@@ -2939,13 +2939,13 @@
})
.c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
.c('item', {
'jid': 'annoyingguy@montague.lit',
'jid': 'absentguy@montague.lit',
'affiliation': 'none',
'role': 'none'
});
_converse.connection._dataRecv(test_utils.createRequest(message));
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();
}));
......
......@@ -480,17 +480,41 @@ converse.plugins.add('converse-muc', {
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 () {
// Register presence and message handlers for this groupchat
const room_jid = this.get('jid');
this.removeHandlers();
this.presence_handler = _converse.connection.addHandler(stanza => {
this.onPresence(stanza);
return true;
},
this.presence_handler = _converse.connection.addHandler(
stanza => (this.onPresence(stanza) || true),
null, 'presence', null, null, room_jid,
{'ignoreNamespaceFragment': true, 'matchBareFromJid': true}
);
this.message_handler = _converse.connection.addHandler(stanza => {
if (sizzle(`message > result[xmlns="${Strophe.NS.MAM}"]`, stanza).pop()) {
// MAM messages are handled in converse-mam.
......@@ -504,61 +528,27 @@ converse.plugins.add('converse-muc', {
}, null, 'message', 'groupchat', null, room_jid,
{'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});
if (occupant) {
occupant.save(data);
} else {
this.occupants.create(data);
}
}
return true;
}, Strophe.NS.MUC_USER, 'message', null, null, room_jid);
this.affiliation_message_handler = _converse.connection.addHandler(
stanza => (this.handleAffiliationChangedMessage(stanza) || true),
Strophe.NS.MUC_USER, 'message', null, null, room_jid
);
},
removeHandlers () {
// Remove the presence and message handlers that were
// registered for this groupchat.
if (this.message_handler) {
if (_converse.connection) {
_converse.connection.deleteHandler(this.message_handler);
}
_converse.connection && _converse.connection.deleteHandler(this.message_handler);
delete this.message_handler;
}
if (this.presence_handler) {
if (_converse.connection) {
_converse.connection.deleteHandler(this.presence_handler);
}
_converse.connection && _converse.connection.deleteHandler(this.presence_handler);
delete this.presence_handler;
}
if (this.muc_notifications_handler) {
if (_converse.connection) {
_converse.connection.deleteHandler(this.muc_notifications_handler);
}
delete this.muc_notifications_handler;
if (this.affiliation_message_handler) {
_converse.connection && _converse.connection.deleteHandler(this.affiliation_message_handler);
delete this.affiliation_message_handler;
}
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