[refs #3210] Replace realm reads for chats with app-db reads

Signed-off-by: Andrey Shovkoplyas <motor4ik@gmail.com>
This commit is contained in:
Dmitry Novotochinov 2018-02-06 12:57:19 +03:00 committed by Andrey Shovkoplyas
parent 15f4c90caa
commit 8abdd77fb4
No known key found for this signature in database
GPG Key ID: EAAB7C8622D860A4
5 changed files with 69 additions and 99 deletions

View File

@ -3,8 +3,8 @@
[clojure.string :as string]
[status-im.ui.components.styles :refer [default-chat-color]]
[status-im.chat.constants :as chat-consts]
[status-im.chat.models :as chat]
[status-im.protocol.core :as protocol]
[status-im.data-store.chats :as chats]
[status-im.data-store.messages :as messages]
[status-im.constants :refer [text-content-type
content-type-command
@ -39,23 +39,22 @@
(fn [{:keys [current-public-key web3 chats]}
[_ {:keys [from]
{:keys [group-id keypair timestamp]} :payload}]]
(let [{:keys [private public]} keypair]
(let [is-active (chats/is-active? group-id)
chat {:chat-id group-id
:public-key public
:private-key private
:updated-at timestamp}]
(when (and (= from (get-in chats [group-id :group-admin]))
(or (not (chats/exists? group-id))
(chats/new-update? timestamp group-id)))
(dispatch [:update-chat! chat])
(when is-active
(protocol/start-watching-group!
{:web3 web3
:group-id group-id
:identity current-public-key
:keypair keypair
:callback #(dispatch [:incoming-message %1 %2])}))))))))
(let [{:keys [private public]} keypair
{:keys [group-admin is-active] :as chat} (get chats group-id)]
(when (and (= from group-admin)
(or (nil? chat)
(chat/new-update? chat timestamp)))
(dispatch [:update-chat! {:chat-id group-id
:public-key public
:private-key private
:updated-at timestamp}])
(when is-active
(protocol/start-watching-group!
{:web3 web3
:group-id group-id
:identity current-public-key
:keypair keypair
:callback #(dispatch [:incoming-message %1 %2])})))))))
(reg-fx
::start-watching-group
@ -162,7 +161,8 @@
(let [contacts' (keep (fn [ident]
(when (not= ident current-public-key)
{:identity ident})) contacts)
chat {:chat-id group-id
chat (get-in db [:chats group-id])
new-chat {:chat-id group-id
:name group-name
:group-chat true
:group-admin from
@ -171,15 +171,15 @@
:contacts contacts'
:added-to-at timestamp
:timestamp timestamp
:is-active true}
exists? (chats/exists? group-id)]
(when (or (not exists?) (chats/new-update? timestamp group-id))
:is-active true}]
(when (or (nil? chat)
(chat/new-update? chat timestamp))
{::start-watching-group (merge {:group-id group-id
:keypair keypair}
(select-keys db [:web3 :current-public-key]))
:dispatch (if exists?
[:update-chat! chat]
[:add-chat group-id chat])})))))
:dispatch (if chat
[:update-chat! new-chat]
[:add-chat group-id new-chat])})))))
(register-handler-fx
:show-profile

View File

@ -59,3 +59,8 @@
"Just like `update-chat` only implicitely updates timestamp"
[cofx chat]
(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)))

View File

@ -34,10 +34,6 @@
[chat-id]
(data-store/get-contacts chat-id))
(defn has-contact?
[chat-id identity]
(data-store/has-contact? chat-id identity))
(defn add-contacts
[chat-id identities]
(data-store/add-contacts chat-id identities))
@ -54,10 +50,6 @@
[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
[chat-id]
(get-property chat-id :removed-at))
@ -69,13 +61,3 @@
(defn set-active
[chat-id 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))))

View File

@ -84,13 +84,6 @@
(realm/get-one-by-field :chat :chat-id chat-id)
(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
[identities contacts added-at]
(doseq [contact-identity identities]

View File

@ -15,6 +15,7 @@
[status-im.protocol.message-cache :as cache]
[status-im.protocol.listeners :as listeners]
[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.keys :as web3.keys]
[status-im.utils.datetime :as datetime]
@ -47,26 +48,6 @@
(let [[{{:keys [message-id]} :payload}] (:event coeffects)]
(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
@ -548,37 +529,44 @@
;;GROUP
(defn- has-contact? [{:keys [contacts]} identity]
(let [identities (set (map :identity contacts))]
(contains? identities identity)))
(handlers/register-handler-fx
:participant-invited-to-group
[re-frame/trim-v
(re-frame/inject-cofx ::has-contact?)]
(fn [{{:keys [current-public-key chats] :as db} :db has-contact? :has-contact?}
[re-frame/trim-v]
(fn [{{:keys [current-public-key chats] :as db} :db}
[{:keys [from]
{: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)
(merge {::participant-invited-to-group-message {:group-id group-id :current-public-key current-public-key
:identity identity :from from :message-id message-id
: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})
::chats-add-contact [group-id identity]}))))))
(handlers/register-handler-fx
::you-removed-from-group
[re-frame/trim-v
(re-frame/inject-cofx ::chats-new-update?)]
(fn [{{:keys [web3]} :db new-update? :new-update?}
[re-frame/trim-v]
(fn [{{:keys [web3] :as db} :db}
[{:keys [from]
{:keys [group-id timestamp message-id] :as payload} :payload}]]
(when new-update?
{::you-removed-from-group-message {:from from :message-id message-id :timestamp timestamp
:group-id group-id}
::stop-watching-group! {:web3 web3
:group-id group-id}
:dispatch [:update-chat! {:chat-id group-id
:removed-from-at timestamp
:is-active false}]})))
(let [chat (get-in db [:chats group-id])
new-update? (chat/new-update? chat timestamp)]
(when new-update?
{::you-removed-from-group-message {:from from
:message-id message-id
:timestamp timestamp
:group-id group-id}
::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
:participant-removed-from-group
@ -599,21 +587,23 @@
(handlers/register-handler-fx
:participant-left-group
[re-frame/trim-v
(re-frame/inject-cofx ::chats-is-active-and-timestamp)]
(fn [{{:keys [current-public-key] :as db} :db chats-is-active-and-timestamp :chats-is-active-and-timestamp}
[re-frame/trim-v]
(fn [{{:keys [current-public-key] :as db} :db}
[{:keys [from]
{:keys [group-id timestamp message-id]} :payload}]]
(when (and (not= current-public-key from)
chats-is-active-and-timestamp)
{::participant-left-group-message {:chat-id group-id
:from from
:message-id message-id
:timestamp timestamp}
::chats-remove-contact [group-id from]
:db (update-in db [:chats group-id :contacts]
#(remove (fn [{:keys [identity]}]
(= identity from)) %))})))
(let [chat (get-in db [:chats group-id])
{chat-is-active :is-active chat-timestamp :timestamp} chat]
(when (and (not= current-public-key from)
chat-is-active
(> timestamp chat-timestamp))
{::participant-left-group-message {:chat-id group-id
:from from
:message-id message-id
:timestamp timestamp}
::chats-remove-contact [group-id from]
:db (update-in db [:chats group-id :contacts]
#(remove (fn [{:keys [identity]}]
(= identity from)) %))}))))
;;ERROR