Dont choke on wrongly serialized messages

Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
Andrea Maria Piana 2018-12-13 09:27:34 +01:00
parent 88c06c53d4
commit 54b9ba5a2e
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
2 changed files with 30 additions and 20 deletions

View File

@ -3,7 +3,6 @@
[status-im.accounts.db :as accounts.db]
[status-im.chat.commands.core :as commands]
[status-im.chat.models :as chat-model]
[status-im.constants :as constants]
[status-im.data-store.user-statuses :as user-statuses-store]
[status-im.utils.datetime :as time]
[status-im.utils.fx :as fx]
@ -78,7 +77,8 @@
db :chats
(reduce
(fn [chats chat-id]
(let [chat-messages (index-messages (get-stored-messages chat-id))
(let [{:keys [messages]} (get-stored-messages chat-id)
chat-messages (index-messages messages)
message-ids (keys chat-messages)
statuses (get-stored-user-statuses chat-id message-ids)
unviewed-messages-ids (get-unviewed-messages-ids statuses public-key)]
@ -138,15 +138,20 @@
get-stored-user-statuses :get-stored-user-statuses
get-referenced-messages :get-referenced-messages :as cofx}]
(when-not (get-in db [:chats current-chat-id :all-loaded?])
(let [loaded-count (count (get-in db [:chats current-chat-id :messages]))
new-messages (get-stored-messages current-chat-id loaded-count)
indexed-messages (index-messages new-messages)
referenced-messages (into empty-message-map
(get-referenced-messages (get-referenced-ids indexed-messages)))
new-message-ids (keys indexed-messages)
new-statuses (get-stored-user-statuses current-chat-id new-message-ids)
public-key (accounts.db/current-public-key cofx)
loaded-unviewed-messages (get-unviewed-messages-ids new-statuses public-key)]
(let [loaded-count (count (get-in db [:chats current-chat-id :messages]))
{:keys [messages
all-loaded?]} (get-stored-messages current-chat-id loaded-count)
already-loaded-messages (get-in db [:chats current-chat-id :messages])
;; We remove those messages that are already loaded, as we might get some duplicates
new-messages (remove (comp already-loaded-messages :message-id)
messages)
indexed-messages (index-messages new-messages)
referenced-messages (into empty-message-map
(get-referenced-messages (get-referenced-ids indexed-messages)))
new-message-ids (keys indexed-messages)
new-statuses (get-stored-user-statuses current-chat-id new-message-ids)
public-key (accounts.db/current-public-key cofx)
loaded-unviewed-messages (get-unviewed-messages-ids new-statuses public-key)]
(fx/merge cofx
{:db (-> db
(update-in [:chats current-chat-id :messages] merge indexed-messages)
@ -154,7 +159,7 @@
(update-in [:chats current-chat-id :referenced-messages]
#(into (apply dissoc % new-message-ids) referenced-messages))
(assoc-in [:chats current-chat-id :all-loaded?]
(> constants/default-number-of-messages (count new-messages))))}
all-loaded?))}
(chat-model/update-chats-unviewed-messages-count
{:chat-id current-chat-id
:new-loaded-unviewed-messages-ids loaded-unviewed-messages})

View File

@ -1,5 +1,6 @@
(ns status-im.data-store.messages
(:require [cljs.tools.reader.edn :as edn]
[taoensso.timbre :as log]
[re-frame.core :as re-frame]
[status-im.constants :as constants]
[status-im.data-store.realm.core :as core]
@ -7,9 +8,12 @@
[status-im.js-dependencies :as dependencies]))
(defn- transform-message [message]
(-> message
(update :message-type keyword)
(update :content edn/read-string)))
(try
(-> message
(update :message-type keyword)
(update :content edn/read-string))
(catch :default e
(log/warn "failed to transform message with " e))))
(defn- get-by-chat-id
([chat-id]
@ -19,7 +23,8 @@
(core/sorted :timestamp :desc)
(core/page from (+ from constants/default-number-of-messages))
(core/all-clj :message))]
(map transform-message messages))))
{:all-loaded? (> constants/default-number-of-messages (count messages))
:messages (keep transform-message messages)})))
(defn get-message-id-by-old [old-message-id]
(when-let
@ -39,10 +44,10 @@
(core/single (core/get-by-field
@core/account-realm
:message :old-message-id response-to)))]
[(or response-to-v2 response-to)
(-> js-message
(core/realm-obj->clj :message)
transform-message)]))
(when-let [deserialized-message (-> js-message
(core/realm-obj->clj :message)
transform-message)]
[(or response-to-v2 response-to) deserialized-message])))
message-ids)))
(def default-values