[#6867] Add on-send-sync to chat.command extension

Signed-off-by: Andrey Shovkoplyas <motor4ik@gmail.com>
This commit is contained in:
Andrey Shovkoplyas 2018-11-27 10:36:51 +01:00
parent 0547bfec0c
commit 028ab522b9
No known key found for this signature in database
GPG Key ID: EAAB7C8622D860A4
6 changed files with 63 additions and 14 deletions

View File

@ -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"}}

View File

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

View File

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

View File

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

View File

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

View File

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