Save whisper identities to db

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

View File

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

View File

@ -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"])
}

View File

@ -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.<ReactPackage>asList(
new MainReactPackage(),
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')
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-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"
}
}

View File

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

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]]
[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))))))