Skip to content

Commit

Permalink
chore: disable moe push preferences if no push token or push permissi…
Browse files Browse the repository at this point in the history
…ons (#34)
  • Loading branch information
ice-hades authored Apr 21, 2023
1 parent fd50d7a commit ad552ca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/store/modules/Analytics/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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'
Expand All @@ -180,13 +184,4 @@ export const AnalyticsAttributesLogger = {
enabledNotificationDomains.toString(),
);
},
updateEmailPreferences: () => {
const enabledNotificationDomains = enabledNotificationDomainsSelector(
'email',
)(store.getState());
Attributes.trackUserAttribute(
'Email preferences',
enabledNotificationDomains.toString(),
);
},
};
26 changes: 26 additions & 0 deletions src/store/modules/Analytics/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit ad552ca

Please sign in to comment.