From 350fc7aa0358ac5a6d763c063ac325a2d1336c23 Mon Sep 17 00:00:00 2001 From: Dmitry Novotochinov Date: Mon, 8 Jan 2018 12:52:48 +0300 Subject: [PATCH] [fix #2870] Mark 1-1 chat as inactive when user removes it --- src/status_im/chat/handlers.cljs | 16 +++++++++------- src/status_im/chat/models.cljs | 5 +++-- src/status_im/chat/models/message.cljs | 8 +++++--- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 5445919c51..da619eb1e0 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -28,16 +28,18 @@ (messages/delete-by-chat-id id))) (defn delete-messages! - [{:keys [current-chat-id]} [_ chat-id]] - (let [id (or chat-id current-chat-id)] - (messages/delete-by-chat-id id))) + [{:keys [current-chat-id chats]} [_ chat-id]] + (let [id (or chat-id current-chat-id) + {:keys [group-chat]} (chats/get-by-id chat-id)] + (when group-chat + (messages/delete-by-chat-id id)))) (defn delete-chat! [_ [_ chat-id]] - (let [{:keys [debug? group-chat]} (chats/get-by-id chat-id)] - (if (and (not debug?) group-chat) - (chats/set-inactive chat-id) - (chats/delete chat-id)))) + (let [{:keys [debug?]} (chats/get-by-id chat-id)] + (if debug? + (chats/delete chat-id) + (chats/set-inactive chat-id)))) (defn remove-pending-messages! [_ [_ chat-id]] diff --git a/src/status_im/chat/models.cljs b/src/status_im/chat/models.cljs index 4fdb4b6154..e51b5c1aac 100644 --- a/src/status_im/chat/models.cljs +++ b/src/status_im/chat/models.cljs @@ -44,8 +44,9 @@ (let [chat (merge (or (get-stored-chat chat-id) (create-new-chat cofx chat-id {})) chat)] - {:db (update-in db [:chats chat-id] merge chat) - :save-chat chat})) + {:db (cond-> db + (:is-active chat) (update-in [:chats chat-id] merge chat)) + :save-chat chat})) ;; TODO (yenda): an upsert is suppose to add the entry if it doesn't ;; exist and update it if it does diff --git a/src/status_im/chat/models/message.cljs b/src/status_im/chat/models/message.cljs index c2a5b8f784..dcab4d137a 100644 --- a/src/status_im/chat/models/message.cljs +++ b/src/status_im/chat/models/message.cljs @@ -42,13 +42,15 @@ :or {clock-value 0}}] (let [{:keys [access-scope->commands-responses] :contacts/keys [contacts]} db {:keys [public-key] :as current-account} (get-current-account db) - chat-identifier (or group-id chat-id from)] + chat-identifier (or group-id chat-id from) + direct-message? (nil? group-id)] ;; proceed with adding message if message is not already stored in realm, ;; it's not from current user (outgoing message) and it's for relevant chat - ;; (either current active chat or new chat not existing yet) + ;; (either current active chat or new chat not existing yet or it's a direct message) (when (and (not (message-exists? message-id)) (not= from public-key) - (pop-up-chat? chat-identifier)) + (or (pop-up-chat? chat-identifier) + direct-message?)) (let [fx (if (get-in db [:chats chat-identifier]) (chat-model/upsert-chat cofx {:chat-id chat-identifier :group-chat (boolean group-id)})