Presence subscription bugfixes.

* Return the promise, so that we can wait.
* `sendContactAddIQ` now returns a promise and doesn't take a callback
* Don't call `destroy` on a contact that's already been removed.
This commit is contained in:
JC Brand 2018-11-10 18:53:22 +01:00
parent 2c6bd6cea1
commit 9d0cfe8f43
3 changed files with 75 additions and 66 deletions

68
dist/converse.js vendored
View File

@ -68616,7 +68616,7 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_5__["default"].plugins
_converse.api.chats.open(attrs.jid, attrs); _converse.api.chats.open(attrs.jid, attrs);
}, },
removeContact(ev) { async removeContact(ev) {
if (ev && ev.preventDefault) { if (ev && ev.preventDefault) {
ev.preventDefault(); ev.preventDefault();
} }
@ -68625,28 +68625,35 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_5__["default"].plugins
return; return;
} }
const result = confirm(__("Are you sure you want to remove this contact?")); if (!confirm(__("Are you sure you want to remove this contact?"))) {
return;
}
if (result === true) { let iq;
this.model.removeFromRoster(iq => {
try {
iq = await this.model.removeFromRoster();
this.remove();
if (this.model.collection) {
// The model might have already been removed as
// result of a roster push.
this.model.destroy(); this.model.destroy();
this.remove(); }
}, function (err) { } catch (e) {
alert(__('Sorry, there was an error while trying to remove %1$s as a contact.', name)); _converse.log(e, Strophe.LogLevel.ERROR);
_converse.log(err, Strophe.LogLevel.ERROR); _converse.api.alert.show(Strophe.LogLevel.ERROR, __('Sorry, there was an error while trying to remove %1$s as a contact.', name));
});
} }
}, },
acceptRequest(ev) { async acceptRequest(ev) {
if (ev && ev.preventDefault) { if (ev && ev.preventDefault) {
ev.preventDefault(); ev.preventDefault();
} }
_converse.roster.sendContactAddIQ(this.model.get('jid'), this.model.getFullname(), [], () => { await _converse.roster.sendContactAddIQ(this.model.get('jid'), this.model.getFullname(), []);
this.model.authorize().subscribe(); this.model.authorize().subscribe();
});
}, },
declineRequest(ev) { declineRequest(ev) {
@ -77112,13 +77119,13 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins
ackUnsubscribe() { ackUnsubscribe() {
/* Upon receiving the presence stanza of type "unsubscribed", /* Upon receiving the presence stanza of type "unsubscribed",
* the user SHOULD acknowledge receipt of that subscription state * the user SHOULD acknowledge receipt of that subscription state
* notification by sending a presence stanza of type "unsubscribe" * notification by sending a presence stanza of type "unsubscribe"
* this step lets the user's server know that it MUST no longer * this step lets the user's server know that it MUST no longer
* send notification of the subscription state change to the user. * send notification of the subscription state change to the user.
* Parameters: * Parameters:
* (String) jid - The Jabber ID of the user who is unsubscribing * (String) jid - The Jabber ID of the user who is unsubscribing
*/ */
_converse.api.send($pres({ _converse.api.send($pres({
'type': 'unsubscribe', 'type': 'unsubscribe',
'to': this.get('jid') 'to': this.get('jid')
@ -77140,9 +77147,9 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins
authorize(message) { authorize(message) {
/* Authorize presence subscription /* Authorize presence subscription
* Parameters: * Parameters:
* (String) message - Optional message to send to the person being authorized * (String) message - Optional message to send to the person being authorized
*/ */
const pres = $pres({ const pres = $pres({
'to': this.get('jid'), 'to': this.get('jid'),
'type': "subscribed" 'type': "subscribed"
@ -77157,11 +77164,11 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins
return this; return this;
}, },
removeFromRoster(callback, errback) { removeFromRoster() {
/* Instruct the XMPP server to remove this contact from our roster /* Instruct the XMPP server to remove this contact from our roster
* Parameters: * Parameters:
* (Function) callback * (Function) callback
*/ */
const iq = $iq({ const iq = $iq({
type: 'set' type: 'set'
}).c('query', { }).c('query', {
@ -77170,10 +77177,7 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins
jid: this.get('jid'), jid: this.get('jid'),
subscription: "remove" subscription: "remove"
}); });
return _converse.api.sendIQ(iq);
_converse.api.sendIQ(iq).then(callback).catch(errback);
return this;
} }
}); });
@ -77319,7 +77323,7 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins
_.each(groups, group => iq.c('group').t(group).up()); _.each(groups, group => iq.c('group').t(group).up());
_converse.api.sendIQ(iq); return _converse.api.sendIQ(iq);
}, },
async addContactToRoster(jid, name, groups, attributes) { async addContactToRoster(jid, name, groups, attributes) {

View File

@ -513,32 +513,38 @@ converse.plugins.add('converse-rosterview', {
_converse.api.chats.open(attrs.jid, attrs); _converse.api.chats.open(attrs.jid, attrs);
}, },
removeContact (ev) { async removeContact (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); } if (ev && ev.preventDefault) { ev.preventDefault(); }
if (!_converse.allow_contact_removal) { return; } if (!_converse.allow_contact_removal) { return; }
const result = confirm(__("Are you sure you want to remove this contact?")); if (!confirm(__("Are you sure you want to remove this contact?"))) { return; }
if (result === true) {
this.model.removeFromRoster( let iq;
(iq) => { try {
this.model.destroy(); iq = await this.model.removeFromRoster();
this.remove(); this.remove();
}, if (this.model.collection) {
function (err) { // The model might have already been removed as
alert(__('Sorry, there was an error while trying to remove %1$s as a contact.', name)); // result of a roster push.
_converse.log(err, Strophe.LogLevel.ERROR); this.model.destroy();
} }
} catch (e) {
_converse.log(e, Strophe.LogLevel.ERROR);
_converse.api.alert.show(
Strophe.LogLevel.ERROR,
__('Sorry, there was an error while trying to remove %1$s as a contact.', name)
); );
} }
}, },
acceptRequest (ev) { async acceptRequest (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); } if (ev && ev.preventDefault) { ev.preventDefault(); }
_converse.roster.sendContactAddIQ(
await _converse.roster.sendContactAddIQ(
this.model.get('jid'), this.model.get('jid'),
this.model.getFullname(), this.model.getFullname(),
[], []
() => { this.model.authorize().subscribe(); }
); );
this.model.authorize().subscribe();
}, },
declineRequest (ev) { declineRequest (ev) {

View File

@ -288,13 +288,13 @@ converse.plugins.add('converse-roster', {
ackUnsubscribe () { ackUnsubscribe () {
/* Upon receiving the presence stanza of type "unsubscribed", /* Upon receiving the presence stanza of type "unsubscribed",
* the user SHOULD acknowledge receipt of that subscription state * the user SHOULD acknowledge receipt of that subscription state
* notification by sending a presence stanza of type "unsubscribe" * notification by sending a presence stanza of type "unsubscribe"
* this step lets the user's server know that it MUST no longer * this step lets the user's server know that it MUST no longer
* send notification of the subscription state change to the user. * send notification of the subscription state change to the user.
* Parameters: * Parameters:
* (String) jid - The Jabber ID of the user who is unsubscribing * (String) jid - The Jabber ID of the user who is unsubscribing
*/ */
_converse.api.send($pres({'type': 'unsubscribe', 'to': this.get('jid')})); _converse.api.send($pres({'type': 'unsubscribe', 'to': this.get('jid')}));
this.removeFromRoster(); this.removeFromRoster();
this.destroy(); this.destroy();
@ -311,9 +311,9 @@ converse.plugins.add('converse-roster', {
authorize (message) { authorize (message) {
/* Authorize presence subscription /* Authorize presence subscription
* Parameters: * Parameters:
* (String) message - Optional message to send to the person being authorized * (String) message - Optional message to send to the person being authorized
*/ */
const pres = $pres({'to': this.get('jid'), 'type': "subscribed"}); const pres = $pres({'to': this.get('jid'), 'type': "subscribed"});
if (message && message !== "") { if (message && message !== "") {
pres.c("status").t(message); pres.c("status").t(message);
@ -322,16 +322,15 @@ converse.plugins.add('converse-roster', {
return this; return this;
}, },
removeFromRoster (callback, errback) { removeFromRoster () {
/* Instruct the XMPP server to remove this contact from our roster /* Instruct the XMPP server to remove this contact from our roster
* Parameters: * Parameters:
* (Function) callback * (Function) callback
*/ */
const iq = $iq({type: 'set'}) const iq = $iq({type: 'set'})
.c('query', {xmlns: Strophe.NS.ROSTER}) .c('query', {xmlns: Strophe.NS.ROSTER})
.c('item', {jid: this.get('jid'), subscription: "remove"}); .c('item', {jid: this.get('jid'), subscription: "remove"});
_converse.api.sendIQ(iq).then(callback).catch(errback); return _converse.api.sendIQ(iq);
return this;
} }
}); });
@ -365,7 +364,7 @@ converse.plugins.add('converse-roster', {
/* Register a handler for roster IQ "set" stanzas, which update /* Register a handler for roster IQ "set" stanzas, which update
* roster contacts. * roster contacts.
*/ */
_converse.connection.addHandler((iq) => { _converse.connection.addHandler(iq => {
_converse.roster.onRosterPush(iq); _converse.roster.onRosterPush(iq);
return true; return true;
}, Strophe.NS.ROSTER, 'iq', "set"); }, Strophe.NS.ROSTER, 'iq', "set");
@ -470,7 +469,7 @@ converse.plugins.add('converse-roster', {
.c('query', {'xmlns': Strophe.NS.ROSTER}) .c('query', {'xmlns': Strophe.NS.ROSTER})
.c('item', { jid, name }); .c('item', { jid, name });
_.each(groups, group => iq.c('group').t(group).up()); _.each(groups, group => iq.c('group').t(group).up());
_converse.api.sendIQ(iq); return _converse.api.sendIQ(iq);
}, },
async addContactToRoster (jid, name, groups, attributes) { async addContactToRoster (jid, name, groups, attributes) {