fix double generation of message's id

This commit is contained in:
Roman Volosovskyi 2016-07-05 18:49:31 +03:00
parent bf92f5f594
commit 96b5c92281
3 changed files with 63 additions and 60 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@ pom.xml.asc
.hgignore
.hg/
.idea
*.iml

View File

@ -2,49 +2,49 @@
(:require [cljs.core.async :refer [<! timeout]]
[status-im.utils.random :as random]
[status-im.protocol.state.state :as state :refer [set-storage
set-handler
set-connection
set-identity
connection
storage]]
set-handler
set-connection
set-identity
connection
storage]]
[status-im.protocol.state.delivery :refer [add-pending-message]]
[status-im.protocol.state.group-chat :refer [save-keypair
get-keypair
get-peer-identities
get-identities
save-identities
save-group-admin
save-group-name
group-admin?
remove-group-data
group-name]]
get-keypair
get-peer-identities
get-identities
save-identities
save-group-admin
save-group-name
group-admin?
remove-group-data
group-name]]
[status-im.protocol.state.discovery :refer [save-topics
save-hashtags
get-topics
save-status
save-name]]
save-hashtags
get-topics
save-status
save-name]]
[status-im.protocol.delivery :refer [start-delivery-loop]]
[status-im.protocol.web3 :refer [listen
make-msg
post-msg
make-web3
create-identity
add-identity
stop-listener]]
make-msg
post-msg
make-web3
create-identity
add-identity
stop-listener]]
[status-im.protocol.handler :refer [handle-incoming-whisper-msg]]
[status-im.protocol.user-handler :refer [invoke-user-handler]]
[status-im.utils.encryption :refer [new-keypair]]
[status-im.protocol.group-chat :refer [send-group-msg
init-group-chat-msg
group-add-participant-msg
group-remove-participant-msg
removed-from-group-msg]]
init-group-chat-msg
group-add-participant-msg
group-remove-participant-msg
removed-from-group-msg]]
[status-im.protocol.discovery :refer [init-discovery
get-hashtag-topics
discovery-response-topic
discovery-search-topic
discovery-search-message
broadcast-status]]
get-hashtag-topics
discovery-response-topic
discovery-search-topic
discovery-search-message
broadcast-status]]
[status-im.protocol.defaults :refer [default-content-type]]
[status-im.utils.logging :as log])
(:require-macros [cljs.core.async.macros :refer [go]]))
@ -96,12 +96,14 @@
(listen connection handle-incoming-whisper-msg {:topics [discovery-response-topic]})
(invoke-user-handler :initialized {:identity account})))))
(defn send-user-msg [{:keys [to content]}]
(let [{:keys [msg-id msg] :as new-msg} (make-msg {:from (state/my-identity)
:to to
:payload {:content content
:content-type default-content-type
:type :user-msg}})]
(defn send-user-msg [{:keys [to content msg-id]}]
(let [{:keys [msg-id msg] :as new-msg}
(make-msg {:from (state/my-identity)
:to to
:msg-id msg-id
:payload {:content content
:content-type default-content-type
:type :user-msg}})]
(add-pending-message msg-id msg)
(post-msg (connection) msg)
new-msg))
@ -117,12 +119,12 @@
(start-group-chat identities nil))
([identities group-name]
(let [group-topic (random/id)
keypair (new-keypair)
store (storage)
connection (connection)
keypair (new-keypair)
store (storage)
connection (connection)
my-identity (state/my-identity)
identities (-> (set identities)
(conj my-identity))]
identities (-> (set identities)
(conj my-identity))]
(save-keypair store group-topic keypair)
(save-identities store group-topic identities)
(save-group-admin store group-topic my-identity)
@ -137,14 +139,14 @@
(defn group-add-participant
"Only call if you are the group-admin"
[group-id new-peer-identity]
(let [store (storage)
(let [store (storage)
my-identity (my-identity)]
(if-not (group-admin? store group-id my-identity)
(log/error "Called group-add-participant but not group admin, group-id:" group-id "my-identity:" my-identity)
(let [connection (connection)
identities (-> (get-identities store group-id)
(conj new-peer-identity))
keypair (get-keypair store group-id)
keypair (get-keypair store group-id)
group-name (group-name store group-id)]
(save-identities store group-id identities)
(let [{:keys [msg-id msg]} (group-add-participant-msg new-peer-identity group-id group-name identities keypair)]
@ -158,14 +160,14 @@
(defn group-remove-participant
"Only call if you are the group-admin"
[group-id identity-to-remove]
(let [store (storage)
(let [store (storage)
my-identity (my-identity)]
(if-not (group-admin? store group-id my-identity)
(log/error "Called group-remove-participant but not group admin, group-id:" group-id "my-identity:" my-identity)
(let [connection (connection)
identities (-> (get-identities store group-id)
(disj identity-to-remove))
keypair (new-keypair)]
keypair (new-keypair)]
(save-identities store group-id identities)
(save-keypair store group-id keypair)
(doseq [ident identities :when (not (= ident my-identity))]
@ -177,7 +179,7 @@
(post-msg connection msg))))))
(defn leave-group-chat [group-id]
(let [store (storage)
(let [store (storage)
my-identity (my-identity)]
(send-group-msg {:group-id group-id
:type :left-group
@ -195,13 +197,13 @@
(defn broadcast-discover-status [name status hashtags]
(log/debug "Broadcasting status: " name status hashtags)
(let [topics (get-hashtag-topics hashtags)]
(stop-broadcasting-discover)
(listen (connection) handle-incoming-whisper-msg {:topics topics})
(save-name name)
(save-topics topics)
(save-hashtags hashtags)
(save-status status)
(broadcast-status)))
(stop-broadcasting-discover)
(listen (connection) handle-incoming-whisper-msg {:topics topics})
(save-name name)
(save-topics topics)
(save-hashtags hashtags)
(save-status status)
(broadcast-status)))
(defn search-discover [hashtags]
(let [{:keys [msg-id msg]} (discovery-search-message hashtags)]

View File

@ -95,15 +95,15 @@
(defn make-msg
"Returns [msg-id msg], `msg` is formed for Web3.shh.post()"
[{:keys [from to ttl topics payload encrypt? public-key clear-info]
[{:keys [from to ttl topics payload encrypt? public-key clear-info msg-id]
:or {ttl status-msg-ttl
topics []}}]
(let [msg-id (random/id)]
{:msg-id msg-id
(let [msg-id' (or msg-id (random/id))]
{:msg-id msg-id'
:msg (cond-> {:ttl ttl
:topics (->> (conj topics status-app-topic)
(mapv from-utf8))
:payload (cond->> (merge payload {:msg-id msg-id})
:payload (cond->> (merge payload {:msg-id msg-id'})
true (str)
encrypt? (encrypt-payload public-key clear-info)
true (from-utf8))}