From 66ef32d3b910c6c43794bc179c3f30dc36245ce9 Mon Sep 17 00:00:00 2001 From: janherich Date: Fri, 5 Oct 2018 00:08:35 +0200 Subject: [PATCH] Move all the legacy handling into transit --- src/status_im/chat/commands/receiving.cljs | 11 +--- src/status_im/chat/commands/sending.cljs | 36 ++--------- src/status_im/chat/models/input.cljs | 2 +- src/status_im/chat/models/message.cljs | 11 +--- src/status_im/constants.cljs | 4 +- .../realm/schemas/account/core.cljs | 15 ++++- .../realm/schemas/account/migrations.cljs | 8 +++ src/status_im/transport/message/transit.cljs | 59 +++++++++++++++---- .../ui/screens/chat/message/message.cljs | 11 +--- .../ui/screens/home/views/inner_item.cljs | 4 +- 10 files changed, 86 insertions(+), 75 deletions(-) diff --git a/src/status_im/chat/commands/receiving.cljs b/src/status_im/chat/commands/receiving.cljs index e49059d3e5..2a71cd5602 100644 --- a/src/status_im/chat/commands/receiving.cljs +++ b/src/status_im/chat/commands/receiving.cljs @@ -2,20 +2,11 @@ (:require [status-im.chat.commands.protocol :as protocol] [status-im.utils.fx :as fx])) -;; TODO(janherich) remove after couple of releases when danger of messages -;; with old command references will be low -(def ^:private old->new-path - {["transactor" :command 83 "send"] ["send" #{:personal-chats}] - ["transactor" :command 83 "request"] ["request" #{:personal-chats}]}) - (defn lookup-command-by-ref "Function which takes message object and looks up associated entry from the `id->command` map if it can be found" [{:keys [content]} id->command] - (when-let [path (or (:command-path content) - (:command-ref content))] - (or (get id->command path) - (get id->command (get old->new-path path))))) + (get id->command (:command-path content))) (fx/defn receive "Performs receive effects for command message. Does nothing diff --git a/src/status_im/chat/commands/sending.cljs b/src/status_im/chat/commands/sending.cljs index b48309f470..7a06fc31ea 100644 --- a/src/status_im/chat/commands/sending.cljs +++ b/src/status_im/chat/commands/sending.cljs @@ -7,40 +7,16 @@ [status-im.chat.models.message :as chat.message] [status-im.utils.fx :as fx])) -;; TODO(janherich) remove after couple of releases when danger of messages -;; with old command references will be low -(defn- new->old - [path parameter-map] - (get {["send" #{:personal-chats}] {:content {:command-ref ["transactor" :command 83 "send"] - :command "send" - :bot "transactor" - :command-scope-bitmask 83} - :content-type constants/content-type-command} - ["request" #{:personal-chats}] {:content {:command-ref ["transactor" :command 83 "request"] - :request-command-ref ["transactor" :command 83 "send"] - :command "request" - :request-command "send" - :bot "transactor" - :command-scope-bitmask 83 - :prefill [(get parameter-map :asset) - (get parameter-map :amount)]} - :content-type constants/content-type-command-request}} - path - {:content-type constants/content-type-command})) - (defn- create-command-message "Create message map from chat-id, command & input parameters" [chat-id type parameter-map cofx] - (let [command-path (commands/command-id type) - ;; TODO(janherich) this is just for backward compatibility, can be removed later - {:keys [content content-type]} (new->old command-path parameter-map) - new-parameter-map (and (satisfies? protocol/EnhancedParameters type) - (protocol/enhance-send-parameters type parameter-map cofx))] + (let [command-path (commands/command-id type) + new-parameter-map (and (satisfies? protocol/EnhancedParameters type) + (protocol/enhance-send-parameters type parameter-map cofx))] {:chat-id chat-id - :content-type content-type - :content (merge {:command-path command-path - :params (or new-parameter-map parameter-map)} - content)})) + :content-type constants/content-type-command + :content {:command-path command-path + :params (or new-parameter-map parameter-map)}})) (fx/defn validate-and-send "Validates and sends command in current chat" diff --git a/src/status_im/chat/models/input.cljs b/src/status_im/chat/models/input.cljs index be6149023d..160497060d 100644 --- a/src/status_im/chat/models/input.cljs +++ b/src/status_im/chat/models/input.cljs @@ -125,7 +125,7 @@ (fx/merge cofx {:db (assoc-in db [:chats current-chat-id :metadata :responding-to-message] nil)} (chat.message/send-message {:chat-id current-chat-id - :content-type constants/text-content-type + :content-type constants/content-type-text :content (cond-> {:text input-text} reply-to-message (assoc :response-to reply-to-message))}) diff --git a/src/status_im/chat/models/message.cljs b/src/status_im/chat/models/message.cljs index 0e2393d2e2..d85a4f4219 100644 --- a/src/status_im/chat/models/message.cljs +++ b/src/status_im/chat/models/message.cljs @@ -101,11 +101,6 @@ message (assoc message :clock-value (utils.clocks/send last-clock-value)))) -(defn- update-legacy-data [{:keys [content-type content] :as message}] - (cond-> message - (= constants/content-type-command-request content-type) - (assoc :content-type constants/content-type-command))) - (fx/defn display-notification [cofx chat-id] (when config/in-app-notifications-enabled? @@ -130,9 +125,7 @@ (commands-receiving/enhance-receive-parameters cofx) (ensure-clock-value chat) ;; TODO (cammellos): Refactor so it's not computed twice - (add-outgoing-status cofx) - ;; TODO (janherich): Remove after couple of releases - update-legacy-data)] + (add-outgoing-status cofx))] (fx/merge cofx {:confirm-messages-processed [{:web3 web3 :js-obj js-obj}]} @@ -207,7 +200,7 @@ :timestamp timestamp :show? true :content content - :content-type constants/text-content-type}) + :content-type constants/content-type-text}) (defn group-message? [{:keys [message-type]}] (#{:group-user-message :public-group-user-message} message-type)) diff --git a/src/status_im/constants.cljs b/src/status_im/constants.cljs index 1bd154a30f..7fc414730e 100644 --- a/src/status_im/constants.cljs +++ b/src/status_im/constants.cljs @@ -6,14 +6,14 @@ (def ethereum-rpc-url "http://localhost:8545") -(def text-content-type "text/plain") +(def content-type-text "text/plain") (def content-type-command "command") (def content-type-command-request "command-request") (def content-type-status "status") (def content-type-emoji "emoji") (def desktop-content-types - #{text-content-type content-type-emoji}) + #{content-type-text content-type-emoji}) (def min-password-length 6) (def max-chat-name-length 20) diff --git a/src/status_im/data_store/realm/schemas/account/core.cljs b/src/status_im/data_store/realm/schemas/account/core.cljs index 10b7a378f1..8ae8f345f2 100644 --- a/src/status_im/data_store/realm/schemas/account/core.cljs +++ b/src/status_im/data_store/realm/schemas/account/core.cljs @@ -142,6 +142,16 @@ browser/v8 dapp-permissions/v9]) +(def v14 [chat/v6 + transport/v6 + contact/v1 + message/v7 + mailserver/v11 + user-status/v1 + local-storage/v1 + browser/v8 + dapp-permissions/v9]) + ;; put schemas ordered by version (def schemas [{:schema v1 :schemaVersion 1 @@ -181,4 +191,7 @@ :migration migrations/v12} {:schema v13 :schemaVersion 13 - :migration migrations/v13}]) + :migration migrations/v13} + {:schema v14 + :schemaVersion 14 + :migration migrations/v14}]) diff --git a/src/status_im/data_store/realm/schemas/account/migrations.cljs b/src/status_im/data_store/realm/schemas/account/migrations.cljs index 841a0d8a1c..36299fb777 100644 --- a/src/status_im/data_store/realm/schemas/account/migrations.cljs +++ b/src/status_im/data_store/realm/schemas/account/migrations.cljs @@ -86,3 +86,11 @@ (defn v13 [old-realm new-realm] (log/debug "migrating v13 account database")) + +(defn v14 [old-realm new-realm] + (log/debug "migrating v14 account database") + (some-> new-realm + (.objects "message") + (.filtered (str "content-type = \"command-request\"")) + (.map (fn [message _ _] + (aset message "content-type" "command"))))) diff --git a/src/status_im/transport/message/transit.cljs b/src/status_im/transport/message/transit.cljs index a3c2044fe9..6dd69e47ba 100644 --- a/src/status_im/transport/message/transit.cljs +++ b/src/status_im/transport/message/transit.cljs @@ -44,14 +44,36 @@ (rep [this {:keys [name profile-image address fcm-token]}] #js [name profile-image address fcm-token])) +;; It's necessary to support old clients understanding only older, verbose command content (`release/0.9.25` and older) +(defn- new->legacy-command-data [{:keys [command-path params] :as content}] + (get {["send" #{:personal-chats}] [{:command-ref ["transactor" :command 83 "send"] + :command "send" + :bot "transactor" + :command-scope-bitmask 83} + constants/content-type-command] + ["request" #{:personal-chats}] [{:command-ref ["transactor" :command 83 "request"] + :request-command-ref ["transactor" :command 83 "send"] + :command "request" + :request-command "send" + :bot "transactor" + :command-scope-bitmask 83 + :prefill [(get params :asset) + (get params :amount)]} + constants/content-type-command-request]} + command-path)) + (deftype MessageHandler [] Object (tag [this v] "c4") (rep [this {:keys [content content-type message-type clock-value timestamp]}] - (if (= content-type constants/text-content-type) - ;; append new content add the end, still pass content the old way at the old index + (condp = content-type + constants/content-type-text ;; append new content add the end, still pass content the old way at the old index #js [(:text content) content-type message-type clock-value timestamp content] - #js [content content-type message-type clock-value timestamp content]))) + constants/content-type-command ;; handle command compatibility issues + (let [[legacy-content legacy-content-type] (new->legacy-command-data content)] + #js [(merge content legacy-content) (or legacy-content-type content-type) message-type clock-value timestamp]) + ;; no need for legacy conversions for rest of the content types + #js [content content-type message-type clock-value timestamp]))) (deftype MessagesSeenHandler [] Object @@ -86,15 +108,31 @@ ;; Reader handlers ;; -(defn- safe-content-parse [content-type content] +(def ^:private legacy-ref->new-path + {["transactor" :command 83 "send"] ["send" #{:personal-chats}] + ["transactor" :command 83 "request"] ["request" #{:personal-chats}]}) + +(defn- legacy->new-command-content [{:keys [command-path command-ref] :as content}] + (if command-path + ;; `:command-path` set, message produced by newer app version, nothing to do + content + ;; we have to look up `:command-path` based on legacy `:command-ref` value (`release/0.9.25` and older) and assoc it to content + (assoc content :command-path (get legacy-ref->new-path command-ref)))) + +(defn- legacy->new-message-data [content content-type] ;; handling only the text content case - (if (= content-type constants/text-content-type) + (cond + (= content-type constants/content-type-text) (if (and (map? content) (string? (:text content))) ;; correctly formatted map - content + [content content-type] ;; create safe `{:text string-content}` value from anything else - {:text (str content)}) - content)) + [{:text (str content)} content-type]) + (or (= content-type constants/content-type-command) + (= content-type constants/content-type-command-request)) + [(legacy->new-command-content content) constants/content-type-command] + :else + [content content-type])) ;; Here we only need to call the record with the arguments parsed from the clojure datastructures (def reader (transit/reader :json @@ -106,9 +144,10 @@ "c3" (fn [[name profile-image address fcm-token]] (v1.contact/ContactRequestConfirmed. name profile-image address fcm-token)) "c4" (fn [[legacy-content content-type message-type clock-value timestamp content]] - (v1.protocol/Message. (safe-content-parse content-type (or content legacy-content)) content-type message-type clock-value timestamp)) + (let [[new-content new-content-type] (legacy->new-message-data (or content legacy-content) content-type)] + (v1.protocol/Message. new-content new-content-type message-type clock-value timestamp))) "c7" (fn [[content content-type message-type clock-value timestamp]] - (v1.protocol/Message. (safe-content-parse content-type content) content-type message-type clock-value timestamp)) + (v1.protocol/Message. content content-type message-type clock-value timestamp)) "c5" (fn [message-ids] (v1.protocol/MessagesSeen. message-ids)) "c6" (fn [[name profile-image address fcm-token]] diff --git a/src/status_im/ui/screens/chat/message/message.cljs b/src/status_im/ui/screens/chat/message/message.cljs index c43bb66d6c..1bc002de26 100644 --- a/src/status_im/ui/screens/chat/message/message.cljs +++ b/src/status_im/ui/screens/chat/message/message.cljs @@ -211,7 +211,7 @@ (defmulti message-content (fn [_ message _] (message :content-type))) -(defmethod message-content constants/text-content-type +(defmethod message-content constants/content-type-text [wrapper message] [wrapper message [text-message message]]) @@ -219,13 +219,6 @@ [_ _] [message-content-status]) -;; TODO(janherich) in the future, `content-type-command-request` will be deprecated -;; as it's the same thing as command -(defmethod message-content constants/content-type-command-request - [wrapper message] - [wrapper message - [message-view message [message-content-command message]]]) - (defmethod message-content constants/content-type-command [wrapper message] [wrapper message @@ -359,7 +352,7 @@ [react/touchable-highlight {:on-press (fn [_] (re-frame/dispatch [:chat.ui/set-chat-ui-props {:messages-focused? true}]) (react/dismiss-keyboard!)) - :on-long-press #(when (= content-type constants/text-content-type) + :on-long-press #(when (= content-type constants/content-type-text) (list-selection/chat-message message-id (:text content) (i18n/label :t/message)))} [react/view {:accessibility-label :chat-item} (let [incoming-group (and group-chat (not outgoing))] 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 a29710359a..22ce8dcf10 100644 --- a/src/status_im/ui/screens/home/views/inner_item.cljs +++ b/src/status_im/ui/screens/home/views/inner_item.cljs @@ -44,9 +44,7 @@ :accessibility-label :chat-message-text} (:text content)] - (contains? #{constants/content-type-command - constants/content-type-command-request} - content-type) + (= constants/content-type-command content-type) [command-short-preview message] :else