diff --git a/src/messenger/comm/intercom.cljs b/src/messenger/comm/intercom.cljs index 5dccdf7cd6..aa033c28a8 100644 --- a/src/messenger/comm/intercom.cljs +++ b/src/messenger/comm/intercom.cljs @@ -33,14 +33,14 @@ ;;; server (defn sign-up [phone-number whisper-identity handler] - (publish! :service [:server :server/sign-up {:phone-number phone-number + (publish! :service [:server :server/sign-up {:phone-number phone-number :whisper-identity whisper-identity - :handler handler}])) + :handler handler}])) (defn sign-up-confirm [confirmation-code handler] (publish! :service [:server :server/sign-up-confirm {:confirmation-code confirmation-code - :handler handler}])) + :handler handler}])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; contacts @@ -56,3 +56,7 @@ (defn protocol-initialized [identity] (publish! :service [:protocol :protocol/initialized {:identity identity}])) + +(defn save-new-msg [from payload] + (publish! :service [:protocol :protocol/save-new-msg {:from from + :payload payload}])) diff --git a/src/messenger/models/messages.cljs b/src/messenger/models/messages.cljs new file mode 100644 index 0000000000..913893ea79 --- /dev/null +++ b/src/messenger/models/messages.cljs @@ -0,0 +1,10 @@ +(ns messenger.models.messages + (:require [messenger.persistence.realm :as r])) + +(defn save-message [from {:keys [msg-id] :as msg}] + (when-not (r/exists? :msgs :msg-id msg-id) + (r/write + (fn [] + (r/create :msgs {:msg-id msg-id + :chat-id from + :msg (with-out-str (pr msg))} true))))) \ No newline at end of file diff --git a/src/messenger/models/protocol.cljs b/src/messenger/models/protocol.cljs index 07f82e3921..17f4432d30 100644 --- a/src/messenger/models/protocol.cljs +++ b/src/messenger/models/protocol.cljs @@ -1,14 +1,20 @@ (ns messenger.models.protocol (:require [messenger.state :as state] - [syng-im.protocol.state.storage :as s])) + [syng-im.protocol.state.storage :as s] + [syng-im.utils.encryption :refer [password-encrypt + password-decrypt]])) (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] - (s/put (state/kv-store) :identity identity)) + (let [password (get-in @state/app-state state/identity-password-path) + encrypted (->> (str identity) + (password-encrypt password))] + (s/put (state/kv-store) :identity encrypted))) (defn current-identity [] - (s/get (state/kv-store) :identity)) + (let [encrypted (s/get (state/kv-store) :identity) + password (get-in @state/app-state state/identity-password-path)] + (when encrypted + (password-decrypt password encrypted)))) diff --git a/src/messenger/persistence/realm.cljs b/src/messenger/persistence/realm.cljs index ea72c9b561..76dcac277a 100644 --- a/src/messenger/persistence/realm.cljs +++ b/src/messenger/persistence/realm.cljs @@ -13,7 +13,12 @@ {:name :kv-store :primaryKey :key :properties {:key "string" - :value "string"}}]}) + :value "string"}} + {:name :msgs + :primaryKey :msg-id + :properties {:msg-id "string" + :chat-id "string" + :msg "string"}}]}) (def realm (js/Realm. (clj->js opts))) @@ -65,5 +70,9 @@ (write (fn [] (.delete realm obj)))) +(defn exists? [schema-name field value] + (> (.-length (get-by-field schema-name field value)) + 0)) + (comment ) \ No newline at end of file diff --git a/src/messenger/persistence/simple_kv_store.cljs b/src/messenger/persistence/simple_kv_store.cljs index fcddc6d3d5..073b915599 100644 --- a/src/messenger/persistence/simple_kv_store.cljs +++ b/src/messenger/persistence/simple_kv_store.cljs @@ -8,14 +8,13 @@ (r/write (fn [] (r/create :kv-store {:key key - :value (str value)} true)))) + :value (with-out-str (pr value))} true)))) (get [_ key] (some-> (r/get-by-field :kv-store :key key) (r/single-cljs) (r/decode-value))) (contains-key? [_ key] - (= 0 - (.-length (r/get-by-field :kv-store :key key)))) + (r/exists? :kv-store :key key)) (delete [_ key] (-> (r/get-by-field :kv-store :key key) (r/single) diff --git a/src/messenger/protocol/protocol_handler.cljs b/src/messenger/protocol/protocol_handler.cljs index 33ec6d55f4..543597a8f2 100644 --- a/src/messenger/protocol/protocol_handler.cljs +++ b/src/messenger/protocol/protocol_handler.cljs @@ -1,10 +1,12 @@ (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.comm.intercom :refer [protocol-initialized + save-new-msg]] [messenger.models.protocol :refer [current-identity]] [messenger.state :refer [kv-store]])) + (defn make-handler [] {:ethereum-rpc-url ethereum-rpc-url :identity (current-identity) @@ -14,9 +16,8 @@ (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)) + :new-msg (let [{:keys [from payload]} event] + (save-new-msg from payload)) ;:msg-acked (let [{:keys [msg-id]} event] ; (add-to-chat "chat" ":" (str "Message " msg-id " was acked"))) ;:delivery-failed (let [{:keys [msg-id]} event] diff --git a/src/messenger/services/protocol.cljs b/src/messenger/services/protocol.cljs index 120d8a0cba..148b034e62 100644 --- a/src/messenger/services/protocol.cljs +++ b/src/messenger/services/protocol.cljs @@ -1,19 +1,25 @@ (ns messenger.services.protocol (:require [messenger.models.protocol :refer [set-initialized update-identity]] + [messenger.models.messages :refer [save-message]] [messenger.comm.intercom :refer [load-user-identity]] [syng-im.utils.logging :as log])) (defmulti protocol (fn [state id args] - id)) + id)) (defmethod protocol :protocol/initialized [state id {:keys [identity] :as args}] - (log/info "handling " id "args = " args) + (log/debug "handling " id "args = " args) (update-identity identity) (load-user-identity) (set-initialized true)) +(defmethod protocol :protocol/save-new-msg + [state id {:keys [from payload] :as args}] + (log/debug "handling " id "args = " args) + (save-message from payload)) + (defn protocol-handler [state [id args]] (log/debug "protocol-handler: " args) (protocol state id args)) diff --git a/src/messenger/state.cljs b/src/messenger/state.cljs index 2df8513ac6..6d412b4293 100644 --- a/src/messenger/state.cljs +++ b/src/messenger/state.cljs @@ -14,6 +14,7 @@ :user-phone-number nil :user-identity nil :confirmation-code nil + :identity-password "replace-me-with-user-entered-password" :channels {:pub-sub-publisher (chan) :pub-sub-publication nil}})) @@ -40,6 +41,7 @@ (def user-notification-path [:user-notification]) (def protocol-initialized-path [:protocol-initialized]) (def simple-store-path [:simple-store]) +(def identity-password-path [:identity-password]) (defn pub-sub-publisher [app] (get-in app pub-sub-bus-path)) (defn kv-store []