Handle only last requested command

This commit is contained in:
virvar 2016-04-07 20:50:53 +03:00
parent 2c8cfa8212
commit c08acdf24a
6 changed files with 145 additions and 111 deletions

View File

@ -27,4 +27,6 @@
[:chats chat-id :command-input :command])
(defn chat-command-content-path [chat-id]
[:chats chat-id :command-input :content])
(defn chat-command-request-path [chat-id]
[:chats chat-id :command-request])
(def new-group-path [:new-group])

View File

@ -12,10 +12,12 @@
[syng-im.models.messages :refer [save-message
update-message!
message-by-id]]
[syng-im.models.commands :refer [set-chat-command
set-chat-command-content
set-chat-command-request]]
[syng-im.handlers.server :as server]
[syng-im.handlers.contacts :as contacts-service]
[syng-im.handlers.commands :refer [set-chat-command
set-chat-command-content]]
[syng-im.handlers.sign-up :as sign-up-service]
[syng-im.models.chats :refer [create-chat]]
@ -146,7 +148,7 @@
(log/debug action "chat-id" chat-id "command" command "content" content)
(let [db (set-chat-input-text db nil)
msg (if (= chat-id "console")
(sign-up-service/send-console-command command content)
(sign-up-service/send-console-command db command content)
;; TODO handle command, now sends as plain message
(let [{msg-id :msg-id
{from :from
@ -242,6 +244,10 @@
(fn [db [_ content]]
(set-chat-command-content db content)))
(register-handler :set-chat-command-request
(fn [db [_ handler]]
(set-chat-command-request db handler)))
(register-handler :show-contacts
(fn [db [action navigator]]
(log/debug action)

View File

@ -1,16 +0,0 @@
(ns syng-im.handlers.commands
(:require [re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.db :as db]
[syng-im.models.commands :refer [get-command]]
[syng-im.utils.utils :refer [log on-error http-post]]
[syng-im.utils.logging :as log]))
(defn set-chat-command-content [db content]
(assoc-in db (db/chat-command-content-path (get-in db db/current-chat-id-path))
content))
(defn set-chat-command [db command-key]
(-> db
(set-chat-command-content nil)
(assoc-in (db/chat-command-path (get-in db db/current-chat-id-path))
(get-command command-key))))

View File

@ -10,35 +10,6 @@
content-type-command
content-type-command-request]]))
(defn intro [db]
(dispatch [:received-msg
{:msg-id "1"
:content "Hello there! It's Syng, a Dapp browser in your phone."
:content-type text-content-type
:outgoing false
:from "console"
:to "me"}])
(dispatch [:received-msg
{:msg-id "2"
:content (str "Syng uses a highly secure key-pair authentication type "
"to provide you a reliable way to access your account")
:content-type text-content-type
:outgoing false
:from "console"
:to "me"}])
(dispatch [:received-msg
{:msg-id "3"
:content (commands/format-command-request-msg-content
:keypair-password
(str "A key pair has been generated and saved to your device. "
"Create a password to secure your key"))
:content-type content-type-command-request
:outgoing false
:from "console"
:to "me"}])
;; (dispatch [:set-chat-command :keypair-password])
db)
(defn send-console-msg [text]
{:msg-id (random/id)
:from "me"
@ -47,7 +18,68 @@
:content-type text-content-type
:outgoing true})
(defn- handle-password [content]
;; -- Send confirmation code and synchronize contacts---------------------------
(defn on-sync-contacts []
(dispatch [:received-msg
{:msg-id (random/id)
:content (str "Your contacts have been synchronized")
:content-type text-content-type
:outgoing false
:from "console"
:to "me"}]))
(defn sync-contacts []
(dispatch [:sync-contacts on-sync-contacts]))
(defn on-send-code-response [body]
(if (:confirmed body)
(do (dispatch [:received-msg
{:msg-id (random/id)
:content "Confirmed"
:content-type text-content-type
:outgoing false
:from "console"
:to "me"}])
(dispatch [:set-chat-command-request nil])
(sync-contacts))
(dispatch [:received-msg
{:msg-id (random/id)
:content "Wrong code"
:content-type text-content-type
:outgoing false
:from "console"
:to "me"}])))
(defn send-code [code]
(dispatch [:sign-up-confirm code on-send-code-response]))
(defn- handle-confirmation-code [command-key content]
(when (= command-key :confirmation-code)
(send-code content)))
;; -- Send phone number ----------------------------------------
(defn on-sign-up-response []
(dispatch [:received-msg
{:msg-id (random/id)
:content (commands/format-command-request-msg-content
:confirmation-code
(str "Thanks! We've sent you a text message with a confirmation "
"code. Please provide that code to confirm your phone number"))
:content-type content-type-command-request
:outgoing false
:from "console"
:to "me"}])
(dispatch [:set-chat-command-request handle-confirmation-code]))
(defn- handle-phone [command-key content]
(when (= command-key :phone)
(let [phone-number (format-phone-number content)]
(dispatch [:sign-up phone-number on-sign-up-response]))))
;; -- Saving password ----------------------------------------
(defn- save-password [password]
;; TODO validate and save password
(dispatch [:received-msg
{:msg-id (random/id)
@ -101,75 +133,50 @@
:content-type content-type-command-request
:outgoing false
:from "console"
:to "me"}]))
:to "me"}])
(dispatch [:set-chat-command-request handle-phone]))
(defn- handle-password [command-key content]
(when (= command-key :keypair-password)
(save-password content)))
;; -- Send phone number ----------------------------------------
(defn on-sign-up-response []
(defn intro [db]
(dispatch [:received-msg
{:msg-id (random/id)
:content (commands/format-command-request-msg-content
:confirmation-code
(str "Thanks! We've sent you a text message with a confirmation "
"code. Please provide that code to confirm your phone number"))
:content-type content-type-command-request
:outgoing false
:from "console"
:to "me"}]))
(defn- handle-phone [content]
(let [phone-number (format-phone-number content)]
(dispatch [:sign-up phone-number on-sign-up-response])))
;; -- Phone number confirmation --------------------------------
(defn on-sync-contacts []
(dispatch [:received-msg
{:msg-id (random/id)
:content (str "Your contacts have been synchronized")
{:msg-id "1"
:content "Hello there! It's Syng, a Dapp browser in your phone."
:content-type text-content-type
:outgoing false
:from "console"
:to "me"}]))
(defn sync-contacts []
(dispatch [:sync-contacts on-sync-contacts]))
(defn on-send-code-response [body]
(if (:confirmed body)
(do (dispatch [:received-msg
{:msg-id (random/id)
:content "Confirmed"
:content-type text-content-type
:outgoing false
:from "console"
:to "me"}])
(sync-contacts))
(dispatch [:received-msg
{:msg-id (random/id)
:content "Wrong code"
:content-type text-content-type
:outgoing false
:from "console"
:to "me"}])))
(defn send-code [code]
(dispatch [:sign-up-confirm code on-send-code-response]))
(defn- handle-confirmation-code [content]
(send-code content))
:to "me"}])
(dispatch [:received-msg
{:msg-id "2"
:content (str "Syng uses a highly secure key-pair authentication type "
"to provide you a reliable way to access your account")
:content-type text-content-type
:outgoing false
:from "console"
:to "me"}])
(dispatch [:received-msg
{:msg-id "3"
:content (commands/format-command-request-msg-content
:keypair-password
(str "A key pair has been generated and saved to your device. "
"Create a password to secure your key"))
:content-type content-type-command-request
:outgoing false
:from "console"
:to "me"}])
(dispatch [:set-chat-command-request handle-password])
;; (dispatch [:set-chat-command :keypair-password])
db)
;; TODO store command key in a separate field
(defn send-console-command [command content]
(when (= command :keypair-password)
(handle-password content))
(when (= command :phone)
(handle-phone content))
(when (= command :confirmation-code)
(handle-confirmation-code content))
(defn send-console-command [db command-key content]
(when-let [command-handler (commands/get-chat-command-request db)]
(command-handler command-key content))
{:msg-id (random/id)
:from "me"
:to "console"
:content (commands/format-command-msg-content command content)
:content (commands/format-command-msg-content command-key content)
:content-type content-type-command
:outgoing true})

View File

@ -3,6 +3,8 @@
[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.db :as db]
[syng-im.models.chat :refer [current-chat-id]]
[syng-im.utils.utils :refer [log toast]]
[syng-im.persistence.realm :as realm]))
@ -52,6 +54,29 @@
(defn get-command [command-key]
(first (filter #(= command-key (:command %)) commands)))
(defn get-chat-command-content [db]
(get-in db (db/chat-command-content-path (current-chat-id db))))
(defn set-chat-command-content [db content]
(assoc-in db (db/chat-command-content-path (get-in db db/current-chat-id-path))
content))
(defn get-chat-command [db]
(get-in db (db/chat-command-path (current-chat-id db))))
(defn set-chat-command [db command-key]
(-> db
(set-chat-command-content nil)
(assoc-in (db/chat-command-path (get-in db db/current-chat-id-path))
(get-command command-key))))
(defn get-chat-command-request [db]
(get-in db (db/chat-command-request-path (current-chat-id db))))
(defn set-chat-command-request [db handler]
(assoc-in db (db/chat-command-request-path (current-chat-id db)) handler))
(defn- map-to-str
[m]
(join ";" (map #(join "=" %) (stringify-keys m))))

View File

@ -9,6 +9,9 @@
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
get-chat-command-content
get-chat-command-request]]
[syng-im.handlers.suggestions :refer [get-suggestions]]))
;; -- Chat --------------------------------------------------------------
@ -39,11 +42,18 @@
(register-sub :get-chat-command
(fn [db _]
(reaction (get-in @db (db/chat-command-path (current-chat-id @db))))))
(-> (get-chat-command @db)
(reaction))))
(register-sub :get-chat-command-content
(fn [db _]
(reaction (get-in @db (db/chat-command-content-path (current-chat-id @db))))))
(-> (get-chat-command-content @db)
(reaction))))
(register-sub :chat-command-request
(fn [db _]
(-> (get-chat-command-request @db)
(reaction))))
;; -- Chats list --------------------------------------------------------------