mirror of
https://github.com/status-im/status-react.git
synced 2025-01-11 03:26:31 +00:00
Fix console responses
Signed-off-by: Andrey Shovkoplyas <motor4ik@gmail.com>
This commit is contained in:
parent
af7c356134
commit
5ee356fe4f
@ -1,21 +1,20 @@
|
|||||||
(ns status-im.chat.console
|
(ns status-im.chat.console
|
||||||
(:require [status-im.ui.components.styles :refer [default-chat-color]]
|
(:require [status-im.ui.components.styles :refer [default-chat-color]]
|
||||||
[status-im.utils.random :as random]
|
|
||||||
[status-im.constants :as constants]
|
[status-im.constants :as constants]
|
||||||
[status-im.utils.clocks :as utils.clocks]
|
[status-im.utils.clocks :as utils.clocks]
|
||||||
[status-im.i18n :as i18n]
|
[status-im.i18n :as i18n]))
|
||||||
[clojure.string :as string]))
|
|
||||||
|
|
||||||
(defn console-message [{:keys [message-id content content-type]
|
(defn console-message [{:keys [timestamp message-id content content-type]}]
|
||||||
:or {message-id (random/id)}}]
|
|
||||||
{:message-id message-id
|
{:message-id message-id
|
||||||
:outgoing false
|
:outgoing false
|
||||||
:chat-id constants/console-chat-id
|
:chat-id constants/console-chat-id
|
||||||
:from constants/console-chat-id
|
:from constants/console-chat-id
|
||||||
:to "me"
|
:to "me"
|
||||||
|
:timestamp timestamp
|
||||||
:clock-value (utils.clocks/send 0)
|
:clock-value (utils.clocks/send 0)
|
||||||
:content content
|
:content content
|
||||||
:content-type content-type})
|
:content-type content-type
|
||||||
|
:show? true})
|
||||||
|
|
||||||
(def chat
|
(def chat
|
||||||
{:chat-id constants/console-chat-id
|
{:chat-id constants/console-chat-id
|
||||||
|
@ -8,47 +8,52 @@
|
|||||||
goog.string.format))
|
goog.string.format))
|
||||||
|
|
||||||
(defn console-respond-command-messages
|
(defn console-respond-command-messages
|
||||||
[{:keys [name] :as command} handler-data random-id-seq]
|
[{:keys [name] :as command} handler-data {:keys [random-id-seq now]}]
|
||||||
(when command
|
(when-let [messages (case name
|
||||||
(case name
|
"js" (let [{:keys [err data messages]} handler-data
|
||||||
"js" (let [{:keys [err data messages]} handler-data
|
content (or err data)
|
||||||
content (or err data)
|
message-events (mapv (fn [{:keys [message type]} id]
|
||||||
message-events (mapv (fn [{:keys [message type]} id]
|
(console-chat/console-message
|
||||||
(console-chat/console-message
|
{:message-id id
|
||||||
{:message-id id
|
:timestamp now
|
||||||
:content (str type ": " message)
|
:content (str type ": " message)
|
||||||
:content-type constants/text-content-type}))
|
:content-type constants/text-content-type}))
|
||||||
messages random-id-seq)]
|
messages random-id-seq)]
|
||||||
(conj message-events
|
(conj message-events
|
||||||
(console-chat/console-message
|
(console-chat/console-message
|
||||||
{:message-id (first random-id-seq)
|
{:message-id (first random-id-seq)
|
||||||
:content (str content)
|
:timestamp now
|
||||||
:content-type constants/text-content-type})))
|
:content (str content)
|
||||||
(log/debug "ignoring command: " name))))
|
:content-type constants/text-content-type})))
|
||||||
|
(log/debug "ignoring command: " name))]
|
||||||
|
{:dispatch [:chat-received-message/add messages]}))
|
||||||
|
|
||||||
(defn faucet-base-url->url [url]
|
(defn faucet-base-url->url [url]
|
||||||
(str url "/donate/0x%s"))
|
(str url "/donate/0x%s"))
|
||||||
|
|
||||||
(defn- faucet-response-event [message-id content]
|
(defn- faucet-response-event [now message-id content]
|
||||||
[:chat-received-message/add
|
[:chat-received-message/add
|
||||||
[(console-chat/console-message
|
[(console-chat/console-message
|
||||||
{:message-id message-id
|
{:message-id message-id
|
||||||
|
:timestamp now
|
||||||
:content content
|
:content content
|
||||||
:content-type constants/text-content-type})]])
|
:content-type constants/text-content-type})]])
|
||||||
|
|
||||||
(def console-commands->fx
|
(def console-commands->fx
|
||||||
{"faucet"
|
{"faucet"
|
||||||
(fn [{:keys [db random-id]} {:keys [params]}]
|
(fn [{:keys [db random-id now]} {:keys [params]}]
|
||||||
(let [current-address (get-in db [:account/account :address])
|
(let [current-address (get-in db [:account/account :address])
|
||||||
faucet-url (faucet-base-url->url (:url params))]
|
faucet-url (faucet-base-url->url (:url params))]
|
||||||
{:http-get {:url (gstring/format faucet-url current-address)
|
{:http-get {:url (gstring/format faucet-url current-address)
|
||||||
:success-event-creator (fn [_]
|
:success-event-creator (fn [_]
|
||||||
(faucet-response-event
|
(faucet-response-event
|
||||||
|
now
|
||||||
random-id
|
random-id
|
||||||
(i18n/label :t/faucet-success)))
|
(i18n/label :t/faucet-success)))
|
||||||
:failure-event-creator (fn [event]
|
:failure-event-creator (fn [event]
|
||||||
(log/error "Faucet error" event)
|
(log/error "Faucet error" event)
|
||||||
(faucet-response-event
|
(faucet-response-event
|
||||||
|
now
|
||||||
random-id
|
random-id
|
||||||
(i18n/label :t/faucet-error)))}}))})
|
(i18n/label :t/faucet-error)))}}))})
|
||||||
|
|
||||||
|
@ -6,8 +6,7 @@
|
|||||||
[status-im.constants :as constants]
|
[status-im.constants :as constants]
|
||||||
[status-im.utils.clocks :as utils.clocks]
|
[status-im.utils.clocks :as utils.clocks]
|
||||||
[status-im.utils.handlers :as handlers]
|
[status-im.utils.handlers :as handlers]
|
||||||
[status-im.utils.handlers-macro :as handlers-macro]
|
[status-im.utils.handlers-macro :as handlers-macro]))
|
||||||
[status-im.utils.random :as random]))
|
|
||||||
|
|
||||||
;;;; Handlers
|
;;;; Handlers
|
||||||
|
|
||||||
@ -62,10 +61,11 @@
|
|||||||
;; TODO(alwx): refactor this when status-im.commands.handlers.jail is refactored
|
;; TODO(alwx): refactor this when status-im.commands.handlers.jail is refactored
|
||||||
(handlers/register-handler-fx
|
(handlers/register-handler-fx
|
||||||
:chat-received-message/bot-response
|
:chat-received-message/bot-response
|
||||||
(fn [{:contacts/keys [contacts]} [_ {:keys [chat-id] :as params} {:keys [result bot-id] :as data}]]
|
message-model/receive-interceptors
|
||||||
|
(fn [{:keys [random-id now]} [{:keys [chat-id] :as params} {:keys [result bot-id] :as data}]]
|
||||||
(let [{:keys [returned context]} result
|
(let [{:keys [returned context]} result
|
||||||
{:keys [markup text-message err]} returned
|
{:keys [markup text-message err]} returned
|
||||||
{:keys [log-messages update-db default-db]} context
|
{:keys [update-db default-db]} context
|
||||||
content (or err text-message)]
|
content (or err text-message)]
|
||||||
(when update-db
|
(when update-db
|
||||||
(re-frame/dispatch [:update-bot-db {:bot bot-id
|
(re-frame/dispatch [:update-bot-db {:bot bot-id
|
||||||
@ -74,27 +74,15 @@
|
|||||||
:bot-id bot-id
|
:bot-id bot-id
|
||||||
:result data
|
:result data
|
||||||
:default-db default-db)])
|
:default-db default-db)])
|
||||||
(doseq [message log-messages]
|
|
||||||
(let [{:keys [message type]} message]
|
|
||||||
(when (or (not= type "debug")
|
|
||||||
js/goog.DEBUG
|
|
||||||
(get-in contacts [chat-id :debug?]))
|
|
||||||
(re-frame/dispatch [:chat-received-message/add
|
|
||||||
[{:message-id (random/id)
|
|
||||||
:content (str type ": " message)
|
|
||||||
:content-type constants/content-type-log-message
|
|
||||||
:outgoing false
|
|
||||||
:clock-value (utils.clocks/send 0)
|
|
||||||
:chat-id chat-id
|
|
||||||
:from chat-id
|
|
||||||
:to "me"}]]))))
|
|
||||||
(when content
|
(when content
|
||||||
(re-frame/dispatch [:chat-received-message/add
|
(re-frame/dispatch [:chat-received-message/add
|
||||||
[{:message-id (random/id)
|
[{:message-id random-id
|
||||||
|
:timestamp now
|
||||||
:content (str content)
|
:content (str content)
|
||||||
:content-type constants/text-content-type
|
:content-type constants/text-content-type
|
||||||
:outgoing false
|
:outgoing false
|
||||||
:clock-value (utils.clocks/send 0)
|
:clock-value (utils.clocks/send 0)
|
||||||
:chat-id chat-id
|
:chat-id chat-id
|
||||||
:from chat-id
|
:from chat-id
|
||||||
:to "me"}]])))))
|
:to "me"
|
||||||
|
:show? true}]])))))
|
||||||
|
@ -126,12 +126,11 @@
|
|||||||
request-command (:request-command content)
|
request-command (:request-command content)
|
||||||
command-request? (and (= content-type constants/content-type-command-request)
|
command-request? (and (= content-type constants/content-type-command-request)
|
||||||
request-command)
|
request-command)
|
||||||
new-timestamp (or timestamp now)
|
|
||||||
add-message-fn (if batch? add-batch-message add-single-message)]
|
add-message-fn (if batch? add-batch-message add-single-message)]
|
||||||
(handlers-macro/merge-fx cofx
|
(handlers-macro/merge-fx cofx
|
||||||
{:confirm-message-processed [{:web3 web3
|
{:confirm-message-processed [{:web3 web3
|
||||||
:js-obj js-obj}]}
|
:js-obj js-obj}]}
|
||||||
(add-message-fn (cond-> (assoc message :timestamp new-timestamp)
|
(add-message-fn (cond-> message
|
||||||
public-key
|
public-key
|
||||||
(assoc :user-statuses {public-key (if current-chat? :seen :received)})
|
(assoc :user-statuses {public-key (if current-chat? :seen :received)})
|
||||||
(not clock-value)
|
(not clock-value)
|
||||||
@ -383,11 +382,6 @@
|
|||||||
:show? true}
|
:show? true}
|
||||||
chat)))
|
chat)))
|
||||||
|
|
||||||
(defn- add-console-responses
|
|
||||||
[command handler-data {:keys [random-id-seq]}]
|
|
||||||
{:dispatch (->> (console-events/console-respond-command-messages command handler-data random-id-seq)
|
|
||||||
(vector :chat-received-message/add))})
|
|
||||||
|
|
||||||
(defn send-command
|
(defn send-command
|
||||||
[{{:keys [current-public-key chats network] :as db} :db :keys [now] :as cofx} params]
|
[{{:keys [current-public-key chats network] :as db} :db :keys [now] :as cofx} params]
|
||||||
(let [{{:keys [handler-data to-message command] :as content} :command chat-id :chat-id} params
|
(let [{{:keys [handler-data to-message command] :as content} :command chat-id :chat-id} params
|
||||||
@ -396,7 +390,7 @@
|
|||||||
request (:request handler-data)]
|
request (:request handler-data)]
|
||||||
(handlers-macro/merge-fx cofx
|
(handlers-macro/merge-fx cofx
|
||||||
(upsert-and-send (prepare-command-message current-public-key chat now request content network))
|
(upsert-and-send (prepare-command-message current-public-key chat now request content network))
|
||||||
(add-console-responses command handler-data)
|
(console-events/console-respond-command-messages command handler-data)
|
||||||
(requests-events/request-answered chat-id to-message))))
|
(requests-events/request-answered chat-id to-message))))
|
||||||
|
|
||||||
(defn invoke-console-command-handler
|
(defn invoke-console-command-handler
|
||||||
|
Loading…
x
Reference in New Issue
Block a user