Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup events and tests #21885

Open
wants to merge 22 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2020f90
chore: add error logging events and effects
seanstrom Dec 30, 2024
4ef6141
chore: restructure tests and effects for delete-message-for-me
seanstrom Dec 30, 2024
767ba11
chore: refactor events and effects and tests for chat-contacts
seanstrom Dec 31, 2024
30e0cae
chore: refactor and add tests for accepting and declining contact req…
seanstrom Dec 31, 2024
79b5480
chore: refactor chat-contacts rf/defn to defn
seanstrom Dec 31, 2024
015e9a5
chore: add :fx key as a mergeable effect for rf/merge when running tests
seanstrom Dec 31, 2024
3a51fde
chore: refactor rf/defn and add tests for delete-message-for-me
seanstrom Dec 31, 2024
37f24c9
chore: remove log effects for contacts
seanstrom Dec 31, 2024
65a967e
chore: remove logs effects for delete-message-for-me
seanstrom Dec 31, 2024
b124a6b
fix: update chat usage of sync-all
seanstrom Dec 31, 2024
05c8702
fix: resolve schema error with missing action-label for contact-reque…
seanstrom Jan 2, 2025
6424c86
fix: ensure chat-contacts event error handlers are compatible with re…
seanstrom Jan 3, 2025
474d14d
fix: ensure delete-message-for-me event error handlers are compatible…
seanstrom Jan 3, 2025
fd9c4cd
chore: refactor and test delete-message events
seanstrom Jan 3, 2025
cf5ae68
chore: refactor log events and effects
seanstrom Jan 3, 2025
8904eb3
chore: use log effect in delete-message
seanstrom Jan 3, 2025
077a336
chore: use log events and effects inside chat-contacts
seanstrom Jan 3, 2025
c03e932
chore: use log effects inside delete-message-for-me
seanstrom Jan 3, 2025
7d404b7
chore: remove unneeded test for delete-message-for-me
seanstrom Jan 3, 2025
97a8c0a
chore: format bottom-drawer component
seanstrom Jan 8, 2025
b92f046
chore: format delete-message
seanstrom Jan 8, 2025
accbbe6
chore: format delete-message-for-me
seanstrom Jan 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/legacy/status_im/chat/models/message.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[legacy.status-im.data-store.messages :as data-store.messages]
[legacy.status-im.utils.deprecated-types :as types]
[react-native.platform :as platform]
[status-im.contexts.chat.messenger.messages.delete-message.events :as delete-message]
[status-im.contexts.chat.messenger.messages.list.events :as message-list]
[status-im.contexts.chat.messenger.messages.list.state :as view.state]
[utils.re-frame :as rf]))
Expand Down Expand Up @@ -119,6 +118,26 @@
(when (get-in db [:messages chat-id message-id])
{:db (assoc-in db [:messages chat-id message-id :outgoing-status] status)}))

(defn- update-db-delete-locally-without-time-limit
"Delete message in re-frame db, used to handle received removed-messages"
[db chat-id message-id deleted-by]
(when (get-in db [:messages chat-id message-id])
(update-in db [:messages chat-id message-id] assoc :deleted? true :deleted-by deleted-by)))

(rf/defn delete-messages-locally
"Mark messages :deleted? localy in client"
{:events [:chat.ui/delete-messages-localy]}
[{:keys [db]} messages chat-id]
(let [new-db (->> messages
(filter #(get-in db [:messages chat-id (:message-id %)]))
(reduce #(update-db-delete-locally-without-time-limit %1
chat-id
(:message-id %2)
(:deleted-by %2))
db))]
(when new-db
(message-list/rebuild-message-list {:db new-db} chat-id))))

(rf/defn handle-removed-messages
[cofx removed-messages]
(let [mark-as-deleted-fx (->> removed-messages
Expand All @@ -127,7 +146,7 @@
:deleted-by (:deletedBy %)))
(group-by :chatId)
(mapv (fn [[chat-id messages]]
(delete-message/delete-messages-localy messages chat-id))))
(delete-messages-locally messages chat-id))))
mark-as-seen-fx (mapv
(fn [removed-message]
(let [chat-id (:chatId removed-message)
Expand Down
3 changes: 2 additions & 1 deletion src/legacy/status_im/test_runner.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@
:transport/confirm-messages-processed
:group-chats/extract-membership-signature
:utils/dispatch-later
:json-rpc/call})
:json-rpc/call
:fx})
Comment on lines +132 to +133
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needed to be added while running tests locally, otherwise the rf/merge function wouldn't return a merged collection of effects.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!


(let [opts (parse-args args)]
(execute-cli opts)))
72 changes: 72 additions & 0 deletions src/status_im/common/log.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,75 @@
:logs/set-level
(fn [level]
(setup level)))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this!

(defn log-error
([error]
(log/error error))
([message error]
(log/error message error))
([message context error]
(log/error message context error)))

(re-frame/reg-event-fx
:log/error
(fn [_ args]
{:fx [[:effects.log/error args]]}))

(re-frame/reg-fx
:effects.log/error
(fn [args]
(apply log-error args)))

(defn log-info
([info]
(log/info info))
([message info]
(log/info message info))
([message context info]
(log/info message context info)))

(re-frame/reg-event-fx
:log/info
(fn [_ args]
{:fx [[:effects.log/info args]]}))

(re-frame/reg-fx
:effects.log/info
(fn [args]
(apply log-info args)))

(defn log-warn
([warning]
(log/warn warning))
([message warning]
(log/warn message warning))
([message context warning]
(log/warn message context warning)))

(re-frame/reg-event-fx
:log/warn
(fn [_ args]
{:fx [[:effects.log/warn args]]}))

(re-frame/reg-fx
:effects.log/warn
(fn [args]
(apply log-warn args)))

(defn log-debug
([value]
(log/debug value))
([message value]
(log/debug message value))
([message context value]
(log/debug message context value)))

(re-frame/reg-event-fx
:log/debug
(fn [_ args]
{:fx [[:effects.log/debug args]]}))

(re-frame/reg-fx
:effects.log/debug
(fn [args]
(apply log-debug args)))
82 changes: 51 additions & 31 deletions src/status_im/contexts/chat/contacts/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[re-frame.core :as re-frame]
[status-im.common.json-rpc.events :as json-rpc]
[status-im.constants :as constants]
[taoensso.timbre :as log]
[utils.i18n :as i18n]
[utils.re-frame :as rf]
[utils.transforms :as transforms]))
Expand Down Expand Up @@ -85,7 +84,7 @@
:params []
:js-response true
:on-success [:contacts/contacts-loaded]
:on-error #(log/error "failed to fetch contacts" %)})))
:on-error [:log/error "failed to fetch contacts"]})))

(defn send-contact-request
[{:keys [db]} [id message]]
Expand All @@ -94,40 +93,61 @@
[{:method "wakuext_sendContactRequest"
:js-response true
:params [{:id id :message (or message (i18n/label :t/add-me-to-your-contacts))}]
:on-error [:contact.ui/send-contact-request-failure id]
:on-error [:contacts/send-contact-request-error id]
:on-success [:transport/message-sent]}]]]}))

(rf/reg-event-fx :contact.ui/send-contact-request send-contact-request)

(defn send-contact-request-failure
(defn send-contact-request-error
[_ [id error]]
(log/error "Failed to send contact request"
{:error error
:event :contact.ui/send-contact-request
:id id}))
{:fx [[:effects.log/error
["Failed to send contact request"
{:id id
:error error
:event :contact.ui/send-contact-request}]]]})

(rf/reg-event-fx :contact.ui/send-contact-request-failure send-contact-request-failure)
(rf/reg-event-fx :contact.ui/send-contact-request-error send-contact-request-error)

(rf/defn remove-contact
(defn remove-contact
"Remove a contact from current account's contact list"
{:events [:contact.ui/remove-contact-pressed]}
[{:keys [db]} {:keys [public-key]}]
{:db (-> db
(assoc-in [:contacts/contacts public-key :added?] false)
(assoc-in [:contacts/contacts public-key :active?] false)
(assoc-in [:contacts/contacts public-key :contact-request-state]
constants/contact-request-state-none))
:json-rpc/call [{:method "wakuext_retractContactRequest"
:params [{:id public-key}]
:js-response true
:on-success #(rf/dispatch [:sanitize-messages-and-process-response %])
:on-error #(log/error "failed to remove contact" public-key %)}]})

(rf/defn update-nickname
{:events [:contacts/update-nickname]}
[_ public-key nickname]
{:json-rpc/call [{:method "wakuext_setContactLocalNickname"
:params [{:id public-key :nickname nickname}]
:js-response true
:on-success #(rf/dispatch [:sanitize-messages-and-process-response %])
:on-error #(log/error "failed to set contact nickname " public-key nickname %)}]})
[{:keys [db]} [{:keys [public-key]}]]
{:db (-> db
(assoc-in [:contacts/contacts public-key :added?] false)
(assoc-in [:contacts/contacts public-key :active?] false)
(assoc-in [:contacts/contacts public-key :contact-request-state]
constants/contact-request-state-none))
:fx [[:json-rpc/call
[{:method "wakuext_retractContactRequest"
:params [{:id public-key}]
:js-response true
:on-success [:sanitize-messages-and-process-response]
:on-error [:contacts/remove-contact-error public-key]}]]]})

(rf/reg-event-fx :contact.ui/remove-contact-pressed remove-contact)
Comment on lines +111 to +126
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we're also migrating towards using the :fx key for re-frame effects.


(defn remove-contact-error
[_ [public-key error]]
{:fx [[:effects.log/error ["failed to remove contact" public-key error]]]})

(rf/reg-event-fx :contacts/remove-contact-error remove-contact-error)

(defn update-nickname
[_ [public-key nickname]]
{:fx [[:json-rpc/call
[{:method "wakuext_setContactLocalNickname"
:params [{:id public-key :nickname nickname}]
:js-response true
:on-success [:sanitize-messages-and-process-response]
:on-error [:contacts/update-nickname-error public-key nickname]}]]]})

(rf/reg-event-fx :contacts/update-nickname update-nickname)

(defn update-nickname-error
[_ [public-key nickname error]]
{:fx [[:effects.log/error
["failed to set contact nickname"
{:public-key public-key
:nickname nickname}
error]]]})

(rf/reg-event-fx :contacts/update-nickname-error update-nickname-error)
44 changes: 42 additions & 2 deletions src/status_im/contexts/chat/contacts/events_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require
[cljs.test :refer [deftest is testing]]
matcher-combinators.test
[status-im.constants :as constants]
[status-im.contexts.chat.contacts.events :as chat.contacts]
[utils.i18n :as i18n]))

Expand All @@ -21,7 +22,7 @@
: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-error [:contacts/send-contact-request-error contact-public-key]
:on-success [:transport/message-sent]}]]]}
(chat.contacts/send-contact-request cofx [contact-public-key])))))

Expand All @@ -36,6 +37,45 @@
:js-response true
:params [{:id contact-public-key
:message custom-message}]
:on-error [:contact.ui/send-contact-request-failure contact-public-key]
:on-error [:contacts/send-contact-request-error contact-public-key]
:on-success [:transport/message-sent]}]]]}
(chat.contacts/send-contact-request cofx [contact-public-key custom-message]))))))

(deftest remove-contact-test
(testing "removes existing contact"
(let [public-key "0x2"
initial-db {:contacts/contacts
{public-key
{:added? true
:active? true
:contact-request-state constants/contact-request-state-mutual}}}
expected-db {:contacts/contacts
{public-key
{:added? false
:active? false
:contact-request-state constants/contact-request-state-none}}}
expected-fx [[:json-rpc/call
[{:method "wakuext_retractContactRequest"
:params [{:id public-key}]
:js-response true
:on-success [:sanitize-messages-and-process-response]
:on-error [:contacts/remove-contact-error public-key]}]]]]
(is (match?
{:db expected-db
:fx expected-fx}
(chat.contacts/remove-contact {:db initial-db}
[{:public-key public-key}]))))))

(deftest update-nickname-test
(testing "updates contact nickname"
(let [public-key "0x2"
new-nickname "Joe"
expected-fx [[:json-rpc/call
[{:method "wakuext_setContactLocalNickname"
:params [{:id public-key :nickname new-nickname}]
:js-response true
:on-success [:sanitize-messages-and-process-response]
:on-error [:contacts/update-nickname-error public-key new-nickname]}]]]]
(is (match?
{:fx expected-fx}
(chat.contacts/update-nickname {:db {}} [public-key new-nickname]))))))
6 changes: 4 additions & 2 deletions src/status_im/contexts/chat/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@
:effects.async-storage/set {:chat-id nil
:key-uid nil}}
(link-preview/reset-all)
(delete-for-me/sync-all)
(delete-message/send-all)
(fn [cofx]
(delete-for-me/sync-all cofx))
(fn [cofx]
(delete-message/send-all cofx))
Comment on lines +154 to +157
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Here we're wrapping these functions because delete-for-me/sync-all and delete-message/send-all are no longer defined with rf/defn.
    • It seems by default that functions defined with rf/defn are composable with rf/merge (when we don't pass all the arguments I think), and rf/merge likes to compose these functions by passing a cofx object.
    • So instead we just manually wrap the function, which seems to have the same behaviour as before.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct @seanstrom. I had to do this a few times in the past.

(offload-messages chat-id)))))

(rf/defn deactivate-chat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
constants/contact-request-state-received)
contact-request-pending? (= contact-request-state
constants/contact-request-state-sent)
contact-request-dismissed? (= contact-request-state
constants/contact-request-state-dismissed)
keycard? (rf/sub [:keycard/keycard-profile?])
keycard-feature-unavailable (rn/use-callback
#(rf/dispatch [:keycard/feature-unavailable-show]))]
Expand Down Expand Up @@ -70,4 +72,8 @@
(i18n/label :t/contact-request-chat-received {:name primary-name})

contact-request-pending?
(i18n/label :t/contact-request-chat-pending))}]]))
(i18n/label :t/contact-request-chat-pending)

contact-request-dismissed?
(i18n/label :t/contact-request-chat-add {:name primary-name}))}]]))

Loading