From 0319f9334c4de4dfef4e147ca4f7cbcb0b6b61c6 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Fri, 10 Jun 2016 15:17:46 +0300 Subject: [PATCH] commands/responses from loaded commands.js in chat --- src/status_im/chat/handlers.cljs | 26 +++++----- src/status_im/chat/subs.cljs | 12 +++++ src/status_im/chat/views/message.cljs | 60 +++++++++++------------ src/status_im/chat/views/plain_input.cljs | 16 +++--- src/status_im/commands/handlers.cljs | 5 +- src/status_im/models/commands.cljs | 11 +++-- 6 files changed, 69 insertions(+), 61 deletions(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 4023473b83..98d9830521 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -102,22 +102,22 @@ [command] (suggestions/check-suggestion db (str text " ")) message (check-author-direction db current-chat-id - {:msg-id (random/id) - :chat-id current-chat-id - :content text - :to current-chat-id - :from identity - :content-type text-content-type - :outgoing true - :timestamp (time/now-ms)})] + {:msg-id (random/id) + :chat-id current-chat-id + :content text + :to current-chat-id + :from identity + :content-type text-content-type + :outgoing true + :timestamp (time/now-ms)})] (if command (commands/set-chat-command db command) (assoc db :new-message (when-not (str/blank? text) message))))) (defn prepare-command [identity chat-id staged-command] - (let [command-key (get-in staged-command [:command :command]) - content {:command (name command-key) - :content (:content staged-command)}] + (let [command-name (get-in staged-command [:command :name]) + content {:command command-name + :content (:content staged-command)}] {:msg-id (random/id) :from identity :to chat-id @@ -301,9 +301,9 @@ (defmethod nav/preload-data! :chat [{:keys [current-chat-id] :as db} [_ _ id]] - (let [chat-id (or id current-chat-id) + (let [chat-id (or id current-chat-id) messages (get-in db [:chats chat-id :messages]) - db' (assoc db :current-chat-id chat-id)] + db' (assoc db :current-chat-id chat-id)] (if (seq messages) db' (-> db' diff --git a/src/status_im/chat/subs.cljs b/src/status_im/chat/subs.cljs index d5df5f5716..c9de7660e5 100644 --- a/src/status_im/chat/subs.cljs +++ b/src/status_im/chat/subs.cljs @@ -50,6 +50,18 @@ (fn [db _] (reaction (commands/get-commands @db)))) +(register-sub :get-responses + (fn [db _] + (let [current-chat (@db :current-chat-id)] + (reaction (or (get-in @db [:chats current-chat :responses]) {}))))) + +(register-sub :get-commands-and-responses + (fn [db _] + (let [current-chat (@db :current-chat-id)] + (reaction _ (or (->> (get-in @db [:chats current-chat]) + ((juxt :commands :responses)) + (apply merge)) {}))))) + (register-sub :get-chat-input-text (fn [db _] (->> [:chats (:current-chat-id @db) :input-text] diff --git a/src/status_im/chat/views/message.cljs b/src/status_im/chat/views/message.cljs index 0b6494acf8..b1215423f2 100644 --- a/src/status_im/chat/views/message.cljs +++ b/src/status_im/chat/views/message.cljs @@ -1,18 +1,19 @@ (ns status-im.chat.views.message + (:require-macros [status-im.utils.views :refer [defview]]) (:require [clojure.string :as s] [re-frame.core :refer [subscribe dispatch]] [status-im.components.react :refer [view - text - image - touchable-highlight]] + text + image + touchable-highlight]] [status-im.chat.styles.message :as st] [status-im.models.commands :refer [parse-command-msg-content - parse-command-request]] + parse-command-request]] [status-im.resources :as res] [status-im.constants :refer [text-content-type - content-type-status - content-type-command - content-type-command-request]])) + content-type-status + content-type-command + content-type-command-request]])) (defn message-date [{:keys [date]}] [view {} @@ -51,7 +52,7 @@ [text {:style st/track-duration-text} "03:39"]]]) (defn message-content-command [content] - (let [commands-atom (subscribe [:get-commands])] + (let [commands-atom (subscribe [:get-commands-and-responses])] (fn [content] (let [commands @commands-atom {:keys [command content]} @@ -60,8 +61,8 @@ [view st/command-container [view (st/command-view command) [text {:style st/command-name} - (:text command)]]] - [image {:source (:icon command) + (str "!" (:name command))]]] + [image {:source {:uri (:icon command)} :style st/command-image}] [text {:style st/command-text} ;; TODO isn't smart @@ -70,34 +71,29 @@ content)]])))) (defn set-chat-command [msg-id command] - (dispatch [:set-response-chat-command msg-id (:command command)])) + (dispatch [:set-response-chat-command msg-id (keyword (:name command))])) (defn label [{:keys [command]}] (->> (when command (name command)) (str "request-"))) -(defn message-content-command-request +(defview message-content-command-request [{:keys [msg-id content from incoming-group]}] - (let [commands-atom (subscribe [:get-commands])] - (fn [{:keys [msg-id content from incoming-group]}] - (let [commands @commands-atom - {:keys [command content]} (parse-command-request commands content)] - [touchable-highlight {:onPress #(set-chat-command msg-id command) - :accessibility-label (label command)} - [view st/comand-request-view - [view st/command-request-message-view - (when incoming-group - [text {:style st/command-request-from-text} - from]) - [text {:style st/style-message-text} - content]] - [view (st/command-request-image-view command) - [image {:source (:request-icon command) - :style st/command-request-image}]] - (when (:request-text command) - [view st/command-request-text-view - [text {:style st/style-sub-text} - (:request-text command)]])]])))) + [commands [:get-responses]] + (let [{:keys [command content]} (parse-command-request commands content)] + [touchable-highlight {:onPress #(set-chat-command msg-id command) + :accessibility-label (label command)} + [view st/comand-request-view + [view st/command-request-message-view + (when incoming-group + [text {:style st/command-request-from-text} from]) + [text {:style st/style-message-text} content]] + [view (st/command-request-image-view command) + [image {:source {:uri (:icon command)} + :style st/command-request-image}]] + (when-let [request-text (:request-text command)] + [view st/command-request-text-view + [text {:style st/style-sub-text} request-text]])]])) (defn message-view [message content] diff --git a/src/status_im/chat/views/plain_input.cljs b/src/status_im/chat/views/plain_input.cljs index dcbc75780a..b932d53032 100644 --- a/src/status_im/chat/views/plain_input.cljs +++ b/src/status_im/chat/views/plain_input.cljs @@ -10,22 +10,18 @@ (defn set-input-message [message] (dispatch [:set-chat-input-text message])) -(defn send [chat input-message] - (let [{:keys [group-chat chat-id]} chat] - (dispatch [:send-chat-msg]))) +(defn send [] (dispatch [:send-chat-msg])) (defn message-valid? [staged-commands message] (or (and (pos? (count message)) (not= "!" message)) (pos? (count staged-commands)))) -(defn try-send [chat staged-commands message] - (when (message-valid? staged-commands message) - (send chat message))) +(defn try-send [staged-commands message] + (when (message-valid? staged-commands message) (send))) (defn plain-message-input-view [] - (let [chat (subscribe [:get-current-chat]) - input-message-atom (subscribe [:get-chat-input-text]) + (let [input-message-atom (subscribe [:get-chat-input-text]) staged-commands-atom (subscribe [:get-chat-staged-commands]) typing-command? (subscribe [:typing-command?])] (fn [] @@ -42,13 +38,13 @@ [text-input {:style st/message-input :autoFocus (pos? (count @staged-commands-atom)) :onChangeText set-input-message - :onSubmitEditing #(try-send @chat @staged-commands-atom + :onSubmitEditing #(try-send @staged-commands-atom input-message)} input-message] ;; TODO emoticons: not implemented [icon :smile st/smile-icon] (when (message-valid? @staged-commands-atom input-message) - [touchable-highlight {:on-press #(send @chat input-message) + [touchable-highlight {:on-press #(send) :accessibility-label :send-message} [view st/send-container [icon :send st/send-icon]]])]])))) diff --git a/src/status_im/commands/handlers.cljs b/src/status_im/commands/handlers.cljs index 75811d9656..9caf18473b 100644 --- a/src/status_im/commands/handlers.cljs +++ b/src/status_im/commands/handlers.cljs @@ -63,9 +63,10 @@ :confirmation-code {:description "Confirmation code" :color "#7099e6" :name "confirmationCode"} - :keypair-password {:description "" + :keypair-password {:description "Keypair password" :color "#7099e6" - :name "keypair-password"}}}) + :name "keypair-password" + :icon "http://localhost:8185/images/deliveryfailed.png"}}}) (defn parse-commands! [_ [identity file]] (parse file diff --git a/src/status_im/models/commands.cljs b/src/status_im/models/commands.cljs index 84b53d47e5..896d53d2c7 100644 --- a/src/status_im/models/commands.cljs +++ b/src/status_im/models/commands.cljs @@ -52,8 +52,11 @@ (defn get-commands [{:keys [current-chat-id] :as db}] (or (get-in db [:chats current-chat-id :commands]) {})) -(defn get-command [db command-key] - ((get-commands db) command-key)) +(defn get-command [{:keys [current-chat-id] :as db} command-key] + ((or (->> (get-in db [:chats current-chat-id]) + ((juxt :commands :responses)) + (apply merge)) + {}) command-key)) (defn find-command [commands command-key] (first (filter #(= command-key (:command %)) commands))) @@ -74,7 +77,7 @@ [{:keys [current-chat-id] :as db} msg-id command-key] (update-in db [:chats current-chat-id :command-input] merge {:content nil - :command (get-command db command-key) + :command (merge (get-command db command-key)) :to-msg-id msg-id})) (defn set-chat-command [db command-key] @@ -111,7 +114,7 @@ #(assoc % msg-id handler))) (defn parse-command-msg-content [commands content] - (update content :command #(find-command commands (keyword %)))) + (update content :command #((keyword %) commands))) (defn parse-command-request [commands content] (update content :command #((keyword %) commands)))