[ISSUE #3520] Allow adding/chatting with contacts from public chats

Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
Andrea Maria Piana 2018-03-12 23:20:32 +00:00 committed by Julien Eluard
parent 2653cebd7d
commit 8c6dde8e06
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
6 changed files with 62 additions and 55 deletions

View File

@ -24,7 +24,7 @@
pending-contact? [:current-contact :pending?]] pending-contact? [:current-contact :pending?]]
(when pending-contact? (when pending-contact?
[react/touchable-highlight [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/view style/add-contact
[react/text {:style style/add-contact-text} [react/text {:style style/add-contact-text}
(i18n/label :t/add-to-contacts)]]]))) (i18n/label :t/add-to-contacts)]]])))

View File

@ -4,6 +4,7 @@
[status-im.data-store.contacts :as contacts] [status-im.data-store.contacts :as contacts]
[clojure.string :as s] [clojure.string :as s]
[status-im.protocol.core :as protocol] [status-im.protocol.core :as protocol]
[status-im.utils.contacts :as utils.contacts]
[status-im.utils.utils :refer [http-post]] [status-im.utils.utils :refer [http-post]]
[status-im.utils.random :as random] [status-im.utils.random :as random]
[taoensso.timbre :as log] [taoensso.timbre :as log]
@ -11,8 +12,6 @@
[status-im.utils.js-resources :as js-res] [status-im.utils.js-resources :as js-res]
[status-im.react-native.js-dependencies :as rn-dependencies] [status-im.react-native.js-dependencies :as rn-dependencies]
[status-im.js-dependencies :as 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.i18n :refer [label]]
[status-im.ui.screens.contacts.navigation] [status-im.ui.screens.contacts.navigation]
[status-im.ui.screens.navigation :as navigation] [status-im.ui.screens.navigation :as navigation]
@ -259,21 +258,12 @@
(assoc-in [:db :contacts/new-identity] "") (assoc-in [:db :contacts/new-identity] "")
(assoc ::save-contact contact))) (assoc ::save-contact contact)))
(register-handler-fx (defn add-contact [{:keys [db] :as fx} chat-or-whisper-id]
: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]
(let [{:keys [chats] :contacts/keys [contacts]} db (let [{:keys [chats] :contacts/keys [contacts]} db
contact (if-let [contact-info (get-in chats [chat-or-whisper-id :contact-info])] contact (if-let [contact-info (get-in chats [chat-or-whisper-id :contact-info])]
(read-string 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 contact' (assoc contact
:address (public-key->address chat-or-whisper-id) :address (public-key->address chat-or-whisper-id)
:pending? false)] :pending? false)]
@ -282,18 +272,21 @@
(discover-events/send-portions-when-contact-exists chat-or-whisper-id) (discover-events/send-portions-when-contact-exists chat-or-whisper-id)
(add-new-contact contact')))) (add-new-contact contact'))))
(register-handler-fx (defn add-contact-and-open-chat [fx whisper-id]
:add-pending-contact (-> fx
(fn [{:keys [db]} [_ chat-or-whisper-id]] (add-contact whisper-id)
(add-pending-contact {:db db} chat-or-whisper-id))) (update :db #(navigation/navigate-to-clean % :home))
(assoc :dispatch [:start-chat whisper-id {:navigation-replace? true}])))
(register-handler-fx (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]] (fn [{:keys [db]} [_ whisper-id]]
(-> {:db db} (add-contact-and-open-chat {:db db} whisper-id)))
(add-pending-contact whisper-id)
(update :db #(navigation/navigate-to-clean % :home))
(assoc :dispatch [:start-chat whisper-id {:navigation-replace? true}]))))
(register-handler-fx (register-handler-fx
:set-contact-identity-from-qr :set-contact-identity-from-qr
@ -383,11 +376,6 @@
(register-handler-fx (register-handler-fx
:add-contact-handler :add-contact-handler
(fn [{:keys [db]}] (fn [{{:contacts/keys [new-identity] :as db} :db}]
(let [{:contacts/keys [new-identity]} db] (when (seq new-identity)
(when (and new-identity (not (string/blank? new-identity))) (add-contact-and-open-chat {:db db} 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}])}))))

View File

@ -98,10 +98,15 @@
(->> (remove :pending? (vals groups)) (->> (remove :pending? (vals groups))
(sort-by :order >)))) (sort-by :order >))))
(reg-sub :contact (reg-sub :current-contact-identity
(fn [db] (fn [db]
(let [identity (:contacts/identity db)] (:contacts/identity db)))
(get-in db [:contacts/contacts identity]))))
(reg-sub :contact
:<- [:get-contacts]
:<- [:current-contact-identity]
(fn [[contacts identity]]
(contacts identity)))
(reg-sub :contact-by-identity (reg-sub :contact-by-identity
(fn [db [_ identity]] (fn [db [_ identity]]

View File

@ -7,6 +7,7 @@
[status-im.ui.components.status-bar.view :as status-bar] [status-im.ui.components.status-bar.view :as status-bar]
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[re-frame.core :as re-frame] [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.toolbar.view :as toolbar]
[status-im.ui.components.list.views :as list])) [status-im.ui.components.list.views :as list]))
@ -19,7 +20,7 @@
(concat (if pending? (concat (if pending?
[{:label (i18n/label :t/add-to-contacts) [{:label (i18n/label :t/add-to-contacts)
:icon :icons/add-contact :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) [{:label (i18n/label :t/in-contacts)
:icon :icons/in-contacts :icon :icons/in-contacts
:disabled? true}]) :disabled? true}])
@ -52,8 +53,10 @@
[profile-info-contact-code-item whisper-identity]]) [profile-info-contact-code-item whisper-identity]])
(defview profile [] (defview profile []
(letsubs [contact [:contact] (letsubs [identity [:current-contact-identity]
maybe-contact [:contact]
chat-id [:get :current-chat-id]] chat-id [:get :current-chat-id]]
(let [contact (or maybe-contact (utils.contacts/whisper-id->new-contact identity))]
[react/view profile.components.styles/profile [react/view profile.components.styles/profile
[status-bar/status-bar] [status-bar/status-bar]
[profile-contact-toolbar] [profile-contact-toolbar]
@ -67,4 +70,4 @@
:action-separator-style styles/action-separator :action-separator-style styles/action-separator
:icon-opts styles/action-icon-opts}] :icon-opts styles/action-icon-opts}]
[react/view styles/contact-profile-info-container [react/view styles/contact-profile-info-container
[profile-info contact]]]])) [profile-info contact]]]])))

View File

@ -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})

View File

@ -110,7 +110,7 @@
contact-request-received (update-contact, watch-contact ;TODO :update-chat!) contact-request-received (update-contact, watch-contact ;TODO :update-chat!)
contact-update-received (update-contact ;TODO :update-chat!) contact-update-received (update-contact ;TODO :update-chat!)
hide-contact (update-contact ;TODO :account-update-keys) 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) status-im.contacts.events/send-contact-request ;TODO :discoveries-send-portions)
create-new-contact-group create-new-contact-group
@ -241,7 +241,8 @@
(def new-contact {:name "" (def new-contact {:name ""
:photo-path "" :photo-path ""
:whisper-identity new-contact-public-key :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 contact (rf/subscribe [:contact-by-identity new-contact-public-key]))
(def current-chat-id (rf/subscribe [:get-current-chat-id])) (def current-chat-id (rf/subscribe [:get-current-chat-id]))
@ -338,11 +339,11 @@
(is (= (assoc received-contact2 :pending? true) (is (= (assoc received-contact2 :pending? true)
(get @contacts new-contact-public-key))))) (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-contact-handler event dispatches next 4 events
;; ;;
;; :add-pending-contact ;; :add-contact
;; :status-im.contacts.events/add-new-contact ;; :status-im.contacts.events/add-new-contact
;; :status-im.contacts.events/send-contact-request ;; :status-im.contacts.events/send-contact-request
;;TODO :discoveries-send-portions ;;TODO :discoveries-send-portions