Commit 53346ff5 authored by JC Brand's avatar JC Brand

roster: refactor to use async/await

parent c05e8110
...@@ -476,13 +476,11 @@ converse.plugins.add('converse-roster', { ...@@ -476,13 +476,11 @@ converse.plugins.add('converse-roster', {
* @param { String } message - An optional message to explain the reason for the subscription request. * @param { String } message - An optional message to explain the reason for the subscription request.
* @param { Object } attributes - Any additional attributes to be stored on the user's model. * @param { Object } attributes - Any additional attributes to be stored on the user's model.
*/ */
addAndSubscribe (jid, name, groups, message, attributes) { async addAndSubscribe (jid, name, groups, message, attributes) {
const handler = (contact) => { const contact = await this.addContactToRoster(jid, name, groups, attributes);
if (contact instanceof _converse.RosterContact) { if (contact instanceof _converse.RosterContact) {
contact.subscribe(message); contact.subscribe(message);
} }
}
this.addContactToRoster(jid, name, groups, attributes).then(handler, handler);
}, },
/** /**
...@@ -535,20 +533,18 @@ converse.plugins.add('converse-roster', { ...@@ -535,20 +533,18 @@ converse.plugins.add('converse-roster', {
}, attributes), {'sort': false}); }, attributes), {'sort': false});
}, },
subscribeBack (bare_jid, presence) { async subscribeBack (bare_jid, presence) {
const contact = this.get(bare_jid); const contact = this.get(bare_jid);
if (contact instanceof _converse.RosterContact) { if (contact instanceof _converse.RosterContact) {
contact.authorize().subscribe(); contact.authorize().subscribe();
} else { } else {
// Can happen when a subscription is retried or roster was deleted // Can happen when a subscription is retried or roster was deleted
const handler = (contact) => { const nickname = get(sizzle(`nick[xmlns="${Strophe.NS.NICK}"]`, presence).pop(), 'textContent', null);
const contact = await this.addContactToRoster(bare_jid, nickname, [], {'subscription': 'from'});
if (contact instanceof _converse.RosterContact) { if (contact instanceof _converse.RosterContact) {
contact.authorize().subscribe(); contact.authorize().subscribe();
} }
} }
const nickname = get(sizzle(`nick[xmlns="${Strophe.NS.NICK}"]`, presence).pop(), 'textContent', null);
this.addContactToRoster(bare_jid, nickname, [], {'subscription': 'from'}).then(handler, handler);
}
}, },
getNumOnlineContacts () { getNumOnlineContacts () {
...@@ -1087,7 +1083,7 @@ converse.plugins.add('converse-roster', { ...@@ -1087,7 +1083,7 @@ converse.plugins.add('converse-roster', {
if (!isString(jid) || !jid.includes('@')) { if (!isString(jid) || !jid.includes('@')) {
throw new TypeError('contacts.add: invalid jid'); throw new TypeError('contacts.add: invalid jid');
} }
_converse.roster.addAndSubscribe(jid, isEmpty(name)? jid: name); return _converse.roster.addAndSubscribe(jid, isEmpty(name)? jid: name);
} }
} }
}); });
......
...@@ -371,10 +371,12 @@ u.isPersistableModel = function (model) { ...@@ -371,10 +371,12 @@ u.isPersistableModel = function (model) {
return model.collection && model.collection.browserStorage; return model.collection && model.collection.browserStorage;
}; };
u.getResolveablePromise = function () { /**
/* Returns a promise object on which `resolve` or `reject` can be * Returns a promise object on which `resolve` or `reject` can be called.
* called. * @private
* @method u#getResolveablePromise
*/ */
u.getResolveablePromise = function () {
const wrapper = { const wrapper = {
isResolved: false, isResolved: false,
isPending: true, isPending: true,
......
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