Skip to content

Commit def505a

Browse files
Refactor: isolate subscribeToUserEvents from ONYXKEYS.SESSION Onyx data
1 parent 2aca6a2 commit def505a

10 files changed

Lines changed: 30 additions & 31 deletions

File tree

src/libs/Navigation/AppNavigator/AuthScreensInitHandler.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ import ONYXKEYS from '@src/ONYXKEYS';
2929
import ROUTES from '@src/ROUTES';
3030
import type {ReportAttributesDerivedValue} from '@src/types/onyx';
3131

32-
function initializePusher(currentUserAccountID?: number, getReportAttributes?: () => ReportAttributesDerivedValue['reports'] | undefined) {
32+
function initializePusher(currentUserAccountID?: number, currentUserEmail?: string, getReportAttributes?: () => ReportAttributesDerivedValue['reports'] | undefined) {
3333
return Pusher.init({
3434
appKey: CONFIG.PUSHER.APP_KEY,
3535
cluster: CONFIG.PUSHER.CLUSTER,
3636
authEndpoint: `${CONFIG.EXPENSIFY.DEFAULT_API_ROOT}api/AuthenticatePusher?`,
3737
}).then(() => {
38-
User.subscribeToUserEvents(currentUserAccountID ?? CONST.DEFAULT_NUMBER_ID, getReportAttributes);
38+
User.subscribeToUserEvents(currentUserAccountID ?? CONST.DEFAULT_NUMBER_ID, currentUserEmail ?? '', getReportAttributes);
3939
});
4040
}
4141

@@ -76,8 +76,8 @@ function AuthScreensInitHandler() {
7676
return;
7777
}
7878
// This means sign in in RHP was successful, so we can subscribe to user events
79-
initializePusher(session?.accountID, () => reportAttributesRef.current);
80-
}, [session?.accountID]);
79+
initializePusher(session?.accountID, session?.email, () => reportAttributesRef.current);
80+
}, [session?.accountID, session?.email]);
8181

8282
useEffect(() => {
8383
const isLoggingInAsNewUser = !!session?.email && SessionUtils.isLoggingInAsNewUser(currentUrl, session.email);
@@ -99,7 +99,7 @@ function AuthScreensInitHandler() {
9999
});
100100
PusherConnectionManager.init();
101101

102-
initializePusher(session?.accountID, () => reportAttributesRef.current).finally(() => {
102+
initializePusher(session?.accountID, session?.email, () => reportAttributesRef.current).finally(() => {
103103
endSpan(CONST.TELEMETRY.SPAN_NAVIGATION.PUSHER_INIT);
104104
});
105105

src/libs/actions/User.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,10 @@ import {showReportActionNotification} from './Report';
6666
import {resendValidateCode as sessionResendValidateCode} from './Session';
6767

6868
let currentUserAccountID: number = CONST.DEFAULT_NUMBER_ID;
69-
let currentEmail = '';
7069
Onyx.connect({
7170
key: ONYXKEYS.SESSION,
7271
callback: (value) => {
7372
currentUserAccountID = value?.accountID ?? CONST.DEFAULT_NUMBER_ID;
74-
currentEmail = value?.email ?? '';
7573
},
7674
});
7775

@@ -619,6 +617,7 @@ function isBlockedFromConcierge(blockedFromConciergeNVP: OnyxEntry<BlockedFromCo
619617
function triggerNotifications<TKey extends OnyxKey>(
620618
onyxUpdates: Array<OnyxServerUpdate<TKey>>,
621619
currentUserAccountIDParam: number,
620+
currentUserEmail: string,
622621
reportAttributes?: ReportAttributesDerivedValue['reports'],
623622
) {
624623
for (const update of onyxUpdates) {
@@ -631,8 +630,8 @@ function triggerNotifications<TKey extends OnyxKey>(
631630

632631
for (const action of reportActions) {
633632
if (action) {
634-
// They aren't connected to a UI anywhere, it's OK to use currentEmail
635-
showReportActionNotification(reportID, action, currentUserAccountIDParam, currentEmail, reportAttributes);
633+
// They aren't connected to a UI anywhere, it's OK to use currentUserEmail
634+
showReportActionNotification(reportID, action, currentUserAccountIDParam, currentUserEmail, reportAttributes);
636635
}
637636
}
638637
}
@@ -652,7 +651,7 @@ const isChannelMuted = (reportId: string, currentUserAccountIDParam: number) =>
652651
});
653652
});
654653

655-
function playSoundForMessageType<TKey extends OnyxKey>(pushJSON: Array<OnyxServerUpdate<TKey>>, currentUserAccountIDParam: number) {
654+
function playSoundForMessageType<TKey extends OnyxKey>(pushJSON: Array<OnyxServerUpdate<TKey>>, currentUserAccountIDParam: number, currentUserEmail: string) {
656655
const reportActionsOnly = pushJSON.filter((update) => update.key?.includes('reportActions_'));
657656
// "reportActions_5134363522480668" -> "5134363522480668"
658657
const reportID = reportActionsOnly
@@ -699,7 +698,7 @@ function playSoundForMessageType<TKey extends OnyxKey>(pushJSON: Array<OnyxServe
699698
}
700699

701700
// mention user
702-
if ('html' in message && typeof message.html === 'string' && message.html.includes(`<mention-user>@${currentEmail}</mention-user>`)) {
701+
if ('html' in message && typeof message.html === 'string' && message.html.includes(`<mention-user>@${currentUserEmail}</mention-user>`)) {
703702
return playSound(SOUNDS.ATTENTION);
704703
}
705704

@@ -859,7 +858,7 @@ function initializePusherPingPong(currentUserAccountIDParam: number) {
859858
* Handles the newest events from Pusher where a single mega multipleEvents contains
860859
* an array of singular events all in one event
861860
*/
862-
function subscribeToUserEvents(currentUserAccountIDParam: number, getReportAttributes?: () => ReportAttributesDerivedValue['reports'] | undefined) {
861+
function subscribeToUserEvents(currentUserAccountIDParam: number, currentUserEmail: string, getReportAttributes?: () => ReportAttributesDerivedValue['reports'] | undefined) {
863862
// If we don't have the user's accountID yet (because the app isn't fully setup yet) we can't subscribe so return early
864863
if (!currentUserAccountIDParam) {
865864
return;
@@ -892,7 +891,7 @@ function subscribeToUserEvents(currentUserAccountIDParam: number, getReportAttri
892891
// See https://github.com/Expensify/App/issues/57961 for more details
893892
const debouncedPlaySoundForMessageType = debounce(
894893
(pushJSONMessage: AnyOnyxServerUpdate[]) => {
895-
playSoundForMessageType(pushJSONMessage, currentUserAccountIDParam);
894+
playSoundForMessageType(pushJSONMessage, currentUserAccountIDParam, currentUserEmail);
896895
},
897896
CONST.TIMING.PLAY_SOUND_MESSAGE_DEBOUNCE_TIME,
898897
{trailing: true},
@@ -914,7 +913,7 @@ function subscribeToUserEvents(currentUserAccountIDParam: number, getReportAttri
914913
}
915914

916915
const onyxUpdatePromise = Onyx.update(pushJSON).then(() => {
917-
triggerNotifications(pushJSON, currentUserAccountIDParam, getReportAttributes?.());
916+
triggerNotifications(pushJSON, currentUserAccountIDParam, currentUserEmail, getReportAttributes?.());
918917
});
919918

920919
// Return a promise when Onyx is done updating so that the OnyxUpdatesManager can properly apply all

tests/actions/IOUTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2299,7 +2299,7 @@ describe('actions/IOU', () => {
22992299

23002300
// Given a test user is signed in with Onyx setup and some initial data
23012301
await signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN);
2302-
subscribeToUserEvents(TEST_USER_ACCOUNT_ID, undefined);
2302+
subscribeToUserEvents(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN, undefined);
23032303
await waitForBatchedUpdates();
23042304
await setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID);
23052305

tests/actions/IOUTest/DeleteMoneyRequestTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
152152

153153
// Given a test user is signed in with Onyx setup and some initial data
154154
await signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN);
155-
subscribeToUserEvents(TEST_USER_ACCOUNT_ID, undefined);
155+
subscribeToUserEvents(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN, undefined);
156156
await waitForBatchedUpdates();
157157
await setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID);
158158

tests/actions/IOUTest/TrackExpenseTest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,7 +1749,7 @@ describe('actions/IOU/TrackExpense', () => {
17491749

17501750
// Given a test user is signed in with Onyx setup and some initial data
17511751
await signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN);
1752-
subscribeToUserEvents(TEST_USER_ACCOUNT_ID, undefined);
1752+
subscribeToUserEvents(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN, undefined);
17531753
await waitForBatchedUpdates();
17541754
await setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID);
17551755

@@ -2054,7 +2054,7 @@ describe('actions/IOU/TrackExpense', () => {
20542054
PusherHelper.setup();
20552055

20562056
await signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN);
2057-
subscribeToUserEvents(TEST_USER_ACCOUNT_ID, undefined);
2057+
subscribeToUserEvents(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN, undefined);
20582058
await waitForBatchedUpdates();
20592059
await setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID);
20602060

tests/actions/ReportTest.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ describe('actions/Report', () => {
241241
// Set up Onyx with some test user data
242242
return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN)
243243
.then(() => {
244-
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, undefined);
244+
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN, undefined);
245245
return waitForBatchedUpdates();
246246
})
247247
.then(() => TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID))
@@ -533,7 +533,7 @@ describe('actions/Report', () => {
533533
.then(waitForNetworkPromises)
534534
.then(() => {
535535
// Given a test user that is subscribed to Pusher events
536-
User.subscribeToUserEvents(USER_1_ACCOUNT_ID, undefined);
536+
User.subscribeToUserEvents(USER_1_ACCOUNT_ID, USER_1_LOGIN, undefined);
537537
return waitForBatchedUpdates();
538538
})
539539
.then(() => TestHelper.setPersonalDetails(USER_1_LOGIN, USER_1_ACCOUNT_ID))
@@ -882,10 +882,10 @@ describe('actions/Report', () => {
882882
};
883883

884884
// Setup user and pusher listeners
885-
return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID)
885+
return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN)
886886
.then(waitForBatchedUpdates)
887887
.then(() => {
888-
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, undefined);
888+
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN, undefined);
889889
return waitForBatchedUpdates();
890890
})
891891
.then(() => {
@@ -942,7 +942,7 @@ describe('actions/Report', () => {
942942
// Set up Onyx with some test user data
943943
return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN)
944944
.then(() => {
945-
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, undefined);
945+
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN, undefined);
946946
return waitForBatchedUpdates();
947947
})
948948
.then(() => TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID))
@@ -1079,7 +1079,7 @@ describe('actions/Report', () => {
10791079
// Set up Onyx with some test user data
10801080
return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN)
10811081
.then(() => {
1082-
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, undefined);
1082+
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN, undefined);
10831083
return waitForBatchedUpdates();
10841084
})
10851085
.then(() => TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID))

tests/ui/AuthScreensInitHandlerTest.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe('AuthScreensInitHandler', () => {
150150
await waitForBatchedUpdatesWithAct();
151151

152152
expect(mockedPusherInit).toHaveBeenCalled();
153-
expect(subscribeToUserEvents).toHaveBeenCalledWith(TEST_ACCOUNT_ID, expect.any(Function));
153+
expect(subscribeToUserEvents).toHaveBeenCalledWith(TEST_ACCOUNT_ID, 'test@test.com', expect.any(Function));
154154
});
155155

156156
it('calls subscribeToUserEvents from sign-in modal effect when SIGN_IN_MODAL is active', async () => {
@@ -164,7 +164,7 @@ describe('AuthScreensInitHandler', () => {
164164

165165
// Both mount effect AND sign-in modal effect fire → 2 calls
166166
expect(subscribeToUserEvents).toHaveBeenCalledTimes(2);
167-
expect(subscribeToUserEvents).toHaveBeenCalledWith(TEST_ACCOUNT_ID, expect.any(Function));
167+
expect(subscribeToUserEvents).toHaveBeenCalledWith(TEST_ACCOUNT_ID, 'test@test.com', expect.any(Function));
168168
});
169169

170170
it('getter passed to subscribeToUserEvents returns report attributes when available', async () => {
@@ -179,7 +179,7 @@ describe('AuthScreensInitHandler', () => {
179179

180180
const mockCalls = (subscribeToUserEvents as jest.Mock).mock.calls;
181181
const firstCallArgs = mockCalls.at(0) as unknown[];
182-
const getter = firstCallArgs.at(1) as () => unknown;
182+
const getter = firstCallArgs.at(2) as () => unknown;
183183
expect(getter()).toEqual(mockReports);
184184
});
185185

@@ -193,7 +193,7 @@ describe('AuthScreensInitHandler', () => {
193193

194194
const mockCalls = (subscribeToUserEvents as jest.Mock).mock.calls;
195195
const firstCallArgs = mockCalls.at(0) as unknown[];
196-
const getter = firstCallArgs.at(1) as () => unknown;
196+
const getter = firstCallArgs.at(2) as () => unknown;
197197
expect(getter()).toBeUndefined();
198198
});
199199

tests/ui/GroupChatNameTests.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ function signInAndGetApp(reportName = '', participantAccountIDs?: number[]): Pro
187187
})
188188
.then(async () => TestHelper.signInWithTestUser(USER_A_ACCOUNT_ID, USER_A_EMAIL, undefined, undefined, 'A'))
189189
.then(() => {
190-
subscribeToUserEvents(USER_A_ACCOUNT_ID, undefined);
190+
subscribeToUserEvents(USER_A_ACCOUNT_ID, USER_A_EMAIL, undefined);
191191
return waitForBatchedUpdates();
192192
})
193193
.then(async () => {

tests/ui/PaginationTest.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ async function signInAndGetApp(): Promise<void> {
226226
await waitForBatchedUpdatesWithAct();
227227

228228
// Start listening for pusher events after navigation settles.
229-
subscribeToUserEvents(USER_A_ACCOUNT_ID, undefined);
229+
subscribeToUserEvents(USER_A_ACCOUNT_ID, USER_A_EMAIL, undefined);
230230
await waitForBatchedUpdates();
231231

232232
await act(async () => {

tests/ui/UnreadIndicatorsTest.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ async function signInAndGetAppWithUnreadChat(): Promise<void> {
175175
renderAppOnce();
176176
await waitForBatchedUpdatesWithAct();
177177

178-
subscribeToUserEvents(USER_A_ACCOUNT_ID, undefined);
178+
subscribeToUserEvents(USER_A_ACCOUNT_ID, USER_A_EMAIL, undefined);
179179

180180
await waitForBatchedUpdates();
181181

0 commit comments

Comments
 (0)