From 6840460ba9155fd80fe1a63edd9c1520f4bbca3e Mon Sep 17 00:00:00 2001 From: virvar Date: Fri, 8 Apr 2016 20:08:40 +0300 Subject: [PATCH] Can response to request message by clicking on message only --- src/syng_im/components/chat/chat_message.cljs | 15 ++-- src/syng_im/db.cljs | 8 +- src/syng_im/handlers.cljs | 16 +++- src/syng_im/handlers/sign_up.cljs | 83 ++++++++++--------- src/syng_im/handlers/suggestions.cljs | 11 ++- src/syng_im/models/commands.cljs | 25 ++++-- 6 files changed, 98 insertions(+), 60 deletions(-) diff --git a/src/syng_im/components/chat/chat_message.cljs b/src/syng_im/components/chat/chat_message.cljs index 2014ece589..4ad20b9580 100644 --- a/src/syng_im/components/chat/chat_message.cljs +++ b/src/syng_im/components/chat/chat_message.cljs @@ -87,13 +87,13 @@ "******" content)]])) -(defn set-chat-command [command] - (dispatch [:set-chat-command (:command command)])) +(defn set-chat-command [msg-id command] + (dispatch [:set-response-chat-command msg-id (:command command)])) -(defn message-content-command-request [content outgoing text-color background-color] +(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 command))} + (set-chat-command msg-id command))} [view {} [view {:style (merge {:marginTop 15 :borderRadius 6 @@ -122,9 +122,9 @@ :top 8 :left 6}}]]]])) -(defn message-content [{:keys [content-type content outgoing text-color background-color]}] +(defn message-content [{:keys [msg-id content-type content outgoing text-color background-color]}] (if (= content-type content-type-command-request) - [message-content-command-request content outgoing text-color background-color] + [message-content-command-request msg-id content outgoing text-color background-color] [view {:style (merge {:borderRadius 6} (if (= content-type text-content-type) {:paddingVertical 12 @@ -175,7 +175,8 @@ :alignItems "flex-end"} {:alignSelf "flex-start" :alignItems "flex-start"}))} - [message-content {:content-type content-type + [message-content {:msg-id msg-id + :content-type content-type :content content :outgoing outgoing :text-color text-color diff --git a/src/syng_im/db.cljs b/src/syng_im/db.cljs index 9bc355ab95..55b9b1f20a 100644 --- a/src/syng_im/db.cljs +++ b/src/syng_im/db.cljs @@ -26,8 +26,12 @@ [:chats chat-id :input-text]) (defn chat-command-path [chat-id] [:chats chat-id :command-input :command]) +(defn chat-command-to-msg-id-path [chat-id] + [:chats chat-id :command-input :to-msg-id]) (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]) +(defn chat-command-requests-path [chat-id] + [:chats chat-id :command-requests]) +(defn chat-command-request-path [chat-id msg-id] + [:chats chat-id :command-requests msg-id]) (def new-group-path [:new-group]) diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs index 4d9bdbac23..607cfbec1b 100644 --- a/src/syng_im/handlers.cljs +++ b/src/syng_im/handlers.cljs @@ -13,11 +13,13 @@ update-message! message-by-id]] [syng-im.models.commands :refer [set-chat-command + set-response-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.suggestions :refer [get-command]] + [syng-im.handlers.suggestions :refer [get-command + handle-command]] [syng-im.handlers.sign-up :as sign-up-service] [syng-im.models.chats :refer [create-chat]] @@ -163,7 +165,9 @@ :content-type text-content-type :outgoing true}))] (save-message chat-id msg) - (signal-chat-updated db chat-id)))) + (-> db + (handle-command command content) + (signal-chat-updated chat-id))))) (register-handler :send-group-chat-msg (fn [db [action chat-id text]] @@ -240,13 +244,17 @@ (fn [db [_ command-key]] (set-chat-command db command-key))) +(register-handler :set-response-chat-command + (fn [db [_ to-msg-id command-key]] + (set-response-chat-command db to-msg-id command-key))) + (register-handler :set-chat-command-content (fn [db [_ content]] (set-chat-command-content db content))) (register-handler :set-chat-command-request - (fn [db [_ handler]] - (set-chat-command-request db handler))) + (fn [db [_ msg-id handler]] + (set-chat-command-request db msg-id handler))) (register-handler :show-contacts (fn [db [action navigator]] diff --git a/src/syng_im/handlers/sign_up.cljs b/src/syng_im/handlers/sign_up.cljs index d84a98170d..3231629394 100644 --- a/src/syng_im/handlers/sign_up.cljs +++ b/src/syng_im/handlers/sign_up.cljs @@ -49,7 +49,6 @@ :outgoing false :from "console" :to "me"}]) - (dispatch [:set-chat-command-request nil]) (sync-contacts)) (dispatch [:received-msg {:msg-id (random/id) @@ -62,25 +61,28 @@ (defn send-code [code] (dispatch [:sign-up-confirm code on-send-code-response])) -(defn- handle-confirmation-code [command-key content] +(defn- handle-confirmation-code [msg-id command-key content] + (dispatch [:set-chat-command-request msg-id nil]) (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])) + (let [msg-id (random/id)] + (dispatch [:received-msg + {:msg-id msg-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 msg-id handle-confirmation-code]))) -(defn- handle-phone [command-key content] +(defn- handle-phone [msg-id command-key content] + (dispatch [:set-chat-command-request msg-id nil]) (when (= command-key :phone) (let [phone-number (format-phone-number content)] (dispatch [:sign-up phone-number on-sign-up-response])))) @@ -131,20 +133,22 @@ :from "console" :to "me"}]) ;; TODO highlight '!phone' - (dispatch [:received-msg - {:msg-id (random/id) - :content (commands/format-command-request-msg-content - :phone - (str "Your phone number is also required to use the app. Type the " - "exclamation mark or hit the icon to open the command list " - "and choose the !phone command")) - :content-type content-type-command-request - :outgoing false - :from "console" - :to "me"}]) - (dispatch [:set-chat-command-request handle-phone])) + (let [msg-id (random/id)] + (dispatch [:received-msg + {:msg-id msg-id + :content (commands/format-command-request-msg-content + :phone + (str "Your phone number is also required to use the app. Type the " + "exclamation mark or hit the icon to open the command list " + "and choose the !phone command")) + :content-type content-type-command-request + :outgoing false + :from "console" + :to "me"}]) + (dispatch [:set-chat-command-request msg-id handle-phone]))) -(defn- handle-password [command-key content] +(defn- handle-password [msg-id command-key content] + (dispatch [:set-chat-command-request msg-id nil]) (when (= command-key :keypair-password) (save-password content))) @@ -164,24 +168,23 @@ :outgoing false :from "console" :to "me"}]) - (dispatch [:received-msg - {:msg-id (random/id) - :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]) + (let [msg-id (random/id)] + (dispatch [:received-msg + {:msg-id msg-id + :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 msg-id handle-password])) ;; (dispatch [:set-chat-command :keypair-password]) db) ;; TODO store command key in a separate field (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" diff --git a/src/syng_im/handlers/suggestions.cljs b/src/syng_im/handlers/suggestions.cljs index 352f54c803..19ad0b3540 100644 --- a/src/syng_im/handlers/suggestions.cljs +++ b/src/syng_im/handlers/suggestions.cljs @@ -2,7 +2,10 @@ (:require [re-frame.core :refer [subscribe dispatch dispatch-sync]] [syng-im.db :as db] [syng-im.models.chat :refer [current-chat-id]] - [syng-im.models.commands :refer [commands suggestions]] + [syng-im.models.commands :refer [commands + suggestions + get-chat-command-request + get-chat-command-to-msg-id]] [syng-im.utils.utils :refer [log on-error http-post]] [syng-im.utils.logging :as log])) @@ -16,3 +19,9 @@ (when (= (get text 0) "!") ;; TODO change 'commands' to 'suggestions' (first (filter #(= (:text %) text) commands)))) + +(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) diff --git a/src/syng_im/models/commands.cljs b/src/syng_im/models/commands.cljs index f778f4f755..483d11042f 100644 --- a/src/syng_im/models/commands.cljs +++ b/src/syng_im/models/commands.cljs @@ -64,17 +64,30 @@ (defn get-chat-command [db] (get-in db (db/chat-command-path (current-chat-id db)))) -(defn set-chat-command [db command-key] +(defn set-response-chat-command [db msg-id 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)))) + (assoc-in (db/chat-command-path (current-chat-id db)) + (get-command command-key)) + (assoc-in (db/chat-command-to-msg-id-path (current-chat-id db)) + msg-id))) + +(defn set-chat-command [db command-key] + (set-response-chat-command db nil command-key)) + +(defn get-chat-command-to-msg-id [db] + (get-in db (db/chat-command-to-msg-id-path (current-chat-id 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)))) -(defn set-chat-command-request [db handler] - (assoc-in db (db/chat-command-request-path (current-chat-id db)) handler)) +(defn set-chat-command-request [db msg-id handler] + (update-in db (db/chat-command-requests-path (current-chat-id db)) + (fn [requests] + (if requests + (assoc requests msg-id handler) + {msg-id handler})))) (defn- map-to-str