Move code for handling own presence into new method

Fix two bugs as well.

- Pass by reference bug for presence defaults
- querySelector for `delay` returned nothing (use sizzle instead)
This commit is contained in:
JC Brand 2018-05-24 15:18:45 +02:00
parent c0080390a7
commit eff656931c

View File

@ -90,9 +90,11 @@
_converse.Presence = Backbone.Model.extend({
defaults: {
defaults () {
return {
'show': 'offline',
'resources': {}
}
},
getHighestPriorityResource () {
@ -123,10 +125,8 @@
const jid = presence.getAttribute('from'),
show = _.propertyOf(presence.querySelector('show'))('textContent') || 'online',
resource = Strophe.getResourceFromJid(jid),
delay = presence.querySelector(
`delay[xmlns="${Strophe.NS.DELAY}"]`
),
timestamp = _.isNull(delay) ? moment().format() : moment(delay.getAttribute('stamp')).format();
delay = sizzle(`delay[xmlns="${Strophe.NS.DELAY}"]`, presence).pop(),
timestamp = _.isNil(delay) ? moment().format() : moment(delay.getAttribute('stamp')).format();
let priority = _.propertyOf(presence.querySelector('priority'))('textContent') || 0;
priority = _.isNaN(parseInt(priority, 10)) ? 0 : parseInt(priority, 10);
@ -642,17 +642,11 @@
}
},
presenceHandler (presence) {
const presence_type = presence.getAttribute('type');
if (presence_type === 'error') { return true; }
handleOwnPresence (presence) {
const jid = presence.getAttribute('from'),
bare_jid = Strophe.getBareJidFromJid(jid),
resource = Strophe.getResourceFromJid(jid),
status_message = _.propertyOf(presence.querySelector('status'))('textContent'),
contact = this.get(bare_jid);
presence_type = presence.getAttribute('type');
if (this.isSelf(bare_jid)) {
if ((_converse.connection.jid !== jid) &&
(presence_type !== 'unavailable') &&
(_converse.synchronize_availability === true ||
@ -662,6 +656,8 @@
// we'll update ours as well.
const show = _.propertyOf(presence.querySelector('show'))('textContent') || 'online';
_converse.xmppstatus.save({'status': show});
const status_message = _.propertyOf(presence.querySelector('status'))('textContent');
if (status_message) {
_converse.xmppstatus.save({'status_message': status_message});
}
@ -683,13 +679,27 @@
// otherwise we're treated as offline.
_converse.xmppstatus.sendPresence();
}
return;
},
presenceHandler (presence) {
const presence_type = presence.getAttribute('type');
if (presence_type === 'error') { return true; }
const jid = presence.getAttribute('from'),
bare_jid = Strophe.getBareJidFromJid(jid);
if (this.isSelf(bare_jid)) {
return this.handleOwnPresence(presence);
} else if (sizzle(`query[xmlns="${Strophe.NS.MUC}"]`, presence).length) {
return; // Ignore MUC
}
const status_message = _.propertyOf(presence.querySelector('status'))('textContent'),
contact = this.get(bare_jid);
if (contact && (status_message !== contact.get('status'))) {
contact.save({'status': status_message});
}
if (presence_type === 'subscribed' && contact) {
contact.ackSubscribe();
} else if (presence_type === 'unsubscribed' && contact) {
@ -699,6 +709,7 @@
} else if (presence_type === 'subscribe') {
this.handleIncomingSubscription(presence);
} else if (presence_type === 'unavailable' && contact) {
const resource = Strophe.getResourceFromJid(jid);
contact.presence.removeResource(resource);
} else if (contact) {
// presence_type is undefined