From 23c25d771529793a6861c2c7a3014d01f2a743eb Mon Sep 17 00:00:00 2001 From: Parvesh Monu Date: Fri, 12 Jul 2024 11:12:49 +0530 Subject: [PATCH] Fix missing avatar and name in members of the community channels and group chats (#20619) --- .../ethereum/module/EncryptionUtils.java | 5 ++ .../ios/RCTStatus/EncryptionUtils.m | 4 ++ modules/react-native-status/nodejs/status.cpp | 33 +++++++++ src/native_module/core.cljs | 5 ++ src/status_im/subs/chats.cljs | 18 ++++- src/status_im/subs/contact.cljs | 67 +++++-------------- src/status_im/subs/contact/utils.cljs | 54 +++++++++++++++ src/tests/test_utils.cljs | 2 + 8 files changed, 136 insertions(+), 52 deletions(-) create mode 100644 src/status_im/subs/contact/utils.cljs diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/EncryptionUtils.java b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/EncryptionUtils.java index bed7b8ac08..d7ca639593 100644 --- a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/EncryptionUtils.java +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/EncryptionUtils.java @@ -92,6 +92,11 @@ public class EncryptionUtils extends ReactContextBaseJavaModule { return Statusgo.hexToUtf8(str); } + @ReactMethod(isBlockingSynchronousMethod = true) + public String serializeLegacyKey(final String publicKey) { + return Statusgo.serializeLegacyKey(publicKey); + } + @ReactMethod public void setBlankPreviewFlag(final Boolean blankPreview) { final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.reactContext); diff --git a/modules/react-native-status/ios/RCTStatus/EncryptionUtils.m b/modules/react-native-status/ios/RCTStatus/EncryptionUtils.m index 99dbf91036..d580dea59f 100644 --- a/modules/react-native-status/ios/RCTStatus/EncryptionUtils.m +++ b/modules/react-native-status/ios/RCTStatus/EncryptionUtils.m @@ -94,6 +94,10 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(hexToUtf8:(NSString *)str) { return StatusgoHexToUtf8(str); } +RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(serializeLegacyKey:(NSString *)str) { + return StatusgoSerializeLegacyKey(str); +} + RCT_EXPORT_METHOD(setBlankPreviewFlag:(BOOL *)newValue) { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; diff --git a/modules/react-native-status/nodejs/status.cpp b/modules/react-native-status/nodejs/status.cpp index 126aece4e0..d236e5edda 100644 --- a/modules/react-native-status/nodejs/status.cpp +++ b/modules/react-native-status/nodejs/status.cpp @@ -672,6 +672,38 @@ void _Sha3(const FunctionCallbackInfo& args) { } +void _SerializeLegacyKey(const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); + Local context = isolate->GetCurrentContext(); + + if (args.Length() != 1) { + // Throw an Error that is passed back to JavaScript + isolate->ThrowException(Exception::TypeError( + String::NewFromUtf8Literal(isolate, "Wrong number of arguments for SerializeLegacyKey"))); + return; + } + + // Check the argument types + + if (!args[0]->IsString()) { + isolate->ThrowException(Exception::TypeError( + String::NewFromUtf8Literal(isolate, "Wrong argument type for 'str'"))); + return; + } + + + String::Utf8Value arg0Obj(isolate, args[0]->ToString(context).ToLocalChecked()); + char *arg0 = *arg0Obj; + + // Call exported Go function, which returns a C string + char *c = SerializeLegacyKey(arg0); + + Local ret = String::NewFromUtf8(isolate, c).ToLocalChecked(); + args.GetReturnValue().Set(ret); + delete c; + +} + void _ToChecksumAddress(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); Local context = isolate->GetCurrentContext(); @@ -1976,6 +2008,7 @@ void init(Local exports) { NODE_SET_METHOD(exports, "checkAddressChecksum", _CheckAddressChecksum); NODE_SET_METHOD(exports, "isAddress", _IsAddress); NODE_SET_METHOD(exports, "sha3", _Sha3); + NODE_SET_METHOD(exports, "serializeLegacyKey", _SerializeLegacyKey); NODE_SET_METHOD(exports, "toChecksumAddress", _ToChecksumAddress); NODE_SET_METHOD(exports, "logout", _Logout); NODE_SET_METHOD(exports, "hashMessage", _HashMessage); diff --git a/src/native_module/core.cljs b/src/native_module/core.cljs index 85b11a7f5e..4f5edc4893 100644 --- a/src/native_module/core.cljs +++ b/src/native_module/core.cljs @@ -333,6 +333,11 @@ :key input-key}) (.deserializeAndCompressKey ^js (encryption) input-key callback)) +(defn serialize-legacy-key + "Compresses an old format public key (0x04...) to the new one zQ..." + [public-key] + (.serializeLegacyKey ^js (encryption) public-key)) + (defn compressed-key->public-key "Provides compressed key to status-go and gets back the uncompressed public key via deserialization" [public-key deserialization-key callback] diff --git a/src/status_im/subs/chats.cljs b/src/status_im/subs/chats.cljs index b804fb4977..4d0d0c9545 100644 --- a/src/status_im/subs/chats.cljs +++ b/src/status_im/subs/chats.cljs @@ -5,7 +5,8 @@ [re-frame.core :as re-frame] [status-im.constants :as constants] [status-im.contexts.chat.events :as chat.events] - [status-im.contexts.profile.utils :as profile.utils])) + [status-im.contexts.profile.utils :as profile.utils] + [status-im.subs.contact.utils :as contact.utils])) (def memo-chats-stack-items (atom nil)) @@ -259,8 +260,19 @@ :chats/photo-path :<- [:contacts/contacts] :<- [:profile/profile-with-image] - (fn [[contacts {:keys [public-key] :as multiaccount}] [_ id]] - (let [contact (or (when (= id public-key) multiaccount) (get contacts id))] + :<- [:mediaserver/port] + :<- [:initials-avatar-font-file] + :<- [:theme] + (fn [[contacts {:keys [public-key] :as multiaccount} port font-file theme] [_ id]] + (let [contact (or (when (= id public-key) multiaccount) + (get contacts id) + (contact.utils/replace-contact-image-uri + {:contact {:public-key id + :customization-color constants/profile-default-color} + :port port + :public-key id + :font-file font-file + :theme theme}))] (profile.utils/photo contact)))) (re-frame/reg-sub diff --git a/src/status_im/subs/contact.cljs b/src/status_im/subs/contact.cljs index 1a866e1b0f..2b5ff555a0 100644 --- a/src/status_im/subs/contact.cljs +++ b/src/status_im/subs/contact.cljs @@ -5,10 +5,10 @@ [legacy.status-im.ui.screens.profile.visibility-status.utils :as visibility-status-utils] [quo.theme] [re-frame.core :as re-frame] - [status-im.common.pixel-ratio :as pixel-ratio] [status-im.constants :as constants] [status-im.contexts.profile.utils :as profile.utils] [status-im.subs.chat.utils :as chat.utils] + [status-im.subs.contact.utils :as contact.utils] [utils.address :as address] [utils.collection] [utils.i18n :as i18n])) @@ -37,50 +37,6 @@ (fn [multiaccount] (get multiaccount :profile-pictures-visibility))) -(defn- replace-contact-image-uri - [contact port public-key font-file theme] - (let [{:keys [images ens-name customization-color]} contact - images - (reduce (fn [acc image] - (let [image-name (:type image) - clock (:clock image) - options {:port port - :ratio pixel-ratio/ratio - :public-key - public-key - :image-name - image-name - ; We pass the clock so that we reload the - ; image if the image is updated - :clock - clock - :theme - theme - :override-ring? - (when ens-name false)}] - (assoc-in acc - [(keyword image-name) :config] - {:type :contact - :options options}))) - images - (vals images)) - - images (if (seq images) - images - {:thumbnail - {:config {:type :initials - :options {:port port - :ratio pixel-ratio/ratio - :public-key public-key - :override-ring? (when ens-name false) - :uppercase-ratio (:uppercase-ratio - constants/initials-avatar-font-conf) - :customization-color customization-color - :theme theme - :font-file font-file}}}})] - - (assoc contact :images images))) - (defn- enrich-contact ([contact] (enrich-contact contact nil nil)) ([{:keys [public-key] :as contact} setting own-public-key] @@ -103,7 +59,12 @@ (defn- reduce-contacts-image-uri [contacts port font-file theme] (reduce-kv (fn [acc public-key contact] - (let [contact (replace-contact-image-uri contact port public-key font-file theme)] + (let [contact (contact.utils/replace-contact-image-uri + {:contact contact + :port port + :public-key public-key + :font-file font-file + :theme theme})] (assoc acc public-key contact))) {} contacts)) @@ -230,7 +191,12 @@ [_ contact-identity ens-name port font-file theme] (let [contact (enrich-contact (public-key-and-ens-name->new-contact contact-identity ens-name))] - (replace-contact-image-uri contact port contact-identity font-file theme))) + (contact.utils/replace-contact-image-uri + {:contact contact + :port port + :public-key contact-identity + :font-file font-file + :theme theme}))) (re-frame/reg-sub :contacts/current-contact @@ -250,7 +216,10 @@ :contacts/contact-by-identity :<- [:contacts/contacts] (fn [contacts [_ contact-identity]] - (get contacts contact-identity {:public-key contact-identity}))) + (get + contacts + contact-identity + (contact.utils/build-contact-from-public-key contact-identity)))) (re-frame/reg-sub :contacts/contact-two-names-by-identity @@ -288,7 +257,7 @@ (assoc public-key current-contact))] (->> members (map #(or (get all-contacts %) - {:public-key %})) + (contact.utils/build-contact-from-public-key %))) (sort-by (comp string/lower-case (fn [{:keys [primary-name name alias public-key]}] (or primary-name diff --git a/src/status_im/subs/contact/utils.cljs b/src/status_im/subs/contact/utils.cljs new file mode 100644 index 0000000000..37862bea26 --- /dev/null +++ b/src/status_im/subs/contact/utils.cljs @@ -0,0 +1,54 @@ +(ns status-im.subs.contact.utils + (:require + [native-module.core :as native-module] + [status-im.common.pixel-ratio :as pixel-ratio] + [status-im.constants :as constants] + [utils.address :as address])) + +(defn replace-contact-image-uri + [{:keys [contact port public-key font-file theme]}] + (let [{:keys [images ens-name customization-color]} contact + images + (reduce (fn [acc image] + (let [image-name (:type image) + clock (:clock image) + options {:port port + :ratio pixel-ratio/ratio + :public-key public-key + :image-name image-name + ; We pass the clock so that we reload the + ; image if the image is updated + :clock clock + :theme theme + :override-ring? (when ens-name false)}] + (assoc-in acc + [(keyword image-name) :config] + {:type :contact + :options options}))) + images + (vals images)) + + images (if (seq images) + images + {:thumbnail + {:config {:type :initials + :options {:port port + :ratio pixel-ratio/ratio + :public-key public-key + :override-ring? (when ens-name false) + :uppercase-ratio (:uppercase-ratio + constants/initials-avatar-font-conf) + :customization-color customization-color + :theme theme + :font-file font-file}}}})] + + (assoc contact :images images))) + + +(defn build-contact-from-public-key + [public-key] + (when public-key + (let [compressed-key (native-module/serialize-legacy-key public-key)] + {:public-key public-key + :compressed-key compressed-key + :primary-name (address/get-shortened-compressed-key (or compressed-key public-key))}))) diff --git a/src/tests/test_utils.cljs b/src/tests/test_utils.cljs index 29b11cb8bb..2ab9f00c80 100644 --- a/src/tests/test_utils.cljs +++ b/src/tests/test_utils.cljs @@ -40,6 +40,8 @@ (clj->js {:sha3 (fn [s] (.sha3 native-status s)) + :serializeLegacyKey + (fn [s] (.serializeLegacyKey native-status s)) :setBlankPreviewFlag identity :encodeTransfer