same-author/direction

This commit is contained in:
Roman Volosovskyi 2016-05-06 15:17:17 +03:00
parent 9548dd23ee
commit 827a354c1e
2 changed files with 43 additions and 36 deletions

View File

@ -144,8 +144,18 @@
:to "me"})) (range n)))
(defn store-message!
[_ [_ {chat-id :from :as msg}]]
(save-message chat-id msg))
[db [_ {chat-id :from
outgoing :outgoing
:as msg}]]
(let [previous-message (peek (get-in db [:chats chat-id :messages]))
msg (merge msg
{:same-author (if previous-message
(= (:from previous-message) outgoing)
true)
:same-direction (if previous-message
(= (:outgoing previous-message) outgoing)
true)})]
(save-message chat-id msg)))
(defn receive-message
[db [_ {chat-id :from :as msg}]]
@ -284,7 +294,10 @@
:to current-chat-id
:from identity
:content-type text-content-type
:outgoing true})))))
:outgoing true
;; todo should be refactored
:same-author false
:same-direction false})))))
(defn prepare-command [identity chat-id staged-command]
(let [command-key (get-in staged-command [:command :command])

View File

@ -20,15 +20,16 @@
(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
(defn save-message
[chat-id {:keys [from to msg-id content content-type outgoing
same-author same-direction]
: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 []
(let [chat (r/single-cljs (r/get-by-field :chats :chat-id chat-id))
last-message (select-chat-last-message chat)
content (if (string? content)
(let [content (if (string? content)
content
(map-to-str content))]
(r/create :msgs {:chat-id chat-id
@ -40,15 +41,8 @@
: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)
(r/create :chats {:chat-id (:chat-id chat)
:last-msg-id msg-id}
true))))))
:same-author same-author
:same-direction same-direction} true))))))
(defn get-messages [chat-id]
(->> (-> (r/get-by-field :msgs :chat-id chat-id)