same-author/direction

This commit is contained in:
Roman Volosovskyi 2016-05-06 16:11:45 +03:00
parent 827a354c1e
commit 357249a49b
1 changed files with 36 additions and 22 deletions

View File

@ -147,7 +147,7 @@
[db [_ {chat-id :from
outgoing :outgoing
:as msg}]]
(let [previous-message (peek (get-in db [:chats chat-id :messages]))
(let [previous-message (first (get-in db [:chats chat-id :messages]))
msg (merge msg
{:same-author (if previous-message
(= (:from previous-message) outgoing)
@ -157,10 +157,22 @@
true)})]
(save-message chat-id msg)))
(defn add-message-to-db
[db chat-id {:keys [from outgoing] :as message}]
(let [messages [:chats chat-id :messages]
previous-message (first (get-in db [:chats chat-id :messages]))
message (merge message
{:same-author (if previous-message
(= (:from previous-message) from)
true)
:same-direction (if previous-message
(= (:outgoing previous-message) outgoing)
true)})]
(update-in db messages conj message)))
(defn receive-message
[db [_ {chat-id :from :as msg}]]
(let [messages [:chats chat-id :messages]]
(update-in db messages conj msg)))
(add-message-to-db db chat-id msg))
(register-handler :received-msg
(-> receive-message
@ -309,7 +321,9 @@
:content content
:content-type content-type-command
:outgoing true
:handler (:handler staged-command)}))
:handler (:handler staged-command)
:same-author false
:same-direction false}))
(defn prepare-staged-commans
[{:keys [current-chat-id identity] :as db} _]
@ -321,13 +335,13 @@
(defn add-message
[{:keys [new-message current-chat-id] :as db}]
(if new-message
(update-in db [:chats current-chat-id :messages] conj new-message)
(add-message-to-db db current-chat-id new-message)
db))
(defn add-commands
[{:keys [new-commands current-chat-id] :as db}]
(reduce
#(update-in %1 [:chats current-chat-id :messages] conj %2)
#(add-message-to-db %1 current-chat-id %2)
db
new-commands))