Send staged commands

This commit is contained in:
virvar 2016-04-21 15:33:53 +03:00
parent 6f1b183fb1
commit 3cde44c5cc
5 changed files with 70 additions and 23 deletions

View File

@ -31,7 +31,8 @@
(defn plain-message-input-view [] (defn plain-message-input-view []
(let [chat (subscribe [:get-current-chat]) (let [chat (subscribe [:get-current-chat])
input-message-atom (subscribe [:get-chat-input-text])] input-message-atom (subscribe [:get-chat-input-text])
staged-commands-atom (subscribe [:get-chat-staged-commands])]
(fn [] (fn []
(let [input-message @input-message-atom] (let [input-message @input-message-atom]
[view {:style {:flexDirection "column"}} [view {:style {:flexDirection "column"}}
@ -67,7 +68,8 @@
:marginRight 18 :marginRight 18
:width 20 :width 20
:height 20}}] :height 20}}]
(when (< 0 (count input-message)) (when (or (< 0 (count input-message))
(< 0 (count @staged-commands-atom)))
[touchable-highlight {:on-press (fn [] [touchable-highlight {:on-press (fn []
(send @chat input-message))} (send @chat input-message))}
[view {:style {:marginTop 10 [view {:style {:marginTop 10

View File

@ -2,7 +2,7 @@
(:require (:require
[re-frame.core :refer [register-handler after dispatch]] [re-frame.core :refer [register-handler after dispatch]]
[schema.core :as s :include-macros true] [schema.core :as s :include-macros true]
[syng-im.db :refer [app-db schema]] [syng-im.db :as db :refer [app-db schema]]
[syng-im.protocol.api :refer [init-protocol]] [syng-im.protocol.api :refer [init-protocol]]
[syng-im.protocol.protocol-handler :refer [make-handler]] [syng-im.protocol.protocol-handler :refer [make-handler]]
[syng-im.models.protocol :refer [update-identity [syng-im.models.protocol :refer [update-identity
@ -24,7 +24,8 @@
[syng-im.handlers.suggestions :refer [get-command [syng-im.handlers.suggestions :refer [get-command
handle-command handle-command
get-command-handler get-command-handler
load-commands]] load-commands
apply-staged-commands]]
[syng-im.handlers.sign-up :as sign-up-service] [syng-im.handlers.sign-up :as sign-up-service]
[syng-im.models.chats :refer [create-chat]] [syng-im.models.chats :refer [create-chat]]
@ -36,7 +37,8 @@
set-chat-input-text]] set-chat-input-text]]
[syng-im.utils.logging :as log] [syng-im.utils.logging :as log]
[syng-im.protocol.api :as api] [syng-im.protocol.api :as api]
[syng-im.constants :refer [text-content-type]] [syng-im.constants :refer [text-content-type
content-type-command]]
[syng-im.navigation :refer [nav-push]] [syng-im.navigation :refer [nav-push]]
[syng-im.utils.crypt :refer [gen-random-bytes]])) [syng-im.utils.crypt :refer [gen-random-bytes]]))
@ -143,25 +145,56 @@
(let [{:keys [chat-id]} (message-by-id msg-id)] (let [{:keys [chat-id]} (message-by-id msg-id)]
(signal-chat-updated db chat-id)))) (signal-chat-updated db chat-id))))
(defn send-staged-commands [db chat-id]
(let [staged-commands (get-in db (db/chat-staged-commands-path chat-id))]
(dorun
(map
(fn [staged-command]
(let [command-key (get-in staged-command [:command :command])
content (commands/format-command-msg-content command-key
(:content staged-command))
msg (if (= chat-id "console")
(sign-up-service/send-console-command db command-key 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 content-type-command
:outgoing true}))]
(save-message chat-id msg)))
staged-commands))
db))
(register-handler :send-chat-msg (register-handler :send-chat-msg
(fn [db [action chat-id text]] (fn [db [action chat-id text]]
(log/debug action "chat-id" chat-id "text" text) (log/debug action "chat-id" chat-id "text" text)
(if-let [command (get-command db text)] (if-let [command (get-command db text)]
(dispatch [:set-chat-command (:command command)]) (do (dispatch [:set-chat-command (:command command)])
(let [msg (if (= chat-id "console") db)
(sign-up-service/send-console-msg text) (let [msg (when (< 0 (count text))
(let [{msg-id :msg-id (if (= chat-id "console")
{from :from (sign-up-service/send-console-msg text)
to :to} :msg} (api/send-user-msg {:to chat-id (let [{msg-id :msg-id
:content text})] {from :from
{:msg-id msg-id to :to} :msg} (api/send-user-msg {:to chat-id
:from from :content text})]
:to to {:msg-id msg-id
:content text :from from
:content-type text-content-type :to to
:outgoing true}))] :content text
(save-message chat-id msg) :content-type text-content-type
(signal-chat-updated db chat-id))))) :outgoing true})))]
(when msg
(save-message chat-id msg))
(-> db
(send-staged-commands chat-id)
(apply-staged-commands)
(signal-chat-updated chat-id))))))
(register-handler :send-chat-command (register-handler :send-chat-command
(fn [db [action chat-id command content]] (fn [db [action chat-id command content]]
@ -266,7 +299,7 @@
(let [db (set-chat-input-text db nil) (let [db (set-chat-input-text db nil)
command-info {:command command command-info {:command command
:content content :content content
:handler (get-command-handler db command content)}] :handler (get-command-handler db (:command command) content)}]
(stage-command db command-info)))) (stage-command db command-info))))
(register-handler :unstage-command (register-handler :unstage-command

View File

@ -188,7 +188,7 @@
{:msg-id (random/id) {:msg-id (random/id)
:from "me" :from "me"
:to "console" :to "console"
:content (commands/format-command-msg-content command-key content) :content content ;; (commands/format-command-msg-content command-key content)
:content-type content-type-command :content-type content-type-command
:outgoing true}) :outgoing true})

View File

@ -6,7 +6,8 @@
suggestions suggestions
get-commands get-commands
get-chat-command-request get-chat-command-request
get-chat-command-to-msg-id]] get-chat-command-to-msg-id
clear-staged-commands]]
[syng-im.utils.utils :refer [log on-error http-get]] [syng-im.utils.utils :refer [log on-error http-get]]
[syng-im.utils.logging :as log])) [syng-im.utils.logging :as log]))
@ -33,6 +34,14 @@
(fn [] (fn []
(command-handler to-msg-id command-key content))))) (command-handler to-msg-id command-key content)))))
(defn apply-staged-commands [db]
(let [staged-commands (get-in db (db/chat-staged-commands-path (current-chat-id db)))]
(dorun (map (fn [staged-command]
(when-let [handler (:handler staged-command)]
(handler)))
staged-commands))
(clear-staged-commands db)))
(defn execute-commands-js [body] (defn execute-commands-js [body]
(.eval js/window body) (.eval js/window body)
(let [commands (.-commands js/window)] (let [commands (.-commands js/window)]

View File

@ -114,6 +114,9 @@
(fn [staged-commands] (fn [staged-commands]
(filterv #(not= % staged-command) staged-commands)))) (filterv #(not= % staged-command) staged-commands))))
(defn clear-staged-commands [db]
(assoc-in db (db/chat-staged-commands-path (current-chat-id db)) []))
(defn get-chat-command-request [db] (defn get-chat-command-request [db]
(get-in db (db/chat-command-request-path (current-chat-id db) (get-in db (db/chat-command-request-path (current-chat-id db)
(get-chat-command-to-msg-id db)))) (get-chat-command-to-msg-id db))))