diff --git a/src/syng_im/components/chat.cljs b/src/syng_im/components/chat.cljs index c5aaa7e351..8f5e9f660d 100644 --- a/src/syng_im/components/chat.cljs +++ b/src/syng_im/components/chat.cljs @@ -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) diff --git a/src/syng_im/models/messages.cljs b/src/syng_im/models/messages.cljs index 3b34b447a1..865487ec20 100644 --- a/src/syng_im/models/messages.cljs +++ b/src/syng_im/models/messages.cljs @@ -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] diff --git a/src/syng_im/persistence/realm.cljs b/src/syng_im/persistence/realm.cljs index a0ba5dca73..7b4f29e2b8 100644 --- a/src/syng_im/persistence/realm.cljs +++ b/src/syng_im/persistence/realm.cljs @@ -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"