init command

move commands and responses to :contacts from :chats
This commit is contained in:
Roman Volosovskyi 2017-04-27 18:16:06 +03:00 committed by Roman Volosovskyi
parent d044b1cf43
commit deb7b2b617
11 changed files with 64 additions and 53 deletions

View File

@ -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!"};
});

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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