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