Skip to content

Commit dcc62ec

Browse files
committed
contacts performance
1 parent 074a18c commit dcc62ec

File tree

11 files changed

+81
-111
lines changed

11 files changed

+81
-111
lines changed

src/status_im/chat/models/loading.cljs

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
[status-im.chat.models.message-list :as message-list]
88
[taoensso.timbre :as log]
99
[status-im.ethereum.json-rpc :as json-rpc]
10-
[clojure.string :as string]
1110
[status-im.chat.models.pin-message :as models.pin-message]))
1211

1312
(defn cursor->clock-value
@@ -99,13 +98,12 @@
9998
(get-in db [:pagination-info chat-id :messages-initialized?])))
10099
(let [already-loaded-messages (get-in db [:messages chat-id])
101100
;; We remove those messages that are already loaded, as we might get some duplicates
102-
{:keys [all-messages new-messages senders]}
101+
{:keys [all-messages new-messages senders contacts]}
103102
(reduce (fn [{:keys [all-messages] :as acc}
104-
{:keys [message-id alias from]
103+
{:keys [message-id from]
105104
:as message}]
106105
(cond-> acc
107-
(and (not (string/blank? alias))
108-
(not (get-in db [:chats chat-id :users from])))
106+
(not (get-in db [:chats chat-id :users from]))
109107
(update :senders assoc from message)
110108

111109
(nil? (get all-messages message-id))
@@ -115,6 +113,7 @@
115113
(update :all-messages assoc message-id message)))
116114
{:all-messages already-loaded-messages
117115
:senders {}
116+
:contacts {}
118117
:new-messages []}
119118
messages)
120119
current-clock-value (get-in db [:pagination-info chat-id :cursor-clock-value])
@@ -134,7 +133,8 @@
134133
(assoc-in [:messages chat-id] all-messages)
135134
(update-in [:message-lists chat-id] message-list/add-many new-messages)
136135
(assoc-in [:pagination-info chat-id :all-loaded?]
137-
(empty? cursor)))})))
136+
(empty? cursor))
137+
(update :contacts/contacts merge contacts))})))
138138

139139
(fx/defn load-more-messages
140140
{:events [:chat.ui/load-more-messages]}

src/status_im/chat/models/message.cljs

+16-14
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
[taoensso.timbre :as log]
1111
[status-im.chat.models.mentions :as mentions]
1212
[clojure.string :as string]
13-
[status-im.contact.db :as contact.db]
1413
[status-im.utils.types :as types]
1514
[status-im.ui.screens.chat.state :as view.state]
1615
[status-im.chat.models.loading :as chat.loading]
17-
[status-im.utils.platform :as platform]))
16+
[status-im.utils.platform :as platform]
17+
[status-im.utils.gfycat.core :as gfycat]))
1818

1919
(defn- message-loaded?
2020
[db chat-id message-id]
@@ -39,14 +39,17 @@
3939
{:events [:chat/add-senders-to-chat-users]}
4040
[{:keys [db]} messages]
4141
(reduce (fn [acc {:keys [chat-id alias name identicon from]}]
42-
(update-in acc [:db :chats chat-id :users] assoc
43-
from
44-
(mentions/add-searchable-phrases
45-
{:alias alias
46-
:name (or name alias)
47-
:identicon identicon
48-
:public-key from
49-
:nickname (get-in db [:contacts/contacts from :nickname])})))
42+
(let [alias (if (string/blank? alias)
43+
(gfycat/generate-gfy from)
44+
alias)]
45+
(update-in acc [:db :chats chat-id :users] assoc
46+
from
47+
(mentions/add-searchable-phrases
48+
{:alias alias
49+
:name (or name alias)
50+
:identicon identicon
51+
:public-key from
52+
:nickname (get-in db [:contacts/contacts from :nickname])}))))
5053
{:db db}
5154
messages))
5255

@@ -56,14 +59,14 @@
5659
(or
5760
(= chat-id (chat-model/my-profile-chat-topic db))
5861
(when-let [pub-key (get-in db [:chats chat-id :profile-public-key])]
59-
(contact.db/added? db pub-key)))))
62+
(get-in db [:contacts/contacts pub-key :added])))))
6063

6164
(defn get-timeline-message [db chat-id message-js]
6265
(when (timeline-message? db chat-id)
6366
(data-store.messages/<-rpc (types/js->clj message-js))))
6467

6568
(defn add-message [{:keys [db] :as acc} message-js chat-id message-id cursor-clock-value]
66-
(let [{:keys [alias replace from clock-value] :as message}
69+
(let [{:keys [replace from clock-value] :as message}
6770
(data-store.messages/<-rpc (types/js->clj message-js))]
6871
(if (message-loaded? db chat-id message-id)
6972
;; If the message is already loaded, it means it's an update, that
@@ -83,8 +86,7 @@
8386
:cursor-clock-value clock-value)
8487

8588
;;conj sender for add-sender-to-chat-users
86-
(and (not (string/blank? alias))
87-
(not (get-in db [:chats chat-id :users from])))
89+
(not (get-in db [:chats chat-id :users from]))
8890
(update :senders assoc from message)
8991

9092
(not (string/blank? replace))

src/status_im/contact/block.cljs

+2-4
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
(let [contact (-> (contact.db/public-key->contact
4747
(:contacts/contacts db)
4848
public-key)
49-
(assoc :last-updated now)
50-
(update :system-tags (fnil conj #{}) :contact/blocked))
49+
(assoc :last-updated now :blocked true))
5150
from-one-to-one-chat? (not (get-in db [:chats (:current-chat-id db) :group-chat]))]
5251
(fx/merge cofx
5352
{:db (-> db
@@ -66,8 +65,7 @@
6665
{:events [:contact.ui/unblock-contact-pressed]}
6766
[{:keys [db now] :as cofx} public-key]
6867
(let [contact (-> (get-in db [:contacts/contacts public-key])
69-
(assoc :last-updated now)
70-
(update :system-tags disj :contact/blocked))]
68+
(assoc :last-updated now :blocked false))]
7169
(fx/merge cofx
7270
{:db (-> db
7371
(update :contacts/blocked disj public-key)

src/status_im/contact/core.cljs

+10-14
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@
4848
contacts))
4949
:dispatch-n (mapcat (fn [{:keys [public-key] :as contact}]
5050
(cond-> []
51-
(contact.db/added? contact)
51+
(:added contact)
5252
(conj [:start-profile-chat public-key])
5353

54-
(contact.db/removed? contact)
54+
(not (:added contact))
5555
(conj [:offload-messages constants/timeline-chat-id])
5656

57-
(contact.db/blocked? contact)
57+
(:blocked contact)
5858
(conj [::contact.block/contact-blocked contact chats])))
5959
contacts)})
6060

@@ -88,8 +88,7 @@
8888
(and nickname (not (string/blank? nickname)))
8989
(assoc :nickname nickname)
9090
:else
91-
(update :system-tags
92-
(fnil #(conj % :contact/added) #{})))]
91+
(assoc :added true))]
9392
(fx/merge cofx
9493
{:db (dissoc db :contacts/new-identity)
9594
:dispatch-n [[:start-profile-chat public-key]
@@ -100,15 +99,12 @@
10099
(fx/defn remove-contact
101100
"Remove a contact from current account's contact list"
102101
{:events [:contact.ui/remove-contact-pressed]}
103-
[{:keys [db] :as cofx} {:keys [public-key] :as contact}]
104-
(let [new-contact (update contact
105-
:system-tags
106-
(fnil #(disj % :contact/added) #{}))]
107-
{:db (assoc-in db [:contacts/contacts public-key] new-contact)
108-
::json-rpc/call [{:method "wakuext_removeContact"
109-
:params [public-key]
110-
:on-success #(log/debug "contact removed successfully")}]
111-
:dispatch [:offload-messages constants/timeline-chat-id]}))
102+
[{:keys [db]} {:keys [public-key]}]
103+
{:db (assoc-in db [:contacts/contacts public-key :added] false)
104+
::json-rpc/call [{:method "wakuext_removeContact"
105+
:params [public-key]
106+
:on-success #(log/debug "contact removed successfully")}]
107+
:dispatch [:offload-messages constants/timeline-chat-id]})
112108

113109
(fx/defn create-contact
114110
"Create entry in contacts"

src/status_im/contact/db.cljs

+16-21
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
{:alias alias
1313
:name alias
1414
:identicon (identicon/identicon public-key)
15-
:public-key public-key
16-
:system-tags #{}}))
15+
:public-key public-key}))
1716

1817
(defn public-key->contact
1918
[contacts public-key]
@@ -79,8 +78,6 @@
7978
([db public-key]
8079
(added? (get-in db [:contacts/contacts public-key]))))
8180

82-
(def removed? (complement added?))
83-
8481
(defn blocked?
8582
([{:keys [system-tags]}]
8683
(contains? system-tags :contact/blocked))
@@ -113,27 +110,25 @@
113110
(defn active?
114111
"Checks that the user is added to the contact and not blocked"
115112
([contact]
116-
(and (added? contact)
117-
(not (blocked? contact))))
113+
(and (:added contact)
114+
(not (:blocked contact))))
118115
([db public-key]
119116
(active? (get-in db [:contacts/contacts public-key]))))
120117

121118
(defn enrich-contact
122119
([contact] (enrich-contact contact nil nil))
123-
([{:keys [system-tags public-key] :as contact} setting own-public-key]
124-
(let [added? (contains? system-tags :contact/added)]
125-
(cond-> (-> contact
126-
(dissoc :ens-verified-at :ens-verification-retries)
127-
(assoc :pending? (pending? contact)
128-
:blocked? (blocked? contact)
129-
:active? (active? contact)
130-
:added? added?)
131-
(multiaccounts/contact-with-names))
132-
(and setting (not= public-key own-public-key)
133-
(or (= setting constants/profile-pictures-visibility-none)
134-
(and (= setting constants/profile-pictures-visibility-contacts-only)
135-
(not added?))))
136-
(dissoc :images)))))
120+
([{:keys [added public-key] :as contact} setting own-public-key]
121+
(cond-> (-> contact
122+
(dissoc :ens-verified-at :ens-verification-retries)
123+
(assoc :blocked? (:blocked contact)
124+
:active? (active? contact)
125+
:added? added)
126+
(multiaccounts/contact-with-names))
127+
(and setting (not= public-key own-public-key)
128+
(or (= setting constants/profile-pictures-visibility-none)
129+
(and (= setting constants/profile-pictures-visibility-contacts-only)
130+
(not added))))
131+
(dissoc :images))))
137132

138133
(defn enrich-contacts
139134
[contacts profile-pictures-visibility own-public-key]
@@ -145,7 +140,7 @@
145140
(defn get-blocked-contacts
146141
[contacts]
147142
(reduce (fn [acc {:keys [public-key] :as contact}]
148-
(if (blocked? contact)
143+
(if (:blocked contact)
149144
(conj acc public-key)
150145
acc))
151146
#{}

src/status_im/contact/db_test.cljs

+3-6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
:identicon "photo1",
2020
:last-online 0,
2121
:public-key
22-
"0x04985040682b77a32bb4bb58268a0719bd24ca4d07c255153fe1eb2ccd5883669627bd1a092d7cc76e8e4b9104327667b19dcda3ac469f572efabe588c38c1985f"
23-
:system-tags #{}}}
22+
"0x04985040682b77a32bb4bb58268a0719bd24ca4d07c255153fe1eb2ccd5883669627bd1a092d7cc76e8e4b9104327667b19dcda3ac469f572efabe588c38c1985f"}}
2423
current-multiaccount {:last-updated 0,
2524
:signed-up? true,
2625
:sharing-usage-data? false,
@@ -36,14 +35,12 @@
3635
:identicon "generated"
3736
:alias "generated"
3837
:admin? true
39-
:public-key "0x04fcf40c526b09ff9fb22f4a5dbd08490ef9b64af700870f8a0ba2133f4251d5607ed83cd9047b8c2796576bc83fa0de23a13a4dced07654b8ff137fe744047917"
40-
:system-tags #{}}
38+
:public-key "0x04fcf40c526b09ff9fb22f4a5dbd08490ef9b64af700870f8a0ba2133f4251d5607ed83cd9047b8c2796576bc83fa0de23a13a4dced07654b8ff137fe744047917"}
4139
{:alias "User A"
4240
:identicon "photo2"
4341
:public-key "0x048a2f8b80c60f89a91b4c1316e56f75b087f446e7b8701ceca06a40142d8efe1f5aa36bd0fee9e248060a8d5207b43ae98bef4617c18c71e66f920f324869c09f"}
4442
{:last-updated 0
4543
:name "User B"
4644
:identicon "photo1"
4745
:last-online 0
48-
:public-key "0x04985040682b77a32bb4bb58268a0719bd24ca4d07c255153fe1eb2ccd5883669627bd1a092d7cc76e8e4b9104327667b19dcda3ac469f572efabe588c38c1985f"
49-
:system-tags #{}}]))))))
46+
:public-key "0x04985040682b77a32bb4bb58268a0719bd24ca4d07c255153fe1eb2ccd5883669627bd1a092d7cc76e8e4b9104327667b19dcda3ac469f572efabe588c38c1985f"}]))))))

src/status_im/data_store/contacts.cljs

+14-24
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,22 @@
55
[taoensso.timbre :as log]))
66

77
(defn <-rpc [contact]
8-
(-> contact
9-
(update :systemTags
10-
#(reduce (fn [acc s]
11-
(conj acc (keyword (subs s 1))))
12-
#{}
13-
%))
14-
(clojure.set/rename-keys {:id :public-key
15-
:ensVerifiedAt :ens-verified-at
16-
:ensVerified :ens-verified
17-
:ensVerificationRetries :ens-verification-retries
18-
:lastENSClockValue :last-ens-clock-value
19-
:systemTags :system-tags
20-
:lastUpdated :last-updated
21-
:localNickname :nickname})))
8+
(clojure.set/rename-keys contact {:id :public-key
9+
:ensVerifiedAt :ens-verified-at
10+
:ensVerified :ens-verified
11+
:ensVerificationRetries :ens-verification-retries
12+
:lastENSClockValue :last-ens-clock-value
13+
:lastUpdated :last-updated
14+
:localNickname :nickname}))
2215

2316
(defn ->rpc [contact]
24-
(-> contact
25-
(update :system-tags #(mapv str %))
26-
(clojure.set/rename-keys {:public-key :id
27-
:ens-verified :ensVerified
28-
:ens-verified-at :ensVerifiedAt
29-
:last-ens-clock-value :lastENSClockValue
30-
:ens-verification-retries :ensVerificationRetries
31-
:system-tags :systemTags
32-
:last-updated :lastUpdated
33-
:nickname :localNickname})))
17+
(clojure.set/rename-keys contact {:public-key :id
18+
:ens-verified :ensVerified
19+
:ens-verified-at :ensVerifiedAt
20+
:last-ens-clock-value :lastENSClockValue
21+
:ens-verification-retries :ensVerificationRetries
22+
:last-updated :lastUpdated
23+
:nickname :localNickname}))
3424

3525
(fx/defn fetch-contacts-rpc
3626
[_ on-success]

src/status_im/data_store/contacts_test.cljs

+5-12
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,26 @@
77
:address "address"
88
:name "name"
99
:identicon "identicon"
10-
:last-updated 1
11-
:system-tags #{:a :b}}
10+
:last-updated 1}
1211
expected-contact {:id "pk"
1312
:address "address"
1413
:name "name"
1514
:identicon "identicon"
1615

17-
:lastUpdated 1
18-
:systemTags #{":a" ":b"}}]
16+
:lastUpdated 1}]
1917
(testing "->rpc"
20-
(is (= expected-contact (update
21-
(c/->rpc contact)
22-
:systemTags
23-
#(into #{} %)))))))
18+
(is (= expected-contact (c/->rpc contact))))))
2419

2520
(deftest contact<-rpc
2621
(let [contact {:id "pk"
2722
:address "address"
2823
:name "name"
2924
:identicon "identicon"
30-
:lastUpdated 1
31-
:systemTags [":a" ":b"]}
25+
:lastUpdated 1}
3226
expected-contact {:public-key "pk"
3327
:address "address"
3428
:name "name"
3529
:identicon "identicon"
36-
:last-updated 1
37-
:system-tags #{:a :b}}]
30+
:last-updated 1}]
3831
(testing "<-rpc"
3932
(is (= expected-contact (c/<-rpc contact))))))

0 commit comments

Comments
 (0)