[#18964] Support custom message when sending contact request (#18998)

* tweak: allow for optional message to be sent when making a contact request

* chore: migrate contact-request event handler from `rf/defn` to `rf/reg-event-fx`

* test: add tests for sending a contact request

* chore: make it easier to test on-success and on-error behaviour after sending a contact request

* tidy: use reframe fx for declaring json-rpc/call

* tidy: remove unused success event handler for send a contact request
This commit is contained in:
Sean Hagstrom 2024-03-05 10:45:16 +00:00 committed by GitHub
parent e771d056a0
commit e783deb5af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 13 deletions

View File

@ -75,20 +75,26 @@
[{:keys [db]} contacts] [{:keys [db]} contacts]
{:db (assoc db :contacts/contacts (into {} (map #(vector (:public-key %) %) contacts)))}) {:db (assoc db :contacts/contacts (into {} (map #(vector (:public-key %) %) contacts)))})
(rf/defn send-contact-request (defn send-contact-request
{:events [:contact.ui/send-contact-request]} [{:keys [db]} [id message]]
[{:keys [db]} id]
(when (not= id (get-in db [:profile/profile :public-key])) (when (not= id (get-in db [:profile/profile :public-key]))
{:json-rpc/call {:fx [[:json-rpc/call
[{:method "wakuext_sendContactRequest" [{:method "wakuext_sendContactRequest"
:js-response true :js-response true
:params [{:id id :message (i18n/label :t/add-me-to-your-contacts)}] :params [{:id id :message (or message (i18n/label :t/add-me-to-your-contacts))}]
:on-error (fn [error] :on-error [:contact.ui/send-contact-request-failure id]
:on-success [:transport/message-sent]}]]]}))
(rf/reg-event-fx :contact.ui/send-contact-request send-contact-request)
(defn send-contact-request-failure
[_ [id error]]
(log/error "Failed to send contact request" (log/error "Failed to send contact request"
{:error error {:error error
:event :contact.ui/send-contact-request :event :contact.ui/send-contact-request
:id id})) :id id}))
:on-success #(rf/dispatch [:transport/message-sent %])}]}))
(rf/reg-event-fx :contact.ui/send-contact-request-failure send-contact-request-failure)
(rf/defn remove-contact (rf/defn remove-contact
"Remove a contact from current account's contact list" "Remove a contact from current account's contact list"

View File

@ -0,0 +1,41 @@
(ns status-im.contexts.chat.contacts.events-test
(:require
[cljs.test :refer [deftest is testing]]
matcher-combinators.test
[status-im.contexts.chat.contacts.events :as chat.contacts]
[utils.i18n :as i18n]))
(deftest send-contact-request-test
(testing "creates nothing when attempting send contact request to self"
(let [profile-public-key "0x1"
cofx {:db {:profile/profile {:public-key profile-public-key}}}]
(is (match? nil (chat.contacts/send-contact-request cofx [profile-public-key])))))
(testing "creates contact request rpc with default message"
(let [profile-public-key "0x1"
contact-public-key "0x2"
cofx {:db {:profile/profile {:public-key profile-public-key}}}]
(is (match?
{:fx [[:json-rpc/call
[{:method "wakuext_sendContactRequest"
:js-response true
:params [{:id contact-public-key
:message (i18n/label :t/add-me-to-your-contacts)}]
:on-error [:contact.ui/send-contact-request-failure contact-public-key]
:on-success [:transport/message-sent]}]]]}
(chat.contacts/send-contact-request cofx [contact-public-key])))))
(testing "creates contact request rpc with custom message"
(let [profile-public-key "0x1"
contact-public-key "0x2"
custom-message "Hey there!"
cofx {:db {:profile/profile {:public-key profile-public-key}}}]
(is (match?
{:fx [[:json-rpc/call
[{:method "wakuext_sendContactRequest"
:js-response true
:params [{:id contact-public-key
:message custom-message}]
:on-error [:contact.ui/send-contact-request-failure contact-public-key]
:on-success [:transport/message-sent]}]]]}
(chat.contacts/send-contact-request cofx [contact-public-key custom-message]))))))