From ad552caaf9930aa091fa3eb7b515bfce8fe0075a Mon Sep 17 00:00:00 2001 From: ice-hades <119406114+ice-hades@users.noreply.github.com> Date: Fri, 21 Apr 2023 12:52:14 +0300 Subject: [PATCH] chore: disable moe push preferences if no push token or push permissions (#34) --- src/store/modules/Analytics/constants.ts | 21 ++++++--------- src/store/modules/Analytics/utils.ts | 26 +++++++++++++++++++ .../hooks/useInitNotifications.tsx | 4 +++ 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 src/store/modules/Analytics/utils.ts diff --git a/src/store/modules/Analytics/constants.ts b/src/store/modules/Analytics/constants.ts index 7d5f3959e..8fb985400 100644 --- a/src/store/modules/Analytics/constants.ts +++ b/src/store/modules/Analytics/constants.ts @@ -15,6 +15,7 @@ import { EventNamesType, TapToMineActionType, } from '@store/modules/Analytics/types'; +import {isPreferencesEnabled} from '@store/modules/Analytics/utils'; import {enabledNotificationDomainsSelector} from '@store/modules/Devices/selectors'; export const EVENT_NAMES = { @@ -163,14 +164,17 @@ export const AnalyticsEventLogger = { }; export const AnalyticsAttributesLogger = { - updateNotificationPreferences: ({ + updateNotificationPreferences: async ({ notificationDeliveryChannel, }: { notificationDeliveryChannel: NotificationDeliveryChannel; }) => { - const enabledNotificationDomains = enabledNotificationDomainsSelector( - notificationDeliveryChannel, - )(store.getState()); + const isEnabled = await isPreferencesEnabled({notificationDeliveryChannel}); + const enabledNotificationDomains = isEnabled + ? enabledNotificationDomainsSelector(notificationDeliveryChannel)( + store.getState(), + ) + : []; const attributeName = notificationDeliveryChannel === 'push' ? 'Push preferences' @@ -180,13 +184,4 @@ export const AnalyticsAttributesLogger = { enabledNotificationDomains.toString(), ); }, - updateEmailPreferences: () => { - const enabledNotificationDomains = enabledNotificationDomainsSelector( - 'email', - )(store.getState()); - Attributes.trackUserAttribute( - 'Email preferences', - enabledNotificationDomains.toString(), - ); - }, }; diff --git a/src/store/modules/Analytics/utils.ts b/src/store/modules/Analytics/utils.ts new file mode 100644 index 000000000..94fd76ac1 --- /dev/null +++ b/src/store/modules/Analytics/utils.ts @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: ice License 1.0 + +import {NotificationDeliveryChannel} from '@api/devices/types'; +import {getFcmToken} from '@services/firebase'; +import {store} from '@store/configureStore'; +import {isPermissionGrantedSelector} from '@store/modules/Permissions/selectors'; + +export async function isPreferencesEnabled({ + notificationDeliveryChannel, +}: { + notificationDeliveryChannel: NotificationDeliveryChannel; +}) { + if (notificationDeliveryChannel === 'push') { + const hasPushPermissions = isPermissionGrantedSelector('pushNotifications')( + store.getState(), + ); + if (!hasPushPermissions) { + return false; + } + const token = await getFcmToken(); + if (!token) { + return false; + } + } + return true; +} diff --git a/src/store/modules/PushNotifications/hooks/useInitNotifications.tsx b/src/store/modules/PushNotifications/hooks/useInitNotifications.tsx index 6468f2d97..3f0d1342b 100644 --- a/src/store/modules/PushNotifications/hooks/useInitNotifications.tsx +++ b/src/store/modules/PushNotifications/hooks/useInitNotifications.tsx @@ -2,6 +2,7 @@ import {ENV} from '@constants/env'; import {userIdSelector} from '@store/modules/Account/selectors'; +import {AnalyticsAttributesLogger} from '@store/modules/Analytics/constants'; import {DeviceActions} from '@store/modules/Devices/actions'; import {isPermissionGrantedSelector} from '@store/modules/Permissions/selectors'; import {useSubscribeToChannelTopic} from '@store/modules/PushNotifications/hooks/useSubscribeToChannelTopic'; @@ -40,6 +41,9 @@ export function useInitNotifications() { dispatch( DeviceActions.UPDATE_DEVICE_METADATA.START.create({forceUpdate: true}), ); + AnalyticsAttributesLogger.updateNotificationPreferences({ + notificationDeliveryChannel: 'push', + }); }, [dispatch, hasPushPermissions, userId]); useSubscribeToChannelTopic('news');