Store last chat msg id in chat entity

This commit is contained in:
virvar 2016-05-02 13:43:13 +03:00
parent a50c973e92
commit c81b0dfc8c
9 changed files with 42 additions and 59 deletions

View File

@ -142,8 +142,7 @@
(defn chat [{:keys [navigator]}] (defn chat [{:keys [navigator]}]
(let [messages (subscribe [:get-chat-messages]) (let [messages (subscribe [:get-chat-messages])
chat (subscribe [:get-current-chat]) chat (subscribe [:get-current-chat])]
last-message-atom (subscribe [:get-chat-last-message])]
(fn [] (fn []
(let [msgs @messages (let [msgs @messages
;_ (log/debug "messages=" msgs) ;_ (log/debug "messages=" msgs)
@ -180,7 +179,7 @@
:onIconClicked (fn [] :onIconClicked (fn []
(nav-pop navigator))} (nav-pop navigator))}
[toolbar-content-chat @chat]]) [toolbar-content-chat @chat]])
(let [last-message @last-message-atom] (let [last-msg-id (:last-msg-id @chat)]
[list-view {:dataSource datasource [list-view {:dataSource datasource
:renderScrollComponent (fn [props] :renderScrollComponent (fn [props]
(invertible-scroll-view (js->clj props))) (invertible-scroll-view (js->clj props)))
@ -189,7 +188,7 @@
(add-msg-color contact-by-identity) (add-msg-color contact-by-identity)
(assoc :group-chat (:group-chat @chat)) (assoc :group-chat (:group-chat @chat))
(assoc :typing typing))] (assoc :typing typing))]
(r/as-element [chat-message msg last-message])))}]) (r/as-element [chat-message msg last-msg-id])))}])
(when (:group-chat @chat) (when (:group-chat @chat)
[typing-all]) [typing-all])
(when (:is-active @chat) (when (:is-active @chat)

View File

@ -356,7 +356,7 @@
(defn chat-message [{:keys [msg-id from content content-type outgoing delivery-status (defn chat-message [{:keys [msg-id from content content-type outgoing delivery-status
date new-day group-chat selected same-author same-direction date new-day group-chat selected same-author same-direction
last-msg typing] :as msg} last-msg typing] :as msg}
last-message] last-msg-id]
[view {} [view {}
(when new-day (when new-day
[message-date {:date date}]) [message-date {:date date}])
@ -371,7 +371,7 @@
:new-day new-day :new-day new-day
:same-author same-author :same-author same-author
:same-direction same-direction :same-direction same-direction
:last-msg (= (:msg-id last-message) msg-id) :last-msg (= last-msg-id msg-id)
:typing typing}] :typing typing}]
[view {} [view {}
(when (= content-type content-type-status) (when (= content-type content-type-status)

View File

@ -38,7 +38,5 @@
[:chats chat-id :command-requests]) [:chats chat-id :command-requests])
(defn chat-command-request-path [chat-id msg-id] (defn chat-command-request-path [chat-id msg-id]
[:chats chat-id :command-requests 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-group-path [:new-group])
(def new-participants-path [:new-participants]) (def new-participants-path [:new-participants])

View File

@ -134,10 +134,9 @@
(fn [db [action {chat-id :from (fn [db [action {chat-id :from
msg-id :msg-id :as msg}]] msg-id :msg-id :as msg}]]
(log/debug action "msg" msg) (log/debug action "msg" msg)
(save-message chat-id msg) (let [db (create-chat db chat-id [chat-id] false)]
(-> db (save-message chat-id msg)
(create-chat chat-id [chat-id] false) (signal-chat-updated db chat-id))))
(signal-chat-updated chat-id))))
(register-handler :group-received-msg (register-handler :group-received-msg
(fn [db [action {chat-id :group-id :as msg}]] (fn [db [action {chat-id :group-id :as msg}]]

View File

@ -1,25 +1,17 @@
(ns syng-im.models.chat (ns syng-im.models.chat
(:require [syng-im.db :as db] (: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))
(defn set-current-chat-id [db chat-id] (defn set-current-chat-id [db chat-id]
(-> db (assoc-in db db/current-chat-id-path chat-id))
(assoc-in db/current-chat-id-path chat-id)
(on-chat-update chat-id)))
(defn current-chat-id [db] (defn current-chat-id [db]
(get-in db db/current-chat-id-path)) (get-in db db/current-chat-id-path))
(defn signal-chat-updated [db chat-id] (defn signal-chat-updated [db chat-id]
(-> db (update-in db (db/updated-chat-signal-path chat-id) (fn [current]
(on-chat-update chat-id) (if current
(update-in (db/updated-chat-signal-path chat-id) (fn [current] (inc current)
(if current 0))))
(inc current)
0)))))
(defn chat-updated? [db chat-id] (defn chat-updated? [db chat-id]
(get-in db (db/updated-chat-signal-path chat-id))) (get-in db (db/updated-chat-signal-path chat-id)))

View File

@ -60,12 +60,13 @@
{:identity ident {:identity ident
:background-color background :background-color background
:text-color text}) identities group-chat-colors)] :text-color text}) identities group-chat-colors)]
(r/create :chats {:chat-id chat-id (r/create :chats {:chat-id chat-id
:is-active true :is-active true
:name chat-name :name chat-name
:group-chat group-chat? :group-chat group-chat?
:timestamp (timestamp) :timestamp (timestamp)
:contacts contacts})))) :contacts contacts
:last-msg-id ""}))))
(add-status-message chat-id) (add-status-message chat-id)
(signal-chats-updated db))))) (signal-chats-updated db)))))

View File

@ -5,11 +5,9 @@
[syng-im.db :as db] [syng-im.db :as db]
[syng-im.utils.logging :as log])) [syng-im.utils.logging :as log]))
(defn get-messages [chat-id] (defn select-chat-last-message [chat]
(r/sorted (r/get-by-field :msgs :chat-id chat-id) :timestamp :desc)) (when-let [last-msg-id (:last-msg-id chat)]
(r/single-cljs (r/get-by-field :msgs :msg-id last-msg-id))))
(defn select-chat-last-message [chat-id]
(r/single-cljs (get-messages chat-id)))
(defn save-message [chat-id {:keys [from to msg-id content content-type outgoing] :or {outgoing false (defn save-message [chat-id {:keys [from to msg-id content content-type outgoing] :or {outgoing false
to nil} :as msg}] to nil} :as msg}]
@ -17,7 +15,8 @@
(when-not (r/exists? :msgs :msg-id msg-id) (when-not (r/exists? :msgs :msg-id msg-id)
(r/write (r/write
(fn [] (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 (r/create :msgs {:chat-id chat-id
:msg-id msg-id :msg-id msg-id
:from from :from from
@ -32,18 +31,17 @@
true) true)
:same-direction (if last-message :same-direction (if last-message
(= (:outgoing last-message) outgoing) (= (: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] (defn message-by-id [msg-id]
(r/single-cljs (r/get-by-field :msgs :msg-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}] (defn update-message! [{:keys [msg-id] :as msg}]
(log/debug "update-message!" msg) (log/debug "update-message!" msg)
(r/write (r/write

View File

@ -41,14 +41,15 @@
:background-color "string"}} :background-color "string"}}
{:name :chats {:name :chats
:primaryKey :chat-id :primaryKey :chat-id
:properties {:chat-id "string" :properties {:chat-id "string"
:name "string" :name "string"
:group-chat {:type "bool" :group-chat {:type "bool"
:indexed true} :indexed true}
:is-active "bool" :is-active "bool"
:timestamp "int" :timestamp "int"
:contacts {:type "list" :contacts {:type "list"
:objectType "chat-contact"}}}]}) :objectType "chat-contact"}
:last-msg-id "string"}}]})
(def realm (js/Realm. (clj->js opts))) (def realm (js/Realm. (clj->js opts)))

View File

@ -7,8 +7,7 @@
[syng-im.models.chats :refer [chats-list [syng-im.models.chats :refer [chats-list
chats-updated? chats-updated?
chat-by-id]] chat-by-id]]
[syng-im.models.messages :refer [get-messages [syng-im.models.messages :refer [get-messages]]
get-chat-last-message]]
[syng-im.models.contacts :refer [contacts-list [syng-im.models.contacts :refer [contacts-list
contacts-list-exclude contacts-list-exclude
contacts-list-include]] contacts-list-include]]
@ -66,10 +65,6 @@
(fn [db _] (fn [db _]
(reaction (get-chat-command-request @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 -------------------------------------------------------------- ;; -- Chats list --------------------------------------------------------------
(register-sub :get-chats (register-sub :get-chats