浏览代码

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

JC Brand 5 年之前
父节点
当前提交
c82e3e9bda
共有 2 个文件被更改,包括 32 次插入9 次删除
  1. 8 7
      src/converse-profile.js
  2. 24 2
      src/headless/converse-vcard.js

+ 8 - 7
src/converse-profile.js

@@ -83,16 +83,17 @@ converse.plugins.add('converse-profile', {
                 reader.readAsDataURL(file);
                 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);
                     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();
             },
             },
 
 

+ 24 - 2
src/headless/converse-vcard.js

@@ -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', {
                 /**
                 /**
                  * 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', {
                  *     // 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', {
                  */
                  */
                 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);
                 }
                 }