Merge pull request #576 from status-im/bug/#573

Fix phone number validation, remove staged-commands leftovers
This commit is contained in:
Roman Volosovskyi 2016-12-27 11:16:05 +02:00 committed by GitHub
commit bc4f9c340b
15 changed files with 115 additions and 205 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,15 +7,9 @@
touchable-highlight]]
[status-im.chat.styles.input :as st]))
(defn cancel-command-input []
(dispatch [:start-cancel-command]))
(defn set-input-message [message]
(dispatch [:set-chat-command-content message]))
(defn send-command []
(dispatch [:stage-command]))
(defn valid? [message validator]
(if validator
(validator message)
@ -30,9 +24,3 @@
(when (not= icon-width width)
(dispatch [:set :command-icon-width width]))))}
[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 []
(dispatch [:send-chat-message]))
(defn message-valid? [staged-commands message]
(or (and (pos? (count message))
(not= "!" message))
(pos? (count staged-commands))))
(defn message-valid? [ message]
(and (pos? (count message))
(not= "!" message)))
(defn button-animation-logic [{:keys [command? val]}]
(fn [_]

View File

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

View File

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

View File

@ -50,15 +50,11 @@
:edit-mode {}
:network :testnet})
(defn chat-staged-commands-path [chat-id]
[:chats chat-id :staged-commands])
(defn chat-command-path [chat-id]
[:chats chat-id :command-input :command])
(defn chat-command-to-message-id-path [chat-id]
[:chats chat-id :command-input :to-message-id])
(defn chat-command-content-path [chat-id]
[: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]
[:chats chat-id :command-requests message-id])

View File

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

View File

@ -74,23 +74,6 @@
[{:keys [current-chat-id] :as db}]
(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
[{:keys [current-chat-id] :as db}]
(get-in db (db/chat-command-request-path current-chat-id

View File

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