Move all the legacy handling into transit

This commit is contained in:
janherich 2018-10-05 00:08:35 +02:00
parent f507484dc8
commit 66ef32d3b9
No known key found for this signature in database
GPG Key ID: C23B473AFBE94D13
10 changed files with 86 additions and 75 deletions

View File

@ -2,20 +2,11 @@
(:require [status-im.chat.commands.protocol :as protocol] (:require [status-im.chat.commands.protocol :as protocol]
[status-im.utils.fx :as fx])) [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 (defn lookup-command-by-ref
"Function which takes message object and looks up associated entry from the "Function which takes message object and looks up associated entry from the
`id->command` map if it can be found" `id->command` map if it can be found"
[{:keys [content]} id->command] [{:keys [content]} id->command]
(when-let [path (or (:command-path content) (get id->command (:command-path content)))
(:command-ref content))]
(or (get id->command path)
(get id->command (get old->new-path path)))))
(fx/defn receive (fx/defn receive
"Performs receive effects for command message. Does nothing "Performs receive effects for command message. Does nothing

View File

@ -7,40 +7,16 @@
[status-im.chat.models.message :as chat.message] [status-im.chat.models.message :as chat.message]
[status-im.utils.fx :as fx])) [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 (defn- create-command-message
"Create message map from chat-id, command & input parameters" "Create message map from chat-id, command & input parameters"
[chat-id type parameter-map cofx] [chat-id type parameter-map cofx]
(let [command-path (commands/command-id type) (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) new-parameter-map (and (satisfies? protocol/EnhancedParameters type)
(protocol/enhance-send-parameters type parameter-map cofx))] (protocol/enhance-send-parameters type parameter-map cofx))]
{:chat-id chat-id {:chat-id chat-id
:content-type content-type :content-type constants/content-type-command
:content (merge {:command-path command-path :content {:command-path command-path
:params (or new-parameter-map parameter-map)} :params (or new-parameter-map parameter-map)}}))
content)}))
(fx/defn validate-and-send (fx/defn validate-and-send
"Validates and sends command in current chat" "Validates and sends command in current chat"

View File

@ -125,7 +125,7 @@
(fx/merge cofx (fx/merge cofx
{:db (assoc-in db [:chats current-chat-id :metadata :responding-to-message] nil)} {:db (assoc-in db [:chats current-chat-id :metadata :responding-to-message] nil)}
(chat.message/send-message {:chat-id current-chat-id (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} :content (cond-> {:text input-text}
reply-to-message reply-to-message
(assoc :response-to reply-to-message))}) (assoc :response-to reply-to-message))})

View File

@ -101,11 +101,6 @@
message message
(assoc message :clock-value (utils.clocks/send last-clock-value)))) (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 (fx/defn display-notification
[cofx chat-id] [cofx chat-id]
(when config/in-app-notifications-enabled? (when config/in-app-notifications-enabled?
@ -130,9 +125,7 @@
(commands-receiving/enhance-receive-parameters cofx) (commands-receiving/enhance-receive-parameters cofx)
(ensure-clock-value chat) (ensure-clock-value chat)
;; TODO (cammellos): Refactor so it's not computed twice ;; TODO (cammellos): Refactor so it's not computed twice
(add-outgoing-status cofx) (add-outgoing-status cofx))]
;; TODO (janherich): Remove after couple of releases
update-legacy-data)]
(fx/merge cofx (fx/merge cofx
{:confirm-messages-processed [{:web3 web3 {:confirm-messages-processed [{:web3 web3
:js-obj js-obj}]} :js-obj js-obj}]}
@ -207,7 +200,7 @@
:timestamp timestamp :timestamp timestamp
:show? true :show? true
:content content :content content
:content-type constants/text-content-type}) :content-type constants/content-type-text})
(defn group-message? [{:keys [message-type]}] (defn group-message? [{:keys [message-type]}]
(#{:group-user-message :public-group-user-message} message-type)) (#{:group-user-message :public-group-user-message} message-type))

View File

@ -6,14 +6,14 @@
(def ethereum-rpc-url "http://localhost:8545") (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 "command")
(def content-type-command-request "command-request") (def content-type-command-request "command-request")
(def content-type-status "status") (def content-type-status "status")
(def content-type-emoji "emoji") (def content-type-emoji "emoji")
(def desktop-content-types (def desktop-content-types
#{text-content-type content-type-emoji}) #{content-type-text content-type-emoji})
(def min-password-length 6) (def min-password-length 6)
(def max-chat-name-length 20) (def max-chat-name-length 20)

View File

@ -142,6 +142,16 @@
browser/v8 browser/v8
dapp-permissions/v9]) 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 ;; put schemas ordered by version
(def schemas [{:schema v1 (def schemas [{:schema v1
:schemaVersion 1 :schemaVersion 1
@ -181,4 +191,7 @@
:migration migrations/v12} :migration migrations/v12}
{:schema v13 {:schema v13
:schemaVersion 13 :schemaVersion 13
:migration migrations/v13}]) :migration migrations/v13}
{:schema v14
:schemaVersion 14
:migration migrations/v14}])

View File

@ -86,3 +86,11 @@
(defn v13 [old-realm new-realm] (defn v13 [old-realm new-realm]
(log/debug "migrating v13 account database")) (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")))))

View File

@ -44,14 +44,36 @@
(rep [this {:keys [name profile-image address fcm-token]}] (rep [this {:keys [name profile-image address fcm-token]}]
#js [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 [] (deftype MessageHandler []
Object Object
(tag [this v] "c4") (tag [this v] "c4")
(rep [this {:keys [content content-type message-type clock-value timestamp]}] (rep [this {:keys [content content-type message-type clock-value timestamp]}]
(if (= content-type constants/text-content-type) (condp = content-type
;; append new content add the end, still pass content the old way at the old index 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 [(: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 [] (deftype MessagesSeenHandler []
Object Object
@ -86,15 +108,31 @@
;; Reader handlers ;; 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 ;; 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))) (if (and (map? content) (string? (:text content)))
;; correctly formatted map ;; correctly formatted map
content [content content-type]
;; create safe `{:text string-content}` value from anything else ;; create safe `{:text string-content}` value from anything else
{:text (str content)}) [{:text (str content)} content-type])
content)) (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 ;; Here we only need to call the record with the arguments parsed from the clojure datastructures
(def reader (transit/reader :json (def reader (transit/reader :json
@ -106,9 +144,10 @@
"c3" (fn [[name profile-image address fcm-token]] "c3" (fn [[name profile-image address fcm-token]]
(v1.contact/ContactRequestConfirmed. 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]] "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]] "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] "c5" (fn [message-ids]
(v1.protocol/MessagesSeen. message-ids)) (v1.protocol/MessagesSeen. message-ids))
"c6" (fn [[name profile-image address fcm-token]] "c6" (fn [[name profile-image address fcm-token]]

View File

@ -211,7 +211,7 @@
(defmulti message-content (fn [_ message _] (message :content-type))) (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]
[wrapper message [text-message message]]) [wrapper message [text-message message]])
@ -219,13 +219,6 @@
[_ _] [_ _]
[message-content-status]) [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 (defmethod message-content constants/content-type-command
[wrapper message] [wrapper message]
[wrapper message [wrapper message
@ -359,7 +352,7 @@
[react/touchable-highlight {:on-press (fn [_] [react/touchable-highlight {:on-press (fn [_]
(re-frame/dispatch [:chat.ui/set-chat-ui-props {:messages-focused? true}]) (re-frame/dispatch [:chat.ui/set-chat-ui-props {:messages-focused? true}])
(react/dismiss-keyboard!)) (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)))} (list-selection/chat-message message-id (:text content) (i18n/label :t/message)))}
[react/view {:accessibility-label :chat-item} [react/view {:accessibility-label :chat-item}
(let [incoming-group (and group-chat (not outgoing))] (let [incoming-group (and group-chat (not outgoing))]

View File

@ -44,9 +44,7 @@
:accessibility-label :chat-message-text} :accessibility-label :chat-message-text}
(:text content)] (:text content)]
(contains? #{constants/content-type-command (= constants/content-type-command content-type)
constants/content-type-command-request}
content-type)
[command-short-preview message] [command-short-preview message]
:else :else