From 028ab522b95793742d78894524926f09b046e23a Mon Sep 17 00:00:00 2001 From: Andrey Shovkoplyas Date: Tue, 27 Nov 2018 10:36:51 +0100 Subject: [PATCH] [#6867] Add on-send-sync to chat.command extension Signed-off-by: Andrey Shovkoplyas --- deps.edn | 2 +- project.clj | 2 +- src/status_im/chat/commands/core.cljs | 36 +++++++++++++++++++-------- src/status_im/chat/models/input.cljs | 9 +++++++ src/status_im/events.cljs | 5 ++++ src/status_im/extensions/core.cljs | 23 ++++++++++++++++- 6 files changed, 63 insertions(+), 14 deletions(-) diff --git a/deps.edn b/deps.edn index cb46030473..d65ce6af1c 100644 --- a/deps.edn +++ b/deps.edn @@ -11,7 +11,7 @@ com.taoensso/timbre {:mvn/version "4.10.0"} hickory {:mvn/version "0.7.1"} com.cognitect/transit-cljs {:mvn/version "0.8.248"} - status-im/pluto {:mvn/version "iteration-4-3"} + status-im/pluto {:mvn/version "iteration-4-4"} mvxcvi/alphabase {:mvn/version "1.0.0"} rasom/cljs-react-navigation {:mvn/version "0.1.4"}} diff --git a/project.clj b/project.clj index b22a8ad2be..e95c21f8da 100644 --- a/project.clj +++ b/project.clj @@ -11,7 +11,7 @@ [com.taoensso/timbre "4.10.0"] [hickory "0.7.1"] [com.cognitect/transit-cljs "0.8.248"] - [status-im/pluto "iteration-4-3"] + [status-im/pluto "iteration-4-4"] [mvxcvi/alphabase "1.0.0"] [rasom/cljs-react-navigation "0.1.4"]] :plugins [[lein-cljsbuild "1.1.7"] diff --git a/src/status_im/chat/commands/core.cljs b/src/status_im/chat/commands/core.cljs index c07219cfd0..9ce16643e8 100644 --- a/src/status_im/chat/commands/core.cljs +++ b/src/status_im/chat/commands/core.cljs @@ -120,23 +120,37 @@ :preview :view :on-send? :event :on-receive? :event + :on-send-sync? :event :parameters? [{:id :keyword :type {:one-of #{:text :phone :password :number}} :placeholder :string :suggestions? :view}]} :hook (reify hooks/Hook - (hook-in [_ id {:keys [description scope parameters preview short-preview on-send on-receive]} cofx] - (let [new-command (reify protocol/Command - (id [_] (name id)) - (scope [_] scope) - (description [_] description) - (parameters [_] (or parameters [])) - (validate [_ _ _]) - (on-send [_ command-message _] (when on-send {:dispatch (on-send command-message)})) - (on-receive [_ command-message _] (when on-receive {:dispatch (on-receive command-message)})) - (short-preview [_ props] (short-preview props)) - (preview [_ props] (preview props)))] + (hook-in [_ id {:keys [description scope parameters preview short-preview on-send on-receive on-send-sync]} cofx] + (let [new-command (if on-send-sync + (reify protocol/Command + (id [_] (name id)) + (scope [_] scope) + (description [_] description) + (parameters [_] (or parameters [])) + (validate [_ _ _]) + (on-send [_ command-message _] (when on-send {:dispatch (on-send command-message)})) + (on-receive [_ command-message _] (when on-receive {:dispatch (on-receive command-message)})) + (short-preview [_ props] (short-preview props)) + (preview [_ props] (preview props)) + protocol/Yielding + (yield-control [_ props _] {:dispatch (on-send-sync props)})) + (reify protocol/Command + (id [_] (name id)) + (scope [_] scope) + (description [_] description) + (parameters [_] (or parameters [])) + (validate [_ _ _]) + (on-send [_ command-message _] (when on-send {:dispatch (on-send command-message)})) + (on-receive [_ command-message _] (when on-receive {:dispatch (on-receive command-message)})) + (short-preview [_ props] (short-preview props)) + (preview [_ props] (preview props))))] (load-commands cofx [new-command]))) (unhook [_ id {:keys [scope]} {:keys [db] :as cofx}] (remove-command (get-in db [:id->command [(name id) scope] :type]) cofx)))}) diff --git a/src/status_im/chat/models/input.cljs b/src/status_im/chat/models/input.cljs index 7bcaaf854f..02347b302d 100644 --- a/src/status_im/chat/models/input.cljs +++ b/src/status_im/chat/models/input.cljs @@ -134,6 +134,15 @@ (set-chat-input-text nil) (process-cooldown))))) +(defn send-plain-text-message-fx + "no command detected, when not empty, proceed by sending text message without command processing" + [{:keys [db] :as cofx} message-text current-chat-id] + (when-not (string/blank? message-text) + (chat.message/send-message cofx {:chat-id current-chat-id + :content-type constants/content-type-text + :content (cond-> {:chat-id current-chat-id + :text message-text})}))) + (fx/defn send-current-message "Sends message from current chat input" [{{:keys [current-chat-id id->command access-scope->command-id] :as db} :db :as cofx}] diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index 185cc5407b..753881e74e 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -698,6 +698,11 @@ current-param-position value)))) +(handlers/register-handler-fx + :chat/send-plain-text-message + (fn [{{:keys [current-chat-id]} :db :as cofx} [_ message-text]] + (chat.input/send-plain-text-message-fx cofx message-text current-chat-id))) + (handlers/register-handler-fx :chat/disable-cooldown (fn [cofx _] diff --git a/src/status_im/extensions/core.cljs b/src/status_im/extensions/core.cljs index cb4529fd9d..faa11e68cb 100644 --- a/src/status_im/extensions/core.cljs +++ b/src/status_im/extensions/core.cljs @@ -18,7 +18,8 @@ [status-im.utils.fx :as fx] status-im.extensions.ethereum [status-im.utils.ethereum.tokens :as tokens] - [status-im.utils.ethereum.core :as ethereum])) + [status-im.utils.ethereum.core :as ethereum] + [status-im.chat.commands.sending :as commands-sending])) (re-frame/reg-fx ::alert @@ -160,6 +161,18 @@ {:db (update-in db [:chats current-chat-id :custom-params] merge params) :dispatch [:chat.ui/set-command-parameter value]})) +(handlers/register-handler-fx + :extensions.chat.command/send-plain-text-message + (fn [_ [_ _ {:keys [value]}]] + {:dispatch [:chat/send-plain-text-message value]})) + +(handlers/register-handler-fx + :extensions.chat.command/send-message + (fn [{{:keys [current-chat-id] :as db} :db :as cofx} [_ {:keys [hook-id]} {:keys [params]}]] + (when hook-id + (when-let [command (last (first (filter #(= (ffirst %) (name hook-id)) (:id->command db))))] + (commands-sending/send cofx current-chat-id command params))))) + (defn operation->fn [k] (case k :plus + @@ -272,6 +285,14 @@ {:permissions [:read] :value :extensions.chat.command/set-parameter-with-custom-params :arguments {:value :string :params :map}} + 'chat.command/send-plain-text-message + {:permissions [:read] + :value :extensions.chat.command/send-plain-text-message + :arguments {:value :string}} + 'chat.command/send-message + {:permissions [:read] + :value :extensions.chat.command/send-message + :arguments {:params :map}} 'log {:permissions [:read] :value :log