fix #573 & remove staged-commands

This commit is contained in:
Roman Volosovskyi 2016-12-22 15:18:09 +02:00
parent 6002d53c97
commit aad122a724
15 changed files with 115 additions and 205 deletions

View File

@ -1710,6 +1710,12 @@ var phoneConfig = {
title: I18n.t('phone_title'), title: I18n.t('phone_title'),
description: I18n.t('phone_description'), description: I18n.t('phone_description'),
color: "#5bb2a2", color: "#5bb2a2",
validator: function (params) {
return {
validationHandler: "phone",
parameters: [params.phone]
};
},
params: [{ params: [{
name: "phone", name: "phone",
type: status.types.PHONE, type: status.types.PHONE,

View File

@ -168,15 +168,14 @@
(register-handler :set-chat-input-text (register-handler :set-chat-input-text
(u/side-effect! (u/side-effect!
(fn [{:keys [current-chat-id] :as db} [_ text]] (fn [{:keys [current-chat-id]} [_ text]]
(let [{:keys [dapp?] :as contact} (get-in db [:contacts current-chat-id])] (if (console? current-chat-id)
(if (console? current-chat-id) (dispatch [::check-input-for-commands text])
(dispatch [::check-input-for-commands text]) (dispatch [::check-suggestions current-chat-id text])))))
(dispatch [::check-suggestions current-chat-id text]))))))
(register-handler :add-to-chat-input-text (register-handler :add-to-chat-input-text
(u/side-effect! (u/side-effect!
(fn [{:keys [chats current-chat-id] :as db} [_ text-to-add]] (fn [{:keys [chats current-chat-id]} [_ text-to-add]]
(let [input-text (get-in chats [current-chat-id :input-text])] (let [input-text (get-in chats [current-chat-id :input-text])]
(dispatch [:set-chat-input-text (str input-text text-to-add)]))))) (dispatch [:set-chat-input-text (str input-text text-to-add)])))))
@ -194,10 +193,10 @@
(let [text' (if (= :commands type) (let [text' (if (= :commands type)
(str command-prefix text) (str command-prefix text)
text)] text)]
(dispatch [::stage-command-with-content command text'])) (dispatch [::set-command-with-content command text']))
(dispatch [::check-suggestions console-chat-id text]))))) (dispatch [::check-suggestions console-chat-id text])))))
(register-handler ::stage-command-with-content (register-handler ::set-command-with-content
(u/side-effect! (u/side-effect!
(fn [_ [_ [command type] text]] (fn [_ [_ [command type] text]]
(dispatch [:set-chat-command command type]) (dispatch [:set-chat-command command type])

View File

@ -30,7 +30,7 @@
0 0
:suggestions] :suggestions]
params {:parameters (or params {}) params {:parameters (or params {})
:context {:data data}}] :context {:data data}}]
(status/call-jail current-chat-id (status/call-jail current-chat-id
path path
params params
@ -76,8 +76,8 @@
content)]))))) content)])))))
(defn invoke-command-preview! (defn invoke-command-preview!
[{:keys [staged-command] :as db} [_ command-input chat-id]] [db command-message [_ command-input chat-id]]
(let [{:keys [command id]} staged-command (let [{:keys [command]} command-message
{:keys [name type]} command {:keys [name type]} command
parameters (:params (or command-input (commands/get-command-input db))) parameters (:params (or command-input (commands/get-command-input db)))
path [(if (= :command type) :commands :responses) path [(if (= :command type) :commands :responses)
@ -86,12 +86,11 @@
params {:parameters parameters params {:parameters parameters
:context {:platform platform/platform}}] :context {:platform platform/platform}}]
(if (and (console? chat-id) (= name "js")) (if (and (console? chat-id) (= name "js"))
(dispatch [:send-chat-message]) (dispatch [:send-chat-message command-message])
(status/call-jail chat-id (status/call-jail chat-id
path path
params params
#(do (dispatch [:command-preview chat-id id %]) #(dispatch [:command-preview command-message %])))))
(dispatch [:send-chat-message]))))))
(defn command-input (defn command-input
([{:keys [current-chat-id] :as db}] ([{:keys [current-chat-id] :as db}]
@ -120,36 +119,30 @@
(handler) (handler)
(dispatch [::finish-command-staging command-input chat-id])))))))) (dispatch [::finish-command-staging command-input chat-id]))))))))
(register-handler :stage-command (register-handler :validate-command
(fn [{:keys [current-chat-id current-account-id] :as db} [_ command-input command]] (u/side-effect!
(let [command-input (or command-input (commands/get-command-input db)) (fn [{:keys [current-chat-id current-account-id] :as db} [_ command-input command]]
command (or command (commands/get-chat-command db))] (let [command-input (or command-input (commands/get-command-input db))
(dispatch [::start-command-validation! {:command-input command-input command (or command (commands/get-chat-command db))]
:command command (dispatch [::start-command-validation! {:command-input command-input
:chat-id current-chat-id :command command
:address current-account-id}]) :chat-id current-chat-id
(assoc-in db [:disable-staging current-chat-id] true)))) :address current-account-id}])))))
(register-handler ::finish-command-staging (register-handler ::finish-command-staging
[(after #(dispatch [:start-cancel-command])) [(after #(dispatch [:start-cancel-command]))]
(after invoke-command-preview!)] (u/side-effect!
(fn [db [_ command-input chat-id]] (fn [db [_ command-input chat-id :as parameters]]
(let [db (assoc-in db [:chats chat-id :input-text] nil) (let [db (assoc-in db [:chats chat-id :input-text] nil)
{:keys [command content to-message-id params]} (or command-input (command-input db)) {:keys [command to-message-id params]} (or command-input (command-input db))
command-info {:command command command-info {:command command
:params params :params params
:to-message to-message-id :to-message to-message-id
:created-at (time/now-ms) :created-at (time/now-ms)
:id (random/id)}] :id (random/id)
(-> db :chat-id chat-id}]
(commands/stage-command command-info) (dispatch [:set-in [:command->chat (:id command-info)] chat-id])
(assoc-in [:command->chat (:id command-info)] chat-id) (invoke-command-preview! db command-info parameters)))))
(assoc :staged-command command-info)
(assoc-in [:disable-staging chat-id] true)))))
(register-handler :unstage-command
(fn [db [_ staged-command]]
(commands/unstage-command db staged-command)))
(defn set-chat-command (defn set-chat-command
[{:keys [current-chat-id] :as db} [_ command-key type]] [{:keys [current-chat-id] :as db} [_ command-key type]]
@ -285,5 +278,4 @@
(let [suggestions-trigger (keyword (:suggestions-trigger command))] (let [suggestions-trigger (keyword (:suggestions-trigger command))]
(if (= :on-send suggestions-trigger) (if (= :on-send suggestions-trigger)
(dispatch [:invoke-commands-suggestions!]) (dispatch [:invoke-commands-suggestions!])
(dispatch [:stage-command])))))) (dispatch [:validate-command]))))))

View File

@ -31,8 +31,8 @@
(register-handler :invoke-console-command-handler! (register-handler :invoke-console-command-handler!
(u/side-effect! (u/side-effect!
(fn [_ [_ {:keys [chat-id staged-command] :as parameters}]] (fn [_ [_ {:keys [chat-id command-message] :as parameters}]]
(let [{:keys [id command params]} staged-command (let [{:keys [id command params]} command-message
{:keys [name]} command] {:keys [name]} command]
(dispatch [:prepare-command! chat-id parameters]) (dispatch [:prepare-command! chat-id parameters])
((console-commands (keyword name)) params id))))) ((console-commands (keyword name)) params id)))))

View File

@ -49,17 +49,17 @@
(register-handler :send-chat-message (register-handler :send-chat-message
(u/side-effect! (u/side-effect!
(fn [{:keys [current-chat-id current-public-key current-account-id] :as db}] (fn [{:keys [current-chat-id current-public-key current-account-id] :as db}
(let [staged-commands (vals (get-in db [:chats current-chat-id :staged-commands])) [_ {:keys [chat-id] :as command-message}]]
text (get-in db [:chats current-chat-id :input-text]) (let [text (get-in db [:chats current-chat-id :input-text])
data {:commands staged-commands data {:command command-message
:message text :message text
:chat-id current-chat-id :chat-id (or chat-id current-chat-id)
:identity current-public-key :identity current-public-key
:address current-account-id}] :address current-account-id}]
(dispatch [:clear-input current-chat-id]) (dispatch [:clear-input current-chat-id])
(cond (cond
(seq staged-commands) command-message
(dispatch [::check-commands-handlers! data]) (dispatch [::check-commands-handlers! data])
(not (s/blank? text)) (not (s/blank? text))
(dispatch [::prepare-message data])))))) (dispatch [::prepare-message data]))))))
@ -70,9 +70,9 @@
(register-handler ::check-commands-handlers! (register-handler ::check-commands-handlers!
(u/side-effect! (u/side-effect!
(fn [_ [_ {:keys [commands message chat-id] :as params}]] (fn [_ [_ {:keys [command message chat-id] :as params}]]
(doseq [{:keys [command] :as message} commands] (let [{:keys [command] :as message} command]
(let [params' (assoc params :staged-command message) (let [params' (assoc params :command-message message)
command-name (:name (:command message))] command-name (:name (:command message))]
(if (:sent-to-jail? message) (if (:sent-to-jail? message)
;; todo there could be other reasons for "long-running" ;; todo there could be other reasons for "long-running"
@ -98,34 +98,27 @@
(register-handler :prepare-command! (register-handler :prepare-command!
(u/side-effect! (u/side-effect!
(fn [{:keys [current-public-key network-status] :as db} (fn [{:keys [current-public-key network-status] :as db}
[_ add-to-chat-id {:keys [chat-id staged-command command handler-data] :as params}]] [_ add-to-chat-id {:keys [chat-id command-message command handler-data] :as params}]]
(let [clock-value (messages/get-last-clock-value chat-id) (let [clock-value (messages/get-last-clock-value chat-id)
request (:request (:handler-data command)) request (:request (:handler-data command))
command' (->> (assoc staged-command :handler-data handler-data) command' (->> (assoc command-message :handler-data handler-data)
(prepare-command current-public-key chat-id clock-value request) (prepare-command current-public-key chat-id clock-value request)
(cu/check-author-direction db chat-id))] (cu/check-author-direction db chat-id))]
(log/debug "Handler data: " request handler-data (dissoc params :commands :staged-command)) (log/debug "Handler data: " request handler-data (dissoc params :commands :command-message))
(dispatch [:update-message-overhead! chat-id network-status]) (dispatch [:update-message-overhead! chat-id network-status])
(dispatch [:clear-command chat-id (:id staged-command)])
(dispatch [::send-command! add-to-chat-id (assoc params :command command')]) (dispatch [::send-command! add-to-chat-id (assoc params :command command')])
(when (cu/console? chat-id) (when (cu/console? chat-id)
(dispatch [:console-respond-command params])) (dispatch `[:console-respond-command params]))
(when (and (= "send" (get-in staged-command [:command :name])) (when (and (= "send" (get-in command-message [:command :name]))
(not= add-to-chat-id wallet-chat-id)) (not= add-to-chat-id wallet-chat-id))
(let [ct (if request (let [ct (if request
c/content-type-wallet-request c/content-type-wallet-request
c/content-type-wallet-command) c/content-type-wallet-command)
staged-command' (assoc staged-command :id (random/id) command-message' (assoc command-message :id (random/id)
:content-type ct) :content-type ct)
params' (assoc params :staged-command staged-command')] params' (assoc params :command-message command-message')]
(dispatch [:prepare-command! wallet-chat-id params']))))))) (dispatch [:prepare-command! wallet-chat-id params'])))))))
(register-handler :clear-command
(fn [db [_ chat-id id]]
(if chat-id
(update-in db [:chats chat-id :staged-commands] dissoc id)
db)))
(register-handler ::send-command! (register-handler ::send-command!
(u/side-effect! (u/side-effect!
(fn [_ [_ add-to-chat-id params]] (fn [_ [_ add-to-chat-id params]]
@ -157,9 +150,9 @@
(register-handler ::invoke-command-handlers! (register-handler ::invoke-command-handlers!
(u/side-effect! (u/side-effect!
(fn [db [_ {:keys [chat-id address staged-command] (fn [db [_ {:keys [chat-id address command-message]
:as parameters}]] :as parameters}]]
(let [{:keys [id command params]} staged-command (let [{:keys [id command params]} command-message
{:keys [type name]} command {:keys [type name]} command
path [(if (= :command type) :commands :responses) path [(if (= :command type) :commands :responses)
name name
@ -169,33 +162,28 @@
:context {:from address :context {:from address
:to to :to to
:message-id id}}] :message-id id}}]
(dispatch [::command-in-processing chat-id id])
(status/call-jail (status/call-jail
chat-id chat-id
path path
params params
#(dispatch [:command-handler! chat-id parameters %])))))) #(dispatch [:command-handler! chat-id parameters %]))))))
(register-handler ::command-in-processing
(fn [db [_ chat-id id]]
(assoc-in db [:chats chat-id :staged-commands id :sent-to-jail?] true)))
(register-handler ::prepare-message (register-handler ::prepare-message
(u/side-effect! (u/side-effect!
(fn [{:keys [network-status] :as db} [_ {:keys [chat-id identity message] :as params}]] (fn [{:keys [network-status] :as db} [_ {:keys [chat-id identity message] :as params}]]
(let [{:keys [group-chat]} (get-in db [:chats chat-id]) (let [{:keys [group-chat]} (get-in db [:chats chat-id])
clock-value (messages/get-last-clock-value chat-id) clock-value (messages/get-last-clock-value chat-id)
message' (cu/check-author-direction message' (cu/check-author-direction
db chat-id db chat-id
{:message-id (random/id) {:message-id (random/id)
:chat-id chat-id :chat-id chat-id
:content message :content message
:from identity :from identity
:content-type text-content-type :content-type text-content-type
:outgoing true :outgoing true
:timestamp (time/now-ms) :timestamp (time/now-ms)
:clock-value (inc clock-value) :clock-value (inc clock-value)
:show? true}) :show? true})
message'' (if group-chat message'' (if group-chat
(assoc message' :group-id chat-id :message-type :group-user-message) (assoc message' :group-id chat-id :message-type :group-user-message)
(assoc message' :to chat-id :message-type :user-message)) (assoc message' :to chat-id :message-type :user-message))
@ -220,7 +208,7 @@
(-> db (-> db
(update-in [:suggestions] dissoc chat-id) (update-in [:suggestions] dissoc chat-id)
(update-in [:has-suggestions?] dissoc chat-id) (update-in [:has-suggestions?] dissoc chat-id)
(assoc-in [:animations :to-response-height chat-id] input-height)))) (assoc-in [:animations :to-response-height chat-id] input-height))))
(register-handler ::send-dapp-message (register-handler ::send-dapp-message
(u/side-effect! (u/side-effect!
@ -267,10 +255,10 @@
(register-handler ::send-message! (register-handler ::send-message!
(u/side-effect! (u/side-effect!
(fn [{:keys [web3 chats network-status] (fn [{:keys [web3 chats network-status]
:as db} [_ {{:keys [message-type] :as db} [_ {{:keys [message-type]
:as message} :message :as message} :message
chat-id :chat-id}]] chat-id :chat-id}]]
(let [{:keys [dapp?] :as contact} (get-in db [:contacts chat-id])] (let [{:keys [dapp?] :as contact} (get-in db [:contacts chat-id])]
(if dapp? (if dapp?
(dispatch [::send-dapp-message chat-id message]) (dispatch [::send-dapp-message chat-id message])

View File

@ -57,7 +57,7 @@
:parameter-idx 0 :parameter-idx 0
:params {"amount" (:amount params)} :params {"amount" (:amount params)}
:to-message-id nil}] :to-message-id nil}]
(dispatch [:stage-command command-input command]))))) (dispatch [:validate-command command-input command])))))
(defn chat-with-command (defn chat-with-command

View File

@ -69,36 +69,15 @@
(get-in @db) (get-in @db)
(reaction)))) (reaction))))
(register-sub :get-chat-staged-commands
(fn [db _]
(->> [:chats (:current-chat-id @db) :staged-commands]
(get-in @db)
vals
(reaction))))
(register-sub :get-chat-staged-commands-ids
(fn [db _]
(->> [:chats (:current-chat-id @db) :staged-commands]
(get-in @db)
vals
(keep :to-message)
(reaction))))
(register-sub :staged-response?
(fn [_ [_ id]]
(let [commands (subscribe [:get-chat-staged-commands])]
(reaction (some #(= id (:to-message %)) @commands)))))
(register-sub :get-message-input-view-height (register-sub :get-message-input-view-height
(fn [db _] (fn [db _]
(reaction (get-in @db [:chats (:current-chat-id @db) :message-input-height])))) (reaction (get-in @db [:chats (:current-chat-id @db) :message-input-height]))))
(register-sub :valid-plain-message? (register-sub :valid-plain-message?
(fn [_ _] (fn [_ _]
(let [input-message (subscribe [:get-chat-input-text]) (let [input-message (subscribe [:get-chat-input-text])]
staged-commands (subscribe [:get-chat-staged-commands])]
(reaction (reaction
(plain-message/message-valid? @staged-commands @input-message))))) (plain-message/message-valid? @input-message)))))
(register-sub :valid-command? (register-sub :valid-command?
(fn [_ [_ validator]] (fn [_ [_ validator]]
@ -157,10 +136,9 @@
(fn [] (fn []
(let [command? (subscribe [:command?]) (let [command? (subscribe [:command?])
type (subscribe [:command-type]) type (subscribe [:command-type])
command-suggestions (subscribe [:get-content-suggestions]) command-suggestions (subscribe [:get-content-suggestions])]
staged-commands (subscribe [:get-chat-staged-commands])]
(reaction (reaction
(cond (and @command? (= @type :response) (not (seq @staged-commands))) (cond (and @command? (= @type :response))
c/request-info-height c/request-info-height
(and @command? (= @type :command) (seq @command-suggestions)) (and @command? (= @type :command) (seq @command-suggestions))
@ -178,12 +156,8 @@
(register-sub :get-requests (register-sub :get-requests
(fn [db] (fn [db]
(let [chat-id (subscribe [:get-current-chat-id]) (let [chat-id (subscribe [:get-current-chat-id])]
staged-ids (subscribe [:get-chat-staged-commands-ids])] (reaction (get-in @db [:chats @chat-id :requests])))))
(reaction
(let [ids (set @staged-ids)
requests (get-in @db [:chats @chat-id :requests])]
(remove #(ids (:message-id %)) requests))))))
(register-sub :get-requests-map (register-sub :get-requests-map
(fn [db] (fn [db]

View File

@ -7,15 +7,9 @@
touchable-highlight]] touchable-highlight]]
[status-im.chat.styles.input :as st])) [status-im.chat.styles.input :as st]))
(defn cancel-command-input []
(dispatch [:start-cancel-command]))
(defn set-input-message [message] (defn set-input-message [message]
(dispatch [:set-chat-command-content message])) (dispatch [:set-chat-command-content message]))
(defn send-command []
(dispatch [:stage-command]))
(defn valid? [message validator] (defn valid? [message validator]
(if validator (if validator
(validator message) (validator message)
@ -30,9 +24,3 @@
(when (not= icon-width width) (when (not= icon-width width)
(dispatch [:set :command-icon-width width]))))} (dispatch [:set :command-icon-width width]))))}
[text {:style st/command-text} (str "!" (:name command))]]]) [text {:style st/command-text} (str "!" (:name command))]]])
(defn cancel-button []
[touchable-highlight {:on-press cancel-command-input}
[view st/cancel-container
[icon :close_gray st/cancel-icon]]])

View File

@ -18,10 +18,9 @@
(defn send [] (defn send []
(dispatch [:send-chat-message])) (dispatch [:send-chat-message]))
(defn message-valid? [staged-commands message] (defn message-valid? [ message]
(or (and (pos? (count message)) (and (pos? (count message))
(not= "!" message)) (not= "!" message)))
(pos? (count staged-commands))))
(defn button-animation-logic [{:keys [command? val]}] (defn button-animation-logic [{:keys [command? val]}]
(fn [_] (fn [_]

View File

@ -26,34 +26,32 @@
(def min-scale 1) (def min-scale 1)
(def max-scale 1.3) (def max-scale 1.3)
(defn button-animation [val to-value loop? answered? staged?] (defn button-animation [val to-value loop? answered?]
(anim/anim-sequence (anim/anim-sequence
[(anim/anim-delay [(anim/anim-delay
(if (and @loop? (or @staged? (not @answered?))) (if (and @loop? (not @answered?))
request-message-icon-scale-delay request-message-icon-scale-delay
0)) 0))
(anim/spring val {:toValue to-value})])) (anim/spring val {:toValue to-value})]))
(defn request-button-animation-logic (defn request-button-animation-logic
[{:keys [to-value val loop? answered? staged?] :as context}] [{:keys [to-value val loop? answered?] :as context}]
(anim/start (anim/start
(button-animation val to-value loop? answered? staged?) (button-animation val to-value loop? answered?)
#(if (and @loop? (or @staged? (not @answered?))) #(if (and @loop? (not @answered?))
(let [new-value (if (= to-value min-scale) max-scale min-scale) (let [new-value (if (= to-value min-scale) max-scale min-scale)
context' (assoc context :to-value new-value)] context' (assoc context :to-value new-value)]
(request-button-animation-logic context')) (request-button-animation-logic context'))
(anim/start (anim/start
(button-animation val min-scale loop? answered? staged?))))) (button-animation val min-scale loop? answered?)))))
(defn request-button [message-id command status-initialized? top-offset?] (defn request-button [message-id command status-initialized? top-offset?]
(let [scale-anim-val (anim/create-value min-scale) (let [scale-anim-val (anim/create-value min-scale)
answered? (subscribe [:is-request-answered? message-id]) answered? (subscribe [:is-request-answered? message-id])
staged? (subscribe [:staged-response? message-id])
loop? (r/atom true) loop? (r/atom true)
context {:to-value max-scale context {:to-value max-scale
:val scale-anim-val :val scale-anim-val
:answered? answered? :answered? answered?
:staged? staged?
:loop? loop?}] :loop? loop?}]
(r/create-class (r/create-class
{:component-did-mount {:component-did-mount

View File

@ -18,7 +18,7 @@
(defn command-handler! (defn command-handler!
[_ [chat-id [_ [chat-id
{:keys [staged-command] :as parameters} {:keys [command-message] :as parameters}
{:keys [result error]}]] {:keys [result error]}]]
(let [{:keys [context returned]} result (let [{:keys [context returned]} result
{handler-error :error} returned] {handler-error :error} returned]
@ -27,13 +27,13 @@
handler-error handler-error
(log/debug :error-from-handler handler-error (log/debug :error-from-handler handler-error
:chat-id chat-id :chat-id chat-id
:command staged-command) :command command-message)
result result
(let [command' (assoc staged-command :handler-data returned) (let [command' (assoc command-message :handler-data returned)
parameters' (assoc parameters :command command')] parameters' (assoc parameters :command command')]
(if (:eth_sendTransaction context) (if (:eth_sendTransaction context)
(dispatch [:wait-for-transaction (:id staged-command) parameters']) (dispatch [:wait-for-transaction (:id command-message) parameters'])
(dispatch [:prepare-command! chat-id parameters']))) (dispatch [:prepare-command! chat-id parameters'])))
(not (or error handler-error)) (not (or error handler-error))
@ -64,14 +64,14 @@
nil))) nil)))
(defn command-preview (defn command-preview
[db [chat-id command-id {:keys [result]}]] [_ [command-message {:keys [result]}]]
(let [result' (:returned result)] (let [result' (:returned result)]
(if result' (dispatch [:send-chat-message
(let [path [:chats chat-id :staged-commands command-id]] (if result'
(update-in db path assoc (assoc command-message
:preview (generate-hiccup result') :preview (generate-hiccup result')
:preview-string (str result'))) :preview-string (str result'))
db))) command-message)])))
(defn print-error-message! [message] (defn print-error-message! [message]
(fn [_ params] (fn [_ params]
@ -98,7 +98,7 @@
(reg-handler :command-preview (reg-handler :command-preview
(after (print-error-message! "Error on command preview")) (after (print-error-message! "Error on command preview"))
command-preview) (u/side-effect! command-preview))
(reg-handler :set-local-storage (reg-handler :set-local-storage
(fn [{:keys [current-chat-id] :as db} [{:keys [data] :as event}]] (fn [{:keys [current-chat-id] :as db} [{:keys [data] :as event}]]

View File

@ -50,15 +50,11 @@
:edit-mode {} :edit-mode {}
:network :testnet}) :network :testnet})
(defn chat-staged-commands-path [chat-id]
[:chats chat-id :staged-commands])
(defn chat-command-path [chat-id] (defn chat-command-path [chat-id]
[:chats chat-id :command-input :command]) [:chats chat-id :command-input :command])
(defn chat-command-to-message-id-path [chat-id] (defn chat-command-to-message-id-path [chat-id]
[:chats chat-id :command-input :to-message-id]) [:chats chat-id :command-input :to-message-id])
(defn chat-command-content-path [chat-id] (defn chat-command-content-path [chat-id]
[:chats chat-id :command-input :content]) [:chats chat-id :command-input :content])
(defn chat-command-requests-path [chat-id]
[:chats chat-id :command-requests])
(defn chat-command-request-path [chat-id message-id] (defn chat-command-request-path [chat-id message-id]
[:chats chat-id :command-requests message-id]) [:chats chat-id :command-requests message-id])

View File

@ -24,8 +24,7 @@
status-im.network.handlers status-im.network.handlers
[status-im.utils.types :as t] [status-im.utils.types :as t]
[status-im.constants :refer [console-chat-id]] [status-im.constants :refer [console-chat-id]]
[status-im.utils.ethereum-network :as enet] [status-im.utils.ethereum-network :as enet]))
[status-im.protocol.core :as protocol]))
;; -- Common -------------------------------------------------------------- ;; -- Common --------------------------------------------------------------

View File

@ -74,23 +74,6 @@
[{:keys [current-chat-id] :as db}] [{:keys [current-chat-id] :as db}]
(get-in db (db/chat-command-to-message-id-path current-chat-id))) (get-in db (db/chat-command-to-message-id-path current-chat-id)))
(defn compare-commands
[{created-at-1 :created-at} {created-at-2 :created-at}]
(compare created-at-1 created-at-2))
(defn stage-command
[{:keys [current-chat-id] :as db} {:keys [id] :as command-info}]
(let [path (db/chat-staged-commands-path current-chat-id)
staged-commands (get-in db path)
staged-coomands' (if (seq staged-commands)
staged-commands
(priority-map-by compare-commands))]
(assoc-in db path (assoc staged-coomands' id command-info))))
(defn unstage-command [db {:keys [id]}]
(update-in db (db/chat-staged-commands-path (:current-chat-id db))
dissoc id))
(defn get-chat-command-request (defn get-chat-command-request
[{:keys [current-chat-id] :as db}] [{:keys [current-chat-id] :as db}]
(get-in db (db/chat-command-request-path current-chat-id (get-in db (db/chat-command-request-path current-chat-id

View File

@ -87,30 +87,18 @@
(update :transactions dissoc hash) (update :transactions dissoc hash)
(update :transactions-queue dissoc hash)))) (update :transactions-queue dissoc hash))))
(defn mark-command-as-pending [db chat-id id]
(let [path [:chats chat-id :staged-commands id]]
(if (get-in db path)
(update-in db path assoc :pending true)
db)))
(register-handler :wait-for-transaction (register-handler :wait-for-transaction
(after (fn [_ [_ message-id]] (after (fn [_ [_ message-id]]
(dispatch [::check-completed-transaction! (dispatch [::check-completed-transaction!
{:message-id message-id}]))) {:message-id message-id}])))
(fn [db [_ message-id {:keys [chat-id command] :as params}]] (fn [db [_ message-id params]]
(let [id (:id command)] (assoc-in db [:transaction-subscribers message-id] params)))
(-> db
(mark-command-as-pending chat-id id)
(assoc-in [:transaction-subscribers message-id] params)))))
(defn remove-pending-message (defn remove-pending-message
[{:keys [command->chat] :as db} message-id] [{:keys [command->chat] :as db} message-id]
(let [chat-id (get command->chat message-id) (let [chat-id (get command->chat message-id)]
path [:chats chat-id :staged-commands]]
(if chat-id (if chat-id
(-> db (update db :transaction-subscribers dissoc message-id)
(update :transaction-subscribers dissoc message-id)
(update-in path dissoc message-id))
db))) db)))
(register-handler ::remove-pending-messages (register-handler ::remove-pending-messages