From 827a354c1e8f603b8c541353ed3452814997da22 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Fri, 6 May 2016 15:17:17 +0300 Subject: [PATCH] same-author/direction --- src/syng_im/handlers.cljs | 33 ++++++++++++++++------- src/syng_im/models/messages.cljs | 46 ++++++++++++++------------------ 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs index 672d0f001b..75bb1983ab 100644 --- a/src/syng_im/handlers.cljs +++ b/src/syng_im/handlers.cljs @@ -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}]] @@ -273,18 +283,21 @@ (defn prepare-message [{: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 " "))] (if command (set-chat-command db command) (assoc db :new-message (when-not (str/blank? text) - {:msg-id (random/id) - :chat-id current-chat-id - :content text - :to current-chat-id - :from identity - :content-type text-content-type - :outgoing true}))))) + {:msg-id (random/id) + :chat-id current-chat-id + :content text + :to current-chat-id + :from identity + :content-type text-content-type + :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]) diff --git a/src/syng_im/models/messages.cljs b/src/syng_im/models/messages.cljs index 117c6436a5..b8fc9cff57 100644 --- a/src/syng_im/models/messages.cljs +++ b/src/syng_im/models/messages.cljs @@ -20,35 +20,29 @@ (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 - to nil} :as msg}] +(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) - content - (map-to-str content))] - (r/create :msgs {:chat-id chat-id - :msg-id msg-id - :from from - :to to - :content content - :content-type content-type - :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)))))) + (fn [] + (let [content (if (string? content) + content + (map-to-str content))] + (r/create :msgs {:chat-id chat-id + :msg-id msg-id + :from from + :to to + :content content + :content-type content-type + :outgoing outgoing + :timestamp (timestamp) + :delivery-status nil + :same-author same-author + :same-direction same-direction} true)))))) (defn get-messages [chat-id] (->> (-> (r/get-by-field :msgs :chat-id chat-id)