Explorar o código

Calculate and save avatar hash upon receipt

JC Brand %!s(int64=7) %!d(string=hai) anos
pai
achega
8c3f379d82
Modificáronse 1 ficheiros con 11 adicións e 6 borrados
  1. 11 6
      src/converse-vcard.js

+ 11 - 6
src/converse-vcard.js

@@ -7,10 +7,10 @@
 /*global define */
 /*global define */
 
 
 (function (root, factory) {
 (function (root, factory) {
-    define(["converse-core", "strophe.vcard"], factory);
-}(this, function (converse) {
+    define(["converse-core", "crypto", "strophe.vcard"], factory);
+}(this, function (converse, CryptoJS) {
     "use strict";
     "use strict";
-    const { Promise, Strophe, _, moment, sizzle } = converse.env;
+    const { Promise, Strophe, SHA1, _, moment, sizzle } = converse.env;
     const u = converse.env.utils;
     const u = converse.env.utils;
 
 
 
 
@@ -21,11 +21,15 @@
             result = {
             result = {
                 'stanza': iq,
                 'stanza': iq,
                 'fullname': _.get(vcard.querySelector('FN'), 'textContent'),
                 'fullname': _.get(vcard.querySelector('FN'), 'textContent'),
-                'image': _.get(vcard.querySelector('BINVAL'), 'textContent'),
-                'image_type': _.get(vcard.querySelector('TYPE'), 'textContent'),
+                'image': _.get(vcard.querySelector('PHOTO BINVAL'), 'textContent'),
+                'image_type': _.get(vcard.querySelector('PHOTO TYPE'), 'textContent'),
                 'url': _.get(vcard.querySelector('URL'), 'textContent')
                 'url': _.get(vcard.querySelector('URL'), 'textContent')
             };
             };
         }
         }
+        if (result.image) {
+            const word_array_from_b64 = CryptoJS.enc.Base64.parse(result['image']);
+            result['image_type'] = CryptoJS.SHA1(word_array_from_b64).toString()
+        }
         if (callback) {
         if (callback) {
             callback(result);
             callback(result);
         }
         }
@@ -121,6 +125,7 @@
                     'fullname': fullname,
                     'fullname': fullname,
                     'image': vcard.image,
                     'image': vcard.image,
                     'image_type': vcard.image_type,
                     'image_type': vcard.image_type,
+                    'image_hash': vcard.image_hash,
                     'url': vcard.url,
                     'url': vcard.url,
                     'vcard_updated': moment().format()
                     'vcard_updated': moment().format()
                 };
                 };
@@ -181,7 +186,7 @@
                         return new Promise((resolve, reject) => {
                         return new Promise((resolve, reject) => {
                             this.get(model, force).then((vcard) => {
                             this.get(model, force).then((vcard) => {
                                 model.save(_.extend(
                                 model.save(_.extend(
-                                    _.pick(vcard, ['fullname', 'url', 'image_type', 'image', 'vcard_updated']),
+                                    _.pick(vcard, ['fullname', 'url', 'image_type', 'image', 'image_hash']),
                                     {'vcard_updated': moment().format()}
                                     {'vcard_updated': moment().format()}
                                 ));
                                 ));
                                 resolve();
                                 resolve();