console commands/responses

Former-commit-id: 61b6599bd3
This commit is contained in:
Roman Volosovskyi 2016-06-09 19:34:09 +03:00
parent 75649d46aa
commit 5689bf787e
10 changed files with 45 additions and 43 deletions

View File

@ -68,7 +68,7 @@
(update-input-text db text))
(defn update-command [db [_ text]]
(let [{:keys [command]} (suggestions/check-suggestion db text)]
(let [[command] (suggestions/check-suggestion db text)]
(commands/set-chat-command db command)))
(register-handler :set-chat-input-text
@ -99,7 +99,7 @@
(defn prepare-message
[{:keys [identity current-chat-id] :as db} _]
(let [text (get-in db [:chats current-chat-id :input-text])
{:keys [command]} (suggestions/check-suggestion db (str text " "))
[command] (suggestions/check-suggestion db (str text " "))
message (check-author-direction
db current-chat-id
{:msg-id (random/id)

View File

@ -20,10 +20,7 @@
[{:keys [current-chat-id] :as db} text]
(let [commands (get-in db [:chats current-chat-id :commands])]
(if (suggestion? text)
;; TODO change 'commands' to 'suggestions'
(->> commands
vals
(filter (can-be-suggested? text)))
(filter (fn [[_ v]] ((can-be-suggested? text) v)) commands)
[])))
(defn get-command [db text]
@ -55,10 +52,10 @@
(defn check-suggestion [db message]
(when-let [suggestion-text (when (string? message)
(re-matches #"^![^\s]+\s" message))]
(let [suggestion-text' (s/trim suggestion-text)
[suggestion] (filter #(= suggestion-text' (:text %))
(get-commands db))]
suggestion)))
(let [suggestion-text' (s/trim suggestion-text)]
(->> (get-commands db)
(filter #(= suggestion-text' (->> % second :name (str "!"))))
first))))
(defn typing-command? [db]
(-> db

View File

@ -32,7 +32,7 @@
[content-suggestions-view]
[view st/command-input-container
[view (st/command-text-container command)
[text {:style st/command-text} (:text command)]]
[text {:style st/command-text} (str "!" (:name command))]]
[text-input (merge {:style st/command-input
:autoFocus true
:onChangeText set-input-message

View File

@ -73,7 +73,7 @@
(dispatch [:set-response-chat-command msg-id (:command command)]))
(defn label [{:keys [command]}]
(->> (name command)
(->> (when command (name command))
(str "request-")))
(defn message-content-command-request

View File

@ -16,7 +16,7 @@
[view st/staged-command-background
[view st/staged-command-info-container
[view (st/staged-command-text-container command)
[text {:style st/staged-command-text} (:text command)]]
[text {:style st/staged-command-text} (str "!" (:name command))]]
[touchable-highlight {:style st/staged-command-cancel
:onPress #(cancel-command-input staged-command)}
[image {:source res/icon-close-gray

View File

@ -14,17 +14,18 @@
(dispatch [:set-chat-command command]))
(defn suggestion-list-item
[{:keys [description]
label :name
:as suggestion}]
[touchable-highlight
{:onPress #(set-command-input (keyword label))}
[view st/suggestion-container
[view st/suggestion-sub-container
[view (st/suggestion-background suggestion)
[text {:style st/suggestion-text} label]]
[text {:style st/value-text} label]
[text {:style st/description-text} description]]]])
[[command {:keys [description]
name :name
:as suggestion}]]
(let [label (str "!" name)]
[touchable-highlight
{:onPress #(set-command-input command)}
[view st/suggestion-container
[view st/suggestion-sub-container
[view (st/suggestion-background suggestion)
[text {:style st/suggestion-text} label]]
[text {:style st/value-text} label]
[text {:style st/description-text} description]]]]))
(defn render-row [row _ _]
(list-item [suggestion-list-item row]))

View File

@ -51,11 +51,28 @@
(defn json->clj [json]
(js->clj (.parse js/JSON json) :keywordize-keys true))
;; todo remove this
(def res {:commands {:location {:description "Send location"
:color "#9a5dcf"
:name "location"}
:phone {:description "Send phone number"
:color "#5fc48d"
:name "phone"}
:help {:description "Help" :color "#9a5dcf" :name "help"}}
:responses {:money {:description "Send money" :color "#5fc48d" :name "money"}
:confirmation-code {:description "Confirmation code"
:color "#7099e6"
:name "confirmationCode"}
:keypair-password {:description ""
:color "#7099e6"
:name "keypair-password"}}})
(defn parse-commands! [_ [identity file]]
(parse file
(fn [result]
(let [commands (json->clj result)]
(dispatch [::add-commands identity file commands])))
;; todo use commands from jail
(dispatch [::add-commands identity file res])))
#(dispatch [::loading-failed! identity ::error-in-jail %])))
(defn validate-hash

View File

@ -96,7 +96,6 @@
(remove #(identities (:whisper-identity %)))
(map #(vector (:whisper-identity %) %))
(into {}))]
(println new-contacts')
(-> db
(update :contacts merge new-contacts')
(assoc :new-contacts (vals new-contacts')))))

View File

@ -14,7 +14,7 @@
(defn get-content-suggestions [db command text]
(or (when command
(when-let [command-suggestions ((:command command) suggestions)]
(when-let [command-suggestions ((keyword (:name command)) suggestions)]
(filterv (fn [s]
(and (.startsWith (:value s) (or text ""))
(not= (:value s) text)))

View File

@ -35,16 +35,6 @@
:icon {:uri "icon_lock_gray"}
:suggestion true
:handler #(dispatch [:sign-up-confirm %])}
{:command :send
:text "!send"
:description (label :t/send-command-description)
:color "#9a5dcf"
:suggestion true}
{:command :request
:text "!request"
:description (label :t/request-command-description)
:color "#48ba30"
:suggestion true}
{:command :keypair-password
:text "!keypair-password"
:description (label :t/keypair-password-command-description)
@ -59,13 +49,11 @@
:color "#9a5dcf"
:suggestion true}])
(defn get-commands [db]
;; todo: temp. must be '(get db :commands)'
;; (get db :commands)
commands)
(defn get-commands [{:keys [current-chat-id] :as db}]
(or (get-in db [:chats current-chat-id :commands]) {}))
(defn get-command [db command-key]
(first (filter #(= command-key (:command %)) (get-commands db))))
((get-commands db) command-key))
(defn find-command [commands command-key]
(first (filter #(= command-key (:command %)) commands)))
@ -126,4 +114,4 @@
(update content :command #(find-command commands (keyword %))))
(defn parse-command-request [commands content]
(update content :command #(find-command commands (keyword %))))
(update content :command #((keyword %) commands)))