commands/responses from loaded commands.js in chat

This commit is contained in:
Roman Volosovskyi 2016-06-10 15:17:46 +03:00
parent 61b6599bd3
commit 0319f9334c
6 changed files with 69 additions and 61 deletions

View File

@ -115,8 +115,8 @@
(assoc db :new-message (when-not (str/blank? text) message))))) (assoc db :new-message (when-not (str/blank? text) message)))))
(defn prepare-command [identity chat-id staged-command] (defn prepare-command [identity chat-id staged-command]
(let [command-key (get-in staged-command [:command :command]) (let [command-name (get-in staged-command [:command :name])
content {:command (name command-key) content {:command command-name
:content (:content staged-command)}] :content (:content staged-command)}]
{:msg-id (random/id) {:msg-id (random/id)
:from identity :from identity

View File

@ -50,6 +50,18 @@
(fn [db _] (fn [db _]
(reaction (commands/get-commands @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 (register-sub :get-chat-input-text
(fn [db _] (fn [db _]
(->> [:chats (:current-chat-id @db) :input-text] (->> [:chats (:current-chat-id @db) :input-text]

View File

@ -1,4 +1,5 @@
(ns status-im.chat.views.message (ns status-im.chat.views.message
(:require-macros [status-im.utils.views :refer [defview]])
(:require [clojure.string :as s] (:require [clojure.string :as s]
[re-frame.core :refer [subscribe dispatch]] [re-frame.core :refer [subscribe dispatch]]
[status-im.components.react :refer [view [status-im.components.react :refer [view
@ -51,7 +52,7 @@
[text {:style st/track-duration-text} "03:39"]]]) [text {:style st/track-duration-text} "03:39"]]])
(defn message-content-command [content] (defn message-content-command [content]
(let [commands-atom (subscribe [:get-commands])] (let [commands-atom (subscribe [:get-commands-and-responses])]
(fn [content] (fn [content]
(let [commands @commands-atom (let [commands @commands-atom
{:keys [command content]} {:keys [command content]}
@ -60,8 +61,8 @@
[view st/command-container [view st/command-container
[view (st/command-view command) [view (st/command-view command)
[text {:style st/command-name} [text {:style st/command-name}
(:text command)]]] (str "!" (:name command))]]]
[image {:source (:icon command) [image {:source {:uri (:icon command)}
:style st/command-image}] :style st/command-image}]
[text {:style st/command-text} [text {:style st/command-text}
;; TODO isn't smart ;; TODO isn't smart
@ -70,34 +71,29 @@
content)]])))) content)]]))))
(defn set-chat-command [msg-id command] (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]}] (defn label [{:keys [command]}]
(->> (when command (name command)) (->> (when command (name command))
(str "request-"))) (str "request-")))
(defn message-content-command-request (defview message-content-command-request
[{:keys [msg-id content from incoming-group]}] [{:keys [msg-id content from incoming-group]}]
(let [commands-atom (subscribe [:get-commands])] [commands [:get-responses]]
(fn [{:keys [msg-id content from incoming-group]}] (let [{:keys [command content]} (parse-command-request commands content)]
(let [commands @commands-atom
{:keys [command content]} (parse-command-request commands content)]
[touchable-highlight {:onPress #(set-chat-command msg-id command) [touchable-highlight {:onPress #(set-chat-command msg-id command)
:accessibility-label (label command)} :accessibility-label (label command)}
[view st/comand-request-view [view st/comand-request-view
[view st/command-request-message-view [view st/command-request-message-view
(when incoming-group (when incoming-group
[text {:style st/command-request-from-text} [text {:style st/command-request-from-text} from])
from]) [text {:style st/style-message-text} content]]
[text {:style st/style-message-text}
content]]
[view (st/command-request-image-view command) [view (st/command-request-image-view command)
[image {:source (:request-icon command) [image {:source {:uri (:icon command)}
:style st/command-request-image}]] :style st/command-request-image}]]
(when (:request-text command) (when-let [request-text (:request-text command)]
[view st/command-request-text-view [view st/command-request-text-view
[text {:style st/style-sub-text} [text {:style st/style-sub-text} request-text]])]]))
(:request-text command)]])]]))))
(defn message-view (defn message-view
[message content] [message content]

View File

@ -10,22 +10,18 @@
(defn set-input-message [message] (defn set-input-message [message]
(dispatch [:set-chat-input-text message])) (dispatch [:set-chat-input-text message]))
(defn send [chat input-message] (defn send [] (dispatch [:send-chat-msg]))
(let [{:keys [group-chat chat-id]} chat]
(dispatch [:send-chat-msg])))
(defn message-valid? [staged-commands message] (defn message-valid? [staged-commands message]
(or (and (pos? (count message)) (or (and (pos? (count message))
(not= "!" message)) (not= "!" message))
(pos? (count staged-commands)))) (pos? (count staged-commands))))
(defn try-send [chat staged-commands message] (defn try-send [staged-commands message]
(when (message-valid? staged-commands message) (when (message-valid? staged-commands message) (send)))
(send chat message)))
(defn plain-message-input-view [] (defn plain-message-input-view []
(let [chat (subscribe [:get-current-chat]) (let [input-message-atom (subscribe [:get-chat-input-text])
input-message-atom (subscribe [:get-chat-input-text])
staged-commands-atom (subscribe [:get-chat-staged-commands]) staged-commands-atom (subscribe [:get-chat-staged-commands])
typing-command? (subscribe [:typing-command?])] typing-command? (subscribe [:typing-command?])]
(fn [] (fn []
@ -42,13 +38,13 @@
[text-input {:style st/message-input [text-input {:style st/message-input
:autoFocus (pos? (count @staged-commands-atom)) :autoFocus (pos? (count @staged-commands-atom))
:onChangeText set-input-message :onChangeText set-input-message
:onSubmitEditing #(try-send @chat @staged-commands-atom :onSubmitEditing #(try-send @staged-commands-atom
input-message)} input-message)}
input-message] input-message]
;; TODO emoticons: not implemented ;; TODO emoticons: not implemented
[icon :smile st/smile-icon] [icon :smile st/smile-icon]
(when (message-valid? @staged-commands-atom input-message) (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} :accessibility-label :send-message}
[view st/send-container [view st/send-container
[icon :send st/send-icon]]])]])))) [icon :send st/send-icon]]])]]))))

View File

@ -63,9 +63,10 @@
:confirmation-code {:description "Confirmation code" :confirmation-code {:description "Confirmation code"
:color "#7099e6" :color "#7099e6"
:name "confirmationCode"} :name "confirmationCode"}
:keypair-password {:description "" :keypair-password {:description "Keypair password"
:color "#7099e6" :color "#7099e6"
:name "keypair-password"}}}) :name "keypair-password"
:icon "http://localhost:8185/images/deliveryfailed.png"}}})
(defn parse-commands! [_ [identity file]] (defn parse-commands! [_ [identity file]]
(parse file (parse file

View File

@ -52,8 +52,11 @@
(defn get-commands [{:keys [current-chat-id] :as db}] (defn get-commands [{:keys [current-chat-id] :as db}]
(or (get-in db [:chats current-chat-id :commands]) {})) (or (get-in db [:chats current-chat-id :commands]) {}))
(defn get-command [db command-key] (defn get-command [{:keys [current-chat-id] :as db} command-key]
((get-commands db) command-key)) ((or (->> (get-in db [:chats current-chat-id])
((juxt :commands :responses))
(apply merge))
{}) command-key))
(defn find-command [commands command-key] (defn find-command [commands command-key]
(first (filter #(= command-key (:command %)) commands))) (first (filter #(= command-key (:command %)) commands)))
@ -74,7 +77,7 @@
[{:keys [current-chat-id] :as db} msg-id command-key] [{:keys [current-chat-id] :as db} msg-id command-key]
(update-in db [:chats current-chat-id :command-input] merge (update-in db [:chats current-chat-id :command-input] merge
{:content nil {:content nil
:command (get-command db command-key) :command (merge (get-command db command-key))
:to-msg-id msg-id})) :to-msg-id msg-id}))
(defn set-chat-command [db command-key] (defn set-chat-command [db command-key]
@ -111,7 +114,7 @@
#(assoc % msg-id handler))) #(assoc % msg-id handler)))
(defn parse-command-msg-content [commands content] (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] (defn parse-command-request [commands content]
(update content :command #((keyword %) commands))) (update content :command #((keyword %) commands)))