From d08ca05d338b5e2a171aa9d4a34641067334bbac Mon Sep 17 00:00:00 2001 From: alwx Date: Mon, 16 Jan 2017 15:41:02 +0300 Subject: [PATCH] Add to Contacts tab does not appear (#636) --- src/status_im/chat/screen.cljs | 4 +- .../components/chat_icon/screen.cljs | 2 +- .../components/sync_state/offline.cljs | 2 +- src/status_im/contacts/handlers.cljs | 8 +--- src/status_im/contacts/styles.cljs | 6 ++- src/status_im/contacts/subs.cljs | 8 +++- src/status_im/contacts/validations.cljs | 2 +- src/status_im/data_store/contacts.cljs | 12 +++--- .../realm/schemas/account/core.cljs | 8 +++- .../realm/schemas/account/v2/chat.cljs | 38 +++++++++++++++++++ .../realm/schemas/account/v2/contact.cljs | 26 +++++++++++++ .../realm/schemas/account/v2/core.cljs | 32 ++++++++++++++++ src/status_im/protocol/handlers.cljs | 34 ++++++++--------- src/status_im/utils/handlers.cljs | 4 +- 14 files changed, 144 insertions(+), 42 deletions(-) create mode 100644 src/status_im/data_store/realm/schemas/account/v2/chat.cljs create mode 100644 src/status_im/data_store/realm/schemas/account/v2/contact.cljs create mode 100644 src/status_im/data_store/realm/schemas/account/v2/core.cljs diff --git a/src/status_im/chat/screen.cljs b/src/status_im/chat/screen.cljs index cb3d6b064b..86b2d74226 100644 --- a/src/status_im/chat/screen.cljs +++ b/src/status_im/chat/screen.cljs @@ -98,8 +98,8 @@ [chat-icon]]])))) (defview add-contact-bar [] - [pending-contact? [:chat :pending-contact?] - chat-id [:get :current-chat-id]] + [chat-id [:get :current-chat-id] + pending-contact? [:current-contact :pending?]] (when pending-contact? [touchable-highlight {:on-press #(dispatch [:add-pending-contact chat-id])} diff --git a/src/status_im/components/chat_icon/screen.cljs b/src/status_im/components/chat_icon/screen.cljs index dc79e7c218..4d943e4a02 100644 --- a/src/status_im/components/chat_icon/screen.cljs +++ b/src/status_im/components/chat_icon/screen.cljs @@ -47,7 +47,7 @@ (defview pending-contact-badge [chat-id {:keys [pending-wrapper pending-outer-circle pending-inner-circle]}] - [pending-contact? [:get-in [:chats chat-id :pending-contact?]]] + [pending-contact? [:get-in [:contacts chat-id :pending?]]] (when pending-contact? [view pending-wrapper [view pending-outer-circle diff --git a/src/status_im/components/sync_state/offline.cljs b/src/status_im/components/sync_state/offline.cljs index 885abbb8fc..e6cfd64fe3 100644 --- a/src/status_im/components/sync_state/offline.cljs +++ b/src/status_im/components/sync_state/offline.cljs @@ -25,7 +25,7 @@ (anim/set-value offline-opacity 0) (when (or (= @network-status :offline) (= @sync-state :offline)) (start-offline-animation offline-opacity))) - pending-contact? (subscribe [:chat :pending-contact?]) + pending-contact? (subscribe [:current-contact :pending?]) view-id (subscribe [:get :view-id])] (r/create-class {:component-did-mount diff --git a/src/status_im/contacts/handlers.cljs b/src/status_im/contacts/handlers.cljs index 1292bfb08d..2bfd27f52c 100644 --- a/src/status_im/contacts/handlers.cljs +++ b/src/status_im/contacts/handlers.cljs @@ -15,7 +15,6 @@ [taoensso.timbre :as log] [cljs.reader :refer [read-string]])) - (defmethod nav/preload-data! :group-contacts [db [_ _ group]] (dissoc @@ -216,12 +215,9 @@ (fn [{:keys [chats contacts]} [_ chat-id]] (let [contact (if-let [contact-info (get-in chats [chat-id :contact-info])] (read-string contact-info) - (-> contacts - (get chat-id) + (-> (get contacts chat-id) (assoc :pending false)))] (dispatch [::prepare-contact contact]) - (dispatch [:update-chat! {:chat-id chat-id - :pending-contact? false}]) (dispatch [:watch-contact contact]) (dispatch [:discoveries-send-portions chat-id]))))) @@ -279,8 +275,6 @@ (after stop-watching-contact) (u/side-effect! (fn [_ [_ {:keys [whisper-identity] :as contact}]] - (dispatch [:update-chat! {:chat-id whisper-identity - :pending-contact? true}]) (dispatch [:update-contact! (assoc contact :pending true)]) (dispatch [:account-update-keys])))) diff --git a/src/status_im/contacts/styles.cljs b/src/status_im/contacts/styles.cljs index 100b7da558..3867077c30 100644 --- a/src/status_im/contacts/styles.cljs +++ b/src/status_im/contacts/styles.cljs @@ -8,7 +8,8 @@ color-light-gray color-separator color-gray2]] - [status-im.components.toolbar.styles :refer [toolbar-background2]])) + [status-im.components.toolbar.styles :refer [toolbar-background2]] + [status-im.utils.platform :as p])) ;; Contacts list @@ -21,7 +22,8 @@ :background-color toolbar-background2}) (def contacts-list-container - {:flex 1}) + {:flex 1 + :margin-bottom (when p/ios? 72)}) (def empty-contact-groups (merge contact-groups diff --git a/src/status_im/contacts/subs.cljs b/src/status_im/contacts/subs.cljs index e62ea1dba0..cc884b8f97 100644 --- a/src/status_im/contacts/subs.cljs +++ b/src/status_im/contacts/subs.cljs @@ -3,6 +3,12 @@ (:require [re-frame.core :refer [register-sub subscribe]] [status-im.utils.identicon :refer [identicon]])) +(register-sub :current-contact + (fn [db [_ k]] + (-> @db + (get-in [:contacts (:current-chat-id @db) k]) + (reaction)))) + (register-sub :get-contacts (fn [db _] (let [contacts (reaction (:contacts @db))] @@ -19,7 +25,7 @@ (register-sub :all-added-contacts (fn [db _] (let [contacts (reaction (:contacts @db))] - (->> (remove #(true? (:pending (second %))) @contacts) + (->> (remove #(true? (:pending? (second %))) @contacts) (sort-contacts) (reaction))))) diff --git a/src/status_im/contacts/validations.cljs b/src/status_im/contacts/validations.cljs index 62bdb6a686..35b33bca70 100644 --- a/src/status_im/contacts/validations.cljs +++ b/src/status_im/contacts/validations.cljs @@ -9,7 +9,7 @@ (defn contact-can-be-added? [identity] (if (contacts/exists? identity) - (:pending (contacts/get-by-id identity)) + (:pending? (contacts/get-by-id identity)) true)) (defn valid-length? [identity] diff --git a/src/status_im/data_store/contacts.cljs b/src/status_im/data_store/contacts.cljs index 7936fc29d1..2a43a4e17e 100644 --- a/src/status_im/data_store/contacts.cljs +++ b/src/status_im/data_store/contacts.cljs @@ -11,13 +11,13 @@ (data-store/get-by-id-cljs whisper-identity)) (defn save - [{:keys [whisper-identity pending] :as contact}] - (let [{pending-db :pending - :as contact-db} (data-store/get-by-id whisper-identity) - contact (assoc contact :pending + [{:keys [whisper-identity pending?] :as contact}] + (let [{pending-db? :pending? + :as contact-db} (data-store/get-by-id whisper-identity) + contact (assoc contact :pending? (boolean (if contact-db - (if (nil? pending) pending-db pending) - pending)))] + (if (nil? pending?) pending-db? pending?) + pending?)))] (data-store/save contact (if contact-db true false)))) (defn save-all diff --git a/src/status_im/data_store/realm/schemas/account/core.cljs b/src/status_im/data_store/realm/schemas/account/core.cljs index 393dd465ca..d1bf440c1c 100644 --- a/src/status_im/data_store/realm/schemas/account/core.cljs +++ b/src/status_im/data_store/realm/schemas/account/core.cljs @@ -1,7 +1,11 @@ (ns status-im.data-store.realm.schemas.account.core - (:require [status-im.data-store.realm.schemas.account.v1.core :as v1])) + (:require [status-im.data-store.realm.schemas.account.v1.core :as v1] + [status-im.data-store.realm.schemas.account.v2.core :as v2])) ; put schemas ordered by version (def schemas [{:schema v1/schema :schemaVersion 1 - :migration v1/migration}]) + :migration v1/migration} + {:schema v2/schema + :schemaVersion 2 + :migration v2/migration}]) diff --git a/src/status_im/data_store/realm/schemas/account/v2/chat.cljs b/src/status_im/data_store/realm/schemas/account/v2/chat.cljs new file mode 100644 index 0000000000..028f7305e6 --- /dev/null +++ b/src/status_im/data_store/realm/schemas/account/v2/chat.cljs @@ -0,0 +1,38 @@ +(ns status-im.data-store.realm.schemas.account.v2.chat + (:require [taoensso.timbre :as log] + [status-im.components.styles :refer [default-chat-color]])) + +(def schema {:name :chat + :primaryKey :chat-id + :properties {:chat-id :string + :name :string + :color {:type :string + :default default-chat-color} + :group-chat {:type :bool + :indexed true} + :group-admin {:type :string + :optional true} + :is-active :bool + :timestamp :int + :contacts {:type :list + :objectType :chat-contact} + :removed-at {:type :int + :optional true} + :removed-from-at {:type :int + :optional true} + :added-to-at {:type :int + :optional true} + :updated-at {:type :int + :optional true} + :last-message-id :string + :message-overhead {:type :int + :default 0} + :public-key {:type :string + :optional true} + :private-key {:type :string + :optional true} + :contact-info {:type :string + :optional true}}}) + +(defn migration [old-realm new-realm] + (log/debug "migrating chat schema v2")) diff --git a/src/status_im/data_store/realm/schemas/account/v2/contact.cljs b/src/status_im/data_store/realm/schemas/account/v2/contact.cljs new file mode 100644 index 0000000000..e7868b1c2f --- /dev/null +++ b/src/status_im/data_store/realm/schemas/account/v2/contact.cljs @@ -0,0 +1,26 @@ +(ns status-im.data-store.realm.schemas.account.v2.contact + (:require [taoensso.timbre :as log])) + +(def schema {:name :contact + :primaryKey :whisper-identity + :properties {:address {:type "string" :optional true} + :whisper-identity "string" + :name {:type "string" :optional true} + :photo-path {:type "string" :optional true} + :last-updated {:type "int" :default 0} + :last-online {:type "int" :default 0} + :pending? {:type "bool" :default false} + :status {:type "string" :optional true} + :public-key {:type :string + :optional true} + :private-key {:type :string + :optional true} + :dapp? {:type :bool + :default false} + :dapp-url {:type :string + :optional true} + :dapp-hash {:type :int + :optional true}}}) + +(defn migration [old-realm new-realm] + (log/debug "migrating contact schema v2")) diff --git a/src/status_im/data_store/realm/schemas/account/v2/core.cljs b/src/status_im/data_store/realm/schemas/account/v2/core.cljs new file mode 100644 index 0000000000..3cdb8d876b --- /dev/null +++ b/src/status_im/data_store/realm/schemas/account/v2/core.cljs @@ -0,0 +1,32 @@ +(ns status-im.data-store.realm.schemas.account.v2.core + (:require [status-im.data-store.realm.schemas.account.v2.chat :as chat] + [status-im.data-store.realm.schemas.account.v1.chat-contact :as chat-contact] + [status-im.data-store.realm.schemas.account.v1.command :as command] + [status-im.data-store.realm.schemas.account.v2.contact :as contact] + [status-im.data-store.realm.schemas.account.v1.discover :as discover] + [status-im.data-store.realm.schemas.account.v1.kv-store :as kv-store] + [status-im.data-store.realm.schemas.account.v1.message :as message] + [status-im.data-store.realm.schemas.account.v1.pending-message :as pending-message] + [status-im.data-store.realm.schemas.account.v1.processed-message :as processed-message] + [status-im.data-store.realm.schemas.account.v1.request :as request] + [status-im.data-store.realm.schemas.account.v1.tag :as tag] + [status-im.data-store.realm.schemas.account.v1.user-status :as user-status] + [taoensso.timbre :as log])) + +(def schema [chat/schema + chat-contact/schema + command/schema + contact/schema + discover/schema + kv-store/schema + message/schema + pending-message/schema + processed-message/schema + request/schema + tag/schema + user-status/schema]) + +(defn migration [old-realm new-realm] + (log/debug "migrating v2 account database: " old-realm new-realm) + (chat/migration old-realm new-realm) + (contact/migration old-realm new-realm)) diff --git a/src/status_im/protocol/handlers.cljs b/src/status_im/protocol/handlers.cljs index d3cfe40fe5..706998a39a 100644 --- a/src/status_im/protocol/handlers.cljs +++ b/src/status_im/protocol/handlers.cljs @@ -385,24 +385,24 @@ (when from (let [{{:keys [name profile-image address status]} :contact {:keys [public private]} :keypair} payload - - contact {:whisper-identity from - :public-key public - :private-key private - :address address - :status status - :photo-path profile-image - :name name} - contact-exists? (get contacts from) - chat {:name name - :chat-id from - :contact-info (prn-str contact) - :pending-contact? true}] - (if contact-exists? - (do + existing-contact (get contacts from) + contact {:whisper-identity from + :public-key public + :private-key private + :address address + :status status + :photo-path profile-image + :name name} + chat {:name name + :chat-id from + :contact-info (prn-str contact)}] + (if-not existing-contact + (let [contact (assoc contact :pending? true)] + (dispatch [:add-contacts [contact]]) + (dispatch [:add-chat from chat])) + (when-not (:pending? existing-contact) (dispatch [:update-contact! contact]) - (dispatch [:watch-contact contact])) - (dispatch [:add-chat from chat]))))))) + (dispatch [:watch-contact contact])))))))) (register-handler ::post-error (u/side-effect! diff --git a/src/status_im/utils/handlers.cljs b/src/status_im/utils/handlers.cljs index 7b42a9ffcf..08c66bad72 100644 --- a/src/status_im/utils/handlers.cljs +++ b/src/status_im/utils/handlers.cljs @@ -35,6 +35,6 @@ (defn identities [contacts] (->> (map second contacts) - (remove (fn [{:keys [dapp? pending]}] - (or pending dapp?))) + (remove (fn [{:keys [dapp? pending?]}] + (or pending? dapp?))) (map :whisper-identity)))