Procházet zdrojové kódy

Contact editing: Only send avatar if changed

Danilo Bargen před 8 roky
rodič
revize
64478767c1
2 změnil soubory, kde provedl 28 přidání a 14 odebrání
  1. 1 1
      src/controller_model/contact.ts
  2. 27 13
      src/services/webclient.ts

+ 1 - 1
src/controller_model/contact.ts

@@ -153,7 +153,7 @@ export class ContactControllerModel implements threema.ControllerModel {
                     this.contact.id,
                     this.firstName,
                     this.lastName,
-                    this.avatarController.getAvatar(),
+                    this.avatarController.avatarChanged ? this.avatarController.getAvatar() : undefined,
                 );
             case ControllerModelMode.NEW:
                 return this.webClientService.addContact(this.identity);

+ 27 - 13
src/services/webclient.ts

@@ -1122,24 +1122,38 @@ export class WebClientService {
      * Modify a contact name or a avatar
      */
     public modifyContact(threemaId: string,
-                         firstName: string,
-                         lastName: string,
+                         firstName?: string,
+                         lastName?: string,
                          avatar?: ArrayBuffer): Promise<threema.ContactReceiver> {
-        const promise = this._sendUpdatePromise(WebClientService.SUB_TYPE_CONTACT, {
+        // Prepare payload data
+        const data = {};
+        if (firstName !== undefined) {
+            data[WebClientService.ARGUMENT_FIRST_NAME] = firstName;
+        }
+        if (lastName !== undefined) {
+            data[WebClientService.ARGUMENT_LAST_NAME] = lastName;
+        }
+        if (avatar !== undefined) {
+            data[WebClientService.ARGUMENT_AVATAR_HIGH_RESOLUTION] = avatar;
+        }
+
+        // If no changes happened, resolve the promise immediately.
+        if (Object.keys(data).length === 0) {
+            this.$log.warn(this.logTag, 'Trying to modify contact without any changes');
+            return Promise.resolve(this.contacts.get(threemaId));
+        }
+
+        // Send update
+        const args = {
             [WebClientService.ARGUMENT_IDENTITY]: threemaId,
-        }, {
-            [WebClientService.ARGUMENT_FIRST_NAME]: firstName,
-            [WebClientService.ARGUMENT_LAST_NAME]: lastName,
-            [WebClientService.ARGUMENT_AVATAR_HIGH_RESOLUTION]: avatar,
-        });
+        };
+        const promise = this._sendUpdatePromise(WebClientService.SUB_TYPE_CONTACT, args, data);
 
+        // If necessary, reset avatar to force a avatar reload
         if (avatar !== undefined) {
-            // reset avatar to force a avatar reload
-            this.receivers.getData({
-                type: 'contact',
-                id: threemaId,
-            } as threema.Receiver).avatar = {};
+            this.contacts.get(threemaId).avatar = {};
         }
+
         return promise;
     }