From bac2bb8b8e40cd9bea1dac7f4d6d3c02a1de5195 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Mon, 18 Jul 2022 12:43:50 +0100 Subject: [PATCH] Use image server for all kind of images and integrate identity rings https://github.com/status-im/status-go/compare/4cc53630...8c35fffd --- src/status_im/signals/core.cljs | 1 + src/status_im/subs.cljs | 88 ++++++++++++++++--- src/status_im/ui/screens/chat/photos.cljs | 13 ++- .../ui/screens/profile/user/views.cljs | 2 +- src/status_im/utils/image_server.cljs | 31 +++++++ 5 files changed, 114 insertions(+), 21 deletions(-) create mode 100644 src/status_im/utils/image_server.cljs diff --git a/src/status_im/signals/core.cljs b/src/status_im/signals/core.cljs index 144d57d7b0..9ff9ad29e7 100644 --- a/src/status_im/signals/core.cljs +++ b/src/status_im/signals/core.cljs @@ -70,6 +70,7 @@ "mailserver.available" (mailserver/handle-mailserver-available cofx (.-id event-js)) "mailserver.not.working" (mailserver/handle-mailserver-not-working cofx) "discovery.summary" (summary cofx (js->clj event-js :keywordize-keys true)) + "mediaserver.started" {:db (assoc db :mediaserver/port (.-port event-js))} "wakuv2.peerstats" (wakuv2-peer-stats cofx (js->clj event-js :keywordize-keys true)) "messages.new" (transport.message/sanitize-messages-and-process-response cofx event-js true) "wallet" (ethereum.subscriptions/new-wallet-event cofx (js->clj event-js :keywordize-keys true)) diff --git a/src/status_im/subs.cljs b/src/status_im/subs.cljs index ced492aad3..d211cf9aa2 100644 --- a/src/status_im/subs.cljs +++ b/src/status_im/subs.cljs @@ -41,7 +41,8 @@ [status-im.utils.money :as money] [status-im.utils.security :as security] [status-im.wallet.db :as wallet.db] - [status-im.wallet.utils :as wallet.utils])) + [status-im.wallet.utils :as wallet.utils] + [status-im.utils.image-server :as image-server])) ;; TOP LEVEL =========================================================================================================== @@ -267,6 +268,9 @@ (reg-root-key-sub :wallet-connect/session-managed :wallet-connect/session-managed) (reg-root-key-sub :contact-requests/pending :contact-requests/pending) +; media-server +(reg-root-key-sub :mediaserver/port :mediaserver/port) + ; Testing (reg-root-key-sub :messenger/started? :messenger/started?) @@ -1303,15 +1307,27 @@ (re-frame/reg-sub :chats/photo-path :<- [:contacts/contacts] + :<- [:profile/multiaccount] + :<- [:mediaserver/port] + (fn [[contacts multiaccount port] [_ id]] + (let [contact (or (get contacts id) + (when (= id (:public-key multiaccount)) + multiaccount))] + (if (nil? contact) + (image-server/get-identicons-uri port id) + (multiaccounts/displayed-photo contact))))) + +(re-frame/reg-sub + :contacts/name-and-photo + :<- [:contacts/contacts] :<- [:multiaccount] - (fn [[contacts multiaccount] [_ id identicon]] + (fn [[contacts multiaccount] [_ id]] (let [contact (or (get contacts id) (when (= id (:public-key multiaccount)) multiaccount) - (if (not (string/blank? identicon)) - {:identicon identicon} - (contact.db/public-key->new-contact id)))] - (multiaccounts/displayed-photo contact)))) + (contact.db/public-key->new-contact id))] + {:name (multiaccounts/displayed-name contact) + :photo (multiaccounts/displayed-photo contact)}))) (re-frame/reg-sub :chats/unread-messages-number @@ -2320,13 +2336,34 @@ (fn [multiaccount] (get multiaccount :profile-pictures-visibility))) +(defn- replace-contact-image-uri [contact port identity] + (let [identicon (image-server/get-identicons-uri port identity) + contact-images (:images contact) + contact-images (reduce (fn [acc image] (let [image-name (:type image) + ; We pass the clock so that we reload the image if the image is updated + clock (:clock image) + uri (image-server/get-contact-image-uri port identity image-name clock)] + (assoc-in acc [(keyword image-name) :uri] uri))) + contact-images + (vals contact-images))] + (assoc contact :identicon identicon :images contact-images))) + +(defn- reduce-contacts-image-uri [contacts port] + (reduce-kv (fn [acc public-key contact] + (let [contact (replace-contact-image-uri contact port public-key)] + (assoc acc public-key contact))) + {} + contacts)) + (re-frame/reg-sub :contacts/contacts :<- [::contacts] :<- [::profile-pictures-visibility] :<- [:multiaccount/public-key] - (fn [[contacts profile-pictures-visibility public-key]] - (contact.db/enrich-contacts contacts profile-pictures-visibility public-key))) + :<- [:mediaserver/port] + (fn [[contacts profile-pictures-visibility public-key port]] + (let [contacts (contact.db/enrich-contacts contacts profile-pictures-visibility public-key)] + (reduce-contacts-image-uri contacts port)))) (re-frame/reg-sub :contacts/active @@ -2386,15 +2423,21 @@ (string/lower-case search-filter)) contacts))))) +(defn- enrich-contact [_ identity ens-name port] + (let [contact (contact.db/enrich-contact + (contact.db/public-key-and-ens-name->new-contact identity ens-name))] + (replace-contact-image-uri contact port identity))) + (re-frame/reg-sub :contacts/current-contact :<- [:contacts/contacts] :<- [:contacts/current-contact-identity] :<- [:contacts/current-contact-ens-name] - (fn [[contacts identity ens-name]] - (or (get contacts identity) - (contact.db/enrich-contact - (contact.db/public-key-and-ens-name->new-contact identity ens-name))))) + :<- [:mediaserver/port] + (fn [[contacts identity ens-name port]] + (let [contact (get contacts identity)] + (cond-> contact + (nil? contact) (enrich-contact identity ens-name port))))) (re-frame/reg-sub :contacts/contact-by-identity @@ -3005,6 +3048,27 @@ (fn [multiaccount] (pos? (count (get multiaccount :images))))) +(defn- replace-multiaccount-image-uri [multiaccount port] + (let [public-key (:public-key multiaccount) + identicon (image-server/get-identicons-uri port public-key) + multiaccount (assoc multiaccount :identicon identicon) + images (:images multiaccount) + images (reduce (fn [acc current] + (let [key-uid (:keyUid current) + image-name (:type current) + uri (image-server/get-account-image-uri port public-key image-name key-uid)] + (conj acc (assoc current :uri uri)))) + [] + images)] + (assoc multiaccount :images images))) + +(re-frame/reg-sub + :profile/multiaccount + :<- [:multiaccount] + :<- [:mediaserver/port] + (fn [[multiaccount port]] + (replace-multiaccount-image-uri multiaccount port))) + (re-frame/reg-sub :mobile-network/syncing-allowed? :<- [:network/type] diff --git a/src/status_im/ui/screens/chat/photos.cljs b/src/status_im/ui/screens/chat/photos.cljs index 2a492fabbc..9d1d60cbcc 100644 --- a/src/status_im/ui/screens/chat/photos.cljs +++ b/src/status_im/ui/screens/chat/photos.cljs @@ -4,20 +4,17 @@ [status-im.ui.screens.chat.styles.photos :as style] [status-im.profile.db :as profile.db] [status-im.multiaccounts.core :as multiaccounts] - [status-im.utils.image :as utils.image] [quo.design-system.colors :as colors] [status-im.ui.components.fast-image :as fast-image])) (def memo-photo-rend (memoize (fn [photo-path size accessibility-label _] - (let [identicon? (when photo-path (profile.db/base64-png? photo-path))] - [react/view {:style (style/photo-container size)} - [fast-image/fast-image {:source (utils.image/source photo-path) - :style (style/photo size) - :accessibility-label (or accessibility-label :chat-icon)}] - (when identicon? - [react/view {:style (style/photo-border size)}])])))) + [react/view {:style (style/photo-container size)} + [fast-image/fast-image {:source {:uri photo-path} + :style (style/photo size) + :accessibility-label (or accessibility-label :chat-icon)}] + [react/view {:style (style/photo-border size)}]]))) ;; "(colors/dark?)" is passed to memoized function to avoid previous themes cache (defn photo [photo-path {:keys [size accessibility-label]}] diff --git a/src/status_im/ui/screens/profile/user/views.cljs b/src/status_im/ui/screens/profile/user/views.cljs index 70d1aa5df2..0c49ea92d9 100644 --- a/src/status_im/ui/screens/profile/user/views.cljs +++ b/src/status_im/ui/screens/profile/user/views.cljs @@ -179,7 +179,7 @@ (defn my-profile [] (fn [] (let [{:keys [public-key ens-verified preferred-name] - :as account} @(re-frame/subscribe [:multiaccount]) + :as account} @(re-frame/subscribe [:profile/multiaccount]) on-share #(re-frame/dispatch [:show-popover {:view :share-chat-key :address public-key diff --git a/src/status_im/utils/image_server.cljs b/src/status_im/utils/image_server.cljs new file mode 100644 index 0000000000..7d6d780344 --- /dev/null +++ b/src/status_im/utils/image_server.cljs @@ -0,0 +1,31 @@ +(ns status-im.utils.image-server + (:require [quo.design-system.colors :as colors] + [status-im.utils.config :as config])) + +(def ^:const image-server-uri-prefix "https://localhost:") +(def ^:const identicons-action "/messages/identicons") +(def ^:const account-images-action "/accountImages") +(def ^:const contact-images-action "/contactImages") + +(defn current-theme [] + (case @colors/theme-type + :light 1 + :dark 2)) + +(defn- timestamp [] + (.getTime (js/Date.))) + +(defn get-identicons-uri [port public-key] + (let [base (str image-server-uri-prefix port identicons-action "?publicKey=" public-key "&theme=" (current-theme) "&clock=" (timestamp))] + (cond-> base + @config/new-ui-enabled? (str "&addRing=1")))) + +(defn get-account-image-uri [port public-key image-name key-uid] + (let [base (str image-server-uri-prefix port account-images-action "?publicKey=" public-key "&keyUid=" key-uid "&imageName=" image-name "&theme=" (current-theme) "&clock=" (timestamp))] + (cond-> base + @config/new-ui-enabled? (str "&addRing=1")))) + +(defn get-contact-image-uri [port public-key image-name clock] + (let [base (str image-server-uri-prefix port contact-images-action "?publicKey=" public-key "&imageName=" image-name "&theme=" (current-theme) "&clock=" clock)] + (cond-> base + @config/new-ui-enabled? (str "&addRing=1")))) \ No newline at end of file