From 5ee356fe4f051f2756d8518df9c8389eb043d7b1 Mon Sep 17 00:00:00 2001 From: janherich Date: Mon, 28 May 2018 15:48:49 +0200 Subject: [PATCH] Fix console responses Signed-off-by: Andrey Shovkoplyas --- src/status_im/chat/console.cljs | 11 +++-- src/status_im/chat/events/console.cljs | 43 +++++++++++-------- .../chat/events/receive_message.cljs | 28 ++++-------- src/status_im/chat/models/message.cljs | 10 +---- 4 files changed, 39 insertions(+), 53 deletions(-) diff --git a/src/status_im/chat/console.cljs b/src/status_im/chat/console.cljs index e01ce8682b..a07d0cfc05 100644 --- a/src/status_im/chat/console.cljs +++ b/src/status_im/chat/console.cljs @@ -1,21 +1,20 @@ (ns status-im.chat.console (:require [status-im.ui.components.styles :refer [default-chat-color]] - [status-im.utils.random :as random] [status-im.constants :as constants] [status-im.utils.clocks :as utils.clocks] - [status-im.i18n :as i18n] - [clojure.string :as string])) + [status-im.i18n :as i18n])) -(defn console-message [{:keys [message-id content content-type] - :or {message-id (random/id)}}] +(defn console-message [{:keys [timestamp message-id content content-type]}] {:message-id message-id :outgoing false :chat-id constants/console-chat-id :from constants/console-chat-id :to "me" + :timestamp timestamp :clock-value (utils.clocks/send 0) :content content - :content-type content-type}) + :content-type content-type + :show? true}) (def chat {:chat-id constants/console-chat-id diff --git a/src/status_im/chat/events/console.cljs b/src/status_im/chat/events/console.cljs index 671479d3ee..0309fbf9d3 100644 --- a/src/status_im/chat/events/console.cljs +++ b/src/status_im/chat/events/console.cljs @@ -8,47 +8,52 @@ goog.string.format)) (defn console-respond-command-messages - [{:keys [name] :as command} handler-data random-id-seq] - (when command - (case name - "js" (let [{:keys [err data messages]} handler-data - content (or err data) - message-events (mapv (fn [{:keys [message type]} id] - (console-chat/console-message - {:message-id id - :content (str type ": " message) - :content-type constants/text-content-type})) - messages random-id-seq)] - (conj message-events - (console-chat/console-message - {:message-id (first random-id-seq) - :content (str content) - :content-type constants/text-content-type}))) - (log/debug "ignoring command: " name)))) + [{:keys [name] :as command} handler-data {:keys [random-id-seq now]}] + (when-let [messages (case name + "js" (let [{:keys [err data messages]} handler-data + content (or err data) + message-events (mapv (fn [{:keys [message type]} id] + (console-chat/console-message + {:message-id id + :timestamp now + :content (str type ": " message) + :content-type constants/text-content-type})) + messages random-id-seq)] + (conj message-events + (console-chat/console-message + {:message-id (first random-id-seq) + :timestamp now + :content (str content) + :content-type constants/text-content-type}))) + (log/debug "ignoring command: " name))] + {:dispatch [:chat-received-message/add messages]})) (defn faucet-base-url->url [url] (str url "/donate/0x%s")) -(defn- faucet-response-event [message-id content] +(defn- faucet-response-event [now message-id content] [:chat-received-message/add [(console-chat/console-message {:message-id message-id + :timestamp now :content content :content-type constants/text-content-type})]]) (def console-commands->fx {"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]) faucet-url (faucet-base-url->url (:url params))] {:http-get {:url (gstring/format faucet-url current-address) :success-event-creator (fn [_] (faucet-response-event + now random-id (i18n/label :t/faucet-success))) :failure-event-creator (fn [event] (log/error "Faucet error" event) (faucet-response-event + now random-id (i18n/label :t/faucet-error)))}}))}) diff --git a/src/status_im/chat/events/receive_message.cljs b/src/status_im/chat/events/receive_message.cljs index dfdacaa143..908bc04b2c 100644 --- a/src/status_im/chat/events/receive_message.cljs +++ b/src/status_im/chat/events/receive_message.cljs @@ -6,8 +6,7 @@ [status-im.constants :as constants] [status-im.utils.clocks :as utils.clocks] [status-im.utils.handlers :as handlers] - [status-im.utils.handlers-macro :as handlers-macro] - [status-im.utils.random :as random])) + [status-im.utils.handlers-macro :as handlers-macro])) ;;;; Handlers @@ -62,10 +61,11 @@ ;; TODO(alwx): refactor this when status-im.commands.handlers.jail is refactored (handlers/register-handler-fx :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 {: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)] (when update-db (re-frame/dispatch [:update-bot-db {:bot bot-id @@ -74,27 +74,15 @@ :bot-id bot-id :result data :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 (re-frame/dispatch [:chat-received-message/add - [{:message-id (random/id) + [{:message-id random-id + :timestamp now :content (str content) :content-type constants/text-content-type :outgoing false :clock-value (utils.clocks/send 0) :chat-id chat-id :from chat-id - :to "me"}]]))))) + :to "me" + :show? true}]]))))) diff --git a/src/status_im/chat/models/message.cljs b/src/status_im/chat/models/message.cljs index c349937d71..5f7e8bbd50 100644 --- a/src/status_im/chat/models/message.cljs +++ b/src/status_im/chat/models/message.cljs @@ -126,12 +126,11 @@ request-command (:request-command content) command-request? (and (= content-type constants/content-type-command-request) request-command) - new-timestamp (or timestamp now) add-message-fn (if batch? add-batch-message add-single-message)] (handlers-macro/merge-fx cofx {:confirm-message-processed [{:web3 web3 :js-obj js-obj}]} - (add-message-fn (cond-> (assoc message :timestamp new-timestamp) + (add-message-fn (cond-> message public-key (assoc :user-statuses {public-key (if current-chat? :seen :received)}) (not clock-value) @@ -383,11 +382,6 @@ :show? true} 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 [{{: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 @@ -396,7 +390,7 @@ request (:request handler-data)] (handlers-macro/merge-fx cofx (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)))) (defn invoke-console-command-handler