tidy up a bit

This commit is contained in:
michaelr 2016-03-10 19:37:38 +02:00
parent 82d74b8f03
commit cf52d8ddf4
2 changed files with 50 additions and 31 deletions

View File

@ -22,7 +22,11 @@
[syng-im.protocol.handler :refer [handle-incoming-whisper-msg]]
[syng-im.protocol.user-handler :refer [invoke-user-handler]]
[syng-im.utils.encryption :refer [new-keypair]]
[syng-im.protocol.group-chat :refer [send-group-msg]]
[syng-im.protocol.group-chat :refer [send-group-msg
init-group-chat-msg
group-add-participant-msg
group-remove-participant-msg
removed-from-group-msg]]
[syng-im.protocol.defaults :refer [default-content-type]])
(:require-macros [cljs.core.async.macros :refer [go]]))
@ -101,12 +105,7 @@
(save-identities store group-topic identities)
(listen connection handle-incoming-whisper-msg {:topics [group-topic]})
(doseq [ident identities :when (not (= ident my-identity))]
(let [{:keys [msg-id msg]} (make-msg {:from my-identity
:to ident
:payload {:type :init-group-chat
:group-topic group-topic
:identities identities
:keypair keypair}})]
(let [{:keys [msg-id msg]} (init-group-chat-msg ident group-topic identities keypair)]
(add-pending-message msg-id msg {:internal? true})
(post-msg connection msg)))
group-topic))
@ -118,12 +117,7 @@
(conj new-peer-identity))
keypair (get-keypair store group-id)]
(save-identities store group-id identities)
(let [{:keys [msg-id msg]} (make-msg {:from (state/my-identity)
:to new-peer-identity
:payload {:type :init-group-chat
:group-topic group-id
:identities identities
:keypair keypair}})]
(let [{:keys [msg-id msg]} (group-add-participant-msg new-peer-identity group-id identities keypair)]
(add-pending-message msg-id msg {:internal? true})
(post-msg connection msg))
(send-group-msg {:group-id group-id
@ -141,18 +135,10 @@
(save-identities store group-id identities)
(save-keypair store group-id keypair)
(doseq [ident identities :when (not (= ident my-identity))]
(let [{:keys [msg-id msg]} (make-msg {:from my-identity
:to ident
:payload {:type :group-removed-participant
:group-topic group-id
:keypair keypair
:removed-identity identity-to-remove}})]
(let [{:keys [msg-id msg]} (group-remove-participant-msg ident group-id keypair identity-to-remove)]
(add-pending-message msg-id msg {:internal? true})
(post-msg connection msg)))
(let [{:keys [msg-id msg]} (make-msg {:from my-identity
:to identity-to-remove
:payload {:type :removed-from-group
:group-topic group-id}})]
(let [{:keys [msg-id msg]} (removed-from-group-msg group-id identity-to-remove)]
(add-pending-message msg-id msg {:internal? true})
(post-msg connection msg))))

View File

@ -7,17 +7,50 @@
[syng-im.protocol.web3 :refer [make-msg
post-msg]]))
(defn group-msg [group-id public-key payload type]
(make-msg {:from (state/my-identity)
:topics [group-id]
:encrypt? true
:public-key public-key
:payload payload
:clear-info {:group-id group-id
:type type}}))
(defn send-group-msg [{:keys [group-id payload type internal?] :or {internal? false}}]
(let [store (storage)
{public-key :public} (get-keypair store group-id)
{:keys [msg-id msg] :as new-msg} (make-msg {:from (state/my-identity)
:topics [group-id]
:encrypt? true
:public-key public-key
:payload payload
:clear-info {:group-id group-id
:type type}})]
{:keys [msg-id msg] :as new-msg} (group-msg group-id public-key payload type)]
(add-pending-message msg-id msg {:identities (get-peer-identities store group-id)
:internal? internal?})
(post-msg (connection) msg)
new-msg))
new-msg))
(defn init-group-chat-msg [to group-topic identities keypair]
(make-msg {:from (state/my-identity)
:to to
:payload {:type :init-group-chat
:group-topic group-topic
:identities identities
:keypair keypair}}))
(defn group-add-participant-msg [to group-id identities keypair]
(make-msg {:from (state/my-identity)
:to to
:payload {:type :init-group-chat
:group-topic group-id
:identities identities
:keypair keypair}}))
(defn group-remove-participant-msg [to group-id keypair identity-to-remove]
(make-msg {:from (state/my-identity)
:to to
:payload {:type :group-removed-participant
:group-topic group-id
:keypair keypair
:removed-identity identity-to-remove}}))
(defn removed-from-group-msg [group-id identity-to-remove]
(make-msg {:from (state/my-identity)
:to identity-to-remove
:payload {:type :removed-from-group
:group-topic group-id}}))