Add display name

This commit is contained in:
Andrea Maria Piana 2022-11-10 18:23:14 +00:00
parent 61fd5fd791
commit 5e814b7a03
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
5 changed files with 128 additions and 30 deletions

View File

@ -6,8 +6,10 @@
(defn <-rpc [contact] (defn <-rpc [contact]
(-> contact (-> contact
(clojure.set/rename-keys {:id :public-key (clojure.set/rename-keys
{:id :public-key
:ensVerifiedAt :ens-verified-at :ensVerifiedAt :ens-verified-at
:displayName :display-name
:ensVerified :ens-verified :ensVerified :ens-verified
:ensVerificationRetries :ens-verification-retries :ensVerificationRetries :ens-verification-retries
:hasAddedUs :has-added-us :hasAddedUs :has-added-us

View File

@ -6,10 +6,12 @@
(let [contact {:id "pk" (let [contact {:id "pk"
:address "address" :address "address"
:name "name" :name "name"
:displayName "display-name"
:identicon "identicon" :identicon "identicon"
:lastUpdated 1} :lastUpdated 1}
expected-contact {:public-key "pk" expected-contact {:public-key "pk"
:address "address" :address "address"
:display-name "display-name"
:mutual? nil :mutual? nil
:name "name" :name "name"
:identicon "identicon" :identicon "identicon"

View File

@ -23,29 +23,36 @@
(defn contact-names (defn contact-names
"Returns map of all existing names for contact" "Returns map of all existing names for contact"
[{:keys [name preferred-name alias public-key ens-verified nickname]}] [{:keys [name
display-name
preferred-name
alias
public-key
ens-verified
nickname]}]
(let [ens-name (or preferred-name (let [ens-name (or preferred-name
name)] name)]
(cond-> {:nickname nickname (cond-> {:nickname nickname
:display-name display-name
:three-words-name (or alias (gfycat/generate-gfy public-key))} :three-words-name (or alias (gfycat/generate-gfy public-key))}
;; Preferred name is our own otherwise we make sure it's verified ;; Preferred name is our own otherwise we make sure it's verified
(or preferred-name (and ens-verified name)) (or preferred-name (and ens-verified name))
(assoc :ens-name (str "@" (or (stateofus/username ens-name) ens-name)))))) (assoc :ens-name (str "@" (or (stateofus/username ens-name) ens-name))))))
;; NOTE: this does a bit of unnecessary work, we could short-circuit the work
;; once the first two are found, i.e don't calculate short key if 2 are already
;; available
(defn contact-two-names (defn contact-two-names
"Returns vector of two names in next order nickname, ens name, three word name, public key" "Returns vector of two names in next order nickname, ens name, display-name, three word name, public key"
[{:keys [names public-key] :as contact} public-key?] [{:keys [names public-key] :as contact} public-key?]
(let [{:keys [nickname ens-name three-words-name]} (or names (contact-names contact)) (let [{:keys [nickname
ens-name
display-name
three-words-name]} (or names (contact-names contact))
short-public-key (when public-key? (utils/get-shortened-address public-key))] short-public-key (when public-key? (utils/get-shortened-address public-key))]
(cond (not (string/blank? nickname)) (->> [nickname ens-name display-name three-words-name short-public-key]
[nickname (or ens-name three-words-name short-public-key)] (remove string/blank?)
(not (string/blank? ens-name)) (take 2))))
[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 (defn contact-with-names
"Returns contact with :names map " "Returns contact with :names map "

View File

@ -0,0 +1,85 @@
(ns status-im.multiaccounts.core-test
(:require [cljs.test :refer-macros [deftest is testing]]
[status-im.multiaccounts.core :as ma]))
(def public-key "0x0461f576da67dc0bca9888cdb4cb28c80285b756b324109da94a081585ed6f007cf00afede6b3ee5638593674fee100b590318fc7bdb0054b8dd9445acea216ad2")
(def short-public-key "0x0461…ad2")
(def random-name "Studious Gold Mustang")
(def override-random-name (str "override" random-name))
(def nickname "nickname")
(def override-nickname (str nickname "override"))
(def display-name "display-name")
(def override-display-name (str display-name "override"))
(def ens-name "jakubgs.eth")
(def formatted-ens (str "@" ens-name))
(def override-ens-name (str "override" ens-name))
(def contact
{:nickname nickname
:name ens-name
:ens-verified true
:public-key public-key
:display-name display-name})
(deftest contact-two-names-test
(testing "names is nil"
(testing "nickname has precedence"
(is
(= [nickname formatted-ens]
(ma/contact-two-names contact public-key))))
(testing "ens name is second option"
(is
(= [formatted-ens display-name]
(ma/contact-two-names
(dissoc contact :nickname)
public-key))))
(testing "ens name is second option but not verified"
(is
(= [display-name random-name]
(ma/contact-two-names
(dissoc contact :nickname :ens-verified)
public-key))))
(testing "display name is third option"
(is
(= [display-name random-name]
(ma/contact-two-names
(dissoc contact :nickname :name)
public-key))))
(testing "3 random words is fallback"
(is
(= [random-name short-public-key]
(ma/contact-two-names
(dissoc contact
:nickname
:name
:display-name)
public-key)))))
(testing "names is provided"
(let [names {:nickname override-nickname
:display-name override-display-name
:three-words-name override-random-name
:ens-name override-ens-name}
contact-with-names (assoc contact :names names)]
(testing "nickname has precedence"
(is
(= [override-nickname override-ens-name]
(ma/contact-two-names contact-with-names public-key))))
(testing "ens name is second option"
(is
(= [override-ens-name override-display-name]
(ma/contact-two-names
(update contact-with-names :names dissoc :nickname)
public-key))))
(testing "display name is third option"
(is
(= [override-display-name override-random-name]
(ma/contact-two-names
(update contact-with-names :names dissoc :nickname :ens-name)
public-key))))
(testing "3 random words is fallback"
(is
(= [override-random-name short-public-key]
(ma/contact-two-names
(update contact-with-names :names dissoc :nickname :ens-name :display-name)
public-key)))))))

View File

@ -50,6 +50,8 @@
accounts-data)) accounts-data))
:logout (fn [] :logout (fn []
(.logout native-status)) (.logout native-status))
:generateAlias (fn [seed]
(.generateAlias native-status seed))
:generateAliasAndIdenticonAsync (fn [seed callback] :generateAliasAndIdenticonAsync (fn [seed callback]
(let [generated-identicon (.identicon native-status seed) (let [generated-identicon (.identicon native-status seed)
generated-alias (.generateAlias native-status seed)] generated-alias (.generateAlias native-status seed)]