diff --git a/syng-im/src/syng_im/components/chat/chat_message.cljs b/syng-im/src/syng_im/components/chat/chat_message.cljs index 96fcb8cb16..ca52ed8d95 100644 --- a/syng-im/src/syng_im/components/chat/chat_message.cljs +++ b/syng-im/src/syng_im/components/chat/chat_message.cljs @@ -7,10 +7,12 @@ touchable-highlight navigator toolbar-android]] + [syng-im.models.commands :as commands] [syng-im.utils.logging :as log] [syng-im.navigation :refer [nav-pop]] [syng-im.resources :as res] - [syng-im.constants :refer [text-content-type]])) + [syng-im.constants :refer [text-content-type + content-type-command]])) (defn message-date [{:keys [date]}] @@ -60,6 +62,29 @@ :lineHeight 15}} "03:39"]]]) + +(defn message-content-command [content] + (let [{:keys [command content]} (commands/parse-command-msg-content content)] + [view {:style {:flexDirection "column"}} + [view {:style {:margin 10 + :backgroundColor (:color command) + :borderRadius 10}} + [text {:style {:marginTop -2 + :marginHorizontal 10 + :fontSize 14 + :fontFamily "Avenir-Roman" + :color "white"}} + (:text command)]] + [text {:style {:marginTop -2 + :marginHorizontal 10 + :fontSize 14 + :fontFamily "Avenir-Roman" + :color "black"}} + ;; TODO isn't smart + (if (= (:command command) :keypair-password) + "******" + content)]])) + (defn message-content [{:keys [content-type content outgoing]}] [view {:style (merge {:borderRadius 6} (if (= content-type text-content-type) @@ -70,13 +95,16 @@ (if outgoing {:backgroundColor "#D3EEEF"} {:backgroundColor "#FBF6E3"}))} - (if (= content-type text-content-type) + (cond + (= content-type text-content-type) [text {:style {:fontSize 14 :fontFamily "Avenir-Roman" :color "#4A5258"}} content] - [message-content-audio {:content content - :content-type content-type}])]) + (= content-type content-type-command) + [message-content-command content] + :else [message-content-audio {:content content + :content-type content-type}])]) (defn message-delivery-status [{:keys [delivery-status]}] [view {:style {:flexDirection "row" diff --git a/syng-im/src/syng_im/components/chat/input/simple_command.cljs b/syng-im/src/syng_im/components/chat/input/simple_command.cljs index e3f35f5500..cef3189904 100644 --- a/syng-im/src/syng_im/components/chat/input/simple_command.cljs +++ b/syng-im/src/syng_im/components/chat/input/simple_command.cljs @@ -14,11 +14,16 @@ (defn cancel-command-input [] (dispatch [:set-input-command nil])) +(defn send-command [chat-id command text] + (dispatch [:send-chat-command chat-id (:command command) text]) + (cancel-command-input)) + (defn simple-command-input-view [command input-options] - (let [message-atom (r/atom nil) - chat-id (subscribe [:get-current-chat-id])] + (let [message-atom (r/atom nil) + chat-id-atom (subscribe [:get-current-chat-id])] (fn [] - (let [message @message-atom] + (let [message @message-atom + chat-id @chat-id-atom] [view {:style {:flexDirection "row"}} [view {:style {:flex 1 :flexDirection "column" @@ -60,9 +65,10 @@ :keyboardType "default" :value message :onChangeText (fn [new-text] - ) + (reset! message-atom new-text)) :onSubmitEditing (fn [e] - )} + (send-command chat-id command message) + (reset! message-atom nil))} input-options)]]] [touchable-highlight {:style {:marginTop 14 :marginRight 16 diff --git a/syng-im/src/syng_im/constants.cljs b/syng-im/src/syng_im/constants.cljs index be97d850af..dd6729fe43 100644 --- a/syng-im/src/syng_im/constants.cljs +++ b/syng-im/src/syng_im/constants.cljs @@ -6,3 +6,4 @@ ;; (def server-address "http://10.0.3.2:3000/") (def text-content-type "text/plain") +(def content-type-command "command") diff --git a/syng-im/src/syng_im/handlers.cljs b/syng-im/src/syng_im/handlers.cljs index 333777e1ac..b83c8893ec 100644 --- a/syng-im/src/syng_im/handlers.cljs +++ b/syng-im/src/syng_im/handlers.cljs @@ -103,6 +103,25 @@ (save-message chat-id msg) (signal-chat-updated db chat-id)))) +(register-handler :send-chat-command + (fn [db [action chat-id command content]] + (log/debug action "chat-id" chat-id "command" command "content" content) + (let [msg (if (= chat-id "console") + (sign-up-service/send-console-command command content) + ;; TODO handle command, now sends as plain message + (let [{msg-id :msg-id + {from :from + to :to} :msg} (api/send-user-msg {:to chat-id + :content content})] + {:msg-id msg-id + :from from + :to to + :content content + :content-type text-content-type + :outgoing true}))] + (save-message chat-id msg) + (signal-chat-updated db chat-id)))) + ;; -- User data -------------------------------------------------------------- (register-handler :set-user-phone-number diff --git a/syng-im/src/syng_im/handlers/sign_up.cljs b/syng-im/src/syng_im/handlers/sign_up.cljs index 6e4f2e9269..5c405f8de7 100644 --- a/syng-im/src/syng_im/handlers/sign_up.cljs +++ b/syng-im/src/syng_im/handlers/sign_up.cljs @@ -1,11 +1,12 @@ (ns syng-im.handlers.sign-up (:require [re-frame.core :refer [subscribe dispatch dispatch-sync]] [syng-im.db :as db] - ;; [syng-im.models.commands :refer [commands suggestions]] + [syng-im.models.commands :as commands] [syng-im.utils.utils :refer [log on-error http-post toast]] [syng-im.utils.logging :as log] [syng-im.utils.random :as random] - [syng-im.constants :refer [text-content-type]])) + [syng-im.constants :refer [text-content-type + content-type-command]])) (defn intro [db] (dispatch [:received-msg {:msg-id "1" @@ -36,3 +37,12 @@ :content text :content-type text-content-type :outgoing true}) + +;; TODO store command key in a separate field +(defn send-console-command [command content] + {:msg-id (random/id) + :from "me" + :to "console" + :content (commands/format-command-msg-content command content) + :content-type content-type-command + :outgoing true}) diff --git a/syng-im/src/syng_im/models/commands.cljs b/syng-im/src/syng_im/models/commands.cljs index f2fc5ed185..780f651a2c 100644 --- a/syng-im/src/syng_im/models/commands.cljs +++ b/syng-im/src/syng_im/models/commands.cljs @@ -1,5 +1,7 @@ (ns syng-im.models.commands - (:require [cljs.core.async :as async :refer [chan put! !]] + (:require [clojure.string :refer [join split]] + [clojure.walk :refer [stringify-keys keywordize-keys]] + [cljs.core.async :as async :refer [chan put! !]] [re-frame.core :refer [subscribe dispatch dispatch-sync]] [syng-im.utils.utils :refer [log toast]] [syng-im.persistence.realm :as realm])) @@ -44,3 +46,19 @@ (defn get-command [command-key] (first (filter #(= command-key (:command %)) commands))) + +(defn- map-to-str + [m] + (join ";" (map #(join "=" %) (stringify-keys m)))) + +(defn- str-to-map + [s] + (keywordize-keys (apply hash-map (split s #"[;=]")))) + +;; TODO store command key in separate field +(defn format-command-msg-content [command content] + (map-to-str {:command (name command) :content content})) + +;; TODO temp +(defn parse-command-msg-content [content] + (update (str-to-map content) :command #(get-command (keyword %)))) diff --git a/syng-im/src/syng_im/subs.cljs b/syng-im/src/syng_im/subs.cljs index 20c120fbd5..0d548c485b 100644 --- a/syng-im/src/syng_im/subs.cljs +++ b/syng-im/src/syng_im/subs.cljs @@ -31,10 +31,7 @@ (register-sub :get-input-command (fn [db _] - (reaction (get-in @db db/input-command-path)) - ;; (let [input-command-name ] - ;; (reaction @input-command-name)) - )) + (reaction (get-in @db db/input-command-path)))) ;; -- Chats list --------------------------------------------------------------