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 " "))
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'

View File

@ -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]

View File

@ -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]

View File

@ -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]]])]]))))

View File

@ -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

View File

@ -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)))