From 2393f6e9bb2587a2eeea891000dfa04cb7817af6 Mon Sep 17 00:00:00 2001 From: daledah Date: Fri, 6 Sep 2024 17:31:36 +0700 Subject: [PATCH 1/7] feat: Subscribe guides to a new presence pusher channel --- src/components/ActiveGuidesEventListener.tsx | 36 +++++++++++++++++++ .../Navigation/AppNavigator/AuthScreens.tsx | 11 ++++-- src/libs/Pusher/pusher.ts | 4 +-- src/libs/actions/Report.ts | 12 +++++++ src/types/onyx/User.ts | 3 ++ 5 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 src/components/ActiveGuidesEventListener.tsx diff --git a/src/components/ActiveGuidesEventListener.tsx b/src/components/ActiveGuidesEventListener.tsx new file mode 100644 index 000000000000..56622c13ba73 --- /dev/null +++ b/src/components/ActiveGuidesEventListener.tsx @@ -0,0 +1,36 @@ +import {useEffect, useRef} from 'react'; +import type {OnyxEntry} from 'react-native-onyx'; +import {withOnyx} from 'react-native-onyx'; +import * as Report from '@userActions/Report'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {User} from '@src/types/onyx'; + +type ActiveGuidesEventListenerOnyxProps = { + user: OnyxEntry; +}; + +type ActiveGuidesEventListenerProps = ActiveGuidesEventListenerOnyxProps; + +function ActiveGuidesEventListener({user}: ActiveGuidesEventListenerProps) { + const didSubscribeToActiveGuides = useRef(false); + useEffect( + () => () => { + if (didSubscribeToActiveGuides.current) { + return; + } + if (user?.isGuide) { + didSubscribeToActiveGuides.current = true; + Report.subscribeToActiveGuides(); + } + }, + + [user], + ); + return null; +} + +export default withOnyx({ + user: { + key: ONYXKEYS.USER, + }, +})(ActiveGuidesEventListener); diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.tsx b/src/libs/Navigation/AppNavigator/AuthScreens.tsx index 5ca9becf8986..0274edb25deb 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.tsx +++ b/src/libs/Navigation/AppNavigator/AuthScreens.tsx @@ -1,8 +1,9 @@ -import React, {memo, useEffect, useMemo, useRef} from 'react'; +import React, {memo, useEffect, useMemo, useRef, useState} from 'react'; import {View} from 'react-native'; import type {OnyxEntry} from 'react-native-onyx'; import Onyx, {withOnyx} from 'react-native-onyx'; import type {ValueOf} from 'type-fest'; +import ActiveGuidesEventListener from '@components/ActiveGuidesEventListener'; import ComposeProviders from '@components/ComposeProviders'; import OptionsListContextProvider from '@components/OptionListContextProvider'; import {SearchContextProvider} from '@components/Search/SearchContext'; @@ -108,7 +109,7 @@ function getCentralPaneScreenInitialParams(screenName: CentralPaneName, initialR } function initializePusher() { - Pusher.init({ + return Pusher.init({ appKey: CONFIG.PUSHER.APP_KEY, cluster: CONFIG.PUSHER.CLUSTER, authEndpoint: `${CONFIG.EXPENSIFY.DEFAULT_API_ROOT}api/AuthenticatePusher?`, @@ -229,6 +230,7 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie [StyleUtils, isSmallScreenWidth, isMediumOrLargerScreenWidth, styles], ); const modal = useRef({}); + const [didPusherInit, setDidPusherInit] = useState(false); let initialReportID: string | undefined; const isInitialRender = useRef(true); @@ -266,7 +268,9 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie NetworkConnection.listenForReconnect(); NetworkConnection.onReconnect(handleNetworkReconnect); PusherConnectionManager.init(); - initializePusher(); + initializePusher().then(() => { + setDidPusherInit(true); + }); // If we are on this screen then we are "logged in", but the user might not have "just logged in". They could be reopening the app // or returning from background. If so, we'll assume they have some app data already and we can call reconnectApp() instead of openApp(). @@ -564,6 +568,7 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie })} + {didPusherInit && } ); } diff --git a/src/libs/Pusher/pusher.ts b/src/libs/Pusher/pusher.ts index a3383dbadb8a..1a511eec391c 100644 --- a/src/libs/Pusher/pusher.ts +++ b/src/libs/Pusher/pusher.ts @@ -156,7 +156,7 @@ function getChannel(channelName: string): Channel | undefined { /** * Binds an event callback to a channel + eventName */ -function bindEventToChannel(channel: Channel | undefined, eventName: EventName, eventCallback: (data: EventData) => void = () => {}) { +function bindEventToChannel(channel: Channel | undefined, eventName?: EventName, eventCallback: (data: EventData) => void = () => {}) { if (!eventName || !channel) { return; } @@ -232,7 +232,7 @@ function bindEventToChannel(channel: Channel */ function subscribe( channelName: string, - eventName: EventName, + eventName?: EventName, eventCallback: (data: EventData) => void = () => {}, onResubscribe = () => {}, ): Promise { diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index bd31d3832605..e5316d387767 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -4088,6 +4088,16 @@ function markAsManuallyExported(reportID: string, connectionName: ConnectionName API.write(WRITE_COMMANDS.MARK_AS_EXPORTED, params, {optimisticData, successData, failureData}); } +function subscribeToActiveGuides() { + Pusher.subscribe(`activeGuides`).catch((error: ReportError) => { + Log.hmmm('[Report] Failed to initially subscribe to Pusher channel', {errorType: error.type, pusherChanelName: 'activeGuides'}); + }); +} + +function unsubscribeToActiveGuides() { + Pusher.unsubscribe(`activeGuides`); +} + export { searchInServer, addComment, @@ -4175,4 +4185,6 @@ export { exportToIntegration, markAsManuallyExported, handleReportChanged, + subscribeToActiveGuides, + unsubscribeToActiveGuides, }; diff --git a/src/types/onyx/User.ts b/src/types/onyx/User.ts index f30ca846ef43..3f12b35c4ebe 100644 --- a/src/types/onyx/User.ts +++ b/src/types/onyx/User.ts @@ -29,6 +29,9 @@ type User = { /** Whether the form is being submitted */ loading?: boolean; + + /** Whether the user is Expensify Guide */ + isGuide?: boolean; }; export default User; From 687f54a70c8dd4fa4ddaadf1ceacc9b781292e38 Mon Sep 17 00:00:00 2001 From: daledah Date: Sat, 14 Sep 2024 09:33:58 +0700 Subject: [PATCH 2/7] fix: update activeGuides channel name --- src/CONST.ts | 1 + src/components/ActiveGuidesEventListener.tsx | 22 ++++++++------------ src/libs/actions/Report.ts | 7 +------ 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index cf3facb0d1d8..14b0d2da5b34 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -1254,6 +1254,7 @@ const CONST = { PUSHER: { PRIVATE_USER_CHANNEL_PREFIX: 'private-encrypted-user-accountID-', PRIVATE_REPORT_CHANNEL_PREFIX: 'private-report-reportID-', + PRESENT_ACTIVE_GUIDES: 'presence-activeGuides', }, EMOJI_SPACER: 'SPACER', diff --git a/src/components/ActiveGuidesEventListener.tsx b/src/components/ActiveGuidesEventListener.tsx index 56622c13ba73..b2733388313f 100644 --- a/src/components/ActiveGuidesEventListener.tsx +++ b/src/components/ActiveGuidesEventListener.tsx @@ -13,19 +13,15 @@ type ActiveGuidesEventListenerProps = ActiveGuidesEventListenerOnyxProps; function ActiveGuidesEventListener({user}: ActiveGuidesEventListenerProps) { const didSubscribeToActiveGuides = useRef(false); - useEffect( - () => () => { - if (didSubscribeToActiveGuides.current) { - return; - } - if (user?.isGuide) { - didSubscribeToActiveGuides.current = true; - Report.subscribeToActiveGuides(); - } - }, - - [user], - ); + useEffect(() => { + if (didSubscribeToActiveGuides.current) { + return; + } + if (user?.isGuide) { + didSubscribeToActiveGuides.current = true; + Report.subscribeToActiveGuides(); + } + }, [user]); return null; } diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 84a633c59ad0..26953b0aa4db 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -4088,15 +4088,11 @@ function markAsManuallyExported(reportID: string, connectionName: ConnectionName } function subscribeToActiveGuides() { - Pusher.subscribe(`activeGuides`).catch((error: ReportError) => { + Pusher.subscribe(`${CONST.PUSHER.PRESENT_ACTIVE_GUIDES}${CONFIG.PUSHER.SUFFIX}`).catch((error: ReportError) => { Log.hmmm('[Report] Failed to initially subscribe to Pusher channel', {errorType: error.type, pusherChanelName: 'activeGuides'}); }); } -function unsubscribeToActiveGuides() { - Pusher.unsubscribe(`activeGuides`); -} - export type {Video}; export { @@ -4186,5 +4182,4 @@ export { markAsManuallyExported, handleReportChanged, subscribeToActiveGuides, - unsubscribeToActiveGuides, }; From 9b8da68e976989ba18a8b596a0d1dc21ef835954 Mon Sep 17 00:00:00 2001 From: daledah Date: Mon, 16 Sep 2024 01:47:07 +0700 Subject: [PATCH 3/7] fix: move subscribe to User.ts --- src/CONST.ts | 2 +- src/components/ActiveGuidesEventListener.tsx | 4 ++-- src/libs/actions/Report.ts | 7 ------- src/libs/actions/User.ts | 8 ++++++++ 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index 14b0d2da5b34..fe09087b866d 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -1254,7 +1254,7 @@ const CONST = { PUSHER: { PRIVATE_USER_CHANNEL_PREFIX: 'private-encrypted-user-accountID-', PRIVATE_REPORT_CHANNEL_PREFIX: 'private-report-reportID-', - PRESENT_ACTIVE_GUIDES: 'presence-activeGuides', + PRESENCE_ACTIVE_GUIDES: 'presence-activeGuides', }, EMOJI_SPACER: 'SPACER', diff --git a/src/components/ActiveGuidesEventListener.tsx b/src/components/ActiveGuidesEventListener.tsx index b2733388313f..1aab40eae7d0 100644 --- a/src/components/ActiveGuidesEventListener.tsx +++ b/src/components/ActiveGuidesEventListener.tsx @@ -1,7 +1,7 @@ import {useEffect, useRef} from 'react'; import type {OnyxEntry} from 'react-native-onyx'; import {withOnyx} from 'react-native-onyx'; -import * as Report from '@userActions/Report'; +import {subscribeToActiveGuides} from '@userActions/User'; import ONYXKEYS from '@src/ONYXKEYS'; import type {User} from '@src/types/onyx'; @@ -19,7 +19,7 @@ function ActiveGuidesEventListener({user}: ActiveGuidesEventListenerProps) { } if (user?.isGuide) { didSubscribeToActiveGuides.current = true; - Report.subscribeToActiveGuides(); + subscribeToActiveGuides(); } }, [user]); return null; diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 26953b0aa4db..147b07f650b2 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -4087,12 +4087,6 @@ function markAsManuallyExported(reportID: string, connectionName: ConnectionName API.write(WRITE_COMMANDS.MARK_AS_EXPORTED, params, {optimisticData, successData, failureData}); } -function subscribeToActiveGuides() { - Pusher.subscribe(`${CONST.PUSHER.PRESENT_ACTIVE_GUIDES}${CONFIG.PUSHER.SUFFIX}`).catch((error: ReportError) => { - Log.hmmm('[Report] Failed to initially subscribe to Pusher channel', {errorType: error.type, pusherChanelName: 'activeGuides'}); - }); -} - export type {Video}; export { @@ -4181,5 +4175,4 @@ export { exportToIntegration, markAsManuallyExported, handleReportChanged, - subscribeToActiveGuides, }; diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 355dcf93338e..ada6b0fda1b4 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -33,6 +33,7 @@ import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import playSound, {SOUNDS} from '@libs/Sound'; import playSoundExcludingMobile from '@libs/Sound/playSoundExcludingMobile'; import Visibility from '@libs/Visibility'; +import CONFIG from '@src/CONFIG'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; @@ -1311,6 +1312,12 @@ function requestRefund() { API.write(WRITE_COMMANDS.REQUEST_REFUND, null); } +function subscribeToActiveGuides() { + Pusher.subscribe(`${CONST.PUSHER.PRESENCE_ACTIVE_GUIDES}${CONFIG.PUSHER.SUFFIX}`).catch(() => { + Log.hmmm('[Report] Failed to initially subscribe to Pusher channel', {pusherChanelName: 'activeGuides'}); + }); +} + export { clearFocusModeNotification, closeAccount, @@ -1349,4 +1356,5 @@ export { requestValidateCodeAction, addPendingContactMethod, clearValidateCodeActionError, + subscribeToActiveGuides, }; From f92c53fa10ad97125cef8f688b3eb41732fe408e Mon Sep 17 00:00:00 2001 From: daledah Date: Mon, 16 Sep 2024 12:13:53 +0700 Subject: [PATCH 4/7] fix: define channel name --- src/libs/actions/User.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 3c135150d7c7..8a030e16a9e8 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -1317,8 +1317,9 @@ function requestRefund() { } function subscribeToActiveGuides() { - Pusher.subscribe(`${CONST.PUSHER.PRESENCE_ACTIVE_GUIDES}${CONFIG.PUSHER.SUFFIX}`).catch(() => { - Log.hmmm('[Report] Failed to initially subscribe to Pusher channel', {pusherChanelName: 'activeGuides'}); + const pusherChannelName = `${CONST.PUSHER.PRESENCE_ACTIVE_GUIDES}${CONFIG.PUSHER.SUFFIX}`; + Pusher.subscribe(pusherChannelName).catch(() => { + Log.hmmm('[Report] Failed to initially subscribe to Pusher channel', {pusherChannelName}); }); } From 0b862d77e22dd20b5fa8c804b751f7988743a314 Mon Sep 17 00:00:00 2001 From: daledah Date: Tue, 17 Sep 2024 11:33:26 +0700 Subject: [PATCH 5/7] refactor: change verbose message --- src/libs/Permissions.ts | 2 +- src/libs/actions/User.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/Permissions.ts b/src/libs/Permissions.ts index 8c47100e465b..2a548354c679 100644 --- a/src/libs/Permissions.ts +++ b/src/libs/Permissions.ts @@ -4,7 +4,7 @@ import type {IOUType} from '@src/CONST'; import type Beta from '@src/types/onyx/Beta'; function canUseAllBetas(betas: OnyxEntry): boolean { - return !!betas?.includes(CONST.BETAS.ALL); + return true || !!betas?.includes(CONST.BETAS.ALL); } function canUseDefaultRooms(betas: OnyxEntry): boolean { diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 8a030e16a9e8..6d7e67fa02f9 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -1319,7 +1319,7 @@ function requestRefund() { function subscribeToActiveGuides() { const pusherChannelName = `${CONST.PUSHER.PRESENCE_ACTIVE_GUIDES}${CONFIG.PUSHER.SUFFIX}`; Pusher.subscribe(pusherChannelName).catch(() => { - Log.hmmm('[Report] Failed to initially subscribe to Pusher channel', {pusherChannelName}); + Log.hmmm('[User] Failed to initially subscribe to Pusher channel', {pusherChannelName}); }); } From 49d1ceaa7cf357574899a99b9b89d6a8f5dab70b Mon Sep 17 00:00:00 2001 From: daledah Date: Wed, 18 Sep 2024 09:42:19 +0700 Subject: [PATCH 6/7] fix: remove unnecessary change --- src/libs/Permissions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/Permissions.ts b/src/libs/Permissions.ts index 2a548354c679..8c47100e465b 100644 --- a/src/libs/Permissions.ts +++ b/src/libs/Permissions.ts @@ -4,7 +4,7 @@ import type {IOUType} from '@src/CONST'; import type Beta from '@src/types/onyx/Beta'; function canUseAllBetas(betas: OnyxEntry): boolean { - return true || !!betas?.includes(CONST.BETAS.ALL); + return !!betas?.includes(CONST.BETAS.ALL); } function canUseDefaultRooms(betas: OnyxEntry): boolean { From 3d543d6188119aab3337453ea7e8e8c19c197632 Mon Sep 17 00:00:00 2001 From: daledah Date: Wed, 18 Sep 2024 14:07:44 +0700 Subject: [PATCH 7/7] refactor: use useOnyx --- src/components/ActiveGuidesEventListener.tsx | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/components/ActiveGuidesEventListener.tsx b/src/components/ActiveGuidesEventListener.tsx index 1aab40eae7d0..2599576c9c23 100644 --- a/src/components/ActiveGuidesEventListener.tsx +++ b/src/components/ActiveGuidesEventListener.tsx @@ -1,17 +1,10 @@ import {useEffect, useRef} from 'react'; -import type {OnyxEntry} from 'react-native-onyx'; -import {withOnyx} from 'react-native-onyx'; +import {useOnyx} from 'react-native-onyx'; import {subscribeToActiveGuides} from '@userActions/User'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {User} from '@src/types/onyx'; -type ActiveGuidesEventListenerOnyxProps = { - user: OnyxEntry; -}; - -type ActiveGuidesEventListenerProps = ActiveGuidesEventListenerOnyxProps; - -function ActiveGuidesEventListener({user}: ActiveGuidesEventListenerProps) { +function ActiveGuidesEventListener() { + const [user] = useOnyx(ONYXKEYS.USER); const didSubscribeToActiveGuides = useRef(false); useEffect(() => { if (didSubscribeToActiveGuides.current) { @@ -25,8 +18,4 @@ function ActiveGuidesEventListener({user}: ActiveGuidesEventListenerProps) { return null; } -export default withOnyx({ - user: { - key: ONYXKEYS.USER, - }, -})(ActiveGuidesEventListener); +export default ActiveGuidesEventListener;