wip protocol integration

Former-commit-id: 2b6fdaa4e6
This commit is contained in:
michaelr 2016-03-17 19:17:21 +02:00
parent b1e595e743
commit ba87aa9f1d
14 changed files with 242 additions and 61 deletions

View File

@ -15,6 +15,7 @@
["do" "clean"
["with-profile" "prod" "cljsbuild" "once" "ios"]
["with-profile" "prod" "cljsbuild" "once" "android"]]}
:figwheel {:nrepl-port 7888}
:profiles
{:dev {:dependencies [[figwheel-sidecar "0.5.0-2"]
[com.cemerick/piggieback "0.2.1"]]

View File

@ -15,7 +15,10 @@
[messenger.android.chat :refer [chat]]
[messenger.comm.pubsub :as pubsub]
[messenger.comm.intercom :as intercom :refer [load-user-phone-number
load-user-whisper-identity]]))
load-user-whisper-identity]]
[messenger.protocol.protocol-handler :refer [make-handler]]
[syng-im.protocol.api :refer [init-protocol]]))
(def app-registry (.-AppRegistry js/React))
@ -60,6 +63,7 @@
(defn init []
(pubsub/setup-pub-sub)
(init-protocol (make-handler))
(load-user-phone-number)
(load-user-whisper-identity)
(om/add-root! state/reconciler AppRoot 1)

View File

@ -9,7 +9,8 @@
[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]]))
[messenger.android.sign-up-confirm :refer [sign-up-confirm]]
[messenger.constants :refer [ethereum-rpc-url]]))
(def nav-atom (atom nil))

View File

@ -10,7 +10,6 @@
(put! publisher [topic message])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; user data
(defn set-user-phone-number [phone-number]
@ -30,6 +29,7 @@
(publish! :service [:user-data :user-data/set-confirmation-code confirmation-code]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; server
(defn sign-up [phone-number whisper-identity handler]
@ -42,7 +42,7 @@
{:confirmation-code confirmation-code
:handler handler}]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; contacts
(defn load-syng-contacts []
@ -52,3 +52,7 @@
(publish! :service [:contacts :contacts/sync-contacts handler]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; protocol
(defn protocol-initialized [identity]
(publish! :service [:protocol :protocol/initialized {:identity identity}]))

View File

@ -2,6 +2,7 @@
(:require
[syng-im.utils.logging :as log]
[messenger.services.user-data :refer [user-data-handler]]
[messenger.services.protocol :refer [protocol-handler]]
[messenger.services.server :refer [server-handler]]
[messenger.services.contacts :refer [contacts-handler]]))
@ -20,6 +21,10 @@
[state service-id args]
(contacts-handler state args))
(defmethod service :protocol
[state service-id args]
(protocol-handler state args))
(defn services-handler [state service-id args]
(log/info "handling " service-id " args = " args)
(service state service-id args))

View File

@ -0,0 +1,3 @@
(ns messenger.constants)
(def ethereum-rpc-url "http://localhost:8545")

View File

@ -0,0 +1,19 @@
(ns messenger.models.protocol
(:require [messenger.state :as state]
[messenger.persistence.realm :as r]))
(defn set-initialized [initialized?]
(swap! state/app-state assoc-in state/protocol-initialized-path initialized?))
;; TODO at least the private key has to be encrypted with user's password
(defn update-identity [identity]
(r/write
(fn []
(r/create :kv-store {:key :identity
:value (str identity)} true))))
(defn current-identity []
(-> (r/get-by-field :kv-store :key :identity)
(r/single-cljs)
(r/decode-value)))

View File

@ -0,0 +1,83 @@
(ns messenger.persistence.realm
(:require [cljs.reader :refer [read-string]]
[syng-im.utils.logging :as log]
[messenger.utils.types :refer [to-string]]))
(set! js/Realm (js/require "realm"))
(def opts {:schema [{:name "Contact"
:properties {:phone-number "string"
:whisper-identity "string"
:name "string"
:photo-path "string"}}
{:name :kv-store
:primaryKey :key
:properties {:key "string"
:value "string"}}]})
(def realm (js/Realm. (clj->js opts)))
(def schema-by-name (->> (:schema opts)
(mapv (fn [{:keys [name] :as schema}]
[name schema]))
(into {})))
(defn field-type [schema-name field]
(get-in schema-by-name [schema-name :properties field]))
(defn write [f]
(.write realm f))
(defn create
([schema-name obj]
(create schema-name obj false))
([schema-name obj update?]
(.create realm (to-string schema-name) (clj->js obj) update?)))
(defmulti to-query (fn [schema-name operator field value]
operator))
(defmethod to-query :eq [schema-name operator field value]
(let [value (to-string value)
query (str (name field) "=" (if (= "string" (field-type schema-name field))
(str "\"" value "\"")
value))
;_ (log/debug query)
]
query))
(defn get-by-field [schema-name field value]
(-> (.objects realm (name schema-name))
(.filtered (to-query schema-name :eq field value))))
(defn single [result]
(-> (aget result 0)))
(defn single-cljs [result]
(-> (aget result 0)
(js->clj :keywordize-keys true)))
(defn decode-value [{:keys [key value]}]
(read-string value))
(comment
(use 'figwheel-sidecar.repl-api)
(cljs-repl)
(def x (-> (get-by-field :kv-store :key :identity)
(single)))
(aget x 1)
(write (fn []
(.delete realm (-> (get-by-field :kv-store :key :identity)
(single)))))
(log/info (.keys js/Object realm))
(log/info (clj->js opts))
(clj->js (clj->js {:a [{:b 123}]}))
)

View File

@ -0,0 +1,44 @@
(ns messenger.protocol.protocol-handler
(:require [syng-im.utils.logging :as log]
[messenger.constants :refer [ethereum-rpc-url]]
[messenger.comm.intercom :refer [protocol-initialized]]
[messenger.models.protocol :refer [current-identity]]))
(defn make-handler []
{:ethereum-rpc-url ethereum-rpc-url
:identity (current-identity)
:storage nil
:handler (fn [{:keys [event-type] :as event}]
(log/info "Event:" (clj->js event))
(case event-type
:initialized (let [{:keys [identity]} event]
(protocol-initialized identity))
;:new-msg (let [{from :from
; {content :content} :payload} event]
; (add-to-chat "chat" from content))
;:msg-acked (let [{:keys [msg-id]} event]
; (add-to-chat "chat" ":" (str "Message " msg-id " was acked")))
;:delivery-failed (let [{:keys [msg-id]} event]
; (add-to-chat "chat" ":" (str "Delivery of message " msg-id " failed")))
;:new-group-chat (let [{:keys [from group-id identities]} event]
; (set-group-id! group-id)
; (set-group-identities identities)
; (add-to-chat "group-chat" ":" (str "Received group chat invitation from " from " for group-id: " group-id)))
;:group-chat-invite-acked (let [{:keys [from group-id]} event]
; (add-to-chat "group-chat" ":" (str "Received ACK for group chat invitation from " from " for group-id: " group-id)))
;:new-group-msg (let [{from :from
; {content :content} :payload} event]
; (add-to-chat "group-chat" from content))
;:group-new-participant (let [{:keys [group-id identity from]} event]
; (add-to-chat "group-chat" ":" (str (shorten from) " added " (shorten identity) " to group chat"))
; (add-identity-to-group-list identity))
;:group-removed-participant (let [{:keys [group-id identity from]} event]
; (add-to-chat "group-chat" ":" (str (shorten from) " removed " (shorten identity) " from group chat"))
; (remove-identity-from-group-list identity))
;:removed-from-group (let [{:keys [group-id from]} event]
; (add-to-chat "group-chat" ":" (str (shorten from) " removed you from group chat")))
;:participant-left-group (let [{:keys [group-id from]} event]
; (add-to-chat "group-chat" ":" (str (shorten from) " left group chat")))
;(add-to-chat "chat" ":" (str "Don't know how to handle " event-type))
(log/info "Don't know how to handle" event-type)
))})

View File

@ -0,0 +1,17 @@
(ns messenger.services.protocol
(:require [messenger.models.protocol :refer [set-initialized
update-identity]]
[syng-im.utils.logging :as log]))
(defmulti protocol (fn [state id args]
id))
(defmethod protocol :protocol/initialized
[state id {:keys [identity] :as args}]
(log/info "handling " id "args = " args)
(update-identity identity)
(set-initialized true))
(defn protocol-handler [state [id args]]
(log/debug "protocol-handler: " args)
(protocol state id args))

View File

@ -26,7 +26,7 @@
(defmethod user-data :user-data/load-phone-number
[state id args]
(log/info "handling " id " args = " args)
(log/debug "handling " id "args = " args)
(load-phone-number))
(defmethod user-data :user-data/load-whisper-identity
@ -48,5 +48,5 @@
(set-confirmation-code confirmation-code))
(defn user-data-handler [state [id args]]
(log/info "user notification: " args)
(log/debug "user-data-handler: " args)
(user-data state id args))

View File

@ -37,5 +37,6 @@
(def pub-sub-bus-path [:channels :pub-sub-publisher])
(def pub-sub-path [:channels :pub-sub-publication])
(def user-notification-path [:user-notification])
(def protocol-initialized-path [:protocol-initialized])
(defn pub-sub-publisher [app] (get-in app pub-sub-bus-path))

View File

@ -1,13 +1,6 @@
(ns messenger.utils.database
(:require [messenger.utils.utils :refer [log toast http-post]]))
(set! js/Realm (js/require "realm"))
(def realm (js/Realm. (clj->js {:schema [{:name "Contact"
:properties {:phone-number "string"
:whisper-identity "string"
:name "string"
:photo-path "string"}}]})))
(ns messenger.android.database
(:require [messenger.android.utils :refer [log toast http-post]]
[messenger.persistence.realm :refer [realm]]))
(defn write [f]
(.write realm f))

View File

@ -0,0 +1,6 @@
(ns messenger.utils.types)
(defn to-string [s]
(if (keyword? s)
(name s)
s))