From 1a7d089ddc31719a5a527acc30479c6cd4a54458 Mon Sep 17 00:00:00 2001 From: virvar Date: Fri, 11 Mar 2016 19:04:12 +0300 Subject: [PATCH] Save whisper identities to db --- .re-natal | 3 +- android/app/build.gradle | 3 ++ .../main/java/com/messenger/MainActivity.java | 4 +- android/settings.gradle | 5 ++- package.json | 3 +- src/messenger/android/crypt.cljs | 2 - src/messenger/android/database.cljs | 41 +++++++++++++++++++ src/messenger/android/sign_up_confirm.cljs | 20 +++++---- 8 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 src/messenger/android/database.cljs diff --git a/.re-natal b/.re-natal index b629b7de5b..74ffef3176 100644 --- a/.re-natal +++ b/.re-natal @@ -5,7 +5,8 @@ "modules": [ "react-native-contacts", "react-native-invertible-scroll-view", - "awesome-phonenumber" + "awesome-phonenumber", + "realm" ], "imageDirs": [ "images" diff --git a/android/app/build.gradle b/android/app/build.gradle index 3642aa0172..f3be637384 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -126,4 +126,7 @@ dependencies { compile project(':react-native-contacts') compile project(':react-native-i18n') compile(name:'geth', ext:'aar') + + compile project(":realm") + compile fileTree(dir: "node_modules/realm/android/libs", include: ["*.jar"]) } diff --git a/android/app/src/main/java/com/messenger/MainActivity.java b/android/app/src/main/java/com/messenger/MainActivity.java index b0fb22e036..e5716b31ca 100644 --- a/android/app/src/main/java/com/messenger/MainActivity.java +++ b/android/app/src/main/java/com/messenger/MainActivity.java @@ -14,6 +14,7 @@ import java.util.Properties; import java.io.File; import com.i18n.reactnativei18n.ReactNativeI18n; +import io.realm.react.RealmReactPackage; public class MainActivity extends ReactActivity { @@ -72,7 +73,8 @@ public class MainActivity extends ReactActivity { return Arrays.asList( new MainReactPackage(), new ReactNativeContacts(), - new ReactNativeI18n() + new ReactNativeI18n(), + new RealmReactPackage() ); } } diff --git a/android/settings.gradle b/android/settings.gradle index adae6376d7..97921ba2db 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -6,4 +6,7 @@ include ':react-native-contacts' project(':react-native-contacts').projectDir = new File(settingsDir, '../node_modules/react-native-contacts/android') include ':react-native-i18n' -project(':react-native-i18n').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-i18n/android') \ No newline at end of file +project(':react-native-i18n').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-i18n/android') +// realm dependency +include ':realm' +project(':realm').projectDir = new File(rootProject.projectDir, '../node_modules/realm/android') diff --git a/package.json b/package.json index 0e1f52c038..be99d1b666 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "react-native": "^0.21.0", "react-native-contacts": "^0.2.1", "react-native-i18n": "0.0.8", - "react-native-invertible-scroll-view": "^0.2.0" + "react-native-invertible-scroll-view": "^0.2.0", + "realm": "^0.10.0" } } diff --git a/src/messenger/android/crypt.cljs b/src/messenger/android/crypt.cljs index 95b8459853..0c015d2844 100644 --- a/src/messenger/android/crypt.cljs +++ b/src/messenger/android/crypt.cljs @@ -2,8 +2,6 @@ (:require [goog.crypt :refer [byteArrayToHex]]) (:import goog.crypt.Sha256)) -(def base-64 (js/require "base-64")) - (def sha-256 (Sha256.)) (defn bytes-to-str [arr] diff --git a/src/messenger/android/database.cljs b/src/messenger/android/database.cljs new file mode 100644 index 0000000000..6bac5a3a57 --- /dev/null +++ b/src/messenger/android/database.cljs @@ -0,0 +1,41 @@ +(ns messenger.android.database + (:require [messenger.android.utils :refer [log toast http-post]])) + +(set! js/Realm (js/require "realm")) + +(def realm (js/Realm. (clj->js {:schema [{:name "Contact" + :properties {:phone-number "string" + :whisper-identity "string"}}]}))) + +(defn write [f] + (.write realm f)) + +(defn get-contacts-objects [] + (.objects realm "Contact")) + +(defn get-contacts [] + (js->clj (get-contacts-objects) :keywordize-keys true)) + +(defn filtered [objs query] + (.filtered objs query)) + +(defn get-count [objs] + (.-length objs)) + +(defn create-contact [{:keys [phone-number whisper-identity]}] + (.create realm "Contact" + (clj->js {:phone-number phone-number + :whisper-identity whisper-identity}))) + +(defn contact-exist? [contacts contact] + (some #(= (:phone-number contact) (:phone-number %)) contacts)) + +(defn add-contacts [contacts] + (write (fn [] + (let [db-contacts (get-contacts)] + (dorun (map (fn [contact] + (if (not (contact-exist? db-contacts contact)) + (create-contact contact) + ;; TODO else override? + )) + contacts)))))) diff --git a/src/messenger/android/sign_up_confirm.cljs b/src/messenger/android/sign_up_confirm.cljs index 5ed762114b..2335dd1aba 100644 --- a/src/messenger/android/sign_up_confirm.cljs +++ b/src/messenger/android/sign_up_confirm.cljs @@ -4,8 +4,7 @@ toolbar-android text-input]] [natal-shell.async-storage :refer [get-item set-item]] [natal-shell.core :refer [with-error-view]] - [natal-shell.alert :refer [alert]] - [natal-shell.toast-android :as toast]) + [natal-shell.alert :refer [alert]]) (:require [om.next :as om :refer-macros [defui]] [re-natal.support :as sup] [syng-im.protocol.whisper :as whisper] @@ -13,6 +12,7 @@ [messenger.android.utils :refer [log toast http-post]] [messenger.android.crypt :refer [encrypt]] [messenger.android.resources :as res] + [messenger.android.database :as db] [messenger.android.contacts :as contacts] [messenger.android.contacts-list :refer [contacts-list]])) @@ -23,10 +23,14 @@ (.replace @nav-atom (clj->js {:component contacts-list :name "contacts-list"})))) -(defn handle-load-contacts-identities-response [identities] - ;; do not keywordize? - ;; save contacts to DB - (show-home-view)) +(defn handle-load-contacts-identities-response [contacts-by-hash data] + (let [contacts (map (fn [contact] + {:phone-number (get contacts-by-hash + (:phone-number-hash contact)) + :whisper-identity (:whisper-identity contact)}) + (js->clj (:contacts data)))] + (db/add-contacts contacts) + (show-home-view))) (defn get-contacts-by-hash [contacts] (let [numbers (reduce (fn [numbers contact] @@ -43,8 +47,8 @@ (defn send-load-contacts-identities [contacts] (let [contacts-by-hash (get-contacts-by-hash contacts) data (keys contacts-by-hash)] - (http-post "get-contacts" {:phone-numbers data} - handle-load-contacts-identities-response + (http-post "get-contacts" {:phone-number-hashes data} + (partial handle-load-contacts-identities-response contacts-by-hash) (fn [error] (toast (str error))))))