Always load messages referenced in responses

This commit is contained in:
janherich 2018-10-09 15:21:25 +02:00
parent 1b6fa1d980
commit 99d33ebbc9
No known key found for this signature in database
GPG Key ID: C23B473AFBE94D13
9 changed files with 76 additions and 24 deletions

View File

@ -67,10 +67,19 @@
{:db db}
(:chats db)))
(defn- get-referenced-ids
"Takes map of message-id->messages and returns set of message ids which are referenced by the original messages,
excluding any message id, which is already in the original map"
[message-id->messages]
(into #{}
(comp (keep (comp :response-to :content))
(filter #(not (contains? message-id->messages %))))
(vals message-id->messages)))
(fx/defn initialize-chats
"Initialize all persisted chats on startup"
[{:keys [db default-dapps all-stored-chats get-stored-messages get-stored-user-statuses
get-stored-unviewed-messages stored-message-ids] :as cofx}]
get-stored-unviewed-messages get-referenced-messages stored-message-ids] :as cofx}]
(let [stored-unviewed-messages (get-stored-unviewed-messages (:current-public-key db))
chats (reduce (fn [acc {:keys [chat-id] :as chat}]
(let [chat-messages (index-messages (get-stored-messages chat-id))
@ -82,7 +91,11 @@
:messages chat-messages
:message-statuses (get-stored-user-statuses chat-id message-ids)
:not-loaded-message-ids (set/difference (get stored-message-ids chat-id)
(set message-ids))))))
(set message-ids))
:referenced-messages (index-messages
(get-referenced-messages
chat-id
(get-referenced-ids chat-messages)))))))
{}
all-stored-chats)]
(fx/merge cofx
@ -118,19 +131,25 @@
"Loads more messages for current chat"
[{{:keys [current-chat-id] :as db} :db
get-stored-messages :get-stored-messages
get-stored-user-statuses :get-stored-user-statuses :as cofx}]
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)
new-message-ids (keys indexed-messages)
new-statuses (get-stored-user-statuses current-chat-id new-message-ids)]
(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 (index-messages
(get-referenced-messages current-chat-id
(get-referenced-ids indexed-messages)))
new-message-ids (keys indexed-messages)
new-statuses (get-stored-user-statuses current-chat-id new-message-ids)]
(fx/merge cofx
{:db (-> db
(update-in [:chats current-chat-id :messages] merge indexed-messages)
(update-in [:chats current-chat-id :message-statuses] merge new-statuses)
(update-in [:chats current-chat-id :not-loaded-message-ids]
#(apply disj % new-message-ids))
(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))))}
(group-chat-messages current-chat-id new-messages)

View File

@ -17,6 +17,7 @@
(s/def :chat/message-groups (s/nilable map?)) ; grouped/sorted messages
(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/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/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/bot-db (s/nilable map?))

View File

@ -120,6 +120,12 @@
(fn [{:keys [message-statuses]}]
(or message-statuses {})))
(reg-sub
:get-current-chat-referenced-messages
:<- [:get-current-chat]
(fn [{:keys [referenced-messages]}]
(or referenced-messages {})))
(defn sort-message-groups
"Sorts message groups according to timestamp of first message in group"
[message-groups messages]
@ -129,15 +135,16 @@
(defn quoted-message-data
"Selects certain data from quoted message which must be available in the view"
[message-id messages]
(let [{:keys [from content]} (get messages message-id)]
[message-id messages referenced-messages]
(let [{:keys [from content]} (get messages message-id
(get referenced-messages message-id))]
{:from from
:text (:text content)}))
(defn messages-with-datemarks-and-statuses
"Converts message groups into sequence of messages interspersed with datemarks,
with correct user statuses associated into message"
[message-groups messages message-statuses]
[message-groups messages message-statuses referenced-messages]
(mapcat (fn [[datemark message-references]]
(into (list {:value datemark
:type :datemark})
@ -148,7 +155,9 @@
:timestamp-str timestamp-str
:user-statuses (get message-statuses message-id))
(:response-to content) ;; quoted message reference
(assoc-in [:content :response-to] (quoted-message-data (:response-to content) messages))))))
(assoc-in [:content :response-to] (quoted-message-data (:response-to content)
messages
referenced-messages))))))
message-references))
message-groups))
@ -222,9 +231,10 @@
:<- [:get-current-chat-messages]
:<- [:get-current-chat-message-groups]
:<- [:get-current-chat-message-statuses]
(fn [[messages message-groups message-statuses]]
:<- [:get-current-chat-referenced-messages]
(fn [[messages message-groups message-statuses referenced-messages]]
(-> (sort-message-groups message-groups messages)
(messages-with-datemarks-and-statuses messages message-statuses)
(messages-with-datemarks-and-statuses messages message-statuses referenced-messages)
messages-stream)))
(reg-sub

View File

@ -20,6 +20,16 @@
(core/all-clj :message))]
(map transform-message messages))))
(defn- get-by-chat-and-messages-ids
[chat-id message-ids]
(when (seq message-ids)
(let [messages (-> @core/account-realm
(.objects "message")
(.filtered (str "chat-id=\"" chat-id "\""
(str " and (" (core/in-query "message-id" message-ids) ")")))
(core/all-clj :message))]
(map transform-message messages))))
(def default-values
{:to nil})
@ -58,9 +68,12 @@
(re-frame/reg-cofx
:data-store/get-unviewed-messages
(fn [cofx _]
(assoc cofx
:get-stored-unviewed-messages
get-unviewed-messages)))
(assoc cofx :get-stored-unviewed-messages get-unviewed-messages)))
(re-frame/reg-cofx
:data-store/get-referenced-messages
(fn [cofx _]
(assoc cofx :get-referenced-messages get-by-chat-and-messages-ids)))
(defn- prepare-content [content]
(if (string? content)

View File

@ -337,3 +337,8 @@
(case op
:and (and-query queries)
:or (or-query queries)))))
(defn in-query
"Constructs IN query"
[field-name ids]
(string/join " or " (map #(str field-name "=\"" % "\"") ids)))

View File

@ -3,9 +3,6 @@
[re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core]))
(defn- in-query [message-ids]
(string/join " or " (map #(str "message-id=\"" % "\"") message-ids)))
(defn- prepare-statuses [statuses]
(reduce (fn [acc {:keys [message-id whisper-identity] :as user-status}]
(assoc-in acc
@ -22,7 +19,7 @@
(.objects "user-status")
(.filtered (str "chat-id=\"" chat-id "\""
(when (seq message-ids)
(str " and (" (in-query message-ids) ")"))))
(str " and (" (core/in-query "message-id" message-ids) ")"))))
(core/all-clj :user-status)
prepare-statuses))

View File

@ -83,6 +83,7 @@
(re-frame/inject-cofx :data-store/get-messages)
(re-frame/inject-cofx :data-store/get-user-statuses)
(re-frame/inject-cofx :data-store/get-unviewed-messages)
(re-frame/inject-cofx :data-store/get-referenced-messages)
(re-frame/inject-cofx :data-store/message-ids)
(re-frame/inject-cofx :data-store/get-local-storage-data)
(re-frame/inject-cofx :data-store/get-all-contacts)
@ -500,7 +501,8 @@
(handlers/register-handler-fx
:chat.ui/load-more-messages
[(re-frame/inject-cofx :data-store/get-messages)
(re-frame/inject-cofx :data-store/get-user-statuses)]
(re-frame/inject-cofx :data-store/get-user-statuses)
(re-frame/inject-cofx :data-store/get-referenced-messages)]
(fn [cofx _]
(chat.loading/load-more-messages cofx)))

View File

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

View File

@ -133,14 +133,18 @@
:margin-right 5}}
link]])
(defn- message-sent? [user-statuses current-public-key]
(not= (get-in user-statuses [current-public-key :status]) :not-sent))
(views/defview message-with-timestamp
[text {:keys [message-id timestamp outgoing content current-public-key]} style]
[text {:keys [message-id timestamp outgoing content current-public-key user-statuses]} style]
[react/view {:style style}
[react/touchable-highlight {:style {}
:on-press #(if (= "right" (.-button (.-nativeEvent %)))
(do (utils/show-popup "" "Message copied to clipboard")
(react/copy-to-clipboard text))
(re-frame/dispatch [:chat.ui/reply-to-message message-id]))}
(when (message-sent? user-statuses current-public-key)
(re-frame/dispatch [:chat.ui/reply-to-message message-id])))}
[react/view {:style styles/message-container}
(when (:response-to content)
[quoted-message (:response-to content) outgoing current-public-key])