From 223d3ad0b6bd1cd335289d5a85ce0fee39dc4a8c Mon Sep 17 00:00:00 2001 From: virvar Date: Thu, 14 Apr 2016 12:49:50 +0300 Subject: [PATCH] Start remote commands --- src/syng_im/android/core.cljs | 2 + src/syng_im/components/chat/chat_message.cljs | 115 ++++++++++-------- src/syng_im/handlers.cljs | 19 ++- src/syng_im/handlers/suggestions.cljs | 23 +++- src/syng_im/models/commands.cljs | 28 ++++- src/syng_im/subs.cljs | 15 ++- src/syng_im/utils/utils.cljs | 14 +++ 7 files changed, 146 insertions(+), 70 deletions(-) diff --git a/src/syng_im/android/core.cljs b/src/syng_im/android/core.cljs index fd1979193e..40db74fbae 100644 --- a/src/syng_im/android/core.cljs +++ b/src/syng_im/android/core.cljs @@ -57,5 +57,7 @@ (dispatch [:initialize-protocol]) (dispatch [:load-user-phone-number]) (dispatch [:load-syng-contacts]) + ;; load commands from remote server (todo: uncomment) + ;; (dispatch [:load-commands]) (dispatch-sync [:init-console-chat]) (.registerComponent app-registry "SyngIm" #(r/reactify-component app-root))) diff --git a/src/syng_im/components/chat/chat_message.cljs b/src/syng_im/components/chat/chat_message.cljs index 4ad20b9580..46ab93eb1a 100644 --- a/src/syng_im/components/chat/chat_message.cljs +++ b/src/syng_im/components/chat/chat_message.cljs @@ -7,7 +7,8 @@ touchable-highlight navigator toolbar-android]] - [syng-im.models.commands :as commands] + [syng-im.models.commands :refer [parse-command-msg-content + parse-command-request-msg-content]] [syng-im.utils.logging :as log] [syng-im.navigation :refer [nav-pop]] [syng-im.resources :as res] @@ -65,62 +66,72 @@ (defn message-content-command [content] - (let [{:keys [command content]} (commands/parse-command-msg-content content)] - [view {:style {:flexDirection "column"}} - [view {:style {:marginTop -5 - :marginLeft 0 - :backgroundColor (:color command) - :borderRadius 10}} - [text {:style {:marginTop 0 - :marginHorizontal 10 - :fontSize 14 - :fontFamily "Avenir-Roman" - :color "white"}} - (:text command)]] - [text {:style {:marginTop 5 - :marginHorizontal 0 - :fontSize 14 - :fontFamily "Avenir-Roman" - :color "black"}} - ;; TODO isn't smart - (if (= (:command command) :keypair-password) - "******" - content)]])) + (let [;; command-msg-atom (subscribe [:parse-command-msg-content content]) + commands-atom (subscribe [:get-commands])] + (fn [content] + (let [;; {:keys [command content]} @command-msg-atom + commands @commands-atom + {:keys [command content]} (parse-command-msg-content commands content)] + [view {:style {:flexDirection "column"}} + [view {:style {:marginTop -5 + :marginLeft 0 + :backgroundColor (:color command) + :borderRadius 10}} + [text {:style {:marginTop 0 + :marginHorizontal 10 + :fontSize 14 + :fontFamily "Avenir-Roman" + :color "white"}} + (:text command)]] + [text {:style {:marginTop 5 + :marginHorizontal 0 + :fontSize 14 + :fontFamily "Avenir-Roman" + :color "black"}} + ;; TODO isn't smart + (if (= (:command command) :keypair-password) + "******" + content)]])))) (defn set-chat-command [msg-id command] (dispatch [:set-response-chat-command msg-id (:command command)])) (defn message-content-command-request [msg-id content outgoing text-color background-color] - (let [{:keys [command content]} (commands/parse-command-request-msg-content content)] - [touchable-highlight {:onPress (fn [] - (set-chat-command msg-id command))} - [view {} - [view {:style (merge {:marginTop 15 - :borderRadius 6 - :paddingVertical 12 - :paddingHorizontal 16} - (if outgoing - {:backgroundColor "#D3EEEF"} - {:backgroundColor background-color}))} - [text {:style (merge {:fontSize 14 - :fontFamily "Avenir-Roman"} - (if outgoing - {:color "#4A5258"} - {:color text-color}))} - content]] - [view {:style {:position "absolute" - :top 0 - :left 20 - :width 30 - :height 30 - :borderRadius 50 - :backgroundColor (:color command)}} - [image {:source res/att - :style {:width 17 - :height 14 - :position "absolute" - :top 8 - :left 6}}]]]])) + (let [;; command-request-atom (subscribe [:parse-command-request-msg-content content]) + commands-atom (subscribe [:get-commands])] + (fn [msg-id content outgoing text-color background-color] + (let [;; {:keys [command content]} @command-request-atom + commands @commands-atom + {:keys [command content]} (parse-command-request-msg-content commands content)] + [touchable-highlight {:onPress (fn [] + (set-chat-command msg-id command))} + [view {} + [view {:style (merge {:marginTop 15 + :borderRadius 6 + :paddingVertical 12 + :paddingHorizontal 16} + (if outgoing + {:backgroundColor "#D3EEEF"} + {:backgroundColor background-color}))} + [text {:style (merge {:fontSize 14 + :fontFamily "Avenir-Roman"} + (if outgoing + {:color "#4A5258"} + {:color text-color}))} + content]] + [view {:style {:position "absolute" + :top 0 + :left 20 + :width 30 + :height 30 + :borderRadius 50 + :backgroundColor (:color command)}} + [image {:source res/att + :style {:width 17 + :height 14 + :position "absolute" + :top 8 + :left 6}}]]]])))) (defn message-content [{:keys [msg-id content-type content outgoing text-color background-color]}] (if (= content-type content-type-command-request) diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs index 607cfbec1b..518105aa00 100644 --- a/src/syng_im/handlers.cljs +++ b/src/syng_im/handlers.cljs @@ -15,11 +15,13 @@ [syng-im.models.commands :refer [set-chat-command set-response-chat-command set-chat-command-content - set-chat-command-request]] + set-chat-command-request + set-commands]] [syng-im.handlers.server :as server] [syng-im.handlers.contacts :as contacts-service] [syng-im.handlers.suggestions :refer [get-command - handle-command]] + handle-command + load-commands]] [syng-im.handlers.sign-up :as sign-up-service] [syng-im.models.chats :refer [create-chat]] @@ -86,6 +88,17 @@ (nav-push navigator route) db)) +(register-handler :load-commands + (fn [db [action]] + (log/debug action) + (load-commands) + db)) + +(register-handler :set-commands + (fn [db [action commands]] + (log/debug action commands) + (set-commands db commands))) + ;; -- Protocol -------------------------------------------------------------- (register-handler :initialize-protocol @@ -130,7 +143,7 @@ (register-handler :send-chat-msg (fn [db [action chat-id text]] (log/debug action "chat-id" chat-id "text" text) - (if-let [command (get-command text)] + (if-let [command (get-command db text)] (dispatch [:set-chat-command (:command command)]) (let [msg (if (= chat-id "console") (sign-up-service/send-console-msg text) diff --git a/src/syng_im/handlers/suggestions.cljs b/src/syng_im/handlers/suggestions.cljs index 19ad0b3540..f0964b60ac 100644 --- a/src/syng_im/handlers/suggestions.cljs +++ b/src/syng_im/handlers/suggestions.cljs @@ -4,24 +4,37 @@ [syng-im.models.chat :refer [current-chat-id]] [syng-im.models.commands :refer [commands suggestions + get-commands get-chat-command-request get-chat-command-to-msg-id]] - [syng-im.utils.utils :refer [log on-error http-post]] + [syng-im.utils.utils :refer [log on-error http-get]] [syng-im.utils.logging :as log])) -(defn get-suggestions [text] +(defn get-suggestions [db text] (if (= (get text 0) "!") ;; TODO change 'commands' to 'suggestions' - (filterv #(.startsWith (:text %) text) commands) + (filterv #(.startsWith (:text %) text) (get-commands db)) [])) -(defn get-command [text] +(defn get-command [db text] (when (= (get text 0) "!") ;; TODO change 'commands' to 'suggestions' - (first (filter #(= (:text %) text) commands)))) + (first (filter #(= (:text %) text) (get-commands db))))) (defn handle-command [db command-key content] (when-let [command-handler (get-chat-command-request db)] (let [to-msg-id (get-chat-command-to-msg-id db)] (command-handler to-msg-id command-key content))) db) + +(defn execute-commands-js [body] + (.eval js/window body) + (let [commands (.-commands js/window)] + (dispatch [:set-commands (map (fn [command] + (update command :command + (fn [command-key] + (keyword command-key)))) + (js->clj commands :keywordize-keys true))]))) + +(defn load-commands [] + (http-get "chat-commands.js" execute-commands-js nil)) diff --git a/src/syng_im/models/commands.cljs b/src/syng_im/models/commands.cljs index 483d11042f..5705401728 100644 --- a/src/syng_im/models/commands.cljs +++ b/src/syng_im/models/commands.cljs @@ -6,8 +6,10 @@ [syng-im.db :as db] [syng-im.models.chat :refer [current-chat-id]] [syng-im.utils.utils :refer [log toast]] + [syng-im.utils.logging :as log] [syng-im.persistence.realm :as realm])) +;; todo delete (def commands [{:command :money :text "!money" :description "Send money" @@ -49,9 +51,21 @@ :color "#9a5dcf" :suggestion true}]) +(defn get-commands [db] + ;; todo: temp. must be '(get db :commands)' + ;; (get db :commands) + commands) + +(defn set-commands [db commands] + (assoc db :commands commands)) + +;; todo delete (def suggestions (filterv :suggestion commands)) -(defn get-command [command-key] +(defn get-command [db command-key] + (first (filter #(= command-key (:command %)) (get-commands db)))) + +(defn find-command [commands command-key] (first (filter #(= command-key (:command %)) commands))) (defn get-chat-command-content [db] @@ -68,7 +82,7 @@ (-> db (set-chat-command-content nil) (assoc-in (db/chat-command-path (current-chat-id db)) - (get-command command-key)) + (get-command db command-key)) (assoc-in (db/chat-command-to-msg-id-path (current-chat-id db)) msg-id))) @@ -102,11 +116,13 @@ (defn format-command-msg-content [command content] (map-to-str {:command (name command) :content content})) -(defn parse-command-msg-content [content] - (update (str-to-map content) :command #(get-command (keyword %)))) +(defn parse-command-msg-content [commands content] + (log/info content) + (log/info (update (str-to-map content) :command #(find-command commands (keyword %)))) + (update (str-to-map content) :command #(find-command commands (keyword %)))) (defn format-command-request-msg-content [command content] (map-to-str {:command (name command) :content content})) -(defn parse-command-request-msg-content [content] - (update (str-to-map content) :command #(get-command (keyword %)))) +(defn parse-command-request-msg-content [commands content] + (update (str-to-map content) :command #(find-command commands (keyword %)))) diff --git a/src/syng_im/subs.cljs b/src/syng_im/subs.cljs index 2b5defef45..a5e5f37477 100644 --- a/src/syng_im/subs.cljs +++ b/src/syng_im/subs.cljs @@ -9,9 +9,12 @@ chat-by-id]] [syng-im.models.messages :refer [get-messages]] [syng-im.models.contacts :refer [contacts-list]] - [syng-im.models.commands :refer [get-chat-command + [syng-im.models.commands :refer [get-commands + get-chat-command get-chat-command-content - get-chat-command-request]] + get-chat-command-request + parse-command-msg-content + parse-command-request-msg-content]] [syng-im.handlers.suggestions :refer [get-suggestions]])) ;; -- Chat -------------------------------------------------------------- @@ -34,7 +37,11 @@ (register-sub :get-suggestions (fn [db _] (let [input-text (reaction (get-in @db (db/chat-input-text-path (current-chat-id @db))))] - (reaction (get-suggestions @input-text))))) + (reaction (get-suggestions @db @input-text))))) + +(register-sub :get-commands + (fn [db _] + (reaction (get-commands @db)))) (register-sub :get-chat-input-text (fn [db _] @@ -103,7 +110,7 @@ :get-contacts (fn [db _] (reaction - (get @db :contacts)))) + (get @db :contacts)))) (register-sub :all-contacts (fn [db _] diff --git a/src/syng_im/utils/utils.cljs b/src/syng_im/utils/utils.cljs index 03c88afd00..8f8c68771b 100644 --- a/src/syng_im/utils/utils.cljs +++ b/src/syng_im/utils/utils.cljs @@ -34,3 +34,17 @@ (.catch (or on-error (fn [error] (toast (str error)))))))) + +(defn http-get + ([action on-success on-error] + (-> (.fetch js/window + (str const/server-address action) + (clj->js {:method "GET"})) + (.then (fn [response] + (log response) + (.text response))) + (.then (fn [text] + (on-success text))) + (.catch (or on-error + (fn [error] + (toast (str error))))))))