Skip to content

Commit ba58ef6

Browse files
authored
Merge pull request Expensify#89758 from shubham1206agra/refactor-subscribeToUserEvents
[NoQA] Refactor: isolate subscribeToUserEvents from ONYXKEYS.SESSION Onyx data
2 parents dac25c7 + def505a commit ba58ef6

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
@@ -68,12 +68,10 @@ import {showReportActionNotification} from './Report';
6868
import {resendValidateCode as sessionResendValidateCode} from './Session';
6969

7070
let currentUserAccountID: number = CONST.DEFAULT_NUMBER_ID;
71-
let currentEmail = '';
7271
Onyx.connect({
7372
key: ONYXKEYS.SESSION,
7473
callback: (value) => {
7574
currentUserAccountID = value?.accountID ?? CONST.DEFAULT_NUMBER_ID;
76-
currentEmail = value?.email ?? '';
7775
},
7876
});
7977

@@ -677,6 +675,7 @@ function isBlockedFromConcierge(blockedFromConciergeNVP: OnyxEntry<BlockedFromCo
677675
function triggerNotifications<TKey extends OnyxKey>(
678676
onyxUpdates: Array<OnyxServerUpdate<TKey>>,
679677
currentUserAccountIDParam: number,
678+
currentUserEmail: string,
680679
reportAttributes?: ReportAttributesDerivedValue['reports'],
681680
) {
682681
for (const update of onyxUpdates) {
@@ -689,8 +688,8 @@ function triggerNotifications<TKey extends OnyxKey>(
689688

690689
for (const action of reportActions) {
691690
if (action) {
692-
// They aren't connected to a UI anywhere, it's OK to use currentEmail
693-
showReportActionNotification(reportID, action, currentUserAccountIDParam, currentEmail, reportAttributes);
691+
// They aren't connected to a UI anywhere, it's OK to use currentUserEmail
692+
showReportActionNotification(reportID, action, currentUserAccountIDParam, currentUserEmail, reportAttributes);
694693
}
695694
}
696695
}
@@ -710,7 +709,7 @@ const isChannelMuted = (reportId: string, currentUserAccountIDParam: number) =>
710709
});
711710
});
712711

713-
function playSoundForMessageType<TKey extends OnyxKey>(pushJSON: Array<OnyxServerUpdate<TKey>>, currentUserAccountIDParam: number) {
712+
function playSoundForMessageType<TKey extends OnyxKey>(pushJSON: Array<OnyxServerUpdate<TKey>>, currentUserAccountIDParam: number, currentUserEmail: string) {
714713
const reportActionsOnly = pushJSON.filter((update) => update.key?.includes('reportActions_'));
715714
// "reportActions_5134363522480668" -> "5134363522480668"
716715
const reportID = reportActionsOnly
@@ -757,7 +756,7 @@ function playSoundForMessageType<TKey extends OnyxKey>(pushJSON: Array<OnyxServe
757756
}
758757

759758
// mention user
760-
if ('html' in message && typeof message.html === 'string' && message.html.includes(`<mention-user>@${currentEmail}</mention-user>`)) {
759+
if ('html' in message && typeof message.html === 'string' && message.html.includes(`<mention-user>@${currentUserEmail}</mention-user>`)) {
761760
return playSound(SOUNDS.ATTENTION);
762761
}
763762

@@ -917,7 +916,7 @@ function initializePusherPingPong(currentUserAccountIDParam: number) {
917916
* Handles the newest events from Pusher where a single mega multipleEvents contains
918917
* an array of singular events all in one event
919918
*/
920-
function subscribeToUserEvents(currentUserAccountIDParam: number, getReportAttributes?: () => ReportAttributesDerivedValue['reports'] | undefined) {
919+
function subscribeToUserEvents(currentUserAccountIDParam: number, currentUserEmail: string, getReportAttributes?: () => ReportAttributesDerivedValue['reports'] | undefined) {
921920
// 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
922921
if (!currentUserAccountIDParam) {
923922
return;
@@ -950,7 +949,7 @@ function subscribeToUserEvents(currentUserAccountIDParam: number, getReportAttri
950949
// See https://github.com/Expensify/App/issues/57961 for more details
951950
const debouncedPlaySoundForMessageType = debounce(
952951
(pushJSONMessage: AnyOnyxServerUpdate[]) => {
953-
playSoundForMessageType(pushJSONMessage, currentUserAccountIDParam);
952+
playSoundForMessageType(pushJSONMessage, currentUserAccountIDParam, currentUserEmail);
954953
},
955954
CONST.TIMING.PLAY_SOUND_MESSAGE_DEBOUNCE_TIME,
956955
{trailing: true},
@@ -972,7 +971,7 @@ function subscribeToUserEvents(currentUserAccountIDParam: number, getReportAttri
972971
}
973972

974973
const onyxUpdatePromise = Onyx.update(pushJSON).then(() => {
975-
triggerNotifications(pushJSON, currentUserAccountIDParam, getReportAttributes?.());
974+
triggerNotifications(pushJSON, currentUserAccountIDParam, currentUserEmail, getReportAttributes?.());
976975
});
977976

978977
// 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
@@ -243,7 +243,7 @@ describe('actions/Report', () => {
243243
// Set up Onyx with some test user data
244244
return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN)
245245
.then(() => {
246-
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, undefined);
246+
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN, undefined);
247247
return waitForBatchedUpdates();
248248
})
249249
.then(() => TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID))
@@ -535,7 +535,7 @@ describe('actions/Report', () => {
535535
.then(waitForNetworkPromises)
536536
.then(() => {
537537
// Given a test user that is subscribed to Pusher events
538-
User.subscribeToUserEvents(USER_1_ACCOUNT_ID, undefined);
538+
User.subscribeToUserEvents(USER_1_ACCOUNT_ID, USER_1_LOGIN, undefined);
539539
return waitForBatchedUpdates();
540540
})
541541
.then(() => TestHelper.setPersonalDetails(USER_1_LOGIN, USER_1_ACCOUNT_ID))
@@ -884,10 +884,10 @@ describe('actions/Report', () => {
884884
};
885885

886886
// Setup user and pusher listeners
887-
return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID)
887+
return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN)
888888
.then(waitForBatchedUpdates)
889889
.then(() => {
890-
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, undefined);
890+
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN, undefined);
891891
return waitForBatchedUpdates();
892892
})
893893
.then(() => {
@@ -944,7 +944,7 @@ describe('actions/Report', () => {
944944
// Set up Onyx with some test user data
945945
return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN)
946946
.then(() => {
947-
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, undefined);
947+
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN, undefined);
948948
return waitForBatchedUpdates();
949949
})
950950
.then(() => TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID))
@@ -1081,7 +1081,7 @@ describe('actions/Report', () => {
10811081
// Set up Onyx with some test user data
10821082
return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN)
10831083
.then(() => {
1084-
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, undefined);
1084+
User.subscribeToUserEvents(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN, undefined);
10851085
return waitForBatchedUpdates();
10861086
})
10871087
.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)