From 703c6f2d5f98c8930613535e262651c4d78d0792 Mon Sep 17 00:00:00 2001 From: virvar Date: Wed, 16 Mar 2016 00:42:23 +0300 Subject: [PATCH 1/3] Login view refactoring --- src/messenger/android/core.cljs | 4 +- src/messenger/android/login.cljs | 79 +++++---------------------- src/messenger/android/utils.cljs | 3 + src/messenger/comm/intercom.cljs | 14 +++++ src/messenger/comm/services.cljs | 9 ++- src/messenger/models/user_data.cljs | 44 +++++++++++++-- src/messenger/services/server.cljs | 21 +++++++ src/messenger/services/user_data.cljs | 35 +++++++++++- 8 files changed, 136 insertions(+), 73 deletions(-) create mode 100644 src/messenger/services/server.cljs diff --git a/src/messenger/android/core.cljs b/src/messenger/android/core.cljs index 17f71359b2..6ea6127b07 100644 --- a/src/messenger/android/core.cljs +++ b/src/messenger/android/core.cljs @@ -13,7 +13,8 @@ [messenger.android.contacts-list :refer [contacts-list]] [messenger.android.chat :refer [chat]] [messenger.comm.pubsub :as pubsub] - [messenger.comm.intercom :refer [load-user-phone-number]])) + [messenger.comm.intercom :refer [load-user-phone-number + load-user-whisper-identity]])) (def app-registry (.-AppRegistry js/React)) @@ -59,5 +60,6 @@ (defn init [] (pubsub/setup-pub-sub) (load-user-phone-number) + (load-user-whisper-identity) (om/add-root! state/reconciler AppRoot 1) (.registerComponent app-registry "Messenger" (fn [] app-root))) diff --git a/src/messenger/android/login.cljs b/src/messenger/android/login.cljs index ec8b10cd8b..9f8c085a71 100644 --- a/src/messenger/android/login.cljs +++ b/src/messenger/android/login.cljs @@ -2,13 +2,12 @@ (:require-macros [natal-shell.components :refer [view text image touchable-highlight list-view 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.core :refer [with-error-view]]) (:require [om.next :as om :refer-macros [defui]] [re-natal.support :as sup] - [syng-im.protocol.web3 :as whisper] [messenger.state :as state] + [messenger.comm.intercom :as intercom :refer [set-user-phone-number + save-user-phone-number]] [messenger.android.utils :refer [log toast http-post]] [messenger.android.resources :as res] [messenger.android.sign-up-confirm :refer [sign-up-confirm]])) @@ -17,78 +16,30 @@ (set! js/PhoneNumber (js/require "awesome-phonenumber")) (def country-code "US") -(def ethereum-rpc-url "http://localhost:8545") (defn show-confirm-view [] (binding [state/*nav-render* false] (.replace @nav-atom (clj->js {:component sign-up-confirm :name "sign-up-confirm"})))) -(defn sign-in [phone-number whisper-identity] - ;; (toast (str "TODO: send number: " phone-number ", " - ;; (subs whisper-identity 0 2) ".." - ;; (subs whisper-identity (- (count whisper-identity) 2) - ;; (count whisper-identity)))) - (http-post "sign-up" - {:phone-number phone-number - :whisper-identity whisper-identity} - (fn [body] - (log body) - (show-confirm-view)))) - -(defn identity-handler [error result] - (if error - (do (toast (str error)) - (.log js/console "error") - (.log js/console error)) - (toast (str result)))) - -(defn get-identity [handler] - (let [web3 (whisper/make-web3 ethereum-rpc-url)] - (str (.newIdentity (whisper/whisper web3) handler)))) - - -(defn get-whisper-identity-handler [phone-number] - (fn [identity] - ;; TODO to test newIdentity. Change to 'identity' to use saved identity. - (if false ;; identity - (sign-in phone-number identity) - (get-identity (fn [error identity] - (if (not error) - (do (set-item "user-whisper-identity" identity) - (swap! state/app-state assoc :user-whisper-identity identity) - (sign-in phone-number identity)) - (toast (str "error" error)))))))) - -(defn load-user-whisper-identity [handler] - (get-item "user-whisper-identity" - (fn [error value] - (if (not error) - (let [whisper-identity (when value (str value))] - (swap! state/app-state assoc :user-whisper-identity whisper-identity) - (handler whisper-identity)) - (toast (str "error" error)))))) - -(defn handle-phone-number [phone-number] - (when phone-number - (load-user-whisper-identity (get-whisper-identity-handler phone-number)))) - -(defn save-phone-number [] - (let [phone-number (:user-phone-number @state/app-state)] - (set-item "user-phone-number" phone-number) - (handle-phone-number phone-number))) +(defn sign-up [] + (let [app-state (state/state) + phone-number (:user-phone-number app-state) + whisper-identity (:user-whisper-identity app-state)] + (save-user-phone-number phone-number) + (intercom/sign-up phone-number whisper-identity show-confirm-view))) (defn update-phone-number [value] (let [formatted (str (.getNumber (js/PhoneNumber. value country-code "international")))] - (swap! state/app-state assoc :user-phone-number - formatted))) + (set-user-phone-number formatted))) (defui Login static om/IQuery (query [this] - '[:user-phone-number]) + '[:user-phone-number :user-whisper-identity]) + Object (render [this] - (let [{:keys [user-phone-number]} (om/props this) + (let [{:keys [user-phone-number user-whisper-identity]} (om/props this) {:keys [nav]} (om/get-computed this)] (reset! nav-atom nav) (view @@ -114,13 +65,13 @@ :fontFamily "Avenir-Roman" :color "#9CBFC0"}} user-phone-number) - (touchable-highlight {:onPress #(save-phone-number) + (touchable-highlight {:onPress #(sign-up) :style {:alignSelf "center" :borderRadius 7 :backgroundColor "#E5F5F6" :width 100}} (text {:style {:marginVertical 10 :textAlign "center"}} - "Sign in"))))))) + "Sign up"))))))) (def login (om/factory Login)) diff --git a/src/messenger/android/utils.cljs b/src/messenger/android/utils.cljs index 23130a5d1e..c3d3b5103a 100644 --- a/src/messenger/android/utils.cljs +++ b/src/messenger/android/utils.cljs @@ -12,6 +12,9 @@ (defn toast [s] (toast/show s (toast/long))) +(defn on-error [error] + (toast (str "error: " error))) + (defn http-post ([action data on-success] (http-post action data on-success nil)) diff --git a/src/messenger/comm/intercom.cljs b/src/messenger/comm/intercom.cljs index 84b2543150..af7c2f5b36 100644 --- a/src/messenger/comm/intercom.cljs +++ b/src/messenger/comm/intercom.cljs @@ -11,9 +11,23 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(defn set-user-phone-number [phone-number] + (publish! :service [:user-data :user-data/set-phone-number phone-number])) + +(defn save-user-phone-number [phone-number] + (publish! :service [:user-data :user-data/save-phone-number phone-number])) + (defn load-user-phone-number [] ;; :service [service_name action_id args_map] (publish! :service [:user-data :user-data/load-phone-number nil])) +(defn load-user-whisper-identity [] + (publish! :service [:user-data :user-data/load-whisper-identity nil])) + +(defn sign-up [phone-number whisper-identity handler] + (publish! :service [:server :server/sign-up {:phone-number phone-number + :whisper-identity whisper-identity + :handler handler}])) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/src/messenger/comm/services.cljs b/src/messenger/comm/services.cljs index 7fd2648bbf..da30f30304 100644 --- a/src/messenger/comm/services.cljs +++ b/src/messenger/comm/services.cljs @@ -1,7 +1,8 @@ (ns messenger.comm.services (:require [syng-im.utils.logging :as log] - [messenger.services.user-data :refer [user-data-handler]])) + [messenger.services.user-data :refer [user-data-handler]] + [messenger.services.server :refer [server-handler]])) (defmulti service (fn [state service-id args] service-id)) @@ -10,6 +11,10 @@ [state service-id args] (user-data-handler state args)) +(defmethod service :server + [state service-id args] + (server-handler state args)) + (defn services-handler [state service-id args] (log/info "handling " service-id " args = " args) - (service state service-id args)) \ No newline at end of file + (service state service-id args)) diff --git a/src/messenger/models/user_data.cljs b/src/messenger/models/user_data.cljs index 88ba2594b2..a961947e99 100644 --- a/src/messenger/models/user_data.cljs +++ b/src/messenger/models/user_data.cljs @@ -1,16 +1,52 @@ (ns messenger.models.user-data (:require-macros - [natal-shell.async-storage :refer [get-item set-item]] - [natal-shell.alert :refer [alert]]) - (:require [messenger.state :as state])) + [natal-shell.async-storage :refer [get-item set-item]]) + (:require [cljs.core.async :as async :refer [chan put! !]] + [syng-im.protocol.web3 :as web3] + [messenger.state :as state] + [messenger.android.utils :refer [log on-error toast]])) +(def ethereum-rpc-url "http://localhost:8545") + +(defn set-phone-number [phone-number] + (swap! state/app-state assoc :user-phone-number phone-number)) + +(defn save-phone-number [phone-number] + (set-item "user-phone-number" phone-number) + (swap! state/app-state assoc :user-phone-number phone-number)) (defn load-phone-number [] (get-item "user-phone-number" (fn [error value] (if error - (alert (str "error" error)) + (on-error error) (swap! state/app-state assoc :user-phone-number (when value (str value))))))) +(defn save-whisper-identity [identity] + (set-item "user-whisper-identity" identity) + (swap! state/app-state assoc :user-whisper-identity identity)) +(defn load-whisper-identity [] + (let [ch (chan)] + (get-item "user-whisper-identity" + (fn [error value] + (log (str "load whisper identity: " value)) + (put! ch + {:error error + :value (let [whisper-identity (when value (str value))] + (swap! state/app-state assoc :user-whisper-identity + whisper-identity) + whisper-identity)}))) + ch)) + +(defn new-whisper-identity [] + (let [ch (chan)] + (let [web3 (web3/make-web3 ethereum-rpc-url)] + (.newIdentity (web3/whisper web3) + (fn [error value] + (log (str "new whisper identity: " value)) + (swap! state/app-state assoc :user-whisper-identity value) + (put! ch {:error error + :value value})))) + ch)) diff --git a/src/messenger/services/server.cljs b/src/messenger/services/server.cljs new file mode 100644 index 0000000000..05669de8fa --- /dev/null +++ b/src/messenger/services/server.cljs @@ -0,0 +1,21 @@ +(ns messenger.services.server + (:require-macros [cljs.core.async.macros :refer [go]]) + (:require [cljs.core.async :as async :refer [chan put! Date: Wed, 16 Mar 2016 14:42:00 +0300 Subject: [PATCH 2/3] Sign up confirmation view refactoring --- src/messenger/android/chat.cljs | 2 +- src/messenger/android/contacts.cljs | 45 ---------- src/messenger/android/contacts_list.cljs | 14 ++- src/messenger/android/login.cljs | 8 +- src/messenger/android/sign_up_confirm.cljs | 75 +++------------- src/messenger/comm/intercom.cljs | 23 ++++- src/messenger/comm/services.cljs | 7 +- src/messenger/models/contacts.cljs | 54 ++++++++++++ src/messenger/models/user_data.cljs | 5 +- src/messenger/services/contacts.cljs | 85 +++++++++++++++++++ src/messenger/services/server.cljs | 18 +++- src/messenger/services/user_data.cljs | 10 ++- src/messenger/{android => utils}/crypt.cljs | 2 +- .../{android => utils}/database.cljs | 4 +- .../{android => utils}/resources.cljs | 2 +- src/messenger/{android => utils}/utils.cljs | 3 +- 16 files changed, 220 insertions(+), 137 deletions(-) delete mode 100644 src/messenger/android/contacts.cljs create mode 100644 src/messenger/models/contacts.cljs create mode 100644 src/messenger/services/contacts.cljs rename src/messenger/{android => utils}/crypt.cljs (91%) rename src/messenger/{android => utils}/database.cljs (94%) rename src/messenger/{android => utils}/resources.cljs (93%) rename src/messenger/{android => utils}/utils.cljs (94%) diff --git a/src/messenger/android/chat.cljs b/src/messenger/android/chat.cljs index 2d7c1fd252..fb2b40ffae 100644 --- a/src/messenger/android/chat.cljs +++ b/src/messenger/android/chat.cljs @@ -8,7 +8,7 @@ (:require [om.next :as om :refer-macros [defui]] [re-natal.support :as sup] [messenger.state :as state] - [messenger.android.resources :as res])) + [messenger.utils.resources :as res])) (set! js/InvertibleScrollView (js/require "react-native-invertible-scroll-view")) diff --git a/src/messenger/android/contacts.cljs b/src/messenger/android/contacts.cljs deleted file mode 100644 index 3c27b1d486..0000000000 --- a/src/messenger/android/contacts.cljs +++ /dev/null @@ -1,45 +0,0 @@ -(ns messenger.android.contacts - (:require [messenger.state :as state] - [messenger.android.utils :refer [log toast http-post]] - [messenger.android.database :as db])) - -(def fake-contacts? true) - -(def react-native-contacts (js/require "react-native-contacts")) - -(defn generate-contact [n] - {:name (str "Contact " n) - :photo-path "" - :phone-numbers [{:label "mobile" :number (apply str (repeat 7 n))}] - :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-phone-contacts [on-success on-error] - (if fake-contacts? - (on-success (generate-contacts 10)) - (.getAll react-native-contacts - (fn [error raw-contacts] - (if (not error) - (let [contacts (map (fn [contact] - (merge (generate-contact 1) - {:name (:givenName contact) - :photo-path (:thumbnailPath contact) - :phone-numbers (:phoneNumbers contact)})) - (js->clj raw-contacts :keywordize-keys true))] - (on-success contacts)) - (when on-error - (on-error error))))))) - -(defn load-whisper-contacts [] - (map (fn [contact] - (merge contact - {:delivery-status (if (< (rand) 0.5) :delivered :seen) - :datetime "15:30" - :new-messages-count (rand-int 3) - :online (< (rand) 0.5)})) - (db/get-contacts))) diff --git a/src/messenger/android/contacts_list.cljs b/src/messenger/android/contacts_list.cljs index 889d74db60..2cf49fe514 100644 --- a/src/messenger/android/contacts_list.cljs +++ b/src/messenger/android/contacts_list.cljs @@ -2,15 +2,13 @@ (:require-macros [natal-shell.components :refer [view text image touchable-highlight list-view toolbar-android]] - [natal-shell.data-source :refer [data-source clone-with-rows]] - [natal-shell.core :refer [with-error-view]] - [natal-shell.alert :refer [alert]]) + [natal-shell.core :refer [with-error-view]]) (: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.utils.utils :refer [log toast http-post]] + [messenger.utils.resources :as res] + [messenger.comm.intercom :as intercom] [messenger.android.chat :refer [chat]])) (def fake-contacts? true) @@ -137,9 +135,7 @@ {:nav nav}))) (defn load-contacts [] - (let [contacts (contacts/load-whisper-contacts)] - (swap! state/app-state update :contacts-ds - #(clone-with-rows % contacts)))) + (intercom/load-syng-contacts)) (defui ContactsList static om/IQuery diff --git a/src/messenger/android/login.cljs b/src/messenger/android/login.cljs index 9f8c085a71..34a4d2a6c0 100644 --- a/src/messenger/android/login.cljs +++ b/src/messenger/android/login.cljs @@ -6,10 +6,9 @@ (:require [om.next :as om :refer-macros [defui]] [re-natal.support :as sup] [messenger.state :as state] - [messenger.comm.intercom :as intercom :refer [set-user-phone-number - save-user-phone-number]] - [messenger.android.utils :refer [log toast http-post]] - [messenger.android.resources :as res] + [messenger.comm.intercom :as intercom :refer [set-user-phone-number]] + [messenger.utils.utils :refer [log toast http-post]] + [messenger.utils.resources :as res] [messenger.android.sign-up-confirm :refer [sign-up-confirm]])) (def nav-atom (atom nil)) @@ -26,7 +25,6 @@ (let [app-state (state/state) phone-number (:user-phone-number app-state) whisper-identity (:user-whisper-identity app-state)] - (save-user-phone-number phone-number) (intercom/sign-up phone-number whisper-identity show-confirm-view))) (defn update-phone-number [value] diff --git a/src/messenger/android/sign_up_confirm.cljs b/src/messenger/android/sign_up_confirm.cljs index c7146f17ca..fc48094c6e 100644 --- a/src/messenger/android/sign_up_confirm.cljs +++ b/src/messenger/android/sign_up_confirm.cljs @@ -5,16 +5,13 @@ [natal-shell.async-storage :refer [get-item set-item]] [natal-shell.core :refer [with-error-view]] [natal-shell.alert :refer [alert]]) - (:require [clojure.string :as cstr] - [om.next :as om :refer-macros [defui]] + (:require [om.next :as om :refer-macros [defui]] [re-natal.support :as sup] [syng-im.protocol.web3 :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.database :as db] - [messenger.android.contacts :as contacts] + [messenger.utils.utils :refer [log toast]] + [messenger.utils.resources :as res] + [messenger.comm.intercom :as intercom :refer [set-confirmation-code]] [messenger.android.contacts-list :refer [contacts-list]])) (def nav-atom (atom nil)) @@ -24,76 +21,28 @@ (.replace @nav-atom (clj->js {:component contacts-list :name "contacts-list"})))) -(defn get-contact-name [phone-contact] - (cstr/join " " - (filter #(not (cstr/blank? %)) - [(:givenName phone-contact) - (:middleName phone-contact) - (:familyName phone-contact)]))) +(defn sync-contacts [] + (intercom/sync-contacts show-home-view)) -(defn handle-load-contacts-identities-response [contacts-by-hash data] - (let [contacts (map (fn [server-contact] - (let [number-info (get contacts-by-hash - (:phone-number-hash server-contact)) - phone-contact (:contact number-info)] - {:phone-number (:number number-info) - :whisper-identity (:whisper-identity server-contact) - :name (get-contact-name phone-contact) - :photo-path (:photo-path phone-contact)})) - (js->clj (:contacts data)))] - (db/add-contacts contacts) - (show-home-view))) - -(defn get-contacts-by-hash [contacts] - (let [numbers-info (reduce (fn [numbers contact] - (into numbers - (map (fn [c] - {:number (:number c) - :contact contact}) - (:phone-numbers contact)))) - '() - contacts)] - (reduce (fn [m number-info] - (let [number (:number number-info) - hash (encrypt number)] - (assoc m hash number-info))) - {} - numbers-info))) - -(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-number-hashes data} - (partial handle-load-contacts-identities-response contacts-by-hash) - (fn [error] - (toast (str error)))))) - -(defn load-contacts [] - (contacts/load-phone-contacts - send-load-contacts-identities - (fn [error] - (toast (str error))))) - -(defn handle-send-code-response [body] +(defn on-send-code-response [body] (log body) (toast (if (:confirmed body) "Confirmed" "Wrong code")) (when (:confirmed body) - (load-contacts))) + ;; TODO user action required + (sync-contacts))) (defn code-valid? [code] (= 4 (count code))) (defn send-code [code] (when (code-valid? code) - (http-post "sign-up-confirm" - {:code code} - handle-send-code-response))) + (intercom/sign-up-confirm code on-send-code-response))) (defn update-code [value] (let [formatted value] - (swap! state/app-state assoc :confirmation-code formatted))) + (set-confirmation-code formatted))) (defui SignUpConfirm static om/IQuery @@ -134,7 +83,7 @@ :style {:alignSelf "center" :borderRadius 7 :backgroundColor "#E5F5F6" - + :width 100}} (text {:style {:marginVertical 10 :textAlign "center"}} diff --git a/src/messenger/comm/intercom.cljs b/src/messenger/comm/intercom.cljs index af7c2f5b36..9199559bfa 100644 --- a/src/messenger/comm/intercom.cljs +++ b/src/messenger/comm/intercom.cljs @@ -11,6 +11,8 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; user data + (defn set-user-phone-number [phone-number] (publish! :service [:user-data :user-data/set-phone-number phone-number])) @@ -24,10 +26,29 @@ (defn load-user-whisper-identity [] (publish! :service [:user-data :user-data/load-whisper-identity nil])) +(defn set-confirmation-code [confirmation-code] + (publish! :service [:user-data :user-data/set-confirmation-code confirmation-code])) + + +;;; server + (defn sign-up [phone-number whisper-identity handler] (publish! :service [:server :server/sign-up {:phone-number phone-number :whisper-identity whisper-identity :handler handler}])) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(defn sign-up-confirm [confirmation-code handler] + (publish! :service [:server :server/sign-up-confirm + {:confirmation-code confirmation-code + :handler handler}])) + +;; contacts + +(defn load-syng-contacts [] + (publish! :service [:contacts :contacts/load-syng-contacts nil])) + +(defn sync-contacts [handler] + (publish! :service [:contacts :contacts/sync-contacts handler])) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/src/messenger/comm/services.cljs b/src/messenger/comm/services.cljs index da30f30304..59a3bda5f4 100644 --- a/src/messenger/comm/services.cljs +++ b/src/messenger/comm/services.cljs @@ -2,7 +2,8 @@ (:require [syng-im.utils.logging :as log] [messenger.services.user-data :refer [user-data-handler]] - [messenger.services.server :refer [server-handler]])) + [messenger.services.server :refer [server-handler]] + [messenger.services.contacts :refer [contacts-handler]])) (defmulti service (fn [state service-id args] service-id)) @@ -15,6 +16,10 @@ [state service-id args] (server-handler state args)) +(defmethod service :contacts + [state service-id args] + (contacts-handler state args)) + (defn services-handler [state service-id args] (log/info "handling " service-id " args = " args) (service state service-id args)) diff --git a/src/messenger/models/contacts.cljs b/src/messenger/models/contacts.cljs new file mode 100644 index 0000000000..2cc6d4a750 --- /dev/null +++ b/src/messenger/models/contacts.cljs @@ -0,0 +1,54 @@ +(ns messenger.models.contacts + (:require-macros [natal-shell.data-source :refer [data-source clone-with-rows]]) + (:require [cljs.core.async :as async :refer [chan put! !]] + [messenger.state :as state] + [messenger.utils.utils :refer [log toast http-post]] + [messenger.utils.database :as db])) + +(def fake-contacts? true) + +(def react-native-contacts (js/require "react-native-contacts")) + +(defn- generate-contact [n] + {:name (str "Contact " n) + :photo-path "" + :phone-numbers [{:label "mobile" :number (apply str (repeat 7 n))}] + :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-phone-contacts [] + (let [ch (chan)] + (if fake-contacts? + (put! ch {:error nil, :contacts (generate-contacts 10)}) + (.getAll react-native-contacts + (fn [error raw-contacts] + (put! ch + {:error error + :contacts + (when (not error) + (map (fn [contact] + (merge (generate-contact 1) + {:name (:givenName contact) + :photo-path (:thumbnailPath contact) + :phone-numbers (:phoneNumbers contact)})) + (js->clj raw-contacts :keywordize-keys true)))})))) + ch)) + +(defn load-syng-contacts [] + (let [contacts (map (fn [contact] + (merge contact + {:delivery-status (if (< (rand) 0.5) :delivered :seen) + :datetime "15:30" + :new-messages-count (rand-int 3) + :online (< (rand) 0.5)})) + (db/get-contacts))] + (swap! state/app-state update :contacts-ds + #(clone-with-rows % contacts)))) + +(defn save-syng-contacts [syng-contacts] + (db/add-contacts syng-contacts)) diff --git a/src/messenger/models/user_data.cljs b/src/messenger/models/user_data.cljs index a961947e99..a1175856b0 100644 --- a/src/messenger/models/user_data.cljs +++ b/src/messenger/models/user_data.cljs @@ -4,7 +4,7 @@ (:require [cljs.core.async :as async :refer [chan put! !]] [syng-im.protocol.web3 :as web3] [messenger.state :as state] - [messenger.android.utils :refer [log on-error toast]])) + [messenger.utils.utils :refer [log on-error toast]])) (def ethereum-rpc-url "http://localhost:8545") @@ -50,3 +50,6 @@ (put! ch {:error error :value value})))) ch)) + +(defn set-confirmation-code [code] + (swap! state/app-state assoc :confirmation-code code)) diff --git a/src/messenger/services/contacts.cljs b/src/messenger/services/contacts.cljs new file mode 100644 index 0000000000..c66b978538 --- /dev/null +++ b/src/messenger/services/contacts.cljs @@ -0,0 +1,85 @@ +(ns messenger.services.contacts + (:require-macros [cljs.core.async.macros :refer [go]]) + (:require [clojure.string :as cstr] + [cljs.core.async :as async :refer [chan put! clj (:contacts data)))) + +(defn- get-contacts-by-hash [contacts] + (let [numbers-info (reduce (fn [numbers contact] + (into numbers + (map (fn [c] + {:number (:number c) + :contact contact}) + (:phone-numbers contact)))) + '() + contacts)] + (reduce (fn [m number-info] + (let [number (:number number-info) + hash (encrypt number)] + (assoc m hash number-info))) + {} + numbers-info))) + +(defn- request-syng-contacts [contacts] + (let [contacts-by-hash (get-contacts-by-hash contacts) + data (keys contacts-by-hash) + ch (chan)] + (http-post "get-contacts" {:phone-number-hashes data} + (fn [data] + (put! ch + (to-syng-contacts contacts-by-hash data)))) + ch)) + +(defn sync-contacts [handler] + (go + (let [result ( Date: Thu, 17 Mar 2016 10:25:20 +0300 Subject: [PATCH 3/3] Microrefactoring --- src/messenger/android/core.cljs | 27 ++++++++++++++------------- src/messenger/state.cljs | 9 +++++---- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/messenger/android/core.cljs b/src/messenger/android/core.cljs index 6ea6127b07..63bfee03b2 100644 --- a/src/messenger/android/core.cljs +++ b/src/messenger/android/core.cljs @@ -9,12 +9,13 @@ (:require [om.next :as om :refer-macros [defui]] [re-natal.support :as sup] [messenger.state :as state] + [messenger.utils.utils :refer [log toast]] [messenger.android.login :refer [login]] [messenger.android.contacts-list :refer [contacts-list]] [messenger.android.chat :refer [chat]] [messenger.comm.pubsub :as pubsub] - [messenger.comm.intercom :refer [load-user-phone-number - load-user-whisper-identity]])) + [messenger.comm.intercom :as intercom :refer [load-user-phone-number + load-user-whisper-identity]])) (def app-registry (.-AppRegistry js/React)) @@ -35,20 +36,20 @@ (defui AppRoot static om/IQuery (query [this] - '[:page :contacts-ds :user-phone-number :confirmation-code]) + '[:contacts-ds :user-phone-number :confirmation-code]) Object (render [this] - (let [{:keys [page]} (om/props this)] - (navigator - {:initialRoute {:component login} - :renderScene (fn [route nav] - (when state/*nav-render* - (init-back-button-handler! nav) - (let [{:keys [component]} - (js->clj route :keywordize-keys true)] - (component (om/computed (om/props this) - {:nav nav})))))})))) + (navigator + {:initialRoute {:component login} + :renderScene (fn [route nav] + (when state/*nav-render* + (init-back-button-handler! nav) + (let [{:keys [component]} + (js->clj route :keywordize-keys true)] + (component (om/computed (om/props this) + {:nav nav})))))}))) +;; TODO to service? (swap! state/app-state assoc :contacts-ds (data-source {:rowHasChanged (fn [row1 row2] (not= row1 row2))})) diff --git a/src/messenger/state.cljs b/src/messenger/state.cljs index e47cc1c6b5..598adf8dd2 100644 --- a/src/messenger/state.cljs +++ b/src/messenger/state.cljs @@ -3,6 +3,10 @@ [om.next :as om] [re-natal.support :as sup])) +(def ^{:dynamic true :private true} *nav-render* + "Flag to suppress navigator re-renders from outside om when pushing/popping." + true) + (set! js/React (js/require "react-native")) (defonce app-state (atom {:component nil @@ -11,9 +15,6 @@ :confirmation-code nil :channels {:pub-sub-publisher (chan) :pub-sub-publication nil}})) -(def ^{:dynamic true :private true} *nav-render* - "Flag to suppress navigator re-renders from outside om when pushing/popping." - true) (defmulti read om/dispatch) (defmethod read :default @@ -37,4 +38,4 @@ (def pub-sub-path [:channels :pub-sub-publication]) (def user-notification-path [:user-notification]) -(defn pub-sub-publisher [app] (get-in app pub-sub-bus-path)) \ No newline at end of file +(defn pub-sub-publisher [app] (get-in app pub-sub-bus-path))