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))) :to "me"})) (range n)))
(defn store-message! (defn store-message!
[_ [_ {chat-id :from :as msg}]] [db [_ {chat-id :from
(save-message chat-id msg)) 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 (defn receive-message
[db [_ {chat-id :from :as msg}]] [db [_ {chat-id :from :as msg}]]
@ -273,18 +283,21 @@
(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)
(assoc db :new-message (when-not (str/blank? text) (assoc db :new-message (when-not (str/blank? text)
{:msg-id (random/id) {:msg-id (random/id)
:chat-id current-chat-id :chat-id current-chat-id
:content text :content text
:to current-chat-id :to current-chat-id
:from identity :from identity
:content-type text-content-type :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] (defn prepare-command [identity chat-id staged-command]
(let [command-key (get-in staged-command [:command :command]) (let [command-key (get-in staged-command [:command :command])

View File

@ -20,35 +20,29 @@
(when-let [last-msg-id (:last-msg-id chat)] (when-let [last-msg-id (:last-msg-id chat)]
(r/single-cljs (r/get-by-field :msgs :msg-id last-msg-id)))) (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
to nil} :as msg}] [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) (log/debug "save-message" chat-id msg)
(when-not (r/exists? :msgs :msg-id msg-id) (when-not (r/exists? :msgs :msg-id msg-id)
(r/write (r/write
(fn [] (fn []
(let [chat (r/single-cljs (r/get-by-field :chats :chat-id chat-id)) (let [content (if (string? content)
last-message (select-chat-last-message chat) content
content (if (string? content) (map-to-str content))]
content (r/create :msgs {:chat-id chat-id
(map-to-str content))] :msg-id msg-id
(r/create :msgs {:chat-id chat-id :from from
:msg-id msg-id :to to
:from from :content content
:to to :content-type content-type
:content content :outgoing outgoing
:content-type content-type :timestamp (timestamp)
:outgoing outgoing :delivery-status nil
:timestamp (timestamp) :same-author same-author
:delivery-status nil :same-direction same-direction} true))))))
: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))))))
(defn get-messages [chat-id] (defn get-messages [chat-id]
(->> (-> (r/get-by-field :msgs :chat-id chat-id) (->> (-> (r/get-by-field :msgs :chat-id chat-id)