From b9a51912fdd0bee67edc280a981b1dc0c83f9f70 Mon Sep 17 00:00:00 2001 From: virvar Date: Mon, 2 May 2016 13:43:13 +0300 Subject: [PATCH] Store last chat msg id in chat entity Former-commit-id: c81b0dfc8c5fab896bb5352115d81638ef89f071 --- src/syng_im/components/chat.cljs | 7 +++-- src/syng_im/components/chat/chat_message.cljs | 4 +-- src/syng_im/db.cljs | 2 -- src/syng_im/handlers.cljs | 7 +++-- src/syng_im/models/chat.cljs | 20 +++++--------- src/syng_im/models/chats.cljs | 13 +++++----- src/syng_im/models/messages.cljs | 26 +++++++++---------- src/syng_im/persistence/realm.cljs | 15 ++++++----- src/syng_im/subs.cljs | 7 +---- 9 files changed, 42 insertions(+), 59 deletions(-) diff --git a/src/syng_im/components/chat.cljs b/src/syng_im/components/chat.cljs index 8f5e9f660d..ab8baa031c 100644 --- a/src/syng_im/components/chat.cljs +++ b/src/syng_im/components/chat.cljs @@ -142,8 +142,7 @@ (defn chat [{:keys [navigator]}] (let [messages (subscribe [:get-chat-messages]) - chat (subscribe [:get-current-chat]) - last-message-atom (subscribe [:get-chat-last-message])] + chat (subscribe [:get-current-chat])] (fn [] (let [msgs @messages ;_ (log/debug "messages=" msgs) @@ -180,7 +179,7 @@ :onIconClicked (fn [] (nav-pop navigator))} [toolbar-content-chat @chat]]) - (let [last-message @last-message-atom] + (let [last-msg-id (:last-msg-id @chat)] [list-view {:dataSource datasource :renderScrollComponent (fn [props] (invertible-scroll-view (js->clj props))) @@ -189,7 +188,7 @@ (add-msg-color contact-by-identity) (assoc :group-chat (:group-chat @chat)) (assoc :typing typing))] - (r/as-element [chat-message msg last-message])))}]) + (r/as-element [chat-message msg last-msg-id])))}]) (when (:group-chat @chat) [typing-all]) (when (:is-active @chat) diff --git a/src/syng_im/components/chat/chat_message.cljs b/src/syng_im/components/chat/chat_message.cljs index d51e9dfd25..01f1e6a2ca 100644 --- a/src/syng_im/components/chat/chat_message.cljs +++ b/src/syng_im/components/chat/chat_message.cljs @@ -356,7 +356,7 @@ (defn chat-message [{:keys [msg-id from content content-type outgoing delivery-status date new-day group-chat selected same-author same-direction last-msg typing] :as msg} - last-message] + last-msg-id] [view {} (when new-day [message-date {:date date}]) @@ -371,7 +371,7 @@ :new-day new-day :same-author same-author :same-direction same-direction - :last-msg (= (:msg-id last-message) msg-id) + :last-msg (= last-msg-id msg-id) :typing typing}] [view {} (when (= content-type content-type-status) diff --git a/src/syng_im/db.cljs b/src/syng_im/db.cljs index 307e279203..025aa1585b 100644 --- a/src/syng_im/db.cljs +++ b/src/syng_im/db.cljs @@ -38,7 +38,5 @@ [:chats chat-id :command-requests]) (defn chat-command-request-path [chat-id msg-id] [:chats chat-id :command-requests msg-id]) -(defn chat-last-message-path [chat-id] - [:chats chat-id :last-message]) (def new-group-path [:new-group]) (def new-participants-path [:new-participants]) diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs index 519b62f566..48d81ffe39 100644 --- a/src/syng_im/handlers.cljs +++ b/src/syng_im/handlers.cljs @@ -134,10 +134,9 @@ (fn [db [action {chat-id :from msg-id :msg-id :as msg}]] (log/debug action "msg" msg) - (save-message chat-id msg) - (-> db - (create-chat chat-id [chat-id] false) - (signal-chat-updated chat-id)))) + (let [db (create-chat db chat-id [chat-id] false)] + (save-message chat-id msg) + (signal-chat-updated db chat-id)))) (register-handler :group-received-msg (fn [db [action {chat-id :group-id :as msg}]] diff --git a/src/syng_im/models/chat.cljs b/src/syng_im/models/chat.cljs index c859c439a5..920442174b 100644 --- a/src/syng_im/models/chat.cljs +++ b/src/syng_im/models/chat.cljs @@ -1,25 +1,17 @@ (ns syng-im.models.chat - (:require [syng-im.db :as db] - [syng-im.models.messages :refer [update-chat-last-message]])) - -(defn on-chat-update [db chat-id] - (update-chat-last-message db chat-id)) + (:require [syng-im.db :as db])) (defn set-current-chat-id [db chat-id] - (-> db - (assoc-in db/current-chat-id-path chat-id) - (on-chat-update chat-id))) + (assoc-in db db/current-chat-id-path chat-id)) (defn current-chat-id [db] (get-in db db/current-chat-id-path)) (defn signal-chat-updated [db chat-id] - (-> db - (on-chat-update chat-id) - (update-in (db/updated-chat-signal-path chat-id) (fn [current] - (if current - (inc current) - 0))))) + (update-in db (db/updated-chat-signal-path chat-id) (fn [current] + (if current + (inc current) + 0)))) (defn chat-updated? [db chat-id] (get-in db (db/updated-chat-signal-path chat-id))) diff --git a/src/syng_im/models/chats.cljs b/src/syng_im/models/chats.cljs index 5295a45cc0..e7094cbe46 100644 --- a/src/syng_im/models/chats.cljs +++ b/src/syng_im/models/chats.cljs @@ -60,12 +60,13 @@ {:identity ident :background-color background :text-color text}) identities group-chat-colors)] - (r/create :chats {:chat-id chat-id - :is-active true - :name chat-name - :group-chat group-chat? - :timestamp (timestamp) - :contacts contacts})))) + (r/create :chats {:chat-id chat-id + :is-active true + :name chat-name + :group-chat group-chat? + :timestamp (timestamp) + :contacts contacts + :last-msg-id ""})))) (add-status-message chat-id) (signal-chats-updated db))))) diff --git a/src/syng_im/models/messages.cljs b/src/syng_im/models/messages.cljs index 865487ec20..e0ebfea898 100644 --- a/src/syng_im/models/messages.cljs +++ b/src/syng_im/models/messages.cljs @@ -5,11 +5,9 @@ [syng-im.db :as db] [syng-im.utils.logging :as log])) -(defn get-messages [chat-id] - (r/sorted (r/get-by-field :msgs :chat-id chat-id) :timestamp :desc)) - -(defn select-chat-last-message [chat-id] - (r/single-cljs (get-messages chat-id))) +(defn select-chat-last-message [chat] + (when-let [last-msg-id (:last-msg-id chat)] + (r/single-cljs (r/get-by-field :msgs :msg-id last-msg-id)))) (defn save-message [chat-id {:keys [from to msg-id content content-type outgoing] :or {outgoing false to nil} :as msg}] @@ -17,7 +15,8 @@ (when-not (r/exists? :msgs :msg-id msg-id) (r/write (fn [] - (let [last-message (select-chat-last-message chat-id)] + (let [chat (r/single-cljs (r/get-by-field :chats :chat-id chat-id)) + last-message (select-chat-last-message chat)] (r/create :msgs {:chat-id chat-id :msg-id msg-id :from from @@ -32,18 +31,17 @@ true) :same-direction (if last-message (= (:outgoing last-message) outgoing) - true)} true)))))) + true)} true) + (r/create :chats {:chat-id (:chat-id chat) + :last-msg-id msg-id} + true)))))) + +(defn get-messages [chat-id] + (r/sorted (r/get-by-field :msgs :chat-id chat-id) :timestamp :desc)) (defn message-by-id [msg-id] (r/single-cljs (r/get-by-field :msgs :msg-id msg-id))) -(defn update-chat-last-message [db chat-id] - (let [last-message (select-chat-last-message chat-id)] - (assoc-in db (db/chat-last-message-path chat-id) last-message))) - -(defn get-chat-last-message [db chat-id] - (get-in db (db/chat-last-message-path chat-id))) - (defn update-message! [{:keys [msg-id] :as msg}] (log/debug "update-message!" msg) (r/write diff --git a/src/syng_im/persistence/realm.cljs b/src/syng_im/persistence/realm.cljs index 7b4f29e2b8..df2bb9cf6f 100644 --- a/src/syng_im/persistence/realm.cljs +++ b/src/syng_im/persistence/realm.cljs @@ -41,14 +41,15 @@ :background-color "string"}} {:name :chats :primaryKey :chat-id - :properties {:chat-id "string" - :name "string" - :group-chat {:type "bool" + :properties {:chat-id "string" + :name "string" + :group-chat {:type "bool" :indexed true} - :is-active "bool" - :timestamp "int" - :contacts {:type "list" - :objectType "chat-contact"}}}]}) + :is-active "bool" + :timestamp "int" + :contacts {:type "list" + :objectType "chat-contact"} + :last-msg-id "string"}}]}) (def realm (js/Realm. (clj->js opts))) diff --git a/src/syng_im/subs.cljs b/src/syng_im/subs.cljs index 8c4f277e34..fd71a3fcfb 100644 --- a/src/syng_im/subs.cljs +++ b/src/syng_im/subs.cljs @@ -7,8 +7,7 @@ [syng-im.models.chats :refer [chats-list chats-updated? chat-by-id]] - [syng-im.models.messages :refer [get-messages - get-chat-last-message]] + [syng-im.models.messages :refer [get-messages]] [syng-im.models.contacts :refer [contacts-list contacts-list-exclude contacts-list-include]] @@ -66,10 +65,6 @@ (fn [db _] (reaction (get-chat-command-request @db)))) -(register-sub :get-chat-last-message - (fn [db _] - (reaction (get-chat-last-message @db (current-chat-id @db))))) - ;; -- Chats list -------------------------------------------------------------- (register-sub :get-chats