Save adjacent message info to realm

This commit is contained in:
virvar 2016-04-29 22:55:54 +03:00
parent 7fb2d74003
commit a50c973e92
3 changed files with 27 additions and 40 deletions

View File

@ -148,31 +148,6 @@
(let [msgs @messages (let [msgs @messages
;_ (log/debug "messages=" msgs) ;_ (log/debug "messages=" msgs)
;; temp ;; temp
msgs-clj (as-> (js->clj msgs) ms
(reduce (fn [items [n m]]
(assoc items (.parseInt js/window n) m
;; (assoc m "from" (if (< 0.5 (rand))
;; "Status"
;; "abc"))
))
{} ms)
(into (sorted-map) ms)
(map (fn [[n current] [_ next]]
[n (-> current
(assoc :same-author
(if next
(= (get current "from") (get next "from"))
true))
(assoc :same-direction
(if next
(= (get current "outgoing") (get next "outgoing"))
true)))
current])
ms (conj (vec (rest ms)) nil))
(reduce (fn [items [n m]]
(assoc items n m))
{} ms))
msgs (clj->js msgs-clj)
typing (:group-chat @chat) typing (:group-chat @chat)
;; end temp ;; end temp
datasource (to-realm-datasource msgs) datasource (to-realm-datasource msgs)

View File

@ -5,30 +5,40 @@
[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]
(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 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}]
(log/debug "save-message" chat-id msg) (log/debug "save-message" chat-id msg)
(when-not (r/exists? :msgs :msg-id msg-id) (when-not (r/exists? :msgs :msg-id msg-id)
(r/write (r/write
(fn [] (fn []
(r/create :msgs {:chat-id chat-id (let [last-message (select-chat-last-message chat-id)]
:msg-id msg-id (r/create :msgs {:chat-id chat-id
:from from :msg-id msg-id
:to to :from from
:content content :to to
:content-type content-type :content content
:outgoing outgoing :content-type content-type
:timestamp (timestamp) :outgoing outgoing
:delivery-status nil} true))))) :timestamp (timestamp)
:delivery-status nil
(defn get-messages [chat-id] :same-author (if last-message
(r/sorted (r/get-by-field :msgs :chat-id chat-id) :timestamp :desc)) (= (:from last-message) from)
true)
:same-direction (if last-message
(= (:outgoing last-message) outgoing)
true)} true))))))
(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] (defn update-chat-last-message [db chat-id]
(let [last-message (r/single-cljs (get-messages chat-id))] (let [last-message (select-chat-last-message chat-id)]
(assoc-in db (db/chat-last-message-path chat-id) last-message))) (assoc-in db (db/chat-last-message-path chat-id) last-message)))
(defn get-chat-last-message [db chat-id] (defn get-chat-last-message [db chat-id]

View File

@ -32,7 +32,9 @@
:indexed true} :indexed true}
:outgoing "bool" :outgoing "bool"
:delivery-status {:type "string" :delivery-status {:type "string"
:optional true}}} :optional true}
:same-author "bool"
:same-direction "bool"}}
{:name :chat-contact {:name :chat-contact
:properties {:identity "string" :properties {:identity "string"
:text-color "string" :text-color "string"