From c56fa0c688577e153cbeb40b5c9bcd9304f9f1b8 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Mon, 5 Jun 2017 13:50:34 +0300 Subject: [PATCH] localStorage in jail --- bots/console/bot.js | 19 ++++----- bots/demo_bot/bot.js | 10 ++++- .../react-native-status/android/build.gradle | 2 +- .../react-native-status/ios/RCTStatus/pom.xml | 2 +- resources/status.js | 9 +++++ src/status_im/commands/handlers/jail.cljs | 10 +++-- src/status_im/commands/handlers/loading.cljs | 25 ++++++------ src/status_im/data_store/local_storage.cljs | 9 +++++ .../data_store/realm/local_storage.cljs | 10 +++++ .../realm/schemas/account/core.cljs | 8 +++- .../realm/schemas/account/v8/core.cljs | 39 +++++++++++++++++++ .../schemas/account/v8/local_storage.cljs | 7 ++++ src/status_im/utils/js_resources.cljs | 8 +++- 13 files changed, 127 insertions(+), 31 deletions(-) create mode 100644 src/status_im/data_store/local_storage.cljs create mode 100644 src/status_im/data_store/realm/local_storage.cljs create mode 100644 src/status_im/data_store/realm/schemas/account/v8/core.cljs create mode 100644 src/status_im/data_store/realm/schemas/account/v8/local_storage.cljs diff --git a/bots/console/bot.js b/bots/console/bot.js index 4617ad6dc8..8854757ffc 100644 --- a/bots/console/bot.js +++ b/bots/console/bot.js @@ -227,14 +227,15 @@ function getJsSuggestions(code, context) { var doc = DOC_MAP; // TODO: what's /c / doing there ??? //console.debug(code); + var previousMessage = localStorage.getItem("previousMessage"); if (!code || code == "" || code == "c ") { code = ""; //console.debug("Last message: " + context.data); - if (context.data != null) { + if (previousMessage != null) { suggestions.push({ title: 'Last command used:', - desc: context.data, - pressValue: context.data + desc: previousMessage, + pressValue: previousMessage }); } var keys = Object.keys(doc); @@ -250,13 +251,13 @@ 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)) { + if (previousMessage != null && + (typeof previousMessage === 'string' || previousMessage instanceof String) && + previousMessage.startsWith(code)) { suggestions.unshift({ title: 'Last command used:', - desc: context.data, - pressValue: context.data + desc: previousMessage, + pressValue: previousMessage }); } var originalCode = code; @@ -355,7 +356,7 @@ function jsHandler(params, context) { messages = []; try { result["text-message"] = JSON.stringify(eval(params.code)); - localStorage.set(params.code); + localStorage.setItem("previousMessage", params.code); } catch (e) { result.err = e; } diff --git a/bots/demo_bot/bot.js b/bots/demo_bot/bot.js index b52c56191f..a93d5c117c 100644 --- a/bots/demo_bot/bot.js +++ b/bots/demo_bot/bot.js @@ -73,13 +73,19 @@ function superSuggestion(params, context) { return {markup: view}; }; -var cnt = 0; - status.addListener("on-message-input-change", superSuggestion); status.addListener("init", superSuggestion); status.addListener("on-message-send", function (params, context) { + cnt = localStorage.getItem("cnt"); + if(!cnt) { + cnt = 0; + } + cnt++; + + localStorage.setItem("cnt", cnt); if (isNaN(params.message)) { + return {"text-message": "Seems that you don't want to send money :(. cnt = " + cnt}; } diff --git a/modules/react-native-status/android/build.gradle b/modules/react-native-status/android/build.gradle index 16403329fa..a586e99405 100644 --- a/modules/react-native-status/android/build.gradle +++ b/modules/react-native-status/android/build.gradle @@ -15,5 +15,5 @@ android { dependencies { compile 'com.facebook.react:react-native:+' compile 'com.instabug.library:instabug:3+' - compile(group: 'status-im', name: 'status-go', version: 'hackaton-alt-gd3704fa', ext: 'aar') + compile(group: 'status-im', name: 'status-go', version: 'jail-signals-gfe007d5', ext: 'aar') } diff --git a/modules/react-native-status/ios/RCTStatus/pom.xml b/modules/react-native-status/ios/RCTStatus/pom.xml index 17e0bd0d7e..2ce963f1d0 100644 --- a/modules/react-native-status/ios/RCTStatus/pom.xml +++ b/modules/react-native-status/ios/RCTStatus/pom.xml @@ -25,7 +25,7 @@ status-im status-go-ios-simulator - hackaton-alt-gd3704fa + jail-signals-gfe007d5 zip true ./ diff --git a/resources/status.js b/resources/status.js index a8bc09328d..04eda5a7ad 100644 --- a/resources/status.js +++ b/resources/status.js @@ -276,3 +276,12 @@ console = (function (old) { } }; }(console)); + +localStorage.setItem = function(key, value) { + localStorageData[key] = value; + localStorage.set(JSON.stringify(localStorageData)); +}; + +localStorage.getItem = function(key) { + return localStorageData[key]; +}; diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs index 5f4071d9ca..09d57cead5 100644 --- a/src/status_im/commands/handlers/jail.cljs +++ b/src/status_im/commands/handlers/jail.cljs @@ -10,7 +10,8 @@ [status-im.constants :refer [console-chat-id]] [status-im.i18n :refer [get-contact-translated]] [taoensso.timbre :as log] - [status-im.commands.utils :as cu])) + [status-im.commands.utils :as cu] + [status-im.data-store.local-storage :as local-storage])) (defn command-handler! [_ [chat-id @@ -86,6 +87,7 @@ (handlers/side-effect! suggestions-events-handler!)) (reg-handler :set-local-storage - (fn [{:keys [current-chat-id] :as db} [{:keys [data] :as event}]] - (log/debug "Got event: " event) - (assoc-in db [:local-storage current-chat-id] data))) + (handlers/side-effect! + (fn [{:keys [current-chat-id] :as db} [{:keys [data chat_id] :as event}]] + (local-storage/set-data {:chat-id chat_id + :data data})))) diff --git a/src/status_im/commands/handlers/loading.cljs b/src/status_im/commands/handlers/loading.cljs index 15484017dc..9b8e74aba7 100644 --- a/src/status_im/commands/handlers/loading.cljs +++ b/src/status_im/commands/handlers/loading.cljs @@ -16,7 +16,8 @@ [status-im.utils.random :as random] [status-im.chat.sign-up :as sign-up] [status-im.bots.constants :as bots-constants] - [status-im.utils.datetime :as time])) + [status-im.utils.datetime :as time] + [status-im.data-store.local-storage :as local-storage])) (defn load-commands! @@ -76,16 +77,18 @@ [_ [{{:keys [whisper-identity]} :contact :keys [callback]} file]] - (status/parse-jail - whisper-identity file - (fn [result] - (let [{:keys [error result]} (json->clj result)] - (log/debug "Parsing commands results: " error result) - (if error - (dispatch [::loading-failed! whisper-identity ::error-in-jail error]) - (do - (dispatch [::add-commands whisper-identity file result]) - (when callback (callback)))))))) + (let [data (local-storage/get-data whisper-identity) + local-storage-js (js-res/local-storage-data data)] + (status/parse-jail + whisper-identity (str local-storage-js file) + (fn [result] + (let [{:keys [error result]} (json->clj result)] + (log/debug "Parsing commands results: " error result) + (if error + (dispatch [::loading-failed! whisper-identity ::error-in-jail error]) + (do + (dispatch [::add-commands whisper-identity file result]) + (when callback (callback))))))))) (defn validate-hash [db [_ file]] diff --git a/src/status_im/data_store/local_storage.cljs b/src/status_im/data_store/local_storage.cljs new file mode 100644 index 0000000000..29d9324068 --- /dev/null +++ b/src/status_im/data_store/local_storage.cljs @@ -0,0 +1,9 @@ +(ns status-im.data-store.local-storage + (:require [status-im.data-store.realm.local-storage :as data-store])) + + +(defn get-data [chat-id] + (:data (data-store/get-by-chat-id chat-id))) + +(defn set-data [local-storage] + (data-store/save local-storage)) diff --git a/src/status_im/data_store/realm/local_storage.cljs b/src/status_im/data_store/realm/local_storage.cljs new file mode 100644 index 0000000000..aac0f2f9ee --- /dev/null +++ b/src/status_im/data_store/realm/local_storage.cljs @@ -0,0 +1,10 @@ +(ns status-im.data-store.realm.local-storage + (:require [status-im.data-store.realm.core :as realm])) + +(defn get-by-chat-id + [chat-id] + (realm/get-one-by-field-clj @realm/account-realm :local-storage :chat-id chat-id)) + +(defn save + [local-storage] + (realm/save @realm/account-realm :local-storage local-storage true)) diff --git a/src/status_im/data_store/realm/schemas/account/core.cljs b/src/status_im/data_store/realm/schemas/account/core.cljs index 5c6f1bd3eb..4ac90cd844 100644 --- a/src/status_im/data_store/realm/schemas/account/core.cljs +++ b/src/status_im/data_store/realm/schemas/account/core.cljs @@ -5,7 +5,8 @@ [status-im.data-store.realm.schemas.account.v4.core :as v4] [status-im.data-store.realm.schemas.account.v5.core :as v5] [status-im.data-store.realm.schemas.account.v6.core :as v6] - [status-im.data-store.realm.schemas.account.v7.core :as v7])) + [status-im.data-store.realm.schemas.account.v7.core :as v7] + [status-im.data-store.realm.schemas.account.v8.core :as v8])) ; put schemas ordered by version (def schemas [{:schema v1/schema @@ -28,4 +29,7 @@ :migration v6/migration} {:schema v7/schema :schemaVersion 7 - :migration v7/migration}]) + :migration v7/migration} + {:schema v8/schema + :schemaVersion 8 + :migration v8/migration}]) diff --git a/src/status_im/data_store/realm/schemas/account/v8/core.cljs b/src/status_im/data_store/realm/schemas/account/v8/core.cljs new file mode 100644 index 0000000000..42c97788e7 --- /dev/null +++ b/src/status_im/data_store/realm/schemas/account/v8/core.cljs @@ -0,0 +1,39 @@ +(ns status-im.data-store.realm.schemas.account.v8.core + (:require [status-im.data-store.realm.schemas.account.v4.chat :as chat] + [status-im.data-store.realm.schemas.account.v1.chat-contact :as chat-contact] + [status-im.data-store.realm.schemas.account.v6.command :as command] + [status-im.data-store.realm.schemas.account.v6.command-parameter :as command-parameter] + [status-im.data-store.realm.schemas.account.v7.contact :as contact] + [status-im.data-store.realm.schemas.account.v1.discover :as discover] + [status-im.data-store.realm.schemas.account.v1.kv-store :as kv-store] + [status-im.data-store.realm.schemas.account.v4.message :as message] + [status-im.data-store.realm.schemas.account.v7.pending-message :as pending-message] + [status-im.data-store.realm.schemas.account.v1.processed-message :as processed-message] + [status-im.data-store.realm.schemas.account.v1.request :as request] + [status-im.data-store.realm.schemas.account.v1.tag :as tag] + [status-im.data-store.realm.schemas.account.v1.user-status :as user-status] + [status-im.data-store.realm.schemas.account.v5.contact-group :as contact-group] + [status-im.data-store.realm.schemas.account.v5.group-contact :as group-contact] + [status-im.data-store.realm.schemas.account.v8.local-storage :as local-storage] + [taoensso.timbre :as log])) + +(def schema [chat/schema + chat-contact/schema + command/schema + command-parameter/schema + contact/schema + discover/schema + kv-store/schema + message/schema + pending-message/schema + processed-message/schema + request/schema + tag/schema + user-status/schema + contact-group/schema + group-contact/schema + local-storage/schema]) + +(defn migration [old-realm new-realm] + (log/debug "migrating v8 account database: " old-realm new-realm) + (contact/migration old-realm new-realm)) diff --git a/src/status_im/data_store/realm/schemas/account/v8/local_storage.cljs b/src/status_im/data_store/realm/schemas/account/v8/local_storage.cljs new file mode 100644 index 0000000000..5b876108f4 --- /dev/null +++ b/src/status_im/data_store/realm/schemas/account/v8/local_storage.cljs @@ -0,0 +1,7 @@ +(ns status-im.data-store.realm.schemas.account.v8.local-storage) + +(def schema {:name :local-storage + :primaryKey :chat-id + :properties {:chat-id "string" + :data {:type "string" + :default "{}"}}}) diff --git a/src/status_im/utils/js_resources.cljs b/src/status_im/utils/js_resources.cljs index f319c35dbb..e5b0fcae56 100644 --- a/src/status_im/utils/js_resources.cljs +++ b/src/status_im/utils/js_resources.cljs @@ -48,4 +48,10 @@ (defn web3-init [provider-address] (str "var providerAddress = \"" provider-address "\";" (slurp "resources/web3_init.js"))) - ;; \ No newline at end of file + +(defn local-storage-data [data] + (str "var localStorageData = " + (if data + data + "{}") + ";"))