From 28f7ea0408aa308056108b38b222fefb6a0694cd Mon Sep 17 00:00:00 2001 From: Julien Eluard Date: Thu, 27 Jun 2019 11:50:07 +0200 Subject: [PATCH] Enable stickers on all envs. Use contenthash to identify stickers Signed-off-by: Julien Eluard --- .env | 1 - .env.jenkins | 1 - .env.nightly | 1 - .env.prod | 1 - src/status_im/chat/models/input.cljs | 6 +-- src/status_im/events.cljs | 4 +- src/status_im/stickers/core.cljs | 16 ++++--- src/status_im/subs.cljs | 6 +-- src/status_im/transport/db.cljs | 2 +- .../ui/screens/chat/input/input.cljs | 2 +- .../ui/screens/chat/message/message.cljs | 3 +- .../ui/screens/chat/stickers/views.cljs | 46 +++++++++---------- .../ui/screens/desktop/main/chat/views.cljs | 5 +- .../screens/desktop/main/tabs/home/views.cljs | 24 +++++----- .../ui/screens/home/views/inner_item.cljs | 3 +- src/status_im/ui/screens/stickers/views.cljs | 13 +++--- src/status_im/utils/config.cljs | 1 - src/status_im/utils/contenthash.cljs | 7 +++ test/cljs/status_im/test/runner.cljs | 2 + test/cljs/status_im/test/stickers/core.cljs | 7 +++ 20 files changed, 86 insertions(+), 65 deletions(-) create mode 100644 test/cljs/status_im/test/stickers/core.cljs diff --git a/.env b/.env index a60dc47e03..c53b00feb6 100644 --- a/.env +++ b/.env @@ -17,7 +17,6 @@ POW_TARGET=0.002 POW_TIME=1 SNOOPY=0 RPC_NETWORKS_ONLY=0 -STICKERS_ENABLED=1 PARTITIONED_TOPIC=0 CONTRACT_NODES=1 MOBILE_UI_FOR_DESKTOP=0 diff --git a/.env.jenkins b/.env.jenkins index e4c2c372a5..205f1fd385 100644 --- a/.env.jenkins +++ b/.env.jenkins @@ -17,7 +17,6 @@ POW_TARGET=0.002 POW_TIME=1 SNOOPY=0 RPC_NETWORKS_ONLY=0 -STICKERS_ENABLED=1 PARTITIONED_TOPIC=0 CONTRACT_NODES=1 MOBILE_UI_FOR_DESKTOP=0 diff --git a/.env.nightly b/.env.nightly index 1375dc8351..67b5f2616a 100644 --- a/.env.nightly +++ b/.env.nightly @@ -15,7 +15,6 @@ POW_TARGET=0.002 POW_TIME=1 SNOOPY=0 RPC_NETWORKS_ONLY=0 -STICKERS_ENABLED=0 PARTITIONED_TOPIC=0 CONTRACT_NODES=1 MOBILE_UI_FOR_DESKTOP=0 diff --git a/.env.prod b/.env.prod index b624f78d8a..a14463e309 100644 --- a/.env.prod +++ b/.env.prod @@ -15,6 +15,5 @@ POW_TARGET=0.002 POW_TIME=1 SNOOPY=0 RPC_NETWORKS_ONLY=1 -STICKERS_ENABLED=0 PARTITIONED_TOPIC=0 MOBILE_UI_FOR_DESKTOP=0 diff --git a/src/status_im/chat/models/input.cljs b/src/status_im/chat/models/input.cljs index 4c4fb6611e..b0ed845fdd 100644 --- a/src/status_im/chat/models/input.cljs +++ b/src/status_im/chat/models/input.cljs @@ -152,12 +152,12 @@ :text message-text})}))) (fx/defn send-sticker-fx - [{:keys [db] :as cofx} {:keys [uri pack]} current-chat-id] - (when-not (string/blank? uri) + [{:keys [db] :as cofx} {:keys [hash pack]} current-chat-id] + (when-not (string/blank? hash) (chat.message/send-message cofx {:chat-id current-chat-id :content-type constants/content-type-sticker :content (cond-> {:chat-id current-chat-id - :uri uri + :hash hash :pack pack :text "Update to latest version to see a nice sticker here!"})}))) diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index 87b490b520..ccdb17f264 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -824,10 +824,10 @@ (handlers/register-handler-fx :chat/send-sticker - (fn [{{:keys [current-chat-id] :account/keys [account]} :db :as cofx} [_ {:keys [uri] :as sticker}]] + (fn [{{:keys [current-chat-id] :account/keys [account]} :db :as cofx} [_ {:keys [hash] :as sticker}]] (fx/merge cofx - (accounts/update-recent-stickers (conj (remove #(= uri %) (:recent-stickers account)) uri)) + (accounts/update-recent-stickers (conj (remove #(= hash %) (:recent-stickers account)) hash)) (chat.input/send-sticker-fx sticker current-chat-id)))) (handlers/register-handler-fx diff --git a/src/status_im/stickers/core.cljs b/src/status_im/stickers/core.cljs index ad4b9bf332..b9a68ede0e 100644 --- a/src/status_im/stickers/core.cljs +++ b/src/status_im/stickers/core.cljs @@ -99,12 +99,18 @@ (assoc :stickers/selected-pack id))} (accounts/update-stickers (conj (:stickers account) (pr-str pack)))))) +(defn valid-sticker? [sticker] + (contains? sticker :hash)) + (fx/defn load-sticker-pack-success [{:keys [db] :as cofx} edn-string id price open?] - (let [pack (assoc (get (edn/read-string edn-string) 'meta) - :id id :price price)] + (let [{:keys [stickers] :as pack} (assoc (get (edn/read-string edn-string) 'meta) + :id id :price price)] (fx/merge cofx - {:db (assoc-in db [:stickers/packs id] pack)} + {:db (cond-> db + (and (seq stickers) + (every? valid-sticker? stickers)) + (assoc-in [:stickers/packs id] pack))} #(when open? (navigation/navigate-to-cofx % :stickers-pack-modal pack))))) @@ -127,9 +133,7 @@ hash) :success-event-creator (fn [o] - [:stickers/load-sticker-pack-success o id price open?]) - :failure-event-creator - (constantly nil)}}) + [:stickers/load-sticker-pack-success o id price open?])}}) (fx/defn load-packs [{:keys [db]}] diff --git a/src/status_im/subs.cljs b/src/status_im/subs.cljs index 8948691223..0262ba8e85 100644 --- a/src/status_im/subs.cljs +++ b/src/status_im/subs.cljs @@ -893,9 +893,9 @@ (fn [[{:keys [id]} packs]] (first (filter #(= (:id %) id) packs)))) -(defn find-pack-id-for-uri [sticker-uri packs] +(defn find-pack-id-for-hash [sticker-uri packs] (some (fn [{:keys [stickers id]}] - (when (some #(= sticker-uri (:uri %)) stickers) + (when (some #(= sticker-uri (:hash %)) stickers) id)) packs)) @@ -904,7 +904,7 @@ :<- [:account/account] :<- [:stickers/installed-packs-vals] (fn [[{:keys [recent-stickers]} packs]] - (map (fn [uri] {:uri uri :pack (find-pack-id-for-uri uri packs)}) recent-stickers))) + (map (fn [hash] {:hash hash :pack (find-pack-id-for-hash hash packs)}) recent-stickers))) ;;EXTENSIONS =========================================================================================================== diff --git a/src/status_im/transport/db.cljs b/src/status_im/transport/db.cljs index b7c4d312f2..664761098e 100644 --- a/src/status_im/transport/db.cljs +++ b/src/status_im/transport/db.cljs @@ -104,7 +104,7 @@ :req-opt [:message.content/response-to])) (spec/def :message.command/content (spec/keys :req-un [:message.content/command-path :message.content/params])) -(spec/def :message.sticker/content (spec/keys :req-un [:message.content/uri])) +(spec/def :message.sticker/content (spec/keys :req-un [:message.content/hash])) (defmulti content-type :content-type) diff --git a/src/status_im/ui/screens/chat/input/input.cljs b/src/status_im/ui/screens/chat/input/input.cljs index 28d18cd3ff..4dbeefdd58 100644 --- a/src/status_im/ui/screens/chat/input/input.cljs +++ b/src/status_im/ui/screens/chat/input/input.cljs @@ -201,7 +201,7 @@ [reply-message-view] [react/view {:style style/input-container} [input-view {:single-line-input? single-line-input? :set-text set-text :state-text state-text}] - (when (and config/stickers-enabled? input-text-empty?) + (when input-text-empty? [stickers/button show-stickers?]) (if input-text-empty? [commands-button] diff --git a/src/status_im/ui/screens/chat/message/message.cljs b/src/status_im/ui/screens/chat/message/message.cljs index ba6c2be958..e0e904f8bc 100644 --- a/src/status_im/ui/screens/chat/message/message.cljs +++ b/src/status_im/ui/screens/chat/message/message.cljs @@ -15,6 +15,7 @@ [status-im.ui.screens.chat.photos :as photos] [status-im.ui.screens.chat.styles.message.message :as style] [status-im.ui.screens.chat.utils :as chat.utils] + [status-im.utils.contenthash :as contenthash] [status-im.utils.platform :as platform]) (:require-macros [status-im.utils.views :refer [defview letsubs]])) @@ -144,7 +145,7 @@ [wrapper {:keys [content] :as message}] [wrapper message [react/image {:style {:margin 10 :width 140 :height 140} - :source {:uri (:uri content)}}]]) + :source {:uri (contenthash/url (:hash content))}}]]) (defmethod message-content :default [wrapper {:keys [content-type] :as message}] diff --git a/src/status_im/ui/screens/chat/stickers/views.cljs b/src/status_im/ui/screens/chat/stickers/views.cljs index a795363a72..3adba1ae09 100644 --- a/src/status_im/ui/screens/chat/stickers/views.cljs +++ b/src/status_im/ui/screens/chat/stickers/views.cljs @@ -1,13 +1,15 @@ (ns status-im.ui.screens.chat.stickers.views (:require-macros [status-im.utils.views :refer [defview letsubs]]) (:require [re-frame.core :as re-frame] + [reagent.core :as reagent] + [status-im.stickers.core :as stickers] [status-im.ui.components.react :as react] [status-im.ui.components.icons.vector-icons :as vector-icons] [status-im.ui.components.colors :as colors] [status-im.i18n :as i18n] [status-im.ui.screens.chat.stickers.styles :as styles] [status-im.ui.components.animation :as anim] - [reagent.core :as reagent] + [status-im.utils.contenthash :as contenthash] [status-im.utils.platform :as platform])) (def icon-size 28) @@ -43,13 +45,14 @@ [react/view {:width window-width :flex 1} [react/scroll-view [react/view {:style styles/stickers-panel} - (for [{:keys [uri] :as sticker} stickers] - ^{:key uri} + (for [{:keys [hash pack] :as sticker} stickers] + ^{:key (str hash)} [react/touchable-highlight {:style {:height 75 :width 75 :margin 5} :on-press #(re-frame/dispatch [:chat/send-sticker sticker])} - [react/image {:style {:resize-mode :cover :width "100%" :height "100%"} - :accessibility-label :sticker-icon - :source {:uri uri}}]])]]]) + [react/view + [react/image {:style {:resize-mode :cover :width "100%" :height "100%"} + :accessibility-label :sticker-icon + :source {:uri (contenthash/url (str "0x" hash))}}]]])]]]) (defview recent-stickers-panel [window-width] (letsubs [stickers [:stickers/recent]] @@ -83,18 +86,19 @@ {:component-will-update (fn [_ [_ installed-packs selected-pack]] (update-scroll-position @ref installed-packs selected-pack window-width)) :component-did-mount #(update-scroll-position @ref installed-packs selected-pack window-width)} - [react/scroll-view {:style {:flex 1} :horizontal true :paging-enabled true - :ref #(reset! ref %) - :shows-horizontal-scroll-indicator false - :on-momentum-scroll-end #(on-scroll % installed-packs window-width) - :scrollEventThrottle 8 - :on-scroll #(reset! scroll-x (.-nativeEvent.contentOffset.x %)) - :on-layout #(reset! content-width (-> % .-nativeEvent .-layout .-width))} - ^{:key "recent"} - [recent-stickers-panel @content-width] - (for [{:keys [stickers id]} installed-packs] - ^{:key (str "sticker" id)} - [stickers-panel (map #(assoc % :pack id) stickers) @content-width])])) + (let [width @content-width] + [react/scroll-view {:style {:flex 1} :horizontal true :paging-enabled true + :ref #(reset! ref %) + :shows-horizontal-scroll-indicator false + :on-momentum-scroll-end #(on-scroll % installed-packs window-width) + :scrollEventThrottle 8 + :on-scroll #(reset! scroll-x (.-nativeEvent.contentOffset.x %)) + :on-layout #(reset! content-width (-> % .-nativeEvent .-layout .-width))} + ^{:key "recent"} + [recent-stickers-panel width] + (for [{:keys [stickers id]} installed-packs] + ^{:key (str "sticker" id)} + [stickers-panel (map #(assoc % :pack id) (filter stickers/valid-sticker? stickers)) width])]))) (defn pack-icon [{:keys [id on-press background-color] :or {on-press #(re-frame/dispatch [:stickers/select-pack id])}} @@ -104,10 +108,6 @@ [react/view {:style (styles/pack-icon background-color icon-size icon-horizontal-margin)} icon]]]) -(defn pack-stickers [packs pack-id] - (let [{:keys [stickers id]} (some #(when (= pack-id (:id %)) %) packs)] - (map #(assoc % :pack id) stickers))) - (defn show-panel-anim [bottom-anim-value alpha-value] (anim/start @@ -157,5 +157,5 @@ [pack-icon {:id id :background-color colors/white} [react/image {:style {:width icon-size :height icon-size :border-radius (/ icon-size 2)} - :source {:uri thumbnail}}]])] + :source {:uri (contenthash/url thumbnail)}}]])] [scroll-indicator]]]]])) diff --git a/src/status_im/ui/screens/desktop/main/chat/views.cljs b/src/status_im/ui/screens/desktop/main/chat/views.cljs index 0f034e1762..387f6b4b39 100644 --- a/src/status_im/ui/screens/desktop/main/chat/views.cljs +++ b/src/status_im/ui/screens/desktop/main/chat/views.cljs @@ -27,7 +27,8 @@ [status-im.ui.screens.desktop.main.chat.emoji :as emoji] [status-im.ui.components.icons.vector-icons :as icons] [status-im.ui.screens.chat.message.gap :as gap] - [status-im.ui.components.animation :as animation]) + [status-im.ui.components.animation :as animation] + [status-im.utils.contenthash :as contenthash]) (:require-macros [status-im.utils.views :as views])) (defn toolbar-chat-view @@ -175,7 +176,7 @@ [_ _ {:keys [content] :as message}] [message-wrapper message [react/image {:style {:margin 10 :width 140 :height 140} - :source {:uri (:uri content)}}]]) + :source {:uri (contenthash/url (:hash content))}}]]) (views/defview message-content-status [text message] [react/view diff --git a/src/status_im/ui/screens/desktop/main/tabs/home/views.cljs b/src/status_im/ui/screens/desktop/main/tabs/home/views.cljs index 8ffef880dd..2ada71480e 100644 --- a/src/status_im/ui/screens/desktop/main/tabs/home/views.cljs +++ b/src/status_im/ui/screens/desktop/main/tabs/home/views.cljs @@ -17,7 +17,8 @@ [reagent.core :as reagent] [status-im.ui.components.action-button.styles :as action-button.styles] [status-im.ui.components.action-button.action-button :as action-button] - [status-im.utils.config :as config])) + [status-im.utils.config :as config] + [status-im.utils.contenthash :as contenthash])) (views/defview chat-list-item-inner-view [{:keys [chat-id name group-chat @@ -54,16 +55,17 @@ :number-of-lines 1 :style (styles/chat-name current?)} name]] - (if (and (:uri (:content last-message) (= constants/content-type-sticker (:content-type last-message)))) - [react/image {:style {:margin 2 :width 30 :height 30} - :source {:uri (:uri (:content last-message))}}] - [react/text {:ellipsize-mode :tail - :number-of-lines 1 - :style styles/chat-last-message} - (if (= constants/content-type-command (:content-type last-message)) - [chat-item/command-short-preview last-message] - (or (:text last-message-content) - (i18n/label :no-messages-yet)))])] + (let [uri (contenthash/url (:content last-message))] + (if (and uri (= constants/content-type-sticker (:content-type last-message))) + [react/image {:style {:margin 2 :width 30 :height 30} + :source {:uri uri}}] + [react/text {:ellipsize-mode :tail + :number-of-lines 1 + :style styles/chat-last-message} + (if (= constants/content-type-command (:content-type last-message)) + [chat-item/command-short-preview last-message] + (or (:text last-message-content) + (i18n/label :no-messages-yet)))]))] [react/view {:style styles/timestamp} [chat-item/message-timestamp (:timestamp last-message)] (when (pos? unviewed-messages-count) diff --git a/src/status_im/ui/screens/home/views/inner_item.cljs b/src/status_im/ui/screens/home/views/inner_item.cljs index 7ad716702a..7a88ea043f 100644 --- a/src/status_im/ui/screens/home/views/inner_item.cljs +++ b/src/status_im/ui/screens/home/views/inner_item.cljs @@ -12,6 +12,7 @@ [status-im.ui.components.list.views :as list] [status-im.ui.components.react :as react] [status-im.ui.screens.home.styles :as styles] + [status-im.utils.contenthash :as contenthash] [status-im.utils.core :as utils] [status-im.utils.datetime :as time]) (:require-macros [status-im.utils.views :refer [defview letsubs]])) @@ -36,7 +37,7 @@ (= constants/content-type-sticker content-type) [react/image {:style {:margin 2 :width 30 :height 30} - :source {:uri (:uri content)}}] + :source {:uri (contenthash/url (:hash content))}}] (string/blank? (:text content)) [react/text {:style styles/last-message-text} diff --git a/src/status_im/ui/screens/stickers/views.cljs b/src/status_im/ui/screens/stickers/views.cljs index 3fba805620..a4b009aea1 100644 --- a/src/status_im/ui/screens/stickers/views.cljs +++ b/src/status_im/ui/screens/stickers/views.cljs @@ -9,6 +9,7 @@ [status-im.ui.components.styles :as components.styles] [status-im.ui.components.toolbar.view :as toolbar] [status-im.ui.screens.stickers.styles :as styles] + [status-im.utils.contenthash :as contenthash] [status-im.utils.money :as money]) (:require-macros [status-im.utils.views :refer [defview letsubs]])) @@ -47,9 +48,9 @@ (defn pack-badge [{:keys [name author price thumbnail preview id installed owned pending] :as pack}] [react/touchable-highlight {:on-press #(re-frame/dispatch [:navigate-to :stickers-pack pack])} [react/view {:margin-bottom 27} - [react/image {:style {:height 200 :border-radius 20} :source {:uri preview}}] + [react/image {:style {:height 200 :border-radius 20} :source {:uri (contenthash/url preview)}}] [react/view {:height 64 :align-items :center :flex-direction :row} - [thumbnail-icon thumbnail 40] + [thumbnail-icon (contenthash/url thumbnail) 40] [react/view {:padding-horizontal 16 :flex 1} [react/text {:accessibility-label :sticker-pack-name} name] [react/text {:style {:color colors/gray :margin-top 6} @@ -79,7 +80,7 @@ [react/keyboard-avoiding-view components.styles/flex [toolbar/simple-toolbar nil modal?] [react/view {:height 74 :align-items :center :flex-direction :row :padding-horizontal 16} - [thumbnail-icon thumbnail 64] + [thumbnail-icon (contenthash/url thumbnail) 64] [react/view {:padding-horizontal 16 :flex 1} [react/text {:style {:typography :header}} name] [react/text {:style {:color colors/gray :margin-top 6}} author]] @@ -89,10 +90,10 @@ [react/view {:style {:padding-top 8 :flex 1}} [react/scroll-view {:keyboard-should-persist-taps :handled :style {:flex 1}} [react/view {:flex-direction :row :flex-wrap :wrap} - (for [{:keys [uri]} stickers] - ^{:key uri} + (for [{:keys [hash]} stickers] + ^{:key hash} [react/image {:style (styles/sticker-image sticker-icon-size) - :source {:uri uri}}])]]]]])) + :source {:uri (contenthash/url hash)}}])]]]]])) (defview pack [] [pack-main false]) diff --git a/src/status_im/utils/config.cljs b/src/status_im/utils/config.cljs index 5ad12f7a08..deda0fd76e 100644 --- a/src/status_im/utils/config.cljs +++ b/src/status_im/utils/config.cljs @@ -27,7 +27,6 @@ (def cached-webviews-enabled? (enabled? (get-config :CACHED_WEBVIEWS_ENABLED 0))) (def snoopy-enabled? (enabled? (get-config :SNOOPY 0))) (def extensions-enabled? (enabled? (get-config :EXTENSIONS 0))) -(def stickers-enabled? (enabled? (get-config :STICKERS_ENABLED 0))) (def hardwallet-enabled? (enabled? (get-config :HARDWALLET_ENABLED 0))) (def dev-build? (enabled? (get-config :DEV_BUILD 0))) (def erc20-contract-warnings-enabled? (enabled? (get-config :ERC20_CONTRACT_WARNINGS))) diff --git a/src/status_im/utils/contenthash.cljs b/src/status_im/utils/contenthash.cljs index fd3450df60..017bc93421 100644 --- a/src/status_im/utils/contenthash.cljs +++ b/src/status_im/utils/contenthash.cljs @@ -5,6 +5,7 @@ (:require [alphabase.base58 :as b58] [alphabase.hex :as hex] [clojure.string :as string] + [status-im.ethereum.core :as ethereum] [status-im.ipfs.core :as ipfs] [status-im.utils.fx :as fx])) @@ -54,6 +55,12 @@ hex/decode b58/encode))}))) +(defn url [hex] + (let [{:keys [namespace hash]} (decode (ethereum/normalized-address hex))] + (case namespace + :ipfs (str "https://ipfs.infura.io/ipfs/" hash) + ""))) + (fx/defn cat [cofx {:keys [contenthash on-success on-failure]}] (let [{:keys [namespace hash]} (decode contenthash)] diff --git a/test/cljs/status_im/test/runner.cljs b/test/cljs/status_im/test/runner.cljs index 78f865df83..3f47849eb8 100644 --- a/test/cljs/status_im/test/runner.cljs +++ b/test/cljs/status_im/test/runner.cljs @@ -44,6 +44,7 @@ [status-im.test.pairing.core] [status-im.test.search.core] [status-im.test.sign-in.flow] + [status-im.test.stickers.core] [status-im.test.transport.core] [status-im.test.tribute-to-talk.core] [status-im.test.tribute-to-talk.db] @@ -127,6 +128,7 @@ 'status-im.test.pairing.core 'status-im.test.search.core 'status-im.test.sign-in.flow + 'status-im.test.stickers.core 'status-im.test.signing.core 'status-im.test.signing.gas 'status-im.test.transport.core diff --git a/test/cljs/status_im/test/stickers/core.cljs b/test/cljs/status_im/test/stickers/core.cljs new file mode 100644 index 0000000000..05a3bbf6a8 --- /dev/null +++ b/test/cljs/status_im/test/stickers/core.cljs @@ -0,0 +1,7 @@ +(ns status-im.test.stickers.core + (:require [cljs.test :refer-macros [deftest is]] + [status-im.stickers.core :as stickers])) + +(deftest valid-sticker? + (is (true? (stickers/valid-sticker? {:hash ""}))) + (is (false? (stickers/valid-sticker? {})))) \ No newline at end of file