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

@ -144,23 +144,35 @@
:to "me"})) (range n))) :to "me"})) (range n)))
(defn store-message! (defn store-message!
[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)
true) true)
:same-direction (if previous-message :same-direction (if previous-message
(= (:outgoing previous-message) outgoing) (= (:outgoing previous-message) outgoing)
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
@ -283,7 +295,7 @@
(defn prepare-message (defn prepare-message
[{:keys [identity current-chat-id] :as db} _] [{:keys [identity current-chat-id] :as db} _]
(let [text (get-in db [:chats current-chat-id :input-text]) (let [text (get-in db [:chats current-chat-id :input-text])
{:keys [command]} (check-suggestion db (str text " "))] {:keys [command]} (check-suggestion db (str text " "))]
(if command (if command
(set-chat-command db command) (set-chat-command db command)
@ -303,13 +315,15 @@
(let [command-key (get-in staged-command [:command :command]) (let [command-key (get-in staged-command [:command :command])
content {:command (name command-key) content {:command (name command-key)
:content (:content staged-command)}] :content (:content staged-command)}]
{:msg-id (random/id) {:msg-id (random/id)
:from identity :from identity
:to chat-id :to chat-id
: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))