implement console message handling and suggestions

This commit is contained in:
Adrian Tiberius 2016-12-02 19:13:55 +02:00
parent fe1850589d
commit 974ff50b02
4 changed files with 139 additions and 29 deletions

View File

@ -1440,6 +1440,15 @@ function getJsSuggestions(code, context) {
if (code.startsWith("c ")) {
code = code.substring(2);
}
if (context.data != null &&
(typeof context.data === 'string' || context.data instanceof String) &&
context.data.startsWith(code)) {
suggestions.unshift({
title: 'Last command used:',
desc: context.data,
pressValue: context.data
});
}
var originalCode = code;
code = cleanCode(code);
var levelCode = getLastLevel(code);
@ -1452,7 +1461,6 @@ function getJsSuggestions(code, context) {
console.log("Level code: " + levelCode);
suggestions = suggestions.concat(getPartialSuggestions(doc, originalCode, code));
}
console.log(suggestions);
return suggestions;
}
@ -1808,3 +1816,10 @@ status.response({
}
});
status.registerFunction("message-suggestions", function(params, context) {
return jsSuggestions({code: params.message}, context);
});
status.registerFunction("message-handler", function(params, context) {
return jsHandler({code: params.message}, context);
});

View File

@ -89,7 +89,8 @@
(after #(dispatch [:set-soft-input-mode :resize]))
(u/side-effect!
(fn [db _]
(dispatch [:animate-cancel-command]))))
(dispatch [:animate-cancel-command])
(dispatch [:cancel-command]))))
(defn update-input-text
[{:keys [current-chat-id] :as db} text]
@ -116,9 +117,21 @@
db))
db))
(defn set-command-suggestions
[db [_ chat-id suggestions]]
(assoc-in db [:command-suggestions chat-id] suggestions))
(register-handler ::set-command-suggestions set-command-suggestions)
(defn check-suggestions
[db [_ chat-id text]]
(let [suggestions (suggestions/get-suggestions db text)]
(let [suggestions (suggestions/get-suggestions db text)
{:keys [dapp?]} (get-in db [:contacts chat-id])]
(when (and dapp? (empty? suggestions))
(if (seq text)
(dispatch [::check-dapp-suggestions chat-id text])
(dispatch [:clear-response-suggestions chat-id])))
(log/debug "Suggestions: " suggestions)
(assoc-in db [:command-suggestions chat-id] suggestions)))
(defn select-suggestion!
@ -128,12 +141,32 @@
(dispatch [:set-chat-command (ffirst suggestions)])
(dispatch [::set-text chat-id text]))))
(register-handler ::check-dapp-suggestions
(u/side-effect!
(fn [db [_ chat-id text]]
(let [data (get-in db [:local-storage chat-id])
path [:functions
:message-suggestions]
params {:parameters {:message text}
:context {:data data}}]
(status/call-jail chat-id
path
params
(fn [{:keys [result] :as data}]
(let [{:keys [returned]} result]
(log/debug "Message suggestions: " returned)
(if returned
(dispatch [:suggestions-handler {:chat-id chat-id} data])
(dispatch [:clear-response-suggestions chat-id])))))))))
(register-handler :set-chat-input-text
(u/side-effect!
(fn [{:keys [current-chat-id]} [_ text]]
(if (console? current-chat-id)
(dispatch [::check-input-for-commands text])
(dispatch [::check-suggestions current-chat-id text])))))
(fn [{:keys [current-chat-id] :as db} [_ text]]
(let [{:keys [dapp?] :as contact} (get-in db [:contacts current-chat-id])]
(log/debug "SET CHAT INPUT TEXT: " current-chat-id text contact dapp?)
(if (console? current-chat-id)
(dispatch [::check-input-for-commands text])
(dispatch [::check-suggestions current-chat-id text]))))))
(def possible-commands
{[:confirmation-code :responses] #(re-matches #"^[\d]{4}$" %)

View File

@ -8,14 +8,16 @@
[re-frame.core :refer [enrich after dispatch path]]
[status-im.chat.utils :as cu]
[status-im.commands.utils :as commands-utils]
[status-im.constants :refer [text-content-type
[status-im.constants :refer [console-chat-id
wallet-chat-id
text-content-type
content-type-command
content-type-command-request
default-number-of-messages] :as c]
[status-im.chat.constants :refer [input-height]]
[status-im.utils.datetime :as datetime]
[status-im.protocol.core :as protocol]
[taoensso.timbre :refer-macros [debug] :as log]
[status-im.constants :refer [console-chat-id wallet-chat-id]]
[status-im.chat.handlers.console :as console]))
(defn prepare-command
@ -207,25 +209,78 @@
(fn [_ [_ {:keys [chat-id message]}]]
(messages/save chat-id message))))
(register-handler :clear-response-suggestions
(fn [db [_ chat-id]]
(-> db
(update-in [:suggestions] dissoc chat-id)
(update-in [:has-suggestions?] dissoc chat-id)
(assoc-in [:animations :to-response-height chat-id] input-height))))
(register-handler ::send-dapp-message
(u/side-effect!
(fn [db [_ chat-id {:keys [content] :as message}]]
(let [data (get-in db [:local-storage chat-id])
path [:functions
:message-handler]
params {:parameters {:message content}
:context {:data data}}]
(dispatch [:clear-response-suggestions chat-id])
(status/call-jail chat-id
path
params
(fn [{:keys [result]}]
(log/debug "Message handler result: " result)
(dispatch [::received-dapp-message chat-id result])))))))
(register-handler ::received-dapp-message
(u/side-effect!
(fn [{:keys [current-chat-id] :as db} [_ chat-id {:keys [returned] :as message}]]
(let [{:keys [data messages err]} returned
content (if err
err
data)]
(doseq [message messages]
(let [{:keys [message type]} message]
(dispatch [:received-message
{:message-id (random/id)
:content (str type ": " message)
:content-type text-content-type
:outgoing false
:chat-id chat-id
:from chat-id
:to "me"}])))
(when content
(dispatch [:received-message
{:message-id (random/id)
:content (str content)
:content-type text-content-type
:outgoing false
:chat-id chat-id
:from chat-id
:to "me"}]))))))
(register-handler ::send-message!
(u/side-effect!
(fn [{:keys [web3 chats]} [_ {{:keys [message-type]
:as message} :message
chat-id :chat-id}]]
(when (and message (cu/not-console? chat-id))
(let [message' (select-keys message [:from :message-id])
payload (select-keys message [:timestamp :content :content-type :clock-value])
options {:web3 web3
:message (assoc message' :payload payload)}]
(if (= message-type :group-user-message)
(let [{:keys [public-key private-key]} (chats chat-id)]
(protocol/send-group-message! (assoc options
:group-id chat-id
:keypair {:public public-key
:private private-key})))
(protocol/send-message! (assoc-in options
[:message :to] (:to message))))))
(dispatch [:inc-clock chat-id]))))
(fn [{:keys [web3 chats] :as db} [_ {{:keys [message-type]
:as message} :message
chat-id :chat-id}]]
(let [{:keys [dapp?] :as contact} (get-in db [:contacts chat-id])]
(if dapp?
(dispatch [::send-dapp-message chat-id message])
(when message
(let [message' (select-keys message [:from :message-id])
payload (select-keys message [:timestamp :content :content-type :clock-value])
options {:web3 web3
:message (assoc message' :payload payload)}]
(if (= message-type :group-user-message)
(let [{:keys [public-key private-key]} (chats chat-id)]
(protocol/send-group-message! (assoc options
:group-id chat-id
:keypair {:public public-key
:private private-key})))
(protocol/send-message! (assoc-in options
[:message :to] (:to message)))))))
(dispatch [:inc-clock chat-id])))))
(register-handler ::send-command-protocol!
(u/side-effect!

View File

@ -7,6 +7,7 @@
[status-im.commands.utils :refer [generate-hiccup reg-handler]]
[clojure.string :as s]
[status-im.components.react :as r]
[status-im.models.commands :as cm]
[status-im.constants :refer [console-chat-id]]
[taoensso.timbre :as log]))
@ -50,11 +51,17 @@
(assoc-in [:has-suggestions? chat-id] (or hiccup webViewUrl)))))
(defn suggestions-events-handler!
[db [[n data]]]
[{:keys [current-chat-id] :as db} [[n data]]]
(log/debug "Suggestion event: " data)
(let [{:keys [dapp?] :as contact} (get-in db [:contacts current-chat-id])
command? (= :command (:type (cm/get-chat-command db)))]
(case (keyword n)
:set-value (dispatch [:fill-chat-command-content data])
:set-value (if command?
(dispatch [:fill-chat-command-content data])
(when dapp?
(dispatch [:set-chat-input-text data])))
;; todo show error?
nil))
nil)))
(defn command-preview
[db [chat-id command-id {:keys [result]}]]