From 93d5ebeb9e9fc89d157d56a45f4a56d3805e498d Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 4 Oct 2016 11:28:56 +0300 Subject: [PATCH] fix #308, #309 Former-commit-id: 00664f8975a4e8210e4ae64e92845b3f95dad88a --- src/status_im/contacts/views/new_contact.cljs | 31 +++++++++++++------ src/status_im/data_store/realm/core.cljs | 15 ++++----- src/status_im/translations/en.cljs | 1 + 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/status_im/contacts/views/new_contact.cljs b/src/status_im/contacts/views/new_contact.cljs index 231d1c7878..b92f7edc20 100644 --- a/src/status_im/contacts/views/new_contact.cljs +++ b/src/status_im/contacts/views/new_contact.cljs @@ -28,7 +28,8 @@ [status-im.i18n :refer [label]] [cljs.spec :as s] [status-im.contacts.validations :as v] - [status-im.contacts.styles :as st])) + [status-im.contacts.styles :as st] + [status-im.utils.hex :refer [normalize-hex]])) (def toolbar-title @@ -52,14 +53,23 @@ :photo-path (identicon id) :whisper-identity id}]))) -(defn- validation-error-message [whisper-identity error] +(defn- validation-error-message + [whisper-identity {:keys [address public-key]} error] (cond - (not (s/valid? ::v/unique-identity whisper-identity)) (label :t/contact-already-added) - (not (s/valid? ::v/whisper-identity whisper-identity)) (label :t/enter-valid-address) + (#{(normalize-hex address) (normalize-hex public-key)} + (normalize-hex whisper-identity)) + (label :t/can-not-add-yourself) + + (not (s/valid? ::v/unique-identity whisper-identity)) + (label :t/contact-already-added) + + (not (s/valid? ::v/whisper-identity whisper-identity)) + (label :t/enter-valid-address) + :else error)) -(defn toolbar-action [new-contact-identity error] - (let [error-message (validation-error-message new-contact-identity error)] +(defn toolbar-action [new-contact-identity account error] + (let [error-message (validation-error-message new-contact-identity account error)] {:image {:source {:uri (if (str/blank? error-message) :icon_ok_blue :icon_ok_disabled)} @@ -68,9 +78,9 @@ (on-add-contact new-contact-identity))})) (defview contact-whisper-id-input [whisper-identity error] - [] + [current-account [:get-current-account]] (let [error (when-not (str/blank? whisper-identity) - (validation-error-message whisper-identity error))] + (validation-error-message whisper-identity current-account error))] [view button-input-container [text-field {:error error @@ -88,7 +98,8 @@ (defview new-contact [] [new-contact-identity [:get :new-contact-identity] - error [:get :new-contact-address-error]] + error [:get :new-contact-address-error] + account [:get-current-account]] [view st/contact-form-container [view [status-bar] @@ -97,7 +108,7 @@ :style icon-back} :handler #(dispatch [:navigate-back])} :custom-content toolbar-title - :action (toolbar-action new-contact-identity error)}]] + :action (toolbar-action new-contact-identity account error)}]] [view st/form-container [contact-whisper-id-input new-contact-identity error]] [view st/address-explication-container diff --git a/src/status_im/data_store/realm/core.cljs b/src/status_im/data_store/realm/core.cljs index 040ace99b7..48178a66b8 100644 --- a/src/status_im/data_store/realm/core.cljs +++ b/src/status_im/data_store/realm/core.cljs @@ -156,12 +156,13 @@ (defmulti to-query (fn [realm schema-name operator field value] operator)) -(defmethod to-query :eq [realm schema-name operator field value] - (let [value (to-string value) - field-type (field-type realm schema-name field) - query (str (name field) "=" (if (= "string" (name field-type)) - (str "\"" value "\"") - value))] +(defmethod to-query :eq [schema schema-name operator field value] + (let [value (to-string value) + field-type (field-type schema schema-name field) + escaped-value (when value (str/replace (str value) #"\"" "\\\"")) + query (str (name field) "=" (if (= "string" (name field-type)) + (str "\"" escaped-value "\"") + value))] query)) (defn get-by-field [realm schema-name field value] @@ -184,4 +185,4 @@ :or (or-query queries))))) (defn exists? [realm schema-name fields] - (pos? (.-length (get-by-fields realm schema-name :and fields)))) \ No newline at end of file + (pos? (.-length (get-by-fields realm schema-name :and fields)))) diff --git a/src/status_im/translations/en.cljs b/src/status_im/translations/en.cljs index c811e34545..305c2202ae 100644 --- a/src/status_im/translations/en.cljs +++ b/src/status_im/translations/en.cljs @@ -157,6 +157,7 @@ :address-explication "Maybe here should be some text explaining what an address is and where to look for it" :enter-valid-address "Please enter a valid address or scan a QR code" :contact-already-added "The contact has already been added" + :can-not-add-yourself "You can't add yourself" :unknown-address "Unknown address"