[fix 4212] update profiles with concurrent contact-requests

This commit is contained in:
Eric Dvorsak 2018-05-31 19:18:39 +02:00 committed by Roman Volosovskyi
parent 5a08383bde
commit b40b042b67
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
3 changed files with 68 additions and 33 deletions

View File

@ -74,7 +74,8 @@
(let [{:keys [web3 current-public-key]} db
chat-transport-info (-> (get-in db [:transport/chats chat-id])
(assoc :sym-key-id sym-key-id
:sym-key sym-key))]
:sym-key sym-key
:topic topic))]
(handlers-macro/merge-fx cofx
{:db (assoc-in db [:transport/chats chat-id] chat-transport-info)
:shh/add-filter {:web3 web3
@ -92,7 +93,8 @@
(let [{:keys [web3 current-public-key]} db
chat-transport-info (-> (get-in db [:transport/chats chat-id])
(assoc :sym-key-id sym-key-id
:sym-key sym-key))]
:sym-key sym-key
:topic topic))]
(handlers-macro/merge-fx cofx
{:db (assoc-in db
[:transport/chats chat-id]

View File

@ -8,29 +8,6 @@
[status-im.utils.handlers :as handlers]
[status-im.data-store.transport :as transport-store]))
(defrecord NewContactKey [sym-key topic message]
message/StatusMessage
(send [this chat-id cofx]
(let [success-event [:transport/set-contact-message-envelope-hash chat-id]]
(protocol/send-with-pubkey {:chat-id chat-id
:payload this
:success-event success-event}
cofx)))
(receive [this chat-id signature cofx]
(let [on-success (fn [sym-key sym-key-id]
(re-frame/dispatch [:contact/add-new-sym-key
{:sym-key-id sym-key-id
:sym-key sym-key
:chat-id chat-id
:topic topic
:message message}]))]
(handlers-macro/merge-fx cofx
{:shh/add-new-sym-keys [{:web3 (get-in cofx [:db :web3])
:sym-key sym-key
:on-success on-success}]}
(protocol/init-chat {:chat-id chat-id
:topic topic})))))
(defrecord ContactRequest [name profile-image address fcm-token]
message/StatusMessage
(send [this chat-id {:keys [db random-id] :as cofx}]
@ -92,3 +69,54 @@
:payload this
:success-event success-event}))))
recipients))))
(defn remove-chat-filter
"Stops the filter for the given chat-id"
[chat-id {:keys [db]}]
(when-let [filter (get-in db [:transport/chats chat-id :filter])]
{:shh/remove-filter filter}))
(defn init-chat
[chat-id topic cofx]
(when-not (get-in cofx [:db :transport/chats chat-id])
(protocol/init-chat {:chat-id chat-id
:topic topic} cofx)))
(defrecord NewContactKey [sym-key topic message]
message/StatusMessage
(send [this chat-id cofx]
(let [success-event [:transport/set-contact-message-envelope-hash chat-id]]
(protocol/send-with-pubkey {:chat-id chat-id
:payload this
:success-event success-event}
cofx)))
(receive [this chat-id signature {:keys [db] :as cofx}]
(let [current-sym-key (get-in db [:transport/chats chat-id :sym-key])
;; NOTE(yenda) to support concurrent contact request without additional
;; interactions we don't save the new key if these conditions are met:
;; - the message is a contact request
;; - we already have a sym-key
;; - this sym-key is first in alphabetical order compared to the new one
save-key? (not (and (= ContactRequest (type message))
current-sym-key
(= current-sym-key
(first (sort [current-sym-key sym-key])))))]
(if save-key?
(let [on-success (fn [sym-key sym-key-id]
(re-frame/dispatch [:contact/add-new-sym-key
{:sym-key-id sym-key-id
:sym-key sym-key
:chat-id chat-id
:topic topic
:message message}]))]
(handlers-macro/merge-fx cofx
{:shh/add-new-sym-keys [{:web3 (:web3 db)
:sym-key sym-key
:on-success on-success}]}
(init-chat chat-id topic)
;; in case of concurrent contact request we want
;; to stop the filter for the previous key before
;; dereferrencing it
(remove-chat-filter chat-id)))
;; if we don't save the key, we read the message directly
(message/receive message chat-id chat-id cofx)))))

View File

@ -7,14 +7,19 @@
[public-key
{:keys [name profile-image address fcm-token]}
{{:contacts/keys [contacts] :as db} :db :as cofx}]
(when-not (get contacts public-key)
(let [contact-props {:whisper-identity public-key
:public-key public-key
:address address
:photo-path profile-image
:name name
:fcm-token fcm-token
:pending? true}]
(let [contact (get contacts public-key)
contact-props {:whisper-identity public-key
:public-key public-key
:address address
:photo-path profile-image
:name name
:fcm-token fcm-token
;;NOTE (yenda) in case of concurrent contact request
:pending? (get contact :pending? true)}]
;;NOTE (yenda) only update if there is changes to the contact
(when-not (= contact-props
(select-keys contact [:whisper-identity :public-key :address
:photo-path :name :fcm-token :pending?]))
(handlers-macro/merge-fx cofx
{:db (update-in db [:contacts/contacts public-key]
merge contact-props)