same-author/direction

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

View File

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