Skip to content

Commit

Permalink
[#11046] Add local contact names
Browse files Browse the repository at this point in the history
Signed-off-by: andrey <motor4ik@gmail.com>
  • Loading branch information
flexsurfer committed Sep 7, 2020
1 parent fea9165 commit 4304a5d
Show file tree
Hide file tree
Showing 23 changed files with 375 additions and 205 deletions.
30 changes: 23 additions & 7 deletions src/status_im/contact/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[status-im.navigation :as navigation]
[status-im.utils.fx :as fx]
[status-im.waku.core :as waku]
[taoensso.timbre :as log]))
[taoensso.timbre :as log]
[clojure.string :as string]))

(fx/defn load-contacts
{:events [::contacts-loaded]}
Expand Down Expand Up @@ -59,7 +60,9 @@
[{:keys [db]} contacts]
{:db (update db :contacts/contacts
#(reduce (fn [acc {:keys [public-key] :as contact}]
(update acc public-key merge contact))
(-> acc
(update public-key merge contact)
(assoc-in [public-key :nickname] (:nickname contact))))
%
contacts))})

Expand All @@ -81,12 +84,15 @@

(fx/defn add-contact
"Add a contact and set pending to false"
[{:keys [db] :as cofx} public-key]
[{:keys [db] :as cofx} public-key nickname]
(when (not= (get-in db [:multiaccount :public-key]) public-key)
(let [contact (-> (get-in db [:contacts/contacts public-key]
(build-contact cofx public-key))
(update :system-tags
(fnil #(conj % :contact/added) #{})))]
(let [contact (cond-> (get-in db [:contacts/contacts public-key]
(build-contact cofx public-key))
nickname
(assoc :nickname nickname)
:else
(update :system-tags
(fnil #(conj % :contact/added) #{})))]
(fx/merge cofx
{:db (dissoc db :contacts/new-identity)}
(upsert-contact contact)
Expand Down Expand Up @@ -179,3 +185,13 @@
:ens-verified true})}

(upsert-contact {:public-key public-key})))

(fx/defn update-nickname
{:events [:contacts/update-nickname]}
[{:keys [db] :as cofx} public-key nickname]
(fx/merge cofx
{:db (if (string/blank? nickname)
(update-in db [:contacts/contacts public-key] dissoc :nickname)
(assoc-in db [:contacts/contacts public-key :nickname] nickname))}
(upsert-contact {:public-key public-key})
(navigation/navigate-back)))
6 changes: 4 additions & 2 deletions src/status_im/contact/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
[clojure.string :as clojure.string]
[status-im.ethereum.core :as ethereum]
[status-im.utils.gfycat.core :as gfycat]
[status-im.utils.identicon :as identicon]))
[status-im.utils.identicon :as identicon]
[status-im.multiaccounts.core :as multiaccounts]))

(defn public-key->new-contact [public-key]
(let [alias (gfycat/generate-gfy public-key)]
Expand Down Expand Up @@ -135,7 +136,8 @@
(assoc :pending? (pending? contact)
:blocked? (blocked? contact)
:active? (active? contact)
:added? (contains? system-tags :contact/added))))
:added? (contains? system-tags :contact/added))
(multiaccounts/contact-with-names)))

(defn enrich-contacts
[contacts]
Expand Down
28 changes: 14 additions & 14 deletions src/status_im/contact/db_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@
admins
contacts
current-multiaccount)
[{:name "generated"
:identicon "generated"
:alias "generated"
:admin? true
:public-key "0x04fcf40c526b09ff9fb22f4a5dbd08490ef9b64af700870f8a0ba2133f4251d5607ed83cd9047b8c2796576bc83fa0de23a13a4dced07654b8ff137fe744047917"
:system-tags #{}}
{:alias "User A"
:photo-path "photo2"
[{:name "generated"
:identicon "generated"
:alias "generated"
:admin? true
:public-key "0x04fcf40c526b09ff9fb22f4a5dbd08490ef9b64af700870f8a0ba2133f4251d5607ed83cd9047b8c2796576bc83fa0de23a13a4dced07654b8ff137fe744047917"
:system-tags #{}}
{:alias "User A"
:photo-path "photo2"
:public-key "0x048a2f8b80c60f89a91b4c1316e56f75b087f446e7b8701ceca06a40142d8efe1f5aa36bd0fee9e248060a8d5207b43ae98bef4617c18c71e66f920f324869c09f"}
{:last-updated 0
:name "User B"
:photo-path "photo1"
:last-online 0
:public-key "0x04985040682b77a32bb4bb58268a0719bd24ca4d07c255153fe1eb2ccd5883669627bd1a092d7cc76e8e4b9104327667b19dcda3ac469f572efabe588c38c1985f"
:system-tags #{}}]))))))
{:last-updated 0
:name "User B"
:photo-path "photo1"
:last-online 0
:public-key "0x04985040682b77a32bb4bb58268a0719bd24ca4d07c255153fe1eb2ccd5883669627bd1a092d7cc76e8e4b9104327667b19dcda3ac469f572efabe588c38c1985f"
:system-tags #{}}]))))))
6 changes: 4 additions & 2 deletions src/status_im/data_store/contacts.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
:ensVerificationRetries :ens-verification-retries
:lastENSClockValue :last-ens-clock-value
:systemTags :system-tags
:lastUpdated :last-updated})))
:lastUpdated :last-updated
:localNickname :nickname})))

(defn ->rpc [contact]
(-> contact
Expand All @@ -41,7 +42,8 @@
:photo-path :photoPath
:tribute-to-talk :tributeToTalk
:system-tags :systemTags
:last-updated :lastUpdated})))
:last-updated :lastUpdated
:nickname :localNickname})))

(fx/defn fetch-contacts-rpc
[cofx on-success]
Expand Down
6 changes: 3 additions & 3 deletions src/status_im/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@
:contact.ui/add-to-contact-pressed
[(re-frame/inject-cofx :random-id-generator)]
(fn [cofx [_ public-key]]
(contact/add-contact cofx public-key)))
(contact/add-contact cofx public-key nil)))

(handlers/register-handler-fx
:contact.ui/block-contact-confirmed
Expand All @@ -842,11 +842,11 @@
(handlers/register-handler-fx
:contact.ui/contact-code-submitted
[(re-frame/inject-cofx :random-id-generator)]
(fn [{{:contacts/keys [new-identity]} :db :as cofx} [_ new-contact?]]
(fn [{{:contacts/keys [new-identity]} :db :as cofx} [_ new-contact? nickname]]
(let [{:keys [public-key ens-name]} new-identity]
(fx/merge cofx
#(if new-contact?
(contact/add-contact % public-key)
(contact/add-contact % public-key nickname)
(chat/start-chat % public-key))
#(when new-contact?
(navigation/navigate-back %))
Expand Down
35 changes: 34 additions & 1 deletion src/status_im/multiaccounts/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,40 @@
[status-im.utils.gfycat.core :as gfycat]
[status-im.utils.identicon :as identicon]
[status-im.utils.theme :as utils.theme]
[status-im.theme.core :as theme]))
[status-im.theme.core :as theme]
[status-im.utils.utils :as utils]
[clojure.string :as string]))

(defn contact-names
"Returns map of all existing names for contact"
[{:keys [name preferred-name alias public-key ens-verified nickname]}]
(let [ens-name (or preferred-name
name)]
(cond-> {:nickname nickname
:three-words-name (or alias (gfycat/generate-gfy public-key))}
;; Preferred name is our own otherwise we make sure it's verified
(or preferred-name (and ens-verified name))
(assoc :ens-name (str "@" (or (stateofus/username ens-name) ens-name))))))

(defn contact-two-names
"Returns vector of two names in next order nickname, ens name, three word name, public key"
[{:keys [names public-key] :as contact} public-key?]
(let [{:keys [nickname ens-name three-words-name]} (or names (contact-names contact))
short-public-key (when public-key? (utils/get-shortened-address public-key))]
(cond (not (string/blank? nickname))
[nickname (or ens-name three-words-name short-public-key)]
(not (string/blank? ens-name))
[ens-name (or three-words-name short-public-key)]
(not (string/blank? three-words-name))
[three-words-name short-public-key]
:else
(when public-key?
[short-public-key short-public-key]))))

(defn contact-with-names
"Returns contact with :names map "
[contact]
(assoc contact :names (contact-names contact)))

(defn displayed-name
"Use preferred name, name or alias in that order"
Expand Down
4 changes: 3 additions & 1 deletion src/status_im/reloader.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require [reagent.core :as reagent]
[status-im.ui.components.react :as react]
[status-im.react-native.resources :as resources]
[status-im.ui.components.colors :as colors]))
[status-im.ui.components.colors :as colors]
[re-frame.core :as re-frame]))

(def cnt (reagent/atom 0))
(defonce cnt-prev (reagent/atom 0))
Expand All @@ -14,6 +15,7 @@
(defn reload []
(reset! warning? false)
(reset! label "reloading UI")
(re-frame/clear-subscription-cache!)
(swap! cnt inc))

(defn build-competed []
Expand Down
45 changes: 22 additions & 23 deletions src/status_im/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1543,16 +1543,17 @@
:<- [:contacts/contacts]
:<- [:contacts/current-contact-identity]
(fn [[contacts identity]]
(or (contacts identity)
(or (get contacts identity)
(-> identity
contact.db/public-key->new-contact
contact.db/enrich-contact))))

(re-frame/reg-sub
:contacts/contact-by-identity
:<- [::contacts]
:<- [:contacts/contacts]
(fn [contacts [_ identity]]
(get contacts identity)))
(or (get contacts identity)
(multiaccounts/contact-with-names {:public-key identity}))))

(re-frame/reg-sub
:contacts/contact-added?
Expand All @@ -1562,27 +1563,23 @@
(contact.db/added? contact)))

(re-frame/reg-sub
:contacts/raw-contact-name-by-identity
:contacts/contact-two-names-by-identity
(fn [[_ identity] _]
[(re-frame/subscribe [:contacts/contact-by-identity identity])])
(fn [[db-contact] _]
(if (and (:ens-verified db-contact) (seq (:name db-contact)))
(str "@" (:name db-contact))
(:alias db-contact))))
[(re-frame/subscribe [:contacts/contact-by-identity identity])
(re-frame/subscribe [:multiaccount])])
(fn [[contact current-multiaccount] [_ identity]]
(let [me? (= (:public-key current-multiaccount) identity)]
(if me?
[(or (:preferred-name current-multiaccount)
(gfycat/generate-gfy identity))]
(multiaccounts/contact-two-names contact false)))))

(re-frame/reg-sub
:contacts/contact-name-by-identity
(fn [[_ identity] _]
[(re-frame/subscribe [:contacts/raw-contact-name-by-identity identity])
(re-frame/subscribe [:multiaccount])])
(fn [[contact-name current-multiaccount] [_ identity]]
(let [me? (= (:public-key current-multiaccount) identity)]
(if me?
(or (:preferred-name current-multiaccount)
(gfycat/generate-gfy identity))
(or (stateofus/username contact-name)
contact-name
(gfycat/generate-gfy identity))))))
[(re-frame/subscribe [:contacts/contact-two-names-by-identity identity])])
(fn [[names] _]
(first names)))

(re-frame/reg-sub
:messages/quote-info
Expand Down Expand Up @@ -1612,8 +1609,7 @@
:<- [::query-current-chat-contacts remove]
(fn [contacts]
(->> contacts
(filter contact.db/added?)
(sort-by (comp clojure.string/lower-case multiaccounts/displayed-name)))))
(filter contact.db/added?))))

(re-frame/reg-sub
:contacts/current-chat-contacts
Expand Down Expand Up @@ -1764,16 +1760,19 @@
(string/lower-case (or alias
(get-in contacts [chat-id :alias])
(gfycat/generate-gfy chat-id)))
"")]

"")
nickname (get-in contacts [chat-id :nickname])]
(or
(string/includes? (string/lower-case (str name)) search-filter)
(string/includes? (string/lower-case alias) search-filter)
(when nickname
(string/includes? (string/lower-case nickname) search-filter))
(and
(get-in contacts [chat-id :ens-verified])
(string/includes? (string/lower-case
(str (get-in contacts [chat-id :name])))
search-filter)))))

(re-frame/reg-sub
:search/filtered-chats
:<- [:chats/active-chats]
Expand Down
8 changes: 0 additions & 8 deletions src/status_im/ui/components/contact/contact.cljs

This file was deleted.

2 changes: 1 addition & 1 deletion src/status_im/ui/screens/add_new/new_chat/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
(if-not validation-result
(if new-contact?
(fx/merge cofx
(contact/add-contact chat-key)
(contact/add-contact chat-key nil)
(navigation/navigate-to-cofx :contacts-list {}))
(chat/start-chat cofx chat-key))
{:utils/show-popup {:title (i18n/label :t/unable-to-read-this-code)
Expand Down
Loading

0 comments on commit 4304a5d

Please sign in to comment.