Save whisper identities to db

This commit is contained in:
virvar 2016-03-11 19:04:12 +03:00
parent 7cf1b5b0b9
commit 1a7d089ddc
8 changed files with 67 additions and 14 deletions

View File

@ -5,7 +5,8 @@
"modules": [ "modules": [
"react-native-contacts", "react-native-contacts",
"react-native-invertible-scroll-view", "react-native-invertible-scroll-view",
"awesome-phonenumber" "awesome-phonenumber",
"realm"
], ],
"imageDirs": [ "imageDirs": [
"images" "images"

View File

@ -126,4 +126,7 @@ dependencies {
compile project(':react-native-contacts') compile project(':react-native-contacts')
compile project(':react-native-i18n') compile project(':react-native-i18n')
compile(name:'geth', ext:'aar') compile(name:'geth', ext:'aar')
compile project(":realm")
compile fileTree(dir: "node_modules/realm/android/libs", include: ["*.jar"])
} }

View File

@ -14,6 +14,7 @@ import java.util.Properties;
import java.io.File; import java.io.File;
import com.i18n.reactnativei18n.ReactNativeI18n; import com.i18n.reactnativei18n.ReactNativeI18n;
import io.realm.react.RealmReactPackage;
public class MainActivity extends ReactActivity { public class MainActivity extends ReactActivity {
@ -72,7 +73,8 @@ public class MainActivity extends ReactActivity {
return Arrays.<ReactPackage>asList( return Arrays.<ReactPackage>asList(
new MainReactPackage(), new MainReactPackage(),
new ReactNativeContacts(), new ReactNativeContacts(),
new ReactNativeI18n() new ReactNativeI18n(),
new RealmReactPackage()
); );
} }
} }

View File

@ -6,4 +6,7 @@ include ':react-native-contacts'
project(':react-native-contacts').projectDir = new File(settingsDir, '../node_modules/react-native-contacts/android') project(':react-native-contacts').projectDir = new File(settingsDir, '../node_modules/react-native-contacts/android')
include ':react-native-i18n' include ':react-native-i18n'
project(':react-native-i18n').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-i18n/android') 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')

View File

@ -10,6 +10,7 @@
"react-native": "^0.21.0", "react-native": "^0.21.0",
"react-native-contacts": "^0.2.1", "react-native-contacts": "^0.2.1",
"react-native-i18n": "0.0.8", "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"
} }
} }

View File

@ -2,8 +2,6 @@
(:require [goog.crypt :refer [byteArrayToHex]]) (:require [goog.crypt :refer [byteArrayToHex]])
(:import goog.crypt.Sha256)) (:import goog.crypt.Sha256))
(def base-64 (js/require "base-64"))
(def sha-256 (Sha256.)) (def sha-256 (Sha256.))
(defn bytes-to-str [arr] (defn bytes-to-str [arr]

View File

@ -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))))))

View File

@ -4,8 +4,7 @@
toolbar-android text-input]] toolbar-android text-input]]
[natal-shell.async-storage :refer [get-item set-item]] [natal-shell.async-storage :refer [get-item set-item]]
[natal-shell.core :refer [with-error-view]] [natal-shell.core :refer [with-error-view]]
[natal-shell.alert :refer [alert]] [natal-shell.alert :refer [alert]])
[natal-shell.toast-android :as toast])
(:require [om.next :as om :refer-macros [defui]] (:require [om.next :as om :refer-macros [defui]]
[re-natal.support :as sup] [re-natal.support :as sup]
[syng-im.protocol.whisper :as whisper] [syng-im.protocol.whisper :as whisper]
@ -13,6 +12,7 @@
[messenger.android.utils :refer [log toast http-post]] [messenger.android.utils :refer [log toast http-post]]
[messenger.android.crypt :refer [encrypt]] [messenger.android.crypt :refer [encrypt]]
[messenger.android.resources :as res] [messenger.android.resources :as res]
[messenger.android.database :as db]
[messenger.android.contacts :as contacts] [messenger.android.contacts :as contacts]
[messenger.android.contacts-list :refer [contacts-list]])) [messenger.android.contacts-list :refer [contacts-list]]))
@ -23,10 +23,14 @@
(.replace @nav-atom (clj->js {:component contacts-list (.replace @nav-atom (clj->js {:component contacts-list
:name "contacts-list"})))) :name "contacts-list"}))))
(defn handle-load-contacts-identities-response [identities] (defn handle-load-contacts-identities-response [contacts-by-hash data]
;; do not keywordize? (let [contacts (map (fn [contact]
;; save contacts to DB {:phone-number (get contacts-by-hash
(show-home-view)) (: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] (defn get-contacts-by-hash [contacts]
(let [numbers (reduce (fn [numbers contact] (let [numbers (reduce (fn [numbers contact]
@ -43,8 +47,8 @@
(defn send-load-contacts-identities [contacts] (defn send-load-contacts-identities [contacts]
(let [contacts-by-hash (get-contacts-by-hash contacts) (let [contacts-by-hash (get-contacts-by-hash contacts)
data (keys contacts-by-hash)] data (keys contacts-by-hash)]
(http-post "get-contacts" {:phone-numbers data} (http-post "get-contacts" {:phone-number-hashes data}
handle-load-contacts-identities-response (partial handle-load-contacts-identities-response contacts-by-hash)
(fn [error] (fn [error]
(toast (str error)))))) (toast (str error))))))