Listen and send confirmation code

This commit is contained in:
virvar 2016-05-18 18:40:01 +03:00
parent 1f5dad71e8
commit 476d36f555
3 changed files with 29 additions and 9 deletions

View File

@ -229,8 +229,13 @@
(let [formatted (format-phone-number phone-number)]
(-> db
(assoc :user-phone-number formatted)
sign-up-service/start-listening-confirmation-code-sms
(server/sign-up formatted sign-up-service/on-sign-up-response)))))
(register-handler :stop-listening-confirmation-code-sms
(fn [db [_]]
(sign-up-service/stop-listening-confirmation-code-sms db)))
(register-handler :sign-up-confirm
(fn [db [_ confirmation-code]]
(server/sign-up-confirm confirmation-code sign-up-service/on-send-code-response)

View File

@ -51,6 +51,7 @@
:from "console"
:to "me"}])
(when (:confirmed body)
(dispatch [:stop-listening-confirmation-code-sms])
(sync-contacts)
;; TODO should be called after sync-contacts?
(dispatch [:set-signed-up true])))
@ -75,9 +76,18 @@
:from "console"
:to "me"}])))
(defn start-listen-confirmation-code-sms []
;; TODO UNDONE listen sms
)
(defn handle-sms [{body :body}]
(when-let [matches (re-matches #"(\d{4})" body)]
(dispatch [:sign-up-confirm (second matches)])))
(defn start-listening-confirmation-code-sms [db]
(when (not (:confirmation-code-sms-listener db))
(assoc db :confirmation-code-sms-listener (add-sms-listener handle-sms))))
(defn stop-listening-confirmation-code-sms [db]
(when-let [listener (:confirmation-code-sms-listener db)]
(remove-sms-listener listener)
(dissoc db :confirmation-code-sms-listener)))
;; -- Saving password ----------------------------------------
(defn save-password [password]

View File

@ -1,14 +1,19 @@
(ns syng-im.utils.sms-listener)
(ns syng-im.utils.sms-listener
(:require [syng-im.components.react :refer [android?]]))
(def sms-listener (js/require "react-native-android-sms-listener"))
(def sms-listener (.-default (js/require "react-native-android-sms-listener")))
;; Only android is supported!
(defn add-sms-listener
"Message format: {originatingAddress: string, body:
string}. Returns cancelable subscription."
"Message format: {:originatingAddress string, :body string}. Returns
cancelable subscription."
[listen-fn]
(.addListener sms-listener listen-fn))
(when android?
(.addListener sms-listener
(fn [message]
(listen-fn (js->clj message :keywordize-keys true))))))
(defn remove-sms-listener [subscription]
(.remove subscription))
(when android?
(.remove subscription)))