From 62f8e120405a741225e79a755a55a7eba32d69ac Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Wed, 10 Apr 2024 09:41:50 +0200 Subject: [PATCH 1/5] adding checks on pusher subscriptions --- src/libs/actions/User.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 2d23edfba93f..e552e818c56e 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -2,6 +2,7 @@ import {isBefore} from 'date-fns'; import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import type {ValueOf} from 'type-fest'; +import * as ActiveClientManager from '@libs/ActiveClientManager'; import * as API from '@libs/API'; import type { AddNewContactMethodParams, @@ -583,6 +584,10 @@ function subscribeToUserEvents() { // Handles the mega multipleEvents from Pusher which contains an array of single events. // Each single event is passed to PusherUtils in order to trigger the callbacks for that event PusherUtils.subscribeToPrivateUserChannelEvent(Pusher.TYPE.MULTIPLE_EVENTS, currentUserAccountID.toString(), (pushJSON) => { + // If this is not the main client, we shouldn't process any data received from pusher. + if (!ActiveClientManager.isClientTheLeader()) { + return; + } // The data for the update is an object, containing updateIDs from the server and an array of onyx updates (this array is the same format as the original format above) // Example: {lastUpdateID: 1, previousUpdateID: 0, updates: [{onyxMethod: 'whatever', key: 'foo', value: 'bar'}]} const updates = { @@ -599,8 +604,9 @@ function subscribeToUserEvents() { playSoundForMessageType(pushJSON); return SequentialQueue.getCurrentRequest().then(() => { - // If we don't have the currentUserAccountID (user is logged out) we don't want to update Onyx with data from Pusher - if (currentUserAccountID === -1) { + // If we don't have the currentUserAccountID (user is logged out) or this is not the + // main client we don't want to update Onyx with data from Pusher + if (currentUserAccountID === -1 || !ActiveClientManager.isClientTheLeader()) { return; } From 917d5e4ea33a4e57e76c6ad501a154b230f96698 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Wed, 10 Apr 2024 09:43:31 +0200 Subject: [PATCH 2/5] added check to urban airship notifications --- .../subscribeToReportCommentPushNotifications.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.ts b/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.ts index 71f68d69acc7..07f4beba5561 100644 --- a/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.ts +++ b/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.ts @@ -1,5 +1,6 @@ import Onyx from 'react-native-onyx'; import * as OnyxUpdates from '@libs/actions/OnyxUpdates'; +import * as ActiveClientManager from '@libs/ActiveClientManager'; import Log from '@libs/Log'; import Navigation from '@libs/Navigation/Navigation'; import getPolicyMemberAccountIDs from '@libs/PolicyMembersUtils'; @@ -30,6 +31,9 @@ Onyx.connect({ */ export default function subscribeToReportCommentPushNotifications() { PushNotification.onReceived(PushNotification.TYPE.REPORT_COMMENT, ({reportID, reportActionID, onyxData, lastUpdateID, previousUpdateID}) => { + if (!ActiveClientManager.isClientTheLeader()) { + return; + } Log.info(`[PushNotification] received report comment notification in the ${Visibility.isVisible() ? 'foreground' : 'background'}`, false, {reportID, reportActionID}); if (onyxData && lastUpdateID && previousUpdateID) { From 45268af04c2ad8a5de7c48611bfc5e6b30395d42 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Wed, 10 Apr 2024 09:44:16 +0200 Subject: [PATCH 3/5] adding log line --- .../subscribeToReportCommentPushNotifications.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.ts b/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.ts index 07f4beba5561..8d64f163217a 100644 --- a/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.ts +++ b/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.ts @@ -32,6 +32,7 @@ Onyx.connect({ export default function subscribeToReportCommentPushNotifications() { PushNotification.onReceived(PushNotification.TYPE.REPORT_COMMENT, ({reportID, reportActionID, onyxData, lastUpdateID, previousUpdateID}) => { if (!ActiveClientManager.isClientTheLeader()) { + Log.info(`[PushNotification] received report comment notification, but ignoring it since this is not the active client`); return; } Log.info(`[PushNotification] received report comment notification in the ${Visibility.isVisible() ? 'foreground' : 'background'}`, false, {reportID, reportActionID}); From 99eec243a69da96f65df22156d66f49de3d5dff0 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Wed, 10 Apr 2024 09:45:46 +0200 Subject: [PATCH 4/5] adding log lines --- .../subscribeToReportCommentPushNotifications.ts | 2 +- src/libs/actions/User.ts | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.ts b/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.ts index 8d64f163217a..36051fa35c56 100644 --- a/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.ts +++ b/src/libs/Notification/PushNotification/subscribeToReportCommentPushNotifications.ts @@ -32,7 +32,7 @@ Onyx.connect({ export default function subscribeToReportCommentPushNotifications() { PushNotification.onReceived(PushNotification.TYPE.REPORT_COMMENT, ({reportID, reportActionID, onyxData, lastUpdateID, previousUpdateID}) => { if (!ActiveClientManager.isClientTheLeader()) { - Log.info(`[PushNotification] received report comment notification, but ignoring it since this is not the active client`); + Log.info('[PushNotification] received report comment notification, but ignoring it since this is not the active client'); return; } Log.info(`[PushNotification] received report comment notification in the ${Visibility.isVisible() ? 'foreground' : 'background'}`, false, {reportID, reportActionID}); diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index e552e818c56e..4740c2dda22b 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -586,6 +586,7 @@ function subscribeToUserEvents() { PusherUtils.subscribeToPrivateUserChannelEvent(Pusher.TYPE.MULTIPLE_EVENTS, currentUserAccountID.toString(), (pushJSON) => { // If this is not the main client, we shouldn't process any data received from pusher. if (!ActiveClientManager.isClientTheLeader()) { + Log.info('[Pusher] Received updates, but ignoring it since this is not the active client'); return; } // The data for the update is an object, containing updateIDs from the server and an array of onyx updates (this array is the same format as the original format above) @@ -606,7 +607,11 @@ function subscribeToUserEvents() { return SequentialQueue.getCurrentRequest().then(() => { // If we don't have the currentUserAccountID (user is logged out) or this is not the // main client we don't want to update Onyx with data from Pusher - if (currentUserAccountID === -1 || !ActiveClientManager.isClientTheLeader()) { + if (currentUserAccountID === -1) { + return; + } + if(!ActiveClientManager.isClientTheLeader()) { + Log.info('[Pusher] Received updates, but ignoring it since this is not the active client'); return; } From 7cd28714f258093185d51ffc4be474577e31402f Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Wed, 10 Apr 2024 09:57:37 +0200 Subject: [PATCH 5/5] prettier --- src/libs/actions/User.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 4740c2dda22b..dcd6e025e23b 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -605,12 +605,12 @@ function subscribeToUserEvents() { playSoundForMessageType(pushJSON); return SequentialQueue.getCurrentRequest().then(() => { - // If we don't have the currentUserAccountID (user is logged out) or this is not the + // If we don't have the currentUserAccountID (user is logged out) or this is not the // main client we don't want to update Onyx with data from Pusher if (currentUserAccountID === -1) { return; } - if(!ActiveClientManager.isClientTheLeader()) { + if (!ActiveClientManager.isClientTheLeader()) { Log.info('[Pusher] Received updates, but ignoring it since this is not the active client'); return; }