From ef3668044b169d461bccf9549db10487cec9f50f Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Mon, 25 Apr 2016 11:27:30 +0300 Subject: [PATCH] fix command as non-command Former-commit-id: d1bc2196fa55024cea80c780b887b0dec5a18b3f --- src/syng_im/handlers.cljs | 8 ++++++-- src/syng_im/handlers/sign_up.cljs | 6 +++--- src/syng_im/handlers/suggestions.cljs | 21 ++++++++++++++++----- src/syng_im/subs.cljs | 5 ++++- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs index 78567bc9be..eb0a8838c7 100644 --- a/src/syng_im/handlers.cljs +++ b/src/syng_im/handlers.cljs @@ -25,7 +25,8 @@ handle-command get-command-handler load-commands - apply-staged-commands]] + apply-staged-commands + check-suggestion]] [syng-im.handlers.sign-up :as sign-up-service] [syng-im.models.chats :refer [create-chat @@ -392,7 +393,10 @@ (register-handler :set-chat-input-text (fn [db [_ text]] - (set-chat-input-text db text))) + (let [{:keys [command]} (check-suggestion db text)] + (-> db + (set-chat-input-text text) + (set-chat-command command))))) (register-handler :set-chat-command (fn [db [_ command-key]] diff --git a/src/syng_im/handlers/sign_up.cljs b/src/syng_im/handlers/sign_up.cljs index 8b366d08bf..515a49f0cb 100644 --- a/src/syng_im/handlers/sign_up.cljs +++ b/src/syng_im/handlers/sign_up.cljs @@ -154,21 +154,21 @@ (defn intro [db] (dispatch [:received-msg - {:msg-id (random/id) + {:msg-id :intro-message1 :content "Hello there! It's Syng, a Dapp browser in your phone." :content-type text-content-type :outgoing false :from "console" :to "me"}]) (dispatch [:received-msg - {:msg-id (random/id) + {:msg-id :intro-message2 :content (str "Syng uses a highly secure key-pair authentication type " "to provide you a reliable way to access your account") :content-type text-content-type :outgoing false :from "console" :to "me"}]) - (let [msg-id (random/id)] + (let [msg-id :intro-message3] (dispatch [:received-msg {:msg-id msg-id :content (commands/format-command-request-msg-content diff --git a/src/syng_im/handlers/suggestions.cljs b/src/syng_im/handlers/suggestions.cljs index 3fabb0dd2d..760d92d646 100644 --- a/src/syng_im/handlers/suggestions.cljs +++ b/src/syng_im/handlers/suggestions.cljs @@ -9,23 +9,27 @@ get-chat-command-to-msg-id clear-staged-commands]] [syng-im.utils.utils :refer [log on-error http-get]] - [syng-im.utils.logging :as log])) + [syng-im.utils.logging :as log] + [clojure.string :as s])) + +(defn suggestion? [text] + (= (get text 0) "!")) (defn get-suggestions [db text] - (if (= (get text 0) "!") + (if (suggestion? text) ;; TODO change 'commands' to 'suggestions' (filterv #(.startsWith (:text %) text) (get-commands db)) [])) (defn get-command [db text] - (when (= (get text 0) "!") + (when (suggestion? text) ;; TODO change 'commands' to 'suggestions' (first (filter #(= (:text %) text) (get-commands db))))) (defn handle-command [db command-key content] (when-let [command-handler (get-chat-command-request db)] - (let [to-msg-id (get-chat-command-to-msg-id db)] - (command-handler to-msg-id command-key content))) + (let [to-msg-id (get-chat-command-to-msg-id db)] + (command-handler to-msg-id command-key content))) db) (defn get-command-handler [db command-key content] @@ -53,3 +57,10 @@ (defn load-commands [] (http-get "chat-commands.js" execute-commands-js nil)) + +(defn check-suggestion [db text] + (when-let [suggestion-text (re-matches #"^![^\s]+\s" text)] + (let [suggestion-text' (s/trim suggestion-text) + [suggestion] (filter #(= suggestion-text' (:text %)) + (get-commands db))] + suggestion))) diff --git a/src/syng_im/subs.cljs b/src/syng_im/subs.cljs index 5fb60872d0..24bb819901 100644 --- a/src/syng_im/subs.cljs +++ b/src/syng_im/subs.cljs @@ -38,7 +38,10 @@ (register-sub :get-suggestions (fn [db _] - (let [input-text (reaction (get-in @db (db/chat-input-text-path (current-chat-id @db))))] + (let [input-text (->> (current-chat-id @db) + db/chat-input-text-path + (get-in @db) + (reaction))] (reaction (get-suggestions @db @input-text))))) (register-sub :get-commands