Ensure valid timestamp for all messages

This commit is contained in:
janherich 2018-06-03 23:44:42 +02:00
parent e3d86beab2
commit 0ad15a79d6
No known key found for this signature in database
GPG Key ID: C23B473AFBE94D13
2 changed files with 30 additions and 20 deletions

View File

@ -124,8 +124,8 @@
(transport/send (protocol/map->MessagesSeen {:message-ids #{message-id}}) chat-id cofx)))
(defn- add-received-message
[batch?
{:keys [from message-id chat-id content content-type timestamp clock-value to-clock-value js-obj] :as message}
[batch?
{:keys [from message-id chat-id content content-type clock-value to-clock-value js-obj] :as message}
{:keys [db now] :as cofx}]
(let [{:keys [web3 current-chat-id view-id access-scope->commands-responses]
:contacts/keys [contacts]} db
@ -158,6 +158,9 @@
(def ^:private add-single-received-message (partial add-received-message false))
(def ^:private add-batch-received-message (partial add-received-message true))
(defn ensure-timestamp [now message]
(update message :timestamp (fnil identity now)))
(defn receive
[{:keys [chat-id message-id] :as message} {:keys [now] :as cofx}]
(handlers-macro/merge-fx cofx
@ -165,11 +168,11 @@
;; We activate a chat again on new messages
:is-active true
:timestamp now})
(add-single-received-message message)))
(add-single-received-message (ensure-timestamp now message))))
(defn receive-many
[messages {:keys [now] :as cofx}]
(let [chat-ids (into #{} (map :chat-id) messages)
[raw-messages {:keys [now] :as cofx}]
(let [chat-ids (into #{} (map :chat-id) raw-messages)
chat-effects (handlers-macro/merge-effects cofx
(fn [chat-id cofx]
(chat-model/upsert-chat {:chat-id chat-id
@ -177,6 +180,7 @@
:timestamp now}
cofx))
chat-ids)
messages (map (partial ensure-timestamp now) raw-messages)
message-effects (handlers-macro/merge-effects chat-effects cofx add-batch-received-message messages)]
(handlers-macro/merge-effects message-effects
cofx

View File

@ -97,21 +97,27 @@
:content "d"
:clock-value 3
:timestamp 3}}}}}}
new-messages '({:message-id 1
:content "b"
:clock-value 1
:timestamp 1
:show? false}
{:message-id 2
:content "c"
:clock-value 2
:timestamp 2
:show? true}
{:message-id 3
:content "d"
:clock-value 3
:timestamp 3
:show? true})]
new-messages [{:message-id 1
:content "b"
:clock-value 1
:timestamp 1
:show? false}
;; TODO(janherich) : rewrite this so `receive-many` is
;; tested instead
;; dont' owerwrite existing timestamp
(message/ensure-timestamp 4
{:message-id 2
:content "c"
:clock-value 2
:timestamp 2
:show? true})
;; provide timestmap if not present
(message/ensure-timestamp 3
{:message-id 3
:content "d"
:clock-value 3
:timestamp 3
:show? true})]]
(testing "New messages are grouped/sorted correctly, hidden messages are not grouped"
(is (= '(2 3)
(map :message-id