fix account database separation

Former-commit-id: fac008b087
This commit is contained in:
Adrian Tiberius 2016-07-18 19:29:51 +03:00
parent d3b8840893
commit 68b6be2f31
14 changed files with 98 additions and 69 deletions

View File

@ -21,7 +21,8 @@
"react-native-camera",
"react-native-qrcode",
"react-native-orientation",
"identicon.js"
"identicon.js",
"react-native-fs"
],
"imageDirs": [
"images"

View File

@ -133,6 +133,7 @@ dependencies {
compile project(':react-native-camera')
compile project(':react-native-status')
compile project(':react-native-orientation')
compile project(':react-native-fs')
//compile(name:'statusgo-android-16', ext:'aar')
compile(group: 'status-im', name: 'status-go', version: '0.1.0-201607011545-da53ec', ext: 'aar')

View File

@ -29,6 +29,8 @@ import com.lwansbrough.RCTCamera.*;
import com.i18n.reactnativei18n.ReactNativeI18n;
import android.content.res.Configuration;
import com.rnfs.RNFSPackage;
public class MainActivity extends ReactActivity {
private static final String TAG = "MainActivity";
@ -112,6 +114,7 @@ public class MainActivity extends ReactActivity {
new RCTCameraPackage(),
new SmsListener(this),
new OrientationPackage(this),
new RNFSPackage(),
new GethPackage()
);
}

View File

@ -26,3 +26,6 @@ project(':react-native-camera').projectDir = new File(rootProject.projectDir,
include ':react-native-orientation', ':app'
project(':react-native-orientation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-orientation/android')
include ':react-native-fs'
project(':react-native-fs').projectDir = new File(settingsDir, '../node_modules/react-native-fs/android')

View File

@ -15,15 +15,16 @@
"react-native-camera": "github:codyhazelwood/react-native-camera",
"react-native-circle-checkbox": "^0.1.3",
"react-native-contacts": "^0.2.4",
"react-native-fs": "^1.5.1",
"react-native-i18n": "0.0.8",
"react-native-invertible-scroll-view": "^1.0.0",
"react-native-linear-gradient": "1.5.7",
"react-native-loading-spinner-overlay": "0.0.8",
"react-native-orientation": "^1.17.0",
"react-native-qrcode": "^0.2.2",
"react-native-randombytes": "^2.1.0",
"react-native-status": "git+ssh://git@github.com/status-im/react-native-status",
"react-native-vector-icons": "^1.3.4",
"react-native-orientation": "^1.17.0",
"realm": "^0.14.0",
"react-native-status": "git+ssh://git@github.com/status-im/react-native-status"
"realm": "^0.14.0"
}
}

View File

@ -47,18 +47,19 @@
(.createAccount geth password (fn [result] (account-created db result password)))
db)))
(defn logged-in [db address]
(let [account (get-in db [:accounts address])]
(log/debug "Logged in: " address account)
(realm/close-account-realm)
(reset! realm/account-realm (realm/create-account-realm address))
(defn initialize-account [db account]
(let [is-login-screen? (= (:view-id db) :login)]
(dispatch [:set :login {}])
(dispatch [:set :is-logged-in true])
(dispatch [:set :user-identity account])
(dispatch [:save-console])
(dispatch [:initialize-account account])
(when (:signed-up db) (dispatch [:navigate-to-clean default-view]))))
(when is-login-screen? (dispatch [:navigate-to-clean default-view]))))
(defn logged-in [db address]
(let [account (get-in db [:accounts address])]
(log/debug "Logged in: " address account)
(realm/change-account-realm address #(when (nil? %)
(initialize-account db account)))))
(register-handler :login-account
(-> (fn [db [_ address password]]

View File

@ -69,7 +69,7 @@
(fn []
(let [startup-view (if @account @view-id (if (contains? #{:login :chat} @view-id) @view-id :accounts))]
(log/debug startup-view)
(case (if @signed-up startup-view :chat)
(case (if true startup-view :chat)
:discovery [main-tabs]
:discovery-tag [discovery-tag]
:add-participants [new-participants]
@ -91,10 +91,6 @@
(dispatch-sync [:reset-app])
(dispatch [:initialize-crypt])
(dispatch [:initialize-geth])
(dispatch [:load-user-phone-number])
;(dispatch [:init-chat])
(init-back-button-handler!)
(.registerComponent app-registry "StatusIm" #(r/reactify-component app-root)))

View File

@ -31,10 +31,9 @@
(assoc db :show-actions show-actions)))
(register-handler :load-more-messages
(fn [{:keys [is-logged-in current-chat-id] :as db} _]
(let [all-loaded? (get-in db [:chats current-chat-id :all-loaded?])
account-realm-exists? (or (not= current-chat-id "console") is-logged-in)]
(if (or all-loaded? (not account-realm-exists?))
(fn [{:keys [current-chat-id] :as db} _]
(let [all-loaded? (get-in db [:chats current-chat-id :all-loaded?])]
(if all-loaded?
db
(let [messages-path [:chats current-chat-id :messages]
messages (get-in db messages-path)
@ -292,22 +291,21 @@
:content content})))))
(defn save-message-to-realm!
[{:keys [is-logged-in new-message current-chat-id]} _]
(when (and new-message is-logged-in)
[{:keys [new-message current-chat-id]} _]
(when new-message
(messages/save-message current-chat-id new-message)))
(defn save-commands-to-realm!
[{:keys [is-logged-in new-commands current-chat-id]} _]
(when is-logged-in
(doseq [new-command new-commands]
(messages/save-message
current-chat-id
(dissoc new-command :rendered-preview :to-message)))))
[{:keys [new-commands current-chat-id]} _]
(doseq [new-command new-commands]
(messages/save-message
current-chat-id
(dissoc new-command :rendered-preview :to-message))))
(defn dispatch-responded-requests!
[{:keys [new-commands current-chat-id]} _]
(doseq [{:keys [is-logged-in to-message]} new-commands]
(when (and to-message is-logged-in)
(doseq [{:keys [to-message]} new-commands]
(when to-message
(dispatch [:request-answered! current-chat-id to-message]))))
(defn invoke-commands-handlers!
@ -411,18 +409,6 @@
((enrich init-chat))
((after load-commands!))))
(register-handler :save-console
(fn [db _]
(when-not (chats/chat-exists? "console")
(let [console-chat (get-in db [:chats "console"])]
(when console-chat
(do
(chats/create-chat console-chat)
(doseq [message (reverse (:messages console-chat))]
(messages/save-message "console" message))))))
db))
(defn initialize-chats
[{:keys [loaded-chats] :as db} _]
(let [chats (->> loaded-chats
@ -443,9 +429,8 @@
((enrich initialize-chats) load-chats!))
(defn store-message!
[{:keys [is-logged-in new-message]} [_ {chat-id :from}]]
(if (or (not= chat-id "console") is-logged-in)
(messages/save-message chat-id new-message)))
[{:keys [new-message]} [_ {chat-id :from}]]
(messages/save-message chat-id new-message))
(defn dispatch-request!
[{:keys [new-message]} [_ {chat-id :from}]]
@ -471,20 +456,15 @@
(messages/save-message chat-id msg))))
(defmethod nav/preload-data! :chat
[{:keys [is-logged-in current-chat-id] :as db} [_ _ id]]
[{:keys [current-chat-id] :as db} [_ _ id]]
(let [chat-id (or id current-chat-id)
messages (get-in db [:chats chat-id :messages])
db' (assoc db :current-chat-id chat-id)
account-realm-exists? (or (not= chat-id "console") is-logged-in)]
(when account-realm-exists?
(dispatch [:load-requests! chat-id]))
db' (assoc db :current-chat-id chat-id)]
(dispatch [:load-requests! chat-id])
(if (seq messages)
db'
(-> db'
((fn [db]
(if account-realm-exists?
(load-messages! db)
db)))
load-messages!
init-chat))))
(defn prepare-chat

View File

@ -6,10 +6,8 @@
[status-im.utils.handlers :as u]))
(defn store-request!
[{:keys [is-logged-in new-request] :as db}]
(if (or (not= (:chat-id new-request) "console") is-logged-in)
(requests/save-request new-request)
db))
[{:keys [new-request] :as db}]
(requests/save-request new-request))
(defn add-request
[db [_ chat-id {:keys [msg-id content]}]]

View File

@ -200,14 +200,14 @@
(fn [db]
(let [{:keys [new-chat] :as db'} (handler db)]
(log/debug new-chat)
(when (and new-chat (not= (:chat-id new-chat) "console"))
(when new-chat
(c/create-chat new-chat))
(dissoc db' :new-chat))))
(def init
(create-chat
(fn [{:keys [is-logged-in chats] :as db}]
(if is-logged-in
(fn [{:keys [chats] :as db}]
(if (chats "console")
db
(-> db
(assoc-in [:chats "console"] console-chat)

View File

@ -3,6 +3,7 @@
[re-frame.core :refer [after dispatch dispatch-sync debug]]
[schema.core :as s :include-macros true]
[status-im.db :refer [app-db schema]]
[status-im.persistence.realm.core :as realm]
[status-im.persistence.simple-kv-store :as kv]
[status-im.protocol.state.storage :as storage]
[status-im.utils.logging :as log]
@ -57,6 +58,7 @@
(register-handler :initialize-db
(fn [_ _]
(realm/reset-account)
(assoc app-db
:user-identity nil)))
@ -73,7 +75,6 @@
(dispatch [:initialize-protocol account])
(dispatch [:initialize-chats])
(dispatch [:load-contacts])
; TODO: initialize protocol here
(dispatch [:init-chat]))))
(register-handler :reset-app

View File

@ -8,7 +8,7 @@
(defn create-request [request]
(r/create :account :requests request true))
(defn save-request [request}]
(defn save-request [request]
(r/write :account
(fn []
(create-request request))))

View File

@ -3,24 +3,31 @@
[status-im.components.styles :refer [default-chat-color]]
[status-im.utils.types :refer [to-string]]
[status-im.utils.utils :as u]
[status-im.utils.fs :as fs]
[status-im.utils.logging :as log]
[status-im.persistence.realm.schemas :refer [base account]]
[clojure.string :as str])
(:refer-clojure :exclude [exists?]))
(def new-account-filename "new-account")
(def realm-class (u/require "realm"))
(defn create-account-realm [address]
(let [opts (merge account {:path (str address ".realm")})]
(when (cljs.core/exists? js/window)
(realm-class. (clj->js opts)))))
(def base-realm
(when (cljs.core/exists? js/window)
(realm-class. (clj->js base))))
(def account-realm (atom nil))
(defn create-account-realm [address]
(let [opts (merge account {:path (str address ".realm")})]
(when (cljs.core/exists? js/window)
(realm-class. (clj->js opts)))))
(defn is-new-account? []
(let [path (.-path @account-realm)
realm_file (str new-account-filename ".realm")]
(str/ends-with? path realm_file)))
(defn realm [schema]
(log/debug "Schema: " schema)
@ -37,6 +44,32 @@
(close :account)
(reset! account-realm nil))
(defn reset-account []
(when @account-realm
(close-account-realm))
(reset! account-realm (create-account-realm new-account-filename))
(.write @account-realm #(.deleteAll @account-realm)))
(defn move-file-handler [address err handler]
(if err
(log/error "Error moving account realm: " (.-message err))
(reset! account-realm (create-account-realm address)))
(handler err))
(defn change-account-realm [address handler]
(let [path (.-path @account-realm)
realm-file (str new-account-filename ".realm")
_ (log/debug "account realm path: " path address)
is-new-account? (str/ends-with? path realm-file)]
(close-account-realm)
(if is-new-account?
(let [new-path (str/replace path realm-file (str address ".realm"))]
(log/debug "account new path: " new-path)
(fs/move-file path new-path #(move-file-handler address % handler)))
(do
(reset! account-realm (create-account-realm address))
(handler nil)))))
(defn get-schema-by-name [opts]
(->> (:schema opts)
(mapv (fn [{:keys [name] :as schema}]
@ -82,7 +115,7 @@
query))
(defn get-by-filter [schema schema-name filter]
(log/debug "Get by filter: " schema schema-name field)
(log/debug "Get by filter: " schema schema-name)
(-> (.objects (realm schema) (name schema-name))
(.filtered filter)))

View File

@ -0,0 +1,11 @@
(ns status-im.utils.fs
(:require [clojure.string :as s]
[status-im.utils.utils :as u]))
(def fs (u/require "react-native-fs"))
(defn move-file [src dst handler]
(let [result (.moveFile fs src dst)
result (.then result #(handler nil %))
result (.catch result #(handler % nil))]
result))