Encrypt contacts to send

This commit is contained in:
virvar 2016-03-09 17:08:07 +03:00
parent fa6e8c913a
commit 56f249e9fb
4 changed files with 85 additions and 28 deletions

View File

@ -0,0 +1,35 @@
(ns messenger.android.contacts
(:require [messenger.state :as state]
[messenger.android.utils :refer [log toast http-post]]))
(def fake-contacts? false)
(def react-native-contacts (js/require "react-native-contacts"))
(defn generate-contact [n]
{:name (str "Contact " n)
:photo ""
:phone-numbers [{:label "mobile" :number "121212"}]
:delivery-status (if (< (rand) 0.5) :delivered :seen)
:datetime "15:30"
:new-messages-count (rand-int 3)
:online (< (rand) 0.5)})
(defn generate-contacts [n]
(map generate-contact (range 1 (inc n))))
(defn load-contacts [on-success on-error]
(if fake-contacts?
(on-success (generate-contacts 100))
(.getAll react-native-contacts
(fn [error raw-contacts]
(if (not error)
(let [contacts (map (fn [contact]
(merge (generate-contact 1)
{:name (:givenName contact)
:photo (:thumbnailPath contact)
:phone-numbers (:phoneNumbers contact)}))
(js->clj raw-contacts :keywordize-keys true))]
(on-success contacts))
(when on-error
(on-error error)))))))

View File

@ -8,7 +8,9 @@
(:require [om.next :as om :refer-macros [defui]]
[re-natal.support :as sup]
[messenger.state :as state]
[messenger.android.utils :refer [log toast http-post]]
[messenger.android.resources :as res]
[messenger.android.contacts :as contacts]
[messenger.android.chat :refer [chat]]))
(def fake-contacts? true)
@ -134,32 +136,13 @@
(contact (om/computed (js->clj row :keywordize-keys true)
{:nav nav})))
(defn generate-contact [n]
{:name (str "Contact " n)
:photo ""
:delivery-status (if (< (rand) 0.5) :delivered :seen)
:datetime "15:30"
:new-messages-count (rand-int 3)
:online (< (rand) 0.5)})
(defn generate-contacts [n]
(map generate-contact (range 1 (inc n))))
(defn load-contacts []
(if fake-contacts?
(swap! state/app-state update :contacts-ds
#(clone-with-rows %
(vec (generate-contacts 100))))
(.getAll react-native-contacts
(fn [error raw-contacts]
(when (not error)
(let [contacts (map (fn [contact]
(merge (generate-contact 1)
{:name (:givenName contact)
:photo (:thumbnailPath contact)}))
(js->clj raw-contacts :keywordize-keys true))]
(swap! state/app-state update :contacts-ds
#(clone-with-rows % contacts))))))))
(contacts/load-contacts
(fn [contacts]
(swap! state/app-state update :contacts-ds
#(clone-with-rows % contacts)))
(fn [error]
(toast (str error)))))
(defui ContactsList
static om/IQuery

View File

@ -0,0 +1,19 @@
(ns messenger.android.crypt
(: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]
(apply str (map char arr)))
(defn str-to-bytes [s]
(map (comp int) s))
(defn encrypt [s]
(.reset sha-256)
(.update sha-256 s)
(-> (.digest sha-256)
byteArrayToHex))

View File

@ -11,7 +11,9 @@
[syng-im.protocol.whisper :as whisper]
[messenger.state :as state]
[messenger.android.utils :refer [log toast http-post]]
[messenger.android.crypt :refer [encrypt]]
[messenger.android.resources :as res]
[messenger.android.contacts :as contacts]
[messenger.android.contacts-list :refer [contacts-list]]))
(def nav-atom (atom nil))
@ -21,11 +23,29 @@
(.replace @nav-atom (clj->js {:component contacts-list
:name "contacts-list"}))))
(defn handle-send-check-contacts-response []
(defn handle-load-contacts-identities-response [identities]
(show-home-view))
(defn send-check-contacts []
)
(defn get-contacts-by-hash [contacts]
(reduce (fn [m contact]
(let [number (get-in contact [:phone-numbers :number])
hash (encrypt number)]
(assoc m hash number)))
contacts))
(defn send-load-contacts-identities [contacts]
(let [contacts-by-hash (get-contacts-by-hash contacts)
data (keys contacts-by-hash)]
(http-post "get-contacts" data
handle-load-contacts-identities-response
(fn [error]
(toast (str error))))))
(defn load-contacts []
(contacts/load-contacts
send-load-contacts-identities
(fn [error]
(toast (str error)))))
(defn handle-send-code-response [body]
(log body)