use global @browse command for opening dapps (#847)

This commit is contained in:
Roman Volosovskyi 2017-04-27 14:46:00 +03:00 committed by Roman Volosovskyi
parent 239a0cbfe6
commit 1fbb447761
10 changed files with 45 additions and 82 deletions

View File

@ -1,10 +1,6 @@
function browseSuggestions(params, context) {
var url;
if (context["dapp-url"]) {
url = context["dapp-url"];
}
if (params.url && params.url !== "undefined" && params.url != "") {
url = params.url;
if (!/^[a-zA-Z-_]+:/.test(url)) {

View File

@ -1,33 +0,0 @@
I18n.translations = {
en: {
browse_title: 'Browser',
browse_description: 'Open web browser'
}
};
status.command({
name: "browse",
title: I18n.t('browse_title'),
description: I18n.t('browse_description'),
color: "#ffa500",
fullscreen: true,
params: [{
name: "url",
optional: true,
type: status.types.TEXT
}],
onSend: function (params, context) {
var url = params.url || params.metadata.url;
if (!/^[a-zA-Z-_]+:/.test(url)) {
url = 'http://' + url;
}
return {
title: params.metadata.name,
dynamicTitle: true,
markup: status.components.bridgedWebView(url)
};
}
});
status.autorun("browse");

View File

@ -150,9 +150,6 @@ var status = {
registerFunction: function (name, fn) {
_status_catalog.functions[name] = fn;
},
autorun: function (commandName) {
_status_catalog.autorun = commandName;
},
localizeNumber: function (num, del, sep) {
return I18n.toNumber(
num.replace(",", "."),

View File

@ -240,7 +240,7 @@
db' (assoc db :current-chat-id chat-id)
commands-loaded? (if js/goog.DEBUG
false
(get-in db [:chats chat-id :commands-loaded]))]
(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])
@ -442,13 +442,14 @@
(let [{:keys [clock-value]} (messages/get-by-id message-id)]
(send-clock-value! db to message-id clock-value)))))
(register-handler :check-autorun
(register-handler :check-and-open-dapp!
(u/side-effect!
(fn [{:keys [current-chat-id contacts] :as db}]
(let [autorun (get-in db [:chats current-chat-id :autorun])]
(when autorun
(fn [{:keys [current-chat-id global-commands contacts] :as db}]
(let [dapp-url (get-in db [:contacts current-chat-id :dapp-url])]
(when dapp-url
(am/go
(dispatch [:select-chat-input-command {:name autorun}])
(dispatch [:select-chat-input-command
(assoc (:browse global-commands) :prefill [dapp-url])])
(a/<! (a/timeout 100))
(dispatch [:send-current-message])))))))

View File

@ -10,11 +10,11 @@
(handlers/register-handler :request-command-data
(handlers/side-effect!
(fn [{:keys [chats current-account-id] :as db}
(fn [{:keys [contacts current-account-id] :as db}
[_ {{:keys [command params content-command type]} :content
:keys [message-id chat-id on-requested jail-id] :as message} data-type]]
(let [jail-id (or jail-id chat-id)]
(if-not (get-in chats [jail-id :commands-loaded])
(if-not (get-in contacts [jail-id :commands-loaded])
(do (dispatch [:add-commands-loading-callback
jail-id
#(dispatch [:request-command-data message data-type])])
@ -22,7 +22,7 @@
(let [path [(if (= :response (keyword type)) :responses :commands)
(if content-command content-command command)
data-type]
to (get-in db [:contacts chat-id :address])
to (get-in contacts [chat-id :address])
params {:parameters params
:context (merge {:platform platform/platform
:from current-account-id

View File

@ -110,22 +110,23 @@
(handlers/register-handler
:load-chat-parameter-box
(handlers/side-effect!
(fn [{:keys [current-chat-id] :as db} [_ {:keys [name type] :as command}]]
(fn [{:keys [current-chat-id] :as db} [_ {:keys [name type bot] :as command}]]
(let [parameter-index (input-model/argument-position db current-chat-id)]
(when (and command (> parameter-index -1))
(let [data (get-in db [:local-storage current-chat-id])
path [(if (= :command type) :commands :responses)
name
:params
parameter-index
:suggestions]
args (-> (get-in db [:chats current-chat-id :input-text])
(input-model/split-command-args)
(rest))
params {:parameters {:args args}
:context (merge {:data data}
(input-model/command-dependent-context-params command))}]
(status/call-jail current-chat-id
(let [jail-id (or bot current-chat-id)
data (get-in db [:local-storage current-chat-id])
path [(if (= :command type) :commands :responses)
name
:params
parameter-index
:suggestions]
args (-> (get-in db [:chats jail-id :input-text])
(input-model/split-command-args)
(rest))
params {:parameters {:args args}
:context (merge {:data data}
(input-model/command-dependent-context-params command))}]
(status/call-jail jail-id
path
params
#(dispatch [:suggestions-handler

View File

@ -98,7 +98,7 @@
(assoc-in db [:chats chat-id :last-message] message)))
(defn commands-loaded? [db chat-id]
(get-in db [:chats chat-id :commands-loaded]))
(get-in db [:contacts chat-id :commands-loaded]))
(def timeout 400)

View File

@ -177,7 +177,7 @@
show-emoji? [:chat-ui-props :show-emoji?]
layout-height [:get :layout-height]
input-text [:chat :input-text]]
{:component-did-mount #(do (dispatch [:check-autorun])
{:component-did-mount #(do (dispatch [:check-and-open-dapp!])
(dispatch [:update-suggestions]))
:component-will-unmount #(dispatch [:set-chat-ui-props {:show-emoji? false}])}
[view {:style st/chat-view

View File

@ -39,14 +39,15 @@
(dispatch [::validate-hash params (js-res/get-resource bot-url)])
(and dapp? dapp-url)
(http-get (s/join "/" [dapp-url "commands.js"])
(fn [response]
(and
(string? (.text response))
(when-let [content-type (.. response -headers (get "Content-Type"))]
(s/includes? "application/javascript" content-type))))
#(dispatch [::validate-hash whisper-identity %])
#(dispatch [::validate-hash whisper-identity js-res/dapp-js]))
(let [url (s/join "/" [dapp-url "commands.js"])]
(http-get url
(fn [response]
(and
(string? (.text response))
(when-let [content-type (.. response -headers (get "Content-Type"))]
(s/includes? "application/javascript" content-type))))
#(dispatch [::validate-hash whisper-identity %])
#(log/debug (str "command.js wasn't found at " url))))
:else
(dispatch [::validate-hash params js-res/commands-js])))
@ -107,7 +108,7 @@
(into {})))
(defn add-commands
[db [id _ {:keys [commands responses autorun]}]]
[db [id _ {:keys [commands responses]}]]
(let [account @(subscribe [:get-current-account])
commands' (filter-forbidden-names account id commands)
global-command (:global commands')
@ -115,12 +116,14 @@
responses' (filter-forbidden-names account id responses)]
(cond-> db
true
(update-in [:contacts id] assoc
:commands-loaded true)
(get-in db [:chats id])
(update-in [:chats id] assoc
:commands (mark-as :command commands'')
:responses (mark-as :response responses')
:commands-loaded true
:autorun autorun
:global-command global-command)
global-command
@ -152,8 +155,8 @@
(reg-handler :check-and-load-commands!
(u/side-effect!
(fn [{:keys [chats]} [identity callback]]
(if (get-in chats [identity :commands-loaded])
(fn [{:keys [contacts]} [identity callback]]
(if (get-in contacts [identity :commands-loaded])
(callback)
(dispatch [:load-commands! identity callback])))))
@ -169,8 +172,8 @@
(reg-handler ::add-commands
[(after save-commands-js!)
(after save-global-command!)
(after #(dispatch [:check-autorun]))
(after #(dispatch [:update-suggestions]))
;;(after #(dispatch [:check-and-open-dapp!]))
;;(after #(dispatch [:update-suggestions]))
(after (fn [_ [id]]
(dispatch [:invoke-commands-loading-callbacks id])
(dispatch [:invoke-chat-loaded-callbacks id])))]

View File

@ -34,8 +34,6 @@
(def status-js (str (slurp "resources/status.js")
(slurp "resources/i18n.js")))
(def dapp-js (str (slurp "resources/dapp.js")))
(def webview-js (slurp "resources/webview.js"))
(def jquery (str
" if (typeof jQuery2 == 'undefined') {"