diff --git a/src/status_im/contact/block.cljs b/src/status_im/contact/block.cljs new file mode 100644 index 0000000000..f2ca18bc42 --- /dev/null +++ b/src/status_im/contact/block.cljs @@ -0,0 +1,115 @@ +(ns status-im.contact.block + (:require [re-frame.core :as re-frame] + [status-im.accounts.db :as accounts.db] + [status-im.chat.models :as chat.models] + [status-im.chat.models.loading :as chat.models.loading] + [status-im.chat.models.message :as chat.models.message] + [status-im.contact.db :as contact.db] + [status-im.data-store.contacts :as contacts-store] + [status-im.i18n :as i18n] + [status-im.ui.screens.navigation :as navigation] + [status-im.utils.fx :as fx])) + +(fx/defn remove-current-chat-id + [{:keys [db] :as cofx}] + (fx/merge cofx + {:db (dissoc db :current-chat-id)} + (navigation/navigate-to-clean :home {}))) + +(defn get-removed-unseen-count + [current-public-key user-statuses removed-messages-ids] + (- (count removed-messages-ids) + (count (filter (fn [[_ statuses]] + (= :seen + (:status (get statuses + current-public-key)))) + user-statuses)))) + +(fx/defn clean-up-chat + [{:keys [db get-stored-user-statuses] :as cofx} chat-id removed-chat-messages] + (let [current-public-key (accounts.db/current-public-key cofx) + removed-messages-ids (map :message-id removed-chat-messages) + user-statuses (get-stored-user-statuses chat-id + removed-messages-ids) + removed-unseen-count (get-removed-unseen-count current-public-key + user-statuses + removed-messages-ids) + db (-> db + ;; remove messages + (update-in [:chats chat-id :messages] + #(apply dissoc % removed-messages-ids)) + ;; remove message statuses + (update-in [:chats chat-id :messages-statuses] + #(apply dissoc % removed-messages-ids)) + ;; remove message groups + (update-in [:chats chat-id] + dissoc :message-groups))] + (fx/merge cofx + {:db db} + ;; update unviewed messages count + (chat.models/upsert-chat + {:chat-id chat-id + :unviewed-messages-count + (- (get-in db [:chats chat-id :unviewed-messages-count]) + removed-unseen-count)}) + ;; recompute message group + (chat.models.loading/group-chat-messages + chat-id + (vals (get-in db [:chats chat-id :messages])))))) + +(fx/defn clean-up-chats + [cofx removed-messages-by-chat] + (apply fx/merge cofx + (map (fn [[chat-id messages]] + (clean-up-chat chat-id messages)) + removed-messages-by-chat))) + +(fx/defn block-contact + [{:keys [db get-user-messages now] :as cofx} public-key] + (let [contact (-> (contact.db/public-key->contact + (:contacts/contacts db) + public-key) + (assoc :last-updated now) + (update :system-tags conj :contact/blocked)) + user-messages (get-user-messages public-key) + user-messages-ids (map :message-id user-messages) + ;; we make sure to remove the 1-1 chat which we delete entirely + removed-messages-by-chat (-> (group-by :chat-id user-messages) + (dissoc public-key)) + from-one-to-one-chat? (not (get-in db [:chats (:current-chat-id db) :group-chat]))] + (fx/merge cofx + {:db (-> db + ;; add the contact to blocked contacts + (update :contacts/blocked conj public-key) + ;; update the contact in contacts list + (assoc-in [:contacts/contacts public-key] contact) + ;; remove the 1-1 chat if it exists + (update-in [:chats] dissoc public-key)) + :data-store/tx [(contacts-store/block-user-tx contact + user-messages-ids)]} + ;;remove the messages from chat + (clean-up-chats removed-messages-by-chat) + (chat.models.message/update-last-messages + (keys removed-messages-by-chat)) + ;; reset navigation to avoid going back to non existing one to one chat + (if from-one-to-one-chat? + remove-current-chat-id + (navigation/navigate-back))))) + +(fx/defn unblock-contact + [{:keys [db now]} public-key] + (let [contact (-> (get-in db [:contacts/contacts public-key]) + (assoc :last-updated now) + (update :system-tags disj :contact/blocked))] + {:db (-> db + (update :contacts/blocked disj public-key) + (assoc-in [:contacts/contacts public-key] contact)) + :data-store/tx [(contacts-store/save-contact-tx contact)]})) + +(fx/defn block-contact-confirmation + [cofx public-key] + {:utils/show-confirmation + {:title (i18n/label :t/block-contact) + :content (i18n/label :t/block-contact-details) + :confirm-button-text (i18n/label :t/to-block) + :on-accept #(re-frame/dispatch [:contact.ui/block-contact-confirmed public-key])}}) diff --git a/src/status_im/contact/core.cljs b/src/status_im/contact/core.cljs index f929c2e4e7..6fc2566fde 100644 --- a/src/status_im/contact/core.cljs +++ b/src/status_im/contact/core.cljs @@ -1,28 +1,17 @@ (ns status-im.contact.core - (:require [re-frame.core :as re-frame] - [status-im.accounts.db :as accounts.db] + (:require [status-im.accounts.db :as accounts.db] [status-im.chat.models :as chat.models] - [clojure.set :as clojure.set] + [status-im.contact-code.core :as contact-code] [status-im.contact.db :as contact.db] [status-im.contact.device-info :as device-info] [status-im.data-store.contacts :as contacts-store] - [status-im.data-store.messages :as data-store.messages] - [status-im.data-store.chats :as data-store.chats] - [status-im.i18n :as i18n] - [status-im.transport.utils :as transport.utils] + [status-im.mailserver.core :as mailserver] [status-im.transport.message.contact :as message.contact] - [status-im.transport.message.public-chat :as transport.public-chat] [status-im.transport.message.protocol :as protocol] - [status-im.contact-code.core :as contact-code] - [status-im.ui.screens.add-new.new-chat.db :as new-chat.db] - [status-im.ui.screens.navigation :as navigation] - [status-im.utils.fx :as fx] - [status-im.utils.utils :as utils] [status-im.transport.partitioned-topic :as transport.topic] + [status-im.ui.screens.navigation :as navigation] [status-im.utils.config :as config] - [status-im.chat.models.loading :as chat.models.loading] - [status-im.chat.models.message :as chat.models.message] - [status-im.mailserver.core :as mailserver])) + [status-im.utils.fx :as fx])) (fx/defn load-contacts [{:keys [db all-contacts]}] @@ -100,128 +89,6 @@ :minPow 1 :callback (constantly nil)}]}}))) -(fx/defn change-system-tag - "remove a system tag from the contact" - [{:keys [db] :as cofx} public-key tag change-fn] - (let [contact (update (get-in db [:contacts/contacts public-key]) - :system-tags (fnil #(change-fn % tag) #{}))] - {:db (assoc-in db [:contacts/contacts public-key] contact) - :data-store/tx [(contacts-store/save-contact-tx contact)]})) - -(fx/defn add-system-tag - "add a system tag to the contact" - [{:keys [db] :as cofx} public-key tag] - (change-system-tag cofx public-key tag conj)) - -(fx/defn remove-system-tag - "remove a system tag from the contact" - [{:keys [db] :as cofx} public-key tag] - (change-system-tag cofx public-key tag disj)) - -(fx/defn block-contact-confirmation - [cofx public-key] - {:utils/show-confirmation - {:title (i18n/label :t/block-contact) - :content (i18n/label :t/block-contact-details) - :confirm-button-text (i18n/label :t/to-block) - :on-accept #(re-frame/dispatch [:contact.ui/block-contact-confirmed public-key])}}) - -(defn get-removed-unseen-count - [current-public-key user-statuses removed-messages-ids] - (- (count removed-messages-ids) - (count (filter (fn [[_ statuses]] - (= :seen - (:status (get statuses - current-public-key)))) - user-statuses)))) - -(fx/defn clean-up-chat - [{:keys [db get-stored-user-statuses] :as cofx} chat-id removed-chat-messages] - (let [current-public-key (accounts.db/current-public-key cofx) - removed-messages-ids (map :message-id removed-chat-messages) - user-statuses (get-stored-user-statuses chat-id - removed-messages-ids) - removed-unseen-count (get-removed-unseen-count current-public-key - user-statuses - removed-messages-ids) - db (-> db - ;; remove messages - (update-in [:chats chat-id :messages] - #(apply dissoc % removed-messages-ids)) - ;; remove message statuses - (update-in [:chats chat-id :messages-statuses] - #(apply dissoc % removed-messages-ids)) - ;; remove message groups - (update-in [:chats chat-id] - dissoc :message-groups))] - (fx/merge cofx - {:db db} - ;; update unviewed messages count - (chat.models/upsert-chat - {:chat-id chat-id - :unviewed-messages-count - (- (get-in db [:chats chat-id :unviewed-messages-count]) - removed-unseen-count)}) - ;; recompute message group - (chat.models.loading/group-chat-messages - chat-id - (vals (get-in db [:chats chat-id :messages])))))) - -(fx/defn clean-up-chats - [cofx removed-messages-by-chat] - (apply fx/merge cofx - (map (fn [[chat-id messages]] - (clean-up-chat chat-id messages)) - removed-messages-by-chat))) - -(fx/defn remove-current-chat-id - [{:keys [db] :as cofx}] - (fx/merge cofx - {:db (dissoc db :current-chat-id)} - (navigation/navigate-to-clean :home {}))) - -(fx/defn block-contact - [{:keys [db get-user-messages now] :as cofx} public-key] - (let [contact (-> (contact.db/public-key->contact - (:contacts/contacts db) - public-key) - (assoc :last-updated now) - (update :system-tags conj :contact/blocked)) - user-messages (get-user-messages public-key) - user-messages-ids (map :message-id user-messages) - ;; we make sure to remove the 1-1 chat which we delete entirely - removed-messages-by-chat (-> (group-by :chat-id user-messages) - (dissoc public-key)) - from-one-to-one-chat? (not (get-in db [:chats (:current-chat-id db) :group-chat]))] - (fx/merge cofx - {:db (-> db - ;; add the contact to blocked contacts - (update :contacts/blocked conj public-key) - ;; update the contact in contacts list - (assoc-in [:contacts/contacts public-key] contact) - ;; remove the 1-1 chat if it exists - (update-in [:chats] dissoc public-key)) - :data-store/tx [(contacts-store/block-user-tx contact - user-messages-ids)]} - ;;remove the messages from chat - (clean-up-chats removed-messages-by-chat) - (chat.models.message/update-last-messages - (keys removed-messages-by-chat)) - ;; reset navigation to avoid going back to non existing one to one chat - (if from-one-to-one-chat? - remove-current-chat-id - (navigation/navigate-back))))) - -(fx/defn unblock-contact - [{:keys [db now]} public-key] - (let [contact (-> (get-in db [:contacts/contacts public-key]) - (assoc :last-updated now) - (update :system-tags disj :contact/blocked))] - {:db (-> db - (update :contacts/blocked disj public-key) - (assoc-in [:contacts/contacts public-key] contact)) - :data-store/tx [(contacts-store/save-contact-tx contact)]})) - (defn handle-contact-update [public-key timestamp @@ -269,21 +136,6 @@ (fx/merge cofx (chat.models/start-chat public-key {:navigation-reset? true}))) -(fx/defn handle-qr-code - [{:keys [db] :as cofx} contact-identity] - (let [current-account (:account/account db) - fx {:db (assoc db :contacts/new-identity contact-identity)} - validation-result (new-chat.db/validate-pub-key db contact-identity)] - (if (some? validation-result) - {:utils/show-popup {:title (i18n/label :t/unable-to-read-this-code) - :content validation-result - :on-dismiss #(re-frame/dispatch [:navigate-to-clean :home])}} - (fx/merge cofx - fx - (if config/partitioned-topic-enabled? - (add-contacts-filter contact-identity :open-chat) - (open-chat contact-identity)))))) - (fx/defn open-contact-toggle-list [{:keys [db :as cofx]}] (fx/merge cofx @@ -291,8 +143,3 @@ :group/selected-contacts #{} :new-chat-name "")} (navigation/navigate-to-cofx :contact-toggle-list nil))) - -(fx/defn add-new-identity-to-contacts - [{{:contacts/keys [new-identity]} :db :as cofx}] - (when (seq new-identity) - (open-chat cofx new-identity))) diff --git a/src/status_im/data_store/contacts.cljs b/src/status_im/data_store/contacts.cljs index 8753d66dcd..ac989808b2 100644 --- a/src/status_im/data_store/contacts.cljs +++ b/src/status_im/data_store/contacts.cljs @@ -1,8 +1,6 @@ (ns status-im.data-store.contacts - (:require [goog.object :as object] - [re-frame.core :as re-frame] - [status-im.data-store.realm.core :as core] - [clojure.set :as clojure.set])) + (:require [re-frame.core :as re-frame] + [status-im.data-store.realm.core :as core])) (defn- deserialize-contact [contact] (-> contact diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index 82b5f626b1..0728bf60d1 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -18,6 +18,7 @@ [status-im.chat.models.message :as chat.message] [status-im.contact-code.core :as contact-code] [status-im.contact-recovery.core :as contact-recovery] + [status-im.contact.block :as contact.block] [status-im.contact.core :as contact] [status-im.extensions.core :as extensions] [status-im.extensions.registry :as extensions.registry] @@ -1674,19 +1675,19 @@ (handlers/register-handler-fx :contact.ui/block-contact-pressed (fn [cofx [_ public-key]] - (contact/block-contact-confirmation cofx public-key))) + (contact.block/block-contact-confirmation cofx public-key))) (handlers/register-handler-fx :contact.ui/block-contact-confirmed [(re-frame/inject-cofx :data-store/get-user-messages) (re-frame/inject-cofx :data-store/get-user-statuses)] (fn [cofx [_ public-key]] - (contact/block-contact cofx public-key))) + (contact.block/block-contact cofx public-key))) (handlers/register-handler-fx :contact.ui/unblock-contact-pressed (fn [cofx [_ public-key]] - (contact/unblock-contact cofx public-key))) + (contact.block/unblock-contact cofx public-key))) (handlers/register-handler-fx :contact/qr-code-scanned