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]}]
(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)

View File

@ -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)

View File

@ -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])

View File

@ -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}]]

View File

@ -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)))

View File

@ -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)))))

View File

@ -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

View File

@ -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)))

View File

@ -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