can create groups and send group messages

Former-commit-id: a66105464e
This commit is contained in:
michaelr 2016-04-03 19:00:40 +03:00
parent 45434fa310
commit a504c1b861
12 changed files with 102 additions and 28 deletions

View File

@ -34,7 +34,7 @@
(add-event-listener "hardwareBackPress" new-listener))))) (add-event-listener "hardwareBackPress" new-listener)))))
(defn app-root [] (defn app-root []
[navigator {:initial-route (clj->js {:view-id :new-group}) [navigator {:initial-route (clj->js {:view-id :chat-list})
:render-scene (fn [route nav] :render-scene (fn [route nav]
(log/debug "route" route) (log/debug "route" route)
(when true ;; nav/*nav-render* (when true ;; nav/*nav-render*
@ -51,6 +51,7 @@
(defn init [] (defn init []
(dispatch-sync [:initialize-db]) (dispatch-sync [:initialize-db])
(dispatch [:initialize-crypt])
(dispatch [:initialize-protocol]) (dispatch [:initialize-protocol])
(dispatch [:load-user-phone-number]) (dispatch [:load-user-phone-number])
(dispatch [:load-syng-contacts]) (dispatch [:load-syng-contacts])

View File

@ -19,7 +19,8 @@
(defn chat [{:keys [navigator]}] (defn chat [{:keys [navigator]}]
(let [messages (subscribe [:get-chat-messages])] (let [messages (subscribe [:get-chat-messages])
chat (subscribe [:get-current-chat])]
(fn [] (fn []
(let [msgs @messages (let [msgs @messages
_ (log/debug "messages=" msgs) _ (log/debug "messages=" msgs)
@ -29,7 +30,8 @@
(when android? (when android?
;; TODO add IOS version ;; TODO add IOS version
[toolbar-android {:logo res/logo-icon [toolbar-android {:logo res/logo-icon
:title "Chat name" :title (or (@chat :name)
"Chat name")
:titleColor "#4A5258" :titleColor "#4A5258"
:subtitle "Last seen just now" :subtitle "Last seen just now"
:subtitleColor "#AAB2B2" :subtitleColor "#AAB2B2"

View File

@ -11,8 +11,8 @@
[reagent.core :as r])) [reagent.core :as r]))
(defn plain-message-input-view [] (defn plain-message-input-view []
(let [text (r/atom "!") (let [text (r/atom "!")
chat-id (subscribe [:get-current-chat-id])] chat (subscribe [:get-current-chat])]
(dispatch [:generate-suggestions @text]) (dispatch [:generate-suggestions @text])
(fn [] (fn []
[view {:style {:flexDirection "column"}} [view {:style {:flexDirection "column"}}
@ -42,7 +42,10 @@
(reset! text new-text) (reset! text new-text)
(r/flush)) (r/flush))
:onSubmitEditing (fn [e] :onSubmitEditing (fn [e]
(dispatch [:send-chat-msg @chat-id @text]) (let [{:keys [group-chat chat-id]} @chat]
(if group-chat
(dispatch [:send-group-chat-msg chat-id @text])
(dispatch [:send-chat-msg chat-id @text])))
(reset! text nil))}] (reset! text nil))}]
[image {:source res/smile [image {:source res/smile
:style {:marginTop 11 :style {:marginTop 11

View File

@ -5,7 +5,8 @@
[syng-im.components.realm :refer [list-view]] [syng-im.components.realm :refer [list-view]]
[syng-im.utils.listview :refer [to-realm-datasource]] [syng-im.utils.listview :refer [to-realm-datasource]]
[syng-im.components.chats.new-group-contact :refer [new-group-contact]] [syng-im.components.chats.new-group-contact :refer [new-group-contact]]
[reagent.core :as r])) [reagent.core :as r]
[syng-im.navigation :refer [nav-pop]]))
(defn new-group [{:keys [navigator]}] (defn new-group [{:keys [navigator]}]
(let [contacts (subscribe [:all-contacts]) (let [contacts (subscribe [:all-contacts])
@ -26,7 +27,10 @@
:icon res/v :icon res/v
:show "always"}] :show "always"}]
:onActionSelected (fn [position] :onActionSelected (fn [position]
(dispatch [:create-new-group navigator]))}]) (dispatch [:create-new-group @group-name navigator]))
:navIcon res/nav-back-icon
:onIconClicked (fn []
(nav-pop navigator))}])
[text-input {:underlineColorAndroid "#9CBFC0" [text-input {:underlineColorAndroid "#9CBFC0"
:style {:marginLeft 5 :style {:marginLeft 5
:marginRight 5 :marginRight 5

View File

@ -25,3 +25,4 @@
(defn updated-chat-signal-path [chat-id] (defn updated-chat-signal-path [chat-id]
[:chats chat-id :chat-updated-signal]) [:chats chat-id :chat-updated-signal])
(def new-group-path [:new-group]) (def new-group-path [:new-group])

View File

@ -26,7 +26,8 @@
[syng-im.utils.logging :as log] [syng-im.utils.logging :as log]
[syng-im.protocol.api :as api] [syng-im.protocol.api :as api]
[syng-im.constants :refer [text-content-type]] [syng-im.constants :refer [text-content-type]]
[syng-im.navigation :refer [nav-push]])) [syng-im.navigation :refer [nav-push]]
[syng-im.utils.crypt :refer [gen-random-bytes]]))
;; -- Middleware ------------------------------------------------------------ ;; -- Middleware ------------------------------------------------------------
;; ;;
@ -52,6 +53,33 @@
(fn [db [_ value]] (fn [db [_ value]]
(assoc db :loading value))) (assoc db :loading value)))
(register-handler :initialize-crypt
(fn [db _]
(log/debug "initializing crypt")
(gen-random-bytes 1024 (fn [{:keys [error buffer]}]
(if error
(do
(log/error "Failed to generate random bytes to initialize sjcl crypto")
(dispatch [:notify-user {:type :error
:error error}]))
(do
(->> (.toString buffer "hex")
(.toBits (.. js/ecc -sjcl -codec -hex))
(.addEntropy (.. js/ecc -sjcl -random)))
(dispatch [:crypt-initialized])))))
db))
(register-handler :crypt-initialized
(fn [db _]
(log/debug "crypt initialized")
db))
(register-handler :navigate-to
(fn [db [action navigator route]]
(log/debug action route)
(nav-push navigator route)
db))
;; -- Protocol -------------------------------------------------------------- ;; -- Protocol --------------------------------------------------------------
(register-handler :initialize-protocol (register-handler :initialize-protocol
@ -103,6 +131,21 @@
(save-message chat-id msg) (save-message chat-id msg)
(signal-chat-updated db chat-id)))) (signal-chat-updated db chat-id))))
(register-handler :send-group-chat-msg
(fn [db [action chat-id text]]
(log/debug action "chat-id" chat-id "text" text)
(let [{msg-id :msg-id
{from :from} :msg} (api/send-group-user-msg {:group-id chat-id
:content text})
msg {:msg-id msg-id
:from from
:to nil
:content text
:content-type text-content-type
:outgoing true}]
(save-message chat-id msg)
(signal-chat-updated db chat-id))))
;; -- User data -------------------------------------------------------------- ;; -- User data --------------------------------------------------------------
(register-handler :set-user-phone-number (register-handler :set-user-phone-number
@ -146,8 +189,9 @@
(register-handler :show-chat (register-handler :show-chat
(fn [db [action chat-id navigator]] (fn [db [action chat-id navigator]]
(log/debug action "chat-id" chat-id) (log/debug action "chat-id" chat-id)
(nav-push navigator {:view-id :chat}) (let [db (set-current-chat-id db chat-id)]
(set-current-chat-id db chat-id))) (dispatch [:navigate-to navigator {:view-id :chat}])
db)))
;; -- Chat -------------------------------------------------------------- ;; -- Chat --------------------------------------------------------------
@ -177,20 +221,16 @@
(update-new-group-selection db identity add?))) (update-new-group-selection db identity add?)))
(register-handler :create-new-group (register-handler :create-new-group
(fn [db [action navigator]] (fn [db [action group-name navigator]]
(log/debug action) (log/debug action)
(let [identities (-> (new-group-selection db) (let [identities (-> (new-group-selection db)
(vec)) (vec))
group-id (api/start-group-chat identities) group-id (api/start-group-chat identities)
db (create-chat db group-id identities)] db (create-chat db group-id identities group-name)]
(dispatch [:show-chat group-id navigator]) (dispatch [:show-chat group-id navigator])
db))) db)))
(comment (comment
(.getProgress (.. js/ecc -sjcl -random))
(js/window.crypto )
js/window
) )

View File

@ -33,4 +33,6 @@
(swap! re-frame.db/app-db (fn [db] (swap! re-frame.db/app-db (fn [db]
(signal-chat-updated db "0x0479a5ed1f38cadfad1db6cd56c4b659b0ebe052bbe9efa950f6660058519fa4ca6be2dda66afa80de96ab00eb97a2605d5267a1e8f4c2a166ab551f6826608cdd"))) (signal-chat-updated db "0x0479a5ed1f38cadfad1db6cd56c4b659b0ebe052bbe9efa950f6660058519fa4ca6be2dda66afa80de96ab00eb97a2605d5267a1e8f4c2a166ab551f6826608cdd")))
(current-chat-id @re-frame.db/app-db)
) )

View File

@ -54,8 +54,12 @@
(-> (r/get-all :chats) (-> (r/get-all :chats)
(r/sorted :timestamp :desc))) (r/sorted :timestamp :desc)))
(defn chat-by-id [chat-id]
(-> (r/get-by-field :chats :chat-id chat-id)
(r/single-cljs)))
(comment (comment
(chat-by-id "1459693168208-31d4942e-ca3b-5c03-a397-cd7a29f777d4")
(chats-list) (chats-list)
(r/delete (chats-list)) (r/delete (chats-list))

View File

@ -94,12 +94,12 @@
(comment (comment
(r/write #(create-contact {:phone-number "0543072333" (r/write #(create-contact {:phone-number "0543072333"
:whisper-identity "0x0479a5ed1f38cadfad1db6cd56c4b659b0ebe052bbe9efa950f6660058519fa4ca6be2dda66afa80de96ab00eb97a2605d5267a1e8f4c2a166ab551f6826608cdd" :whisper-identity "0x04b6552945c18ebca487c8a829365d3812a246e1cd00d775f47248a21a61bad0912409b8bd18dc0604d1df494cea001cce85098906df231d2a431067734ecc5a21"
:name "Mr. Bean" :name "Mr. Bean"
:photo-path ""})) :photo-path ""}))
(r/write #(create-contact {:phone-number "0544828649" (r/write #(create-contact {:phone-number "0544828649"
:whisper-identity "0x042fc4ef6525be53a6358da6db9d9019184e0fd04b5bd67a6faa252c3d0f4efdf57bdafc2c03b39daa2d0deabe49bb727c272c59ade8857f93a6ef27be77517e9e" :whisper-identity "0x043d9e25c6cf89941849cf5e4439084a93002f757cfd49fef411d4793d888b408dfa5bc54ac5989f65da8d764dc332f06b646f3cfae194a0801f6090b272a0c56e"
:name "Mr. Batman" :name "Mr. Batman"
:photo-path ""})) :photo-path ""}))

View File

@ -23,7 +23,8 @@
:primaryKey :msg-id :primaryKey :msg-id
:properties {:msg-id "string" :properties {:msg-id "string"
:from "string" :from "string"
:to "string" :to {:type "string"
:optional true}
:content "string" ;; TODO make it ArrayBuffer :content "string" ;; TODO make it ArrayBuffer
:content-type "string" :content-type "string"
:timestamp "int" :timestamp "int"
@ -121,12 +122,12 @@
(write #(.create realm "msgs" (clj->js {:msg-id "12" (write #(.create realm "msgs" (clj->js {:msg-id "12"
:content "sdfd" :content "sdfd"
:from "sdfsd" :from "sdfsd"
:chat-id "56" :chat-id "56"
:content-type "fg" :content-type "fg"
:timestamp 2 :timestamp 2
:outgoing true :outgoing true
:to "sfs" :to "sfs"
:delivery-status "seen"}) true)) :delivery-status "seen"}) true))
(.addListener realm "change" (fn [& args] (.addListener realm "change" (fn [& args]
@ -137,5 +138,4 @@
; ... ; ...
; }); ; });
) )

View File

@ -5,7 +5,8 @@
[syng-im.models.chat :refer [current-chat-id [syng-im.models.chat :refer [current-chat-id
chat-updated?]] chat-updated?]]
[syng-im.models.chats :refer [chats-list [syng-im.models.chats :refer [chats-list
chats-updated?]] chats-updated?
chat-by-id]]
[syng-im.models.messages :refer [get-messages]] [syng-im.models.messages :refer [get-messages]]
[syng-im.models.contacts :refer [contacts-list]])) [syng-im.models.contacts :refer [contacts-list]]))
@ -47,6 +48,14 @@
(let [_ @chats-updated] (let [_ @chats-updated]
(chats-list)))))) (chats-list))))))
(register-sub :get-current-chat
(fn [db _]
(let [current-chat-id (-> (current-chat-id @db)
(reaction))]
(-> (when-let [chat-id @current-chat-id]
(chat-by-id chat-id))
(reaction)))))
;; -- User data -------------------------------------------------------------- ;; -- User data --------------------------------------------------------------
(register-sub (register-sub

View File

@ -2,6 +2,8 @@
(:require [goog.crypt :refer [byteArrayToHex]]) (:require [goog.crypt :refer [byteArrayToHex]])
(:import goog.crypt.Sha256)) (:import goog.crypt.Sha256))
(set! js/window.RnRandomBytes (js/require "react-native-randombytes"))
(def sha-256 (Sha256.)) (def sha-256 (Sha256.))
(defn bytes-to-str [arr] (defn bytes-to-str [arr]
@ -15,3 +17,9 @@
(.update sha-256 s) (.update sha-256 s)
(-> (.digest sha-256) (-> (.digest sha-256)
byteArrayToHex)) byteArrayToHex))
(defn gen-random-bytes [length cb]
(.randomBytes js/window.RnRandomBytes length (fn [& [err buf]]
(if err
(cb {:error err})
(cb {:buffer buf})))))