Start remote commands

Former-commit-id: 223d3ad0b6
This commit is contained in:
virvar 2016-04-14 12:49:50 +03:00
parent 763eb979b8
commit 48e67fc0f6
7 changed files with 146 additions and 70 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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