2019-02-22 12:26:40 +00:00
|
|
|
(ns status-im.contact.block
|
|
|
|
(:require [re-frame.core :as re-frame]
|
|
|
|
[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]
|
2019-08-05 06:17:55 +00:00
|
|
|
[status-im.data-store.chats :as chats-store]
|
2019-02-22 12:26:40 +00:00
|
|
|
[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)}
|
2019-06-12 06:25:18 +00:00
|
|
|
(navigation/navigate-to-cofx :home {})))
|
2019-02-22 12:26:40 +00:00
|
|
|
|
|
|
|
(fx/defn clean-up-chat
|
2019-08-05 06:17:55 +00:00
|
|
|
[{:keys [db] :as cofx}
|
|
|
|
public-key
|
|
|
|
{:keys [chat-id
|
|
|
|
unviewed-messages-count
|
|
|
|
last-message-content
|
2019-08-30 13:55:56 +00:00
|
|
|
last-message-timestamp
|
2019-08-05 06:17:55 +00:00
|
|
|
last-message-content-type]}]
|
|
|
|
(let [removed-messages-ids (keep
|
|
|
|
(fn [[message-id {:keys [from]}]]
|
|
|
|
(when (= from public-key)
|
|
|
|
message-id))
|
|
|
|
(get-in db [:chats chat-id :messages]))
|
2019-02-22 12:26:40 +00:00
|
|
|
db (-> db
|
|
|
|
;; remove messages
|
|
|
|
(update-in [:chats chat-id :messages]
|
|
|
|
#(apply dissoc % removed-messages-ids))
|
|
|
|
;; remove message groups
|
|
|
|
(update-in [:chats chat-id]
|
2019-08-05 06:17:55 +00:00
|
|
|
dissoc :message-groups)
|
|
|
|
(update-in [:chats chat-id]
|
|
|
|
assoc
|
|
|
|
:unviewed-messages-count unviewed-messages-count
|
|
|
|
:last-message-content last-message-content
|
2019-08-30 13:55:56 +00:00
|
|
|
:last-message-timestamp last-message-timestamp
|
2019-08-05 06:17:55 +00:00
|
|
|
:last-message-content-type last-message-content-type))]
|
2019-02-22 12:26:40 +00:00
|
|
|
(fx/merge cofx
|
|
|
|
{:db db}
|
|
|
|
;; recompute message group
|
|
|
|
(chat.models.loading/group-chat-messages
|
|
|
|
chat-id
|
|
|
|
(vals (get-in db [:chats chat-id :messages]))))))
|
|
|
|
|
2019-08-05 06:17:55 +00:00
|
|
|
(fx/defn contact-blocked
|
|
|
|
{:events [::contact-blocked]}
|
|
|
|
[{:keys [db] :as cofx} {:keys [public-key]} chats]
|
|
|
|
(let [fxs (map #(clean-up-chat public-key %) chats)]
|
|
|
|
(apply fx/merge cofx fxs)))
|
2019-02-22 12:26:40 +00:00
|
|
|
|
|
|
|
(fx/defn block-contact
|
2019-08-05 06:17:55 +00:00
|
|
|
[{:keys [db now] :as cofx} public-key]
|
2019-02-22 12:26:40 +00:00
|
|
|
(let [contact (-> (contact.db/public-key->contact
|
|
|
|
(:contacts/contacts db)
|
|
|
|
public-key)
|
|
|
|
(assoc :last-updated now)
|
2019-08-24 23:51:04 +00:00
|
|
|
(update :system-tags (fnil conj #{}) :contact/blocked))
|
2019-02-22 12:26:40 +00:00
|
|
|
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
|
2019-08-24 23:51:04 +00:00
|
|
|
(update :contacts/blocked (fnil conj #{}) public-key)
|
2019-02-22 12:26:40 +00:00
|
|
|
;; update the contact in contacts list
|
|
|
|
(assoc-in [:contacts/contacts public-key] contact)
|
|
|
|
;; remove the 1-1 chat if it exists
|
2019-08-05 06:17:55 +00:00
|
|
|
(update-in [:chats] dissoc public-key))}
|
|
|
|
(contacts-store/block contact #(re-frame/dispatch [::contact-blocked contact (map chats-store/<-rpc %)]))
|
2019-02-22 12:26:40 +00:00
|
|
|
;; 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
|
2019-08-03 13:01:17 +00:00
|
|
|
[{:keys [db now] :as cofx} public-key]
|
2019-02-22 12:26:40 +00:00
|
|
|
(let [contact (-> (get-in db [:contacts/contacts public-key])
|
|
|
|
(assoc :last-updated now)
|
|
|
|
(update :system-tags disj :contact/blocked))]
|
2019-08-03 13:01:17 +00:00
|
|
|
(fx/merge cofx
|
|
|
|
{:db (-> db
|
|
|
|
(update :contacts/blocked disj public-key)
|
|
|
|
(assoc-in [:contacts/contacts public-key] contact))}
|
2019-09-19 17:12:58 +00:00
|
|
|
(contacts-store/save-contact contact))))
|