[refs #3210] Replace realm reads for chats with app-db reads
Signed-off-by: Andrey Shovkoplyas <motor4ik@gmail.com>
This commit is contained in:
parent
15f4c90caa
commit
8abdd77fb4
|
@ -3,8 +3,8 @@
|
||||||
[clojure.string :as string]
|
[clojure.string :as string]
|
||||||
[status-im.ui.components.styles :refer [default-chat-color]]
|
[status-im.ui.components.styles :refer [default-chat-color]]
|
||||||
[status-im.chat.constants :as chat-consts]
|
[status-im.chat.constants :as chat-consts]
|
||||||
|
[status-im.chat.models :as chat]
|
||||||
[status-im.protocol.core :as protocol]
|
[status-im.protocol.core :as protocol]
|
||||||
[status-im.data-store.chats :as chats]
|
|
||||||
[status-im.data-store.messages :as messages]
|
[status-im.data-store.messages :as messages]
|
||||||
[status-im.constants :refer [text-content-type
|
[status-im.constants :refer [text-content-type
|
||||||
content-type-command
|
content-type-command
|
||||||
|
@ -39,23 +39,22 @@
|
||||||
(fn [{:keys [current-public-key web3 chats]}
|
(fn [{:keys [current-public-key web3 chats]}
|
||||||
[_ {:keys [from]
|
[_ {:keys [from]
|
||||||
{:keys [group-id keypair timestamp]} :payload}]]
|
{:keys [group-id keypair timestamp]} :payload}]]
|
||||||
(let [{:keys [private public]} keypair]
|
(let [{:keys [private public]} keypair
|
||||||
(let [is-active (chats/is-active? group-id)
|
{:keys [group-admin is-active] :as chat} (get chats group-id)]
|
||||||
chat {:chat-id group-id
|
(when (and (= from group-admin)
|
||||||
:public-key public
|
(or (nil? chat)
|
||||||
:private-key private
|
(chat/new-update? chat timestamp)))
|
||||||
:updated-at timestamp}]
|
(dispatch [:update-chat! {:chat-id group-id
|
||||||
(when (and (= from (get-in chats [group-id :group-admin]))
|
:public-key public
|
||||||
(or (not (chats/exists? group-id))
|
:private-key private
|
||||||
(chats/new-update? timestamp group-id)))
|
:updated-at timestamp}])
|
||||||
(dispatch [:update-chat! chat])
|
(when is-active
|
||||||
(when is-active
|
(protocol/start-watching-group!
|
||||||
(protocol/start-watching-group!
|
{:web3 web3
|
||||||
{:web3 web3
|
:group-id group-id
|
||||||
:group-id group-id
|
:identity current-public-key
|
||||||
:identity current-public-key
|
:keypair keypair
|
||||||
:keypair keypair
|
:callback #(dispatch [:incoming-message %1 %2])})))))))
|
||||||
:callback #(dispatch [:incoming-message %1 %2])}))))))))
|
|
||||||
|
|
||||||
(reg-fx
|
(reg-fx
|
||||||
::start-watching-group
|
::start-watching-group
|
||||||
|
@ -162,7 +161,8 @@
|
||||||
(let [contacts' (keep (fn [ident]
|
(let [contacts' (keep (fn [ident]
|
||||||
(when (not= ident current-public-key)
|
(when (not= ident current-public-key)
|
||||||
{:identity ident})) contacts)
|
{:identity ident})) contacts)
|
||||||
chat {:chat-id group-id
|
chat (get-in db [:chats group-id])
|
||||||
|
new-chat {:chat-id group-id
|
||||||
:name group-name
|
:name group-name
|
||||||
:group-chat true
|
:group-chat true
|
||||||
:group-admin from
|
:group-admin from
|
||||||
|
@ -171,15 +171,15 @@
|
||||||
:contacts contacts'
|
:contacts contacts'
|
||||||
:added-to-at timestamp
|
:added-to-at timestamp
|
||||||
:timestamp timestamp
|
:timestamp timestamp
|
||||||
:is-active true}
|
:is-active true}]
|
||||||
exists? (chats/exists? group-id)]
|
(when (or (nil? chat)
|
||||||
(when (or (not exists?) (chats/new-update? timestamp group-id))
|
(chat/new-update? chat timestamp))
|
||||||
{::start-watching-group (merge {:group-id group-id
|
{::start-watching-group (merge {:group-id group-id
|
||||||
:keypair keypair}
|
:keypair keypair}
|
||||||
(select-keys db [:web3 :current-public-key]))
|
(select-keys db [:web3 :current-public-key]))
|
||||||
:dispatch (if exists?
|
:dispatch (if chat
|
||||||
[:update-chat! chat]
|
[:update-chat! new-chat]
|
||||||
[:add-chat group-id chat])})))))
|
[:add-chat group-id new-chat])})))))
|
||||||
|
|
||||||
(register-handler-fx
|
(register-handler-fx
|
||||||
:show-profile
|
:show-profile
|
||||||
|
|
|
@ -59,3 +59,8 @@
|
||||||
"Just like `update-chat` only implicitely updates timestamp"
|
"Just like `update-chat` only implicitely updates timestamp"
|
||||||
[cofx chat]
|
[cofx chat]
|
||||||
(update-chat cofx (assoc chat :timestamp (:now cofx))))
|
(update-chat cofx (assoc chat :timestamp (:now cofx))))
|
||||||
|
|
||||||
|
(defn new-update? [{:keys [added-to-at removed-at removed-from-at]} timestamp]
|
||||||
|
(and (> timestamp added-to-at)
|
||||||
|
(> timestamp removed-at)
|
||||||
|
(> timestamp removed-from-at)))
|
||||||
|
|
|
@ -34,10 +34,6 @@
|
||||||
[chat-id]
|
[chat-id]
|
||||||
(data-store/get-contacts chat-id))
|
(data-store/get-contacts chat-id))
|
||||||
|
|
||||||
(defn has-contact?
|
|
||||||
[chat-id identity]
|
|
||||||
(data-store/has-contact? chat-id identity))
|
|
||||||
|
|
||||||
(defn add-contacts
|
(defn add-contacts
|
||||||
[chat-id identities]
|
[chat-id identities]
|
||||||
(data-store/add-contacts chat-id identities))
|
(data-store/add-contacts chat-id identities))
|
||||||
|
@ -54,10 +50,6 @@
|
||||||
[chat-id property-name]
|
[chat-id property-name]
|
||||||
(data-store/get-property chat-id property-name))
|
(data-store/get-property chat-id property-name))
|
||||||
|
|
||||||
(defn is-active?
|
|
||||||
[chat-id]
|
|
||||||
(get-property chat-id :is-active))
|
|
||||||
|
|
||||||
(defn removed-at
|
(defn removed-at
|
||||||
[chat-id]
|
[chat-id]
|
||||||
(get-property chat-id :removed-at))
|
(get-property chat-id :removed-at))
|
||||||
|
@ -69,13 +61,3 @@
|
||||||
(defn set-active
|
(defn set-active
|
||||||
[chat-id active?]
|
[chat-id active?]
|
||||||
(save-property chat-id :is-active active?))
|
(save-property chat-id :is-active active?))
|
||||||
|
|
||||||
(defn new-update?
|
|
||||||
[timestamp chat-id]
|
|
||||||
(let
|
|
||||||
[{:keys [added-to-at removed-at removed-from-at added-at]}
|
|
||||||
(get-by-id chat-id)]
|
|
||||||
(and (> timestamp added-to-at)
|
|
||||||
(> timestamp removed-at)
|
|
||||||
(> timestamp removed-from-at)
|
|
||||||
(> timestamp added-at))))
|
|
||||||
|
|
|
@ -84,13 +84,6 @@
|
||||||
(realm/get-one-by-field :chat :chat-id chat-id)
|
(realm/get-one-by-field :chat :chat-id chat-id)
|
||||||
(object/get "contacts")))
|
(object/get "contacts")))
|
||||||
|
|
||||||
(defn has-contact?
|
|
||||||
[chat-id identity]
|
|
||||||
(let [contacts (get-contacts chat-id)
|
|
||||||
contact (.find contacts (fn [object _ _]
|
|
||||||
(= identity (object/get object "identity"))))]
|
|
||||||
(if contact true false)))
|
|
||||||
|
|
||||||
(defn- save-contacts
|
(defn- save-contacts
|
||||||
[identities contacts added-at]
|
[identities contacts added-at]
|
||||||
(doseq [contact-identity identities]
|
(doseq [contact-identity identities]
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
[status-im.protocol.message-cache :as cache]
|
[status-im.protocol.message-cache :as cache]
|
||||||
[status-im.protocol.listeners :as listeners]
|
[status-im.protocol.listeners :as listeners]
|
||||||
[status-im.chat.models.message :as models.message]
|
[status-im.chat.models.message :as models.message]
|
||||||
|
[status-im.chat.models :as chat]
|
||||||
[status-im.protocol.web3.inbox :as inbox]
|
[status-im.protocol.web3.inbox :as inbox]
|
||||||
[status-im.protocol.web3.keys :as web3.keys]
|
[status-im.protocol.web3.keys :as web3.keys]
|
||||||
[status-im.utils.datetime :as datetime]
|
[status-im.utils.datetime :as datetime]
|
||||||
|
@ -47,26 +48,6 @@
|
||||||
(let [[{{:keys [message-id]} :payload}] (:event coeffects)]
|
(let [[{{:keys [message-id]} :payload}] (:event coeffects)]
|
||||||
(assoc coeffects :message-by-id (messages/get-by-id message-id)))))
|
(assoc coeffects :message-by-id (messages/get-by-id message-id)))))
|
||||||
|
|
||||||
(re-frame/reg-cofx
|
|
||||||
::chats-new-update?
|
|
||||||
(fn [coeffects _]
|
|
||||||
(let [[{{:keys [group-id timestamp]} :payload}] (:event coeffects)]
|
|
||||||
(assoc coeffects :new-update? (chats/new-update? timestamp group-id)))))
|
|
||||||
|
|
||||||
(re-frame/reg-cofx
|
|
||||||
::chats-is-active-and-timestamp
|
|
||||||
(fn [coeffects _]
|
|
||||||
(let [[{{:keys [group-id timestamp]} :payload}] (:event coeffects)]
|
|
||||||
(assoc coeffects :chats-is-active-and-timestamp
|
|
||||||
(and (chats/is-active? group-id)
|
|
||||||
(> timestamp (chats/get-property group-id :timestamp)))))))
|
|
||||||
|
|
||||||
(re-frame/reg-cofx
|
|
||||||
::has-contact?
|
|
||||||
(fn [coeffects _]
|
|
||||||
(let [[{{:keys [group-id identity]} :payload}] (:event coeffects)]
|
|
||||||
(assoc coeffects :has-contact? (chats/has-contact? group-id identity)))))
|
|
||||||
|
|
||||||
|
|
||||||
;;;; FX
|
;;;; FX
|
||||||
|
|
||||||
|
@ -548,37 +529,44 @@
|
||||||
|
|
||||||
;;GROUP
|
;;GROUP
|
||||||
|
|
||||||
|
(defn- has-contact? [{:keys [contacts]} identity]
|
||||||
|
(let [identities (set (map :identity contacts))]
|
||||||
|
(contains? identities identity)))
|
||||||
|
|
||||||
(handlers/register-handler-fx
|
(handlers/register-handler-fx
|
||||||
:participant-invited-to-group
|
:participant-invited-to-group
|
||||||
[re-frame/trim-v
|
[re-frame/trim-v]
|
||||||
(re-frame/inject-cofx ::has-contact?)]
|
(fn [{{:keys [current-public-key chats] :as db} :db}
|
||||||
(fn [{{:keys [current-public-key chats] :as db} :db has-contact? :has-contact?}
|
|
||||||
[{:keys [from]
|
[{:keys [from]
|
||||||
{:keys [group-id identity message-id timestamp]} :payload}]]
|
{:keys [group-id identity message-id timestamp]} :payload}]]
|
||||||
(let [admin (get-in chats [group-id :group-admin])]
|
(let [chat (get-in db [:chats group-id])
|
||||||
|
admin (:group-admin chats)]
|
||||||
(when (= from admin)
|
(when (= from admin)
|
||||||
(merge {::participant-invited-to-group-message {:group-id group-id :current-public-key current-public-key
|
(merge {::participant-invited-to-group-message {:group-id group-id :current-public-key current-public-key
|
||||||
:identity identity :from from :message-id message-id
|
:identity identity :from from :message-id message-id
|
||||||
:timestamp timestamp}}
|
:timestamp timestamp}}
|
||||||
(when-not (and (= current-public-key identity) has-contact?)
|
(when-not (and (= current-public-key identity) (has-contact? chat identity))
|
||||||
{:db (update-in db [:chats group-id :contacts] conj {:identity identity})
|
{:db (update-in db [:chats group-id :contacts] conj {:identity identity})
|
||||||
::chats-add-contact [group-id identity]}))))))
|
::chats-add-contact [group-id identity]}))))))
|
||||||
|
|
||||||
(handlers/register-handler-fx
|
(handlers/register-handler-fx
|
||||||
::you-removed-from-group
|
::you-removed-from-group
|
||||||
[re-frame/trim-v
|
[re-frame/trim-v]
|
||||||
(re-frame/inject-cofx ::chats-new-update?)]
|
(fn [{{:keys [web3] :as db} :db}
|
||||||
(fn [{{:keys [web3]} :db new-update? :new-update?}
|
|
||||||
[{:keys [from]
|
[{:keys [from]
|
||||||
{:keys [group-id timestamp message-id] :as payload} :payload}]]
|
{:keys [group-id timestamp message-id] :as payload} :payload}]]
|
||||||
(when new-update?
|
(let [chat (get-in db [:chats group-id])
|
||||||
{::you-removed-from-group-message {:from from :message-id message-id :timestamp timestamp
|
new-update? (chat/new-update? chat timestamp)]
|
||||||
:group-id group-id}
|
(when new-update?
|
||||||
::stop-watching-group! {:web3 web3
|
{::you-removed-from-group-message {:from from
|
||||||
:group-id group-id}
|
:message-id message-id
|
||||||
:dispatch [:update-chat! {:chat-id group-id
|
:timestamp timestamp
|
||||||
:removed-from-at timestamp
|
:group-id group-id}
|
||||||
:is-active false}]})))
|
::stop-watching-group! {:web3 web3
|
||||||
|
:group-id group-id}
|
||||||
|
:dispatch [:update-chat! {:chat-id group-id
|
||||||
|
:removed-from-at timestamp
|
||||||
|
:is-active false}]}))))
|
||||||
|
|
||||||
(handlers/register-handler-fx
|
(handlers/register-handler-fx
|
||||||
:participant-removed-from-group
|
:participant-removed-from-group
|
||||||
|
@ -599,21 +587,23 @@
|
||||||
|
|
||||||
(handlers/register-handler-fx
|
(handlers/register-handler-fx
|
||||||
:participant-left-group
|
:participant-left-group
|
||||||
[re-frame/trim-v
|
[re-frame/trim-v]
|
||||||
(re-frame/inject-cofx ::chats-is-active-and-timestamp)]
|
(fn [{{:keys [current-public-key] :as db} :db}
|
||||||
(fn [{{:keys [current-public-key] :as db} :db chats-is-active-and-timestamp :chats-is-active-and-timestamp}
|
|
||||||
[{:keys [from]
|
[{:keys [from]
|
||||||
{:keys [group-id timestamp message-id]} :payload}]]
|
{:keys [group-id timestamp message-id]} :payload}]]
|
||||||
(when (and (not= current-public-key from)
|
(let [chat (get-in db [:chats group-id])
|
||||||
chats-is-active-and-timestamp)
|
{chat-is-active :is-active chat-timestamp :timestamp} chat]
|
||||||
{::participant-left-group-message {:chat-id group-id
|
(when (and (not= current-public-key from)
|
||||||
:from from
|
chat-is-active
|
||||||
:message-id message-id
|
(> timestamp chat-timestamp))
|
||||||
:timestamp timestamp}
|
{::participant-left-group-message {:chat-id group-id
|
||||||
::chats-remove-contact [group-id from]
|
:from from
|
||||||
:db (update-in db [:chats group-id :contacts]
|
:message-id message-id
|
||||||
#(remove (fn [{:keys [identity]}]
|
:timestamp timestamp}
|
||||||
(= identity from)) %))})))
|
::chats-remove-contact [group-id from]
|
||||||
|
:db (update-in db [:chats group-id :contacts]
|
||||||
|
#(remove (fn [{:keys [identity]}]
|
||||||
|
(= identity from)) %))}))))
|
||||||
|
|
||||||
;;ERROR
|
;;ERROR
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue