From 8c6dde8e067ce7369c93c6296eb34a6d5f1b76d3 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Mon, 12 Mar 2018 23:20:32 +0000 Subject: [PATCH] [ISSUE #3520] Allow adding/chatting with contacts from public chats Signed-off-by: Julien Eluard --- src/status_im/chat/screen.cljs | 2 +- src/status_im/ui/screens/contacts/events.cljs | 50 +++++++------------ src/status_im/ui/screens/contacts/subs.cljs | 11 ++-- .../ui/screens/profile/contact/views.cljs | 35 +++++++------ src/status_im/utils/contacts.cljs | 10 ++++ test/cljs/status_im/test/contacts/events.cljs | 9 ++-- 6 files changed, 62 insertions(+), 55 deletions(-) create mode 100644 src/status_im/utils/contacts.cljs diff --git a/src/status_im/chat/screen.cljs b/src/status_im/chat/screen.cljs index eff001d447..e4a9ac02f4 100644 --- a/src/status_im/chat/screen.cljs +++ b/src/status_im/chat/screen.cljs @@ -24,7 +24,7 @@ pending-contact? [:current-contact :pending?]] (when pending-contact? [react/touchable-highlight - {:on-press #(re-frame/dispatch [:add-pending-contact chat-id])} + {:on-press #(re-frame/dispatch [:add-contact chat-id])} [react/view style/add-contact [react/text {:style style/add-contact-text} (i18n/label :t/add-to-contacts)]]]))) diff --git a/src/status_im/ui/screens/contacts/events.cljs b/src/status_im/ui/screens/contacts/events.cljs index 235af74737..14796c1c5b 100644 --- a/src/status_im/ui/screens/contacts/events.cljs +++ b/src/status_im/ui/screens/contacts/events.cljs @@ -4,6 +4,7 @@ [status-im.data-store.contacts :as contacts] [clojure.string :as s] [status-im.protocol.core :as protocol] + [status-im.utils.contacts :as utils.contacts] [status-im.utils.utils :refer [http-post]] [status-im.utils.random :as random] [taoensso.timbre :as log] @@ -11,8 +12,6 @@ [status-im.utils.js-resources :as js-res] [status-im.react-native.js-dependencies :as rn-dependencies] [status-im.js-dependencies :as dependencies] - [status-im.utils.identicon :refer [identicon]] - [status-im.utils.gfycat.core :refer [generate-gfy]] [status-im.i18n :refer [label]] [status-im.ui.screens.contacts.navigation] [status-im.ui.screens.navigation :as navigation] @@ -259,21 +258,12 @@ (assoc-in [:db :contacts/new-identity] "") (assoc ::save-contact contact))) -(register-handler-fx - :add-new-contact-and-open-chat - (fn [{:keys [db]} [_ {:keys [whisper-identity] :as contact}]] - (when-not (get-in db [:contacts/contacts whisper-identity]) - (let [contact (assoc contact :address (public-key->address whisper-identity))] - (-> {:db db} - (add-new-contact contact) - (update :db #(navigation/navigate-to-clean % :home)) - (assoc :dispatch [:start-chat whisper-identity {:navigation-replace? true}])))))) - -(defn add-pending-contact [{:keys [db] :as fx} chat-or-whisper-id] +(defn add-contact [{:keys [db] :as fx} chat-or-whisper-id] (let [{:keys [chats] :contacts/keys [contacts]} db contact (if-let [contact-info (get-in chats [chat-or-whisper-id :contact-info])] (read-string contact-info) - (get contacts chat-or-whisper-id)) + (or (get contacts chat-or-whisper-id) + (utils.contacts/whisper-id->new-contact chat-or-whisper-id))) contact' (assoc contact :address (public-key->address chat-or-whisper-id) :pending? false)] @@ -282,18 +272,21 @@ (discover-events/send-portions-when-contact-exists chat-or-whisper-id) (add-new-contact contact')))) -(register-handler-fx - :add-pending-contact - (fn [{:keys [db]} [_ chat-or-whisper-id]] - (add-pending-contact {:db db} chat-or-whisper-id))) +(defn add-contact-and-open-chat [fx whisper-id] + (-> fx + (add-contact whisper-id) + (update :db #(navigation/navigate-to-clean % :home)) + (assoc :dispatch [:start-chat whisper-id {:navigation-replace? true}]))) (register-handler-fx - :add-pending-contact-and-open-chat + :add-contact + (fn [{:keys [db]} [_ chat-or-whisper-id]] + (add-contact {:db db} chat-or-whisper-id))) + +(register-handler-fx + :add-contact-and-open-chat (fn [{:keys [db]} [_ whisper-id]] - (-> {:db db} - (add-pending-contact whisper-id) - (update :db #(navigation/navigate-to-clean % :home)) - (assoc :dispatch [:start-chat whisper-id {:navigation-replace? true}])))) + (add-contact-and-open-chat {:db db} whisper-id))) (register-handler-fx :set-contact-identity-from-qr @@ -383,11 +376,6 @@ (register-handler-fx :add-contact-handler - (fn [{:keys [db]}] - (let [{:contacts/keys [new-identity]} db] - (when (and new-identity (not (string/blank? new-identity))) - {:dispatch (if (get-in db [:contacts/contacts new-identity]) - [:add-pending-contact-and-open-chat new-identity] - [:add-new-contact-and-open-chat {:name (generate-gfy new-identity) - :photo-path (identicon new-identity) - :whisper-identity new-identity}])})))) + (fn [{{:contacts/keys [new-identity] :as db} :db}] + (when (seq new-identity) + (add-contact-and-open-chat {:db db} new-identity)))) diff --git a/src/status_im/ui/screens/contacts/subs.cljs b/src/status_im/ui/screens/contacts/subs.cljs index 7d64810892..5f9c2b2ebe 100644 --- a/src/status_im/ui/screens/contacts/subs.cljs +++ b/src/status_im/ui/screens/contacts/subs.cljs @@ -98,10 +98,15 @@ (->> (remove :pending? (vals groups)) (sort-by :order >)))) -(reg-sub :contact +(reg-sub :current-contact-identity (fn [db] - (let [identity (:contacts/identity db)] - (get-in db [:contacts/contacts identity])))) + (:contacts/identity db))) + +(reg-sub :contact + :<- [:get-contacts] + :<- [:current-contact-identity] + (fn [[contacts identity]] + (contacts identity))) (reg-sub :contact-by-identity (fn [db [_ identity]] diff --git a/src/status_im/ui/screens/profile/contact/views.cljs b/src/status_im/ui/screens/profile/contact/views.cljs index 57582b45ed..770f6c7ea2 100644 --- a/src/status_im/ui/screens/profile/contact/views.cljs +++ b/src/status_im/ui/screens/profile/contact/views.cljs @@ -7,6 +7,7 @@ [status-im.ui.components.status-bar.view :as status-bar] [status-im.i18n :as i18n] [re-frame.core :as re-frame] + [status-im.utils.contacts :as utils.contacts] [status-im.ui.components.toolbar.view :as toolbar] [status-im.ui.components.list.views :as list])) @@ -19,7 +20,7 @@ (concat (if pending? [{:label (i18n/label :t/add-to-contacts) :icon :icons/add-contact - :action #(re-frame/dispatch [:add-pending-contact chat-id])}] + :action #(re-frame/dispatch [:add-contact whisper-identity])}] [{:label (i18n/label :t/in-contacts) :icon :icons/in-contacts :disabled? true}]) @@ -52,19 +53,21 @@ [profile-info-contact-code-item whisper-identity]]) (defview profile [] - (letsubs [contact [:contact] + (letsubs [identity [:current-contact-identity] + maybe-contact [:contact] chat-id [:get :current-chat-id]] - [react/view profile.components.styles/profile - [status-bar/status-bar] - [profile-contact-toolbar] - [react/scroll-view - [react/view profile.components.styles/profile-form - [profile.components/profile-header contact false false nil nil]] - [list/action-list (actions contact chat-id) - {:container-style styles/action-container - :action-style styles/action - :action-label-style styles/action-label - :action-separator-style styles/action-separator - :icon-opts styles/action-icon-opts}] - [react/view styles/contact-profile-info-container - [profile-info contact]]]])) \ No newline at end of file + (let [contact (or maybe-contact (utils.contacts/whisper-id->new-contact identity))] + [react/view profile.components.styles/profile + [status-bar/status-bar] + [profile-contact-toolbar] + [react/scroll-view + [react/view profile.components.styles/profile-form + [profile.components/profile-header contact false false nil nil]] + [list/action-list (actions contact chat-id) + {:container-style styles/action-container + :action-style styles/action + :action-label-style styles/action-label + :action-separator-style styles/action-separator + :icon-opts styles/action-icon-opts}] + [react/view styles/contact-profile-info-container + [profile-info contact]]]]))) diff --git a/src/status_im/utils/contacts.cljs b/src/status_im/utils/contacts.cljs new file mode 100644 index 0000000000..af3b6da1a5 --- /dev/null +++ b/src/status_im/utils/contacts.cljs @@ -0,0 +1,10 @@ +(ns status-im.utils.contacts + (:require + [status-im.utils.identicon :as identicon] + [status-im.utils.gfycat.core :as gfycat])) + +(defn whisper-id->new-contact [whisper-id] + {:name (gfycat/generate-gfy whisper-id) + :photo-path (identicon/identicon whisper-id) + :pending? true + :whisper-identity whisper-id}) diff --git a/test/cljs/status_im/test/contacts/events.cljs b/test/cljs/status_im/test/contacts/events.cljs index 7ce74760a0..fee9c20660 100644 --- a/test/cljs/status_im/test/contacts/events.cljs +++ b/test/cljs/status_im/test/contacts/events.cljs @@ -110,7 +110,7 @@ contact-request-received (update-contact, watch-contact ;TODO :update-chat!) contact-update-received (update-contact ;TODO :update-chat!) hide-contact (update-contact ;TODO :account-update-keys) - add-contact-handler (add-pending-contact, status-im.contacts.events/add-new-contact + add-contact-handler (add-contact, status-im.contacts.events/add-new-contact status-im.contacts.events/send-contact-request ;TODO :discoveries-send-portions) create-new-contact-group @@ -241,7 +241,8 @@ (def new-contact {:name "" :photo-path "" :whisper-identity new-contact-public-key - :address new-contact-address}) + :address new-contact-address + :pending? false}) (def contact (rf/subscribe [:contact-by-identity new-contact-public-key])) (def current-chat-id (rf/subscribe [:get-current-chat-id])) @@ -338,11 +339,11 @@ (is (= (assoc received-contact2 :pending? true) (get @contacts new-contact-public-key))))) - (testing ":add-contact-handler event - :add-pending-contact" + (testing ":add-contact-handler event - :add-contact" ;; :add-contact-handler event dispatches next 4 events ;; - ;; :add-pending-contact + ;; :add-contact ;; :status-im.contacts.events/add-new-contact ;; :status-im.contacts.events/send-contact-request ;;TODO :discoveries-send-portions