Commit c82e3e9b authored by JC Brand's avatar JC Brand

Update `api.vcard.set` to also update the VCard model

parent b4e4248f
......@@ -83,16 +83,17 @@ converse.plugins.add('converse-profile', {
reader.readAsDataURL(file);
},
setVCard (data) {
api.vcard.set(_converse.bare_jid, data)
.then(() => api.vcard.update(this.model.vcard, true))
.catch((err) => {
async setVCard (data) {
try {
await api.vcard.set(_converse.bare_jid, data);
} catch (err) {
log.fatal(err);
api.show('error', __('Error'), [
this.alert([
__("Sorry, an error happened while trying to save your profile data."),
__("You can check your browser's developer console for any error output.")
]);
});
].join(" "));
return;
}
this.modal.hide();
},
......
......@@ -67,6 +67,12 @@ converse.plugins.add('converse-vcard', {
api.promises.add('VCardsInitialized');
/**
* Represents a VCard
* @class
* @namespace _converse.VCard
* @memberOf _converse
*/
_converse.VCard = Model.extend({
defaults: {
'image': _converse.DEFAULT_IMAGE,
......@@ -268,6 +274,10 @@ converse.plugins.add('converse-vcard', {
/**
* Enables setting new values for a VCard.
*
* Sends out an IQ stanza to set the user's VCard and if
* successful, it updates the {@link _converse.VCard}
* for the passed in JID.
*
* @method _converse.api.vcard.set
* @param {string} jid The JID for which the VCard should be set
* @param {object} data A map of VCard keys and values
......@@ -282,12 +292,19 @@ converse.plugins.add('converse-vcard', {
* // Failure
* }).
*/
set (jid, data) {
async set (jid, data) {
if (!jid) {
throw Error("No jid provided for the VCard data");
}
const vcard_el = Strophe.xmlHtmlNode(tpl_vcard(data)).firstElementChild;
return api.sendIQ(createStanza("set", jid, vcard_el));
let result;
try {
result = await api.sendIQ(createStanza("set", jid, vcard_el));
} catch (e) {
throw (e);
}
await api.vcard.update(jid, true);
return result;
},
/**
......@@ -345,6 +362,11 @@ converse.plugins.add('converse-vcard', {
*/
async update (model, force) {
const data = await this.get(model, force);
model = isString(model) ? _converse.vcards.findWhere({'jid': model}) : model;
if (!model) {
log.error(`Could not find a VCard model for ${model}`);
return;
}
delete data['stanza']
model.save(data);
}
......
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