From deb7b2b61718c5d01525564554458094a9fdd9ed Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 27 Apr 2017 18:16:06 +0300 Subject: [PATCH] init command move commands and responses to :contacts from :chats --- bots/mailman/bot.js | 4 ++ src/status_im/chat/handlers.cljs | 46 +++++++++++-------- src/status_im/chat/handlers/input.cljs | 15 +++--- src/status_im/chat/handlers/send_message.cljs | 17 +++---- .../chat/handlers/webview_bridge.cljs | 2 +- src/status_im/chat/models/input.cljs | 3 +- src/status_im/chat/models/suggestions.cljs | 4 +- src/status_im/chat/subs.cljs | 6 +-- src/status_im/chat/views/input/input.cljs | 2 +- src/status_im/commands/handlers/loading.cljs | 9 ++-- src/status_im/components/status.cljs | 9 ++++ 11 files changed, 64 insertions(+), 53 deletions(-) diff --git a/bots/mailman/bot.js b/bots/mailman/bot.js index a1ef5625a6..cafbcf4df0 100644 --- a/bots/mailman/bot.js +++ b/bots/mailman/bot.js @@ -47,3 +47,7 @@ status.command({ type: status.types.TEXT, placeholder: I18n.t('location_address') }); + +status.addListener("init", function (params, context) { + return {"text-message": "Hello, man!"}; +}); diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 206ea3d292..ac987341f2 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -234,25 +234,33 @@ (defmethod nav/preload-data! :chat [{:keys [current-chat-id] :as db} [_ _ id]] - (let [chat-id (or id current-chat-id) - messages (get-in db [:chats chat-id :messages]) - command? (= :command (get-in db [:edit-mode chat-id])) - db' (assoc db :current-chat-id chat-id) - commands-loaded? (if js/goog.DEBUG - false - (get-in db [:contacts chat-id :commands-loaded]))] - (dispatch [:load-requests! chat-id]) - ;; todo rewrite this. temporary fix for https://github.com/status-im/status-react/issues/607 - #_(dispatch [:load-commands! chat-id]) - (if-not commands-loaded? - (dispatch [:load-commands! chat-id]) - (dispatch [:invoke-chat-loaded-callbacks chat-id])) - (if (and (seq messages) - (not= (count messages) 1)) - db' - (-> db' - load-messages! - init-chat)))) + (let [chat-id (or id current-chat-id) + messages (get-in db [:chats chat-id :messages]) + command? (= :command (get-in db [:edit-mode chat-id])) + db' (-> db + (assoc :current-chat-id chat-id) + (assoc-in [:chats chat-id :was-opened?] true)) + commands-loaded? (get-in db [:contacts chat-id :commands-loaded]) + bot-url (get-in db [:contacts chat-id :bot-url]) + was-opened? (get-in db [:chats chat-id :was-opened?]) + call-init-command #(when (and (not was-opened?) bot-url) + (status/call-function! + {:chat-id chat-id + :function :init}))] + (dispatch [:load-requests! chat-id]) + ;; todo rewrite this. temporary fix for https://github.com/status-im/status-react/issues/607 + #_(dispatch [:load-commands! chat-id]) + (if-not commands-loaded? + (dispatch [:load-commands! chat-id call-init-command]) + (do + (call-init-command) + (dispatch [:invoke-chat-loaded-callbacks chat-id]))) + (if (and (seq messages) + (not= (count messages) 1)) + db' + (-> db' + load-messages! + init-chat)))) (register-handler :add-chat-loaded-callback (fn [db [_ chat-id callback]] diff --git a/src/status_im/chat/handlers/input.cljs b/src/status_im/chat/handlers/input.cljs index c4e80e430c..f6dbc91290 100644 --- a/src/status_im/chat/handlers/input.cljs +++ b/src/status_im/chat/handlers/input.cljs @@ -277,15 +277,12 @@ ::check-dapp-suggestions (handlers/side-effect! (fn [db [_ chat-id text]] - (let [data (get-in db [:local-storage chat-id]) - path [:functions :on-message-input-change] - params {:parameters {:message text} - :context {:data data}}] - (status/call-jail chat-id - path - params - #(dispatch [:received-bot-response - {:chat-id chat-id} %])))))) + (let [data (get-in db [:local-storage chat-id])] + (status/call-function! + {:chat-id chat-id + :function :on-message-input-change + :parameters {:message text} + :context {:data data}}))))) (handlers/register-handler :clear-seq-arguments diff --git a/src/status_im/chat/handlers/send_message.cljs b/src/status_im/chat/handlers/send_message.cljs index 56968736a1..f6659d99a4 100644 --- a/src/status_im/chat/handlers/send_message.cljs +++ b/src/status_im/chat/handlers/send_message.cljs @@ -191,17 +191,12 @@ (register-handler ::send-dapp-message (u/side-effect! (fn [db [_ chat-id {:keys [content]}]] - (let [data (get-in db [:local-storage chat-id]) - path [:functions :on-message-send] - params {:parameters {:message content} - :context {:data data}}] - (status/call-jail chat-id - path - params - (fn [resp] - (log/debug "Message handler result: " resp) - (dispatch [:received-bot-response - {:chat-id chat-id} resp]))))))) + (let [data (get-in db [:local-storage chat-id])] + (status/call-function! + {:chat-id chat-id + :function :on-message-send + :parameters {:message content} + :context {:data data}}))))) (register-handler :received-bot-response (u/side-effect! diff --git a/src/status_im/chat/handlers/webview_bridge.cljs b/src/status_im/chat/handlers/webview_bridge.cljs index 2328f1dc86..b1d4dc96fd 100644 --- a/src/status_im/chat/handlers/webview_bridge.cljs +++ b/src/status_im/chat/handlers/webview_bridge.cljs @@ -61,7 +61,7 @@ (register-handler ::send-command (u/side-effect! (fn [{:keys [current-chat-id] :as db} [_ command-key params]] - (let [command (get-in db [:chats current-chat-id :commands command-key]) + (let [command (get-in db [:contacts current-chat-id :commands command-key]) command-input {:content "0" :command command :parameter-idx 0 diff --git a/src/status_im/chat/models/input.cljs b/src/status_im/chat/models/input.cljs index 246555747b..d3fecbf487 100644 --- a/src/status_im/chat/models/input.cljs +++ b/src/status_im/chat/models/input.cljs @@ -14,7 +14,8 @@ (dec (count text))))) (defn possible-chat-actions [{:keys [global-commands] :as db} chat-id] - (let [{:keys [commands requests responses]} (get-in db [:chats chat-id]) + (let [{:keys [requests]} (get-in db [:chats chat-id]) + {:keys [commands responses]} (get-in db [:contacts chat-id]) commands' (into {} (map (fn [[k v]] [k [v :any]]) (merge global-commands commands))) responses' (into {} (map (fn [{:keys [message-id type]}] diff --git a/src/status_im/chat/models/suggestions.cljs b/src/status_im/chat/models/suggestions.cljs index 8b1e194492..5713b9b23e 100644 --- a/src/status_im/chat/models/suggestions.cljs +++ b/src/status_im/chat/models/suggestions.cljs @@ -23,12 +23,12 @@ (let [requests (get-in db [:chats current-chat-id :requests])] (->> requests (map (fn [{:keys [type] :as v}] - (assoc v :name (get-in db [:chats current-chat-id :responses type :name])))) + (assoc v :name (get-in db [:contacts current-chat-id :responses type :name])))) (filter (fn [v] ((can-be-suggested? text) v)))))) (defn get-command-suggestions [{:keys [current-chat-id] :as db} text] - (let [commands (get-in db [:chats current-chat-id :commands])] + (let [commands (get-in db [:contacts current-chat-id :commands])] (filter (fn [[_ v]] ((can-be-suggested? text) v)) commands))) (defn get-global-command-suggestions diff --git a/src/status_im/chat/subs.cljs b/src/status_im/chat/subs.cljs index 5a02514541..354f62c527 100644 --- a/src/status_im/chat/subs.cljs +++ b/src/status_im/chat/subs.cljs @@ -62,13 +62,13 @@ (register-sub :get-commands (fn [db [_ chat-id]] (let [current-chat (or chat-id (@db :current-chat-id))] - (reaction (or (get-in @db [:chats current-chat :commands]) {}))))) + (reaction (or (get-in @db [:contacts current-chat :commands]) {}))))) (register-sub :get-responses (fn [db [_ chat-id]] (let [current-chat (or chat-id (@db :current-chat-id))] - (reaction (or (get-in @db [:chats current-chat :responses]) {}))))) + (reaction (or (get-in @db [:contacts current-chat :responses]) {}))))) (register-sub :possible-chat-actions @@ -159,7 +159,7 @@ (register-sub :get-response (fn [db [_ n]] (let [chat-id (subscribe [:get-current-chat-id])] - (reaction (get-in @db [:chats @chat-id :responses n]))))) + (reaction (get-in @db [:contacts @chat-id :responses n]))))) (register-sub :is-request-answered? (fn [_ [_ message-id]] diff --git a/src/status_im/chat/views/input/input.cljs b/src/status_im/chat/views/input/input.cljs index cb45ffa024..0b551ffd32 100644 --- a/src/status_im/chat/views/input/input.cljs +++ b/src/status_im/chat/views/input/input.cljs @@ -36,7 +36,7 @@ (defview commands-view [] [commands [:chat :command-suggestions] - responses [:chat :responses] + responses [:get-responses] requests [:chat :request-suggestions] show-suggestions? [:show-suggestions?]] [view style/commands-root diff --git a/src/status_im/commands/handlers/loading.cljs b/src/status_im/commands/handlers/loading.cljs index 2ab882bade..71b52db116 100644 --- a/src/status_im/commands/handlers/loading.cljs +++ b/src/status_im/commands/handlers/loading.cljs @@ -21,8 +21,8 @@ (defn load-commands! [{:keys [current-chat-id contacts]} [identity callback]] (let [identity' (or identity current-chat-id) - contact (or (get contacts identity') - sign-up/console-contact)] + contact (or (get contacts identity') + sign-up/console-contact)] (when identity' (dispatch [::fetch-commands! {:contact contact :callback callback}]))) @@ -118,10 +118,7 @@ true (update-in [:contacts id] assoc - :commands-loaded true) - - (get-in db [:chats id]) - (update-in [:chats id] assoc + :commands-loaded true :commands (mark-as :command commands'') :responses (mark-as :response responses') :global-command global-command) diff --git a/src/status_im/components/status.cljs b/src/status_im/components/status.cljs index 8301c9d639..cab11a70c1 100644 --- a/src/status_im/components/status.cljs +++ b/src/status_im/components/status.cljs @@ -146,6 +146,15 @@ (callback r')))] (.callJail status chat-id (cljs->json path) (cljs->json params') cb)))))) +(defn call-function! + [{:keys [chat-id function] :as opts}] + (let [path [:functions function] + params (select-keys opts [:parameters :context])] + (call-jail + chat-id + path + params + #(dispatch [:received-bot-response {:chat-id chat-id} %])))) (defn set-soft-input-mode [mode] (when status