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 (