From cf52d8ddf46549484424e182c14d9db73f0ded46 Mon Sep 17 00:00:00 2001 From: michaelr Date: Thu, 10 Mar 2016 19:37:38 +0200 Subject: [PATCH] tidy up a bit --- protocol/src/cljs/syng_im/protocol/api.cljs | 32 ++++-------- .../src/cljs/syng_im/protocol/group_chat.cljs | 49 ++++++++++++++++--- 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/protocol/src/cljs/syng_im/protocol/api.cljs b/protocol/src/cljs/syng_im/protocol/api.cljs index 02d0bdf..6babaf4 100644 --- a/protocol/src/cljs/syng_im/protocol/api.cljs +++ b/protocol/src/cljs/syng_im/protocol/api.cljs @@ -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)))) diff --git a/protocol/src/cljs/syng_im/protocol/group_chat.cljs b/protocol/src/cljs/syng_im/protocol/group_chat.cljs index 65d4ea5..af4f383 100644 --- a/protocol/src/cljs/syng_im/protocol/group_chat.cljs +++ b/protocol/src/cljs/syng_im/protocol/group_chat.cljs @@ -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)) \ No newline at end of file + 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}})) \ No newline at end of file