[#6460] [#6438] Deduplication-ids

This commit is contained in:
janherich 2018-10-23 14:27:24 +02:00 committed by Roman Volosovskyi
parent 82c3997f07
commit 76165cc7cb
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
8 changed files with 46 additions and 28 deletions

View File

@ -79,7 +79,8 @@
(fx/defn initialize-chats (fx/defn initialize-chats
"Initialize all persisted chats on startup" "Initialize all persisted chats on startup"
[{:keys [db default-dapps all-stored-chats get-stored-messages get-stored-user-statuses [{:keys [db default-dapps all-stored-chats get-stored-messages get-stored-user-statuses
get-stored-unviewed-messages get-referenced-messages stored-message-ids] :as cofx}] get-stored-unviewed-messages get-referenced-messages stored-message-ids
stored-deduplication-ids] :as cofx}]
(let [stored-unviewed-messages (get-stored-unviewed-messages (:current-public-key db)) (let [stored-unviewed-messages (get-stored-unviewed-messages (:current-public-key db))
chats (reduce (fn [acc {:keys [chat-id] :as chat}] chats (reduce (fn [acc {:keys [chat-id] :as chat}]
(let [chat-messages (index-messages (get-stored-messages chat-id)) (let [chat-messages (index-messages (get-stored-messages chat-id))
@ -90,6 +91,7 @@
:unviewed-messages unviewed-ids :unviewed-messages unviewed-ids
:messages chat-messages :messages chat-messages
:message-statuses (get-stored-user-statuses chat-id message-ids) :message-statuses (get-stored-user-statuses chat-id message-ids)
:deduplication-ids (get stored-deduplication-ids chat-id)
:not-loaded-message-ids (set/difference (get stored-message-ids chat-id) :not-loaded-message-ids (set/difference (get stored-message-ids chat-id)
(set message-ids)) (set message-ids))
:referenced-messages (index-messages :referenced-messages (index-messages

View File

@ -152,14 +152,13 @@
(chat-loading/group-chat-messages chat-id (get chat->message chat-id)))) (chat-loading/group-chat-messages chat-id (get chat->message chat-id))))
(defn- add-to-chat? (defn- add-to-chat?
[{:keys [db]} {:keys [chat-id clock-value message-id message-id-old-format] :as message}] [{:keys [db]} {:keys [chat-id clock-value message-id from] :as message}]
(let [{:keys [deleted-at-clock-value messages not-loaded-message-ids]} (let [deduplication-id (messages-store/deduplication-id from chat-id clock-value)
{:keys [deleted-at-clock-value messages not-loaded-message-ids deduplication-ids]}
(get-in db [:chats chat-id])] (get-in db [:chats chat-id])]
(not (or (get messages message-id) (not (or (get messages message-id)
;; TODO(rasom): remove this condition
;; on when 0.9.29 will not be available for users
(and message-id-old-format (get messages message-id-old-format))
(get not-loaded-message-ids message-id) (get not-loaded-message-ids message-id)
(get deduplication-ids deduplication-id)
(>= deleted-at-clock-value clock-value))))) (>= deleted-at-clock-value clock-value)))))
(defn- filter-messages [cofx messages] (defn- filter-messages [cofx messages]

View File

@ -18,6 +18,7 @@
(s/def :chat/message-statuses (s/nilable map?)) ; message/user statuses indexed by two level index (s/def :chat/message-statuses (s/nilable map?)) ; message/user statuses indexed by two level index
(s/def :chat/not-loaded-message-ids (s/nilable set?)) ; set of message-ids not yet fully loaded from persisted state (s/def :chat/not-loaded-message-ids (s/nilable set?)) ; set of message-ids not yet fully loaded from persisted state
(s/def :chat/referenced-messages (s/nilable map?)) ; map of messages indexed by message-id which are not displayed directly, but referenced by other messages (s/def :chat/referenced-messages (s/nilable map?)) ; map of messages indexed by message-id which are not displayed directly, but referenced by other messages
(s/def :chat/deduplication-ids (s/nilable set?)) ; set of helper deduplication ids
(s/def :chat/last-clock-value (s/nilable number?)) ; last logical clock value of messages in chat (s/def :chat/last-clock-value (s/nilable number?)) ; last logical clock value of messages in chat
(s/def :chat/loaded-chats (s/nilable seq?)) (s/def :chat/loaded-chats (s/nilable seq?))
(s/def :chat/bot-db (s/nilable map?)) (s/def :chat/bot-db (s/nilable map?))

View File

@ -3,7 +3,8 @@
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.data-store.realm.core :as core] [status-im.data-store.realm.core :as core]
[status-im.utils.core :as utils])) [status-im.utils.core :as utils]
[status-im.js-dependencies :as dependencies]))
(defn- transform-message [message] (defn- transform-message [message]
(-> message (-> message
@ -52,6 +53,33 @@
(aget msg "message-id")))))) (aget msg "message-id"))))))
@chat-id->message-id)))) @chat-id->message-id))))
(defn- sha3 [s]
(.sha3 dependencies/Web3.prototype s))
(defn deduplication-id
"Computes deduplication id from message sender-pk, chat-id and clock-value"
[sender-pk chat-id clock-value]
(sha3 (str sender-pk chat-id clock-value)))
(re-frame/reg-cofx
:data-store/deduplication-ids
(fn [cofx _]
(assoc cofx :stored-deduplication-ids (let [chat-id->message-id (volatile! {})]
(-> @core/account-realm
(.objects "message")
(.map (fn [msg _ _]
(let [chat-id (aget msg "chat-id")
sender-pk (aget msg "from")
clock-value (aget msg "clock-value")]
(vswap! chat-id->message-id
#(update %
(aget msg "chat-id")
(fnil conj #{})
(deduplication-id sender-pk
chat-id
clock-value)))))))
@chat-id->message-id))))
(defn- get-unviewed-messages (defn- get-unviewed-messages
[public-key] [public-key]
(into {} (into {}

View File

@ -92,6 +92,7 @@
(re-frame/inject-cofx :data-store/get-unviewed-messages) (re-frame/inject-cofx :data-store/get-unviewed-messages)
(re-frame/inject-cofx :data-store/get-referenced-messages) (re-frame/inject-cofx :data-store/get-referenced-messages)
(re-frame/inject-cofx :data-store/message-ids) (re-frame/inject-cofx :data-store/message-ids)
(re-frame/inject-cofx :data-store/deduplication-ids)
(re-frame/inject-cofx :data-store/get-local-storage-data) (re-frame/inject-cofx :data-store/get-local-storage-data)
(re-frame/inject-cofx :data-store/get-all-contacts) (re-frame/inject-cofx :data-store/get-all-contacts)
(re-frame/inject-cofx :data-store/get-all-mailservers) (re-frame/inject-cofx :data-store/get-all-mailservers)

View File

@ -105,18 +105,13 @@
this) this)
(send-with-pubkey cofx params))))) (send-with-pubkey cofx params)))))
(receive [this chat-id signature _ cofx] (receive [this chat-id signature _ cofx]
(let [old-message (Message. (:text content) content-type message-type
clock-value timestamp)]
{:chat-received-message/add-fx {:chat-received-message/add-fx
[(assoc (into {} this) [(assoc (into {} this)
:message-id (transport.utils/message-id this) :message-id (transport.utils/message-id this)
;; TODO(rasom): remove this condition
;; on when 0.9.29 will not be available for users
:message-id-old-format (transport.utils/message-id-old-format old-message)
:show? true :show? true
:chat-id chat-id :chat-id chat-id
:from signature :from signature
:js-obj (:js-obj cofx))]})) :js-obj (:js-obj cofx))]})
(validate [this] (validate [this]
(if (spec/valid? :message/message this) (if (spec/valid? :message/message this)
this this

View File

@ -22,15 +22,6 @@
[message] [message]
(sha3 (pr-str message))) (sha3 (pr-str message)))
(defn message-id-old-format
"Get an old format message-id.
To be removed on 8th day after 0.9.30"
[message]
(-> message
pr-str
(clojure.string/replace "message.protocol" "message.v1.protocol")
sha3))
(defn get-topic (defn get-topic
"Get the topic of a group chat or public chat from the chat-id" "Get the topic of a group chat or public chat from the chat-id"
[chat-id] [chat-id]

View File

@ -296,6 +296,7 @@
:chat/message-groups :chat/message-groups
:chat/message-statuses :chat/message-statuses
:chat/not-loaded-message-ids :chat/not-loaded-message-ids
:chat/deduplication-ids
:chat/referenced-messages :chat/referenced-messages
:chat/last-clock-value :chat/last-clock-value
:chat/loaded-chats :chat/loaded-chats