Save adjacent message info to realm

Former-commit-id: a50c973e92
This commit is contained in:
virvar 2016-04-29 22:55:54 +03:00
parent 58644aacca
commit 539605634c
3 changed files with 27 additions and 40 deletions

View File

@ -148,31 +148,6 @@
(let [msgs @messages
;_ (log/debug "messages=" msgs)
;; 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)
;; end temp
datasource (to-realm-datasource msgs)

View File

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

View File

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