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

@ -102,22 +102,22 @@
[command] (suggestions/check-suggestion db (str text " ")) [command] (suggestions/check-suggestion db (str text " "))
message (check-author-direction message (check-author-direction
db current-chat-id db current-chat-id
{:msg-id (random/id) {:msg-id (random/id)
:chat-id current-chat-id :chat-id current-chat-id
:content text :content text
:to current-chat-id :to current-chat-id
:from identity :from identity
:content-type text-content-type :content-type text-content-type
:outgoing true :outgoing true
:timestamp (time/now-ms)})] :timestamp (time/now-ms)})]
(if command (if command
(commands/set-chat-command db command) (commands/set-chat-command db command)
(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
:to chat-id :to chat-id
@ -301,9 +301,9 @@
(defmethod nav/preload-data! :chat (defmethod nav/preload-data! :chat
[{:keys [current-chat-id] :as db} [_ _ id]] [{: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]) 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) (if (seq messages)
db' db'
(-> db' (-> db'

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,18 +1,19 @@
(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
text text
image image
touchable-highlight]] touchable-highlight]]
[status-im.chat.styles.message :as st] [status-im.chat.styles.message :as st]
[status-im.models.commands :refer [parse-command-msg-content [status-im.models.commands :refer [parse-command-msg-content
parse-command-request]] parse-command-request]]
[status-im.resources :as res] [status-im.resources :as res]
[status-im.constants :refer [text-content-type [status-im.constants :refer [text-content-type
content-type-status content-type-status
content-type-command content-type-command
content-type-command-request]])) content-type-command-request]]))
(defn message-date [{:keys [date]}] (defn message-date [{:keys [date]}]
[view {} [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 [touchable-highlight {:onPress #(set-chat-command msg-id command)
{:keys [command content]} (parse-command-request commands content)] :accessibility-label (label command)}
[touchable-highlight {:onPress #(set-chat-command msg-id command) [view st/comand-request-view
:accessibility-label (label command)} [view st/command-request-message-view
[view st/comand-request-view (when incoming-group
[view st/command-request-message-view [text {:style st/command-request-from-text} from])
(when incoming-group [text {:style st/style-message-text} content]]
[text {:style st/command-request-from-text} [view (st/command-request-image-view command)
from]) [image {:source {:uri (:icon command)}
[text {:style st/style-message-text} :style st/command-request-image}]]
content]] (when-let [request-text (:request-text command)]
[view (st/command-request-image-view command) [view st/command-request-text-view
[image {:source (:request-icon command) [text {:style st/style-sub-text} request-text]])]]))
:style st/command-request-image}]]
(when (:request-text command)
[view st/command-request-text-view
[text {:style st/style-sub-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)))