@metamask-previews/notification-services-controller 18.3.0-preview-35a5c8a7 → 18.3.0-preview-a649a76b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -6
- package/dist/NotificationServicesController/NotificationServicesController.cjs +32 -32
- package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.d.cts +6 -7
- package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.d.mts +6 -7
- package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.mjs +31 -31
- package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs +10 -10
- package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +5 -5
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +5 -5
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +9 -9
- package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
- package/package.json +2 -3
|
@@ -16,7 +16,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
16
16
|
var _NotificationServicesPushController_instances, _NotificationServicesPushController_pushListenerUnsubscribe, _NotificationServicesPushController_env, _NotificationServicesPushController_config, _NotificationServicesPushController_registerMessageHandlers, _NotificationServicesPushController_clearLoadingStates, _NotificationServicesPushController_getAndAssertBearerToken, _NotificationServicesPushController_updatePushState;
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.defaultState = void 0;
|
|
19
|
-
const
|
|
19
|
+
const base_controller_1 = require("@metamask/base-controller");
|
|
20
20
|
const loglevel_1 = __importDefault(require("loglevel"));
|
|
21
21
|
const services_1 = require("./services/services.cjs");
|
|
22
22
|
const controllerName = 'NotificationServicesPushController';
|
|
@@ -29,19 +29,19 @@ const metadata = {
|
|
|
29
29
|
isPushEnabled: {
|
|
30
30
|
includeInStateLogs: true,
|
|
31
31
|
persist: true,
|
|
32
|
-
|
|
32
|
+
anonymous: true,
|
|
33
33
|
usedInUi: true,
|
|
34
34
|
},
|
|
35
35
|
fcmToken: {
|
|
36
36
|
includeInStateLogs: false,
|
|
37
37
|
persist: true,
|
|
38
|
-
|
|
38
|
+
anonymous: true,
|
|
39
39
|
usedInUi: true,
|
|
40
40
|
},
|
|
41
41
|
isUpdatingFCMToken: {
|
|
42
42
|
includeInStateLogs: false,
|
|
43
43
|
persist: false,
|
|
44
|
-
|
|
44
|
+
anonymous: true,
|
|
45
45
|
usedInUi: true,
|
|
46
46
|
},
|
|
47
47
|
};
|
|
@@ -62,7 +62,7 @@ const defaultPushEnv = {
|
|
|
62
62
|
* managing the FCM token, and communicating with the server to register or unregister the device for push notifications.
|
|
63
63
|
* Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.
|
|
64
64
|
*/
|
|
65
|
-
class NotificationServicesPushController extends
|
|
65
|
+
class NotificationServicesPushController extends base_controller_1.BaseController {
|
|
66
66
|
constructor({ messenger, state, env, config, }) {
|
|
67
67
|
super({
|
|
68
68
|
messenger,
|
|
@@ -227,16 +227,16 @@ class NotificationServicesPushController extends next_1.BaseController {
|
|
|
227
227
|
}
|
|
228
228
|
}
|
|
229
229
|
_NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _NotificationServicesPushController_env = new WeakMap(), _NotificationServicesPushController_config = new WeakMap(), _NotificationServicesPushController_instances = new WeakSet(), _NotificationServicesPushController_registerMessageHandlers = function _NotificationServicesPushController_registerMessageHandlers() {
|
|
230
|
-
this.
|
|
231
|
-
this.
|
|
232
|
-
this.
|
|
233
|
-
this.
|
|
230
|
+
this.messagingSystem.registerActionHandler('NotificationServicesPushController:enablePushNotifications', this.enablePushNotifications.bind(this));
|
|
231
|
+
this.messagingSystem.registerActionHandler('NotificationServicesPushController:disablePushNotifications', this.disablePushNotifications.bind(this));
|
|
232
|
+
this.messagingSystem.registerActionHandler('NotificationServicesPushController:updateTriggerPushNotifications', this.updateTriggerPushNotifications.bind(this));
|
|
233
|
+
this.messagingSystem.registerActionHandler('NotificationServicesPushController:subscribeToPushNotifications', this.subscribeToPushNotifications.bind(this));
|
|
234
234
|
}, _NotificationServicesPushController_clearLoadingStates = function _NotificationServicesPushController_clearLoadingStates() {
|
|
235
235
|
this.update((state) => {
|
|
236
236
|
state.isUpdatingFCMToken = false;
|
|
237
237
|
});
|
|
238
238
|
}, _NotificationServicesPushController_getAndAssertBearerToken = async function _NotificationServicesPushController_getAndAssertBearerToken() {
|
|
239
|
-
const bearerToken = await this.
|
|
239
|
+
const bearerToken = await this.messagingSystem.call('AuthenticationController:getBearerToken');
|
|
240
240
|
if (!bearerToken) {
|
|
241
241
|
loglevel_1.default.error('Failed to enable push notifications: BearerToken token is missing.');
|
|
242
242
|
throw new Error('BearerToken token is missing');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NotificationServicesPushController.cjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAKA,yDAAgE;AAGhE,wDAA2B;AAE3B,sDAG6B;AAK7B,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAqE/C,QAAA,YAAY,GAA4C;IACnE,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,EAAE;IACZ,kBAAkB,EAAE,KAAK;CAC1B,CAAC;AACF,MAAM,QAAQ,GAA2D;IACvE,aAAa,EAAE;QACb,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,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AAkB,GAAG,KAAK,CAAC;QACnC,CAAC,CAAC,CAAC;KACJ;IAED,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS,EAAE;QAC9B,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,iDAAiD;YACjD,+EAA+E;YAC/E,KAAK,CAAC,aAAa,GAAG,KAAK,CAAC;YAC5B,KAAK,CAAC,kBAAkB,GAAG,KAAK,CAAC;QACnC,CAAC,CAAC,CAAC;KACJ;IAED,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE;QAC7B,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,aAAa,GAAG,IAAI,CAAC;YAC3B,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;YAClC,KAAK,CAAC,kBAAkB,GAAG,KAAK,CAAC;QACnC,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;kBArGkB,kCAAkC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller/next';\nimport { BaseController } from '@metamask/base-controller/next';\nimport type { Messenger } from '@metamask/messenger';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\nimport type { PushService } from './types/push-service-interface';\nimport type { Types } from '../NotificationServicesController';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n isPushEnabled: boolean;\n fcmToken: string;\n isUpdatingFCMToken: boolean;\n};\n\nexport type NotificationServicesPushControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\nexport type NotificationServicesPushControllerSubscribeToNotificationsAction = {\n type: `${typeof controllerName}:subscribeToPushNotifications`;\n handler: NotificationServicesPushController['subscribeToPushNotifications'];\n};\n\nexport type Actions =\n | NotificationServicesPushControllerGetStateAction\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | NotificationServicesPushControllerSubscribeToNotificationsAction;\n\ntype AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClickedEvent = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type Events =\n | NotificationServicesPushControllerStateChangeEvent\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClickedEvent;\n\nexport type NotificationServicesPushControllerMessenger = Messenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events\n>;\n\nexport const defaultState: NotificationServicesPushControllerState = {\n isPushEnabled: true,\n fcmToken: '',\n isUpdatingFCMToken: false,\n};\nconst metadata: StateMetadata<NotificationServicesPushControllerState> = {\n isPushEnabled: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n fcmToken: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n isUpdatingFCMToken: {\n includeInStateLogs: false,\n persist: false,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n};\n\nconst defaultPushEnv: PushNotificationEnv = {\n apiKey: '',\n authDomain: '',\n storageBucket: '',\n projectId: '',\n messagingSenderId: '',\n appId: '',\n measurementId: '',\n vapidKey: '',\n};\n\nexport type ControllerConfig = {\n /**\n * User locale for server push notifications\n */\n getLocale?: () => string;\n\n /**\n * Global switch to determine to use push notifications\n * Allows us to control Builds on extension (MV2 vs MV3)\n */\n isPushFeatureEnabled?: boolean;\n\n /**\n * determine the config used for push notification services\n */\n platform: 'extension' | 'mobile';\n\n /**\n * Push Service Interface\n * - create reg token\n * - delete reg token\n * - subscribe to push notifications\n */\n pushService: PushService;\n};\n\ntype StateCommand =\n | { type: 'enable'; fcmToken: string }\n | { type: 'disable' }\n | { type: 'update'; fcmToken: string };\n\n/**\n * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.\n * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.\n * It is responsible for registering and unregistering the service worker that listens for push notifications,\n * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.\n * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.\n */\nexport default class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n readonly #env: PushNotificationEnv;\n\n readonly #config: ControllerConfig;\n\n constructor({\n messenger,\n state,\n env,\n config,\n }: {\n messenger: NotificationServicesPushControllerMessenger;\n state: NotificationServicesPushControllerState;\n /** Push Environment is only required for extension */\n env?: PushNotificationEnv;\n config: ControllerConfig;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n this.#env = env ?? defaultPushEnv;\n this.#config = config;\n\n this.#registerMessageHandlers();\n this.#clearLoadingStates();\n }\n\n #registerMessageHandlers(): void {\n this.messenger.registerActionHandler(\n 'NotificationServicesPushController:enablePushNotifications',\n this.enablePushNotifications.bind(this),\n );\n this.messenger.registerActionHandler(\n 'NotificationServicesPushController:disablePushNotifications',\n this.disablePushNotifications.bind(this),\n );\n this.messenger.registerActionHandler(\n 'NotificationServicesPushController:updateTriggerPushNotifications',\n this.updateTriggerPushNotifications.bind(this),\n );\n this.messenger.registerActionHandler(\n 'NotificationServicesPushController:subscribeToPushNotifications',\n this.subscribeToPushNotifications.bind(this),\n );\n }\n\n #clearLoadingStates(): void {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n async #getAndAssertBearerToken() {\n const bearerToken = await this.messenger.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n log.error(\n 'Failed to enable push notifications: BearerToken token is missing.',\n );\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n #updatePushState(command: StateCommand) {\n if (command.type === 'enable') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'disable') {\n this.update((state) => {\n // Note we do not want to clear the old FCM token\n // We can send it as an old token to our backend to cleanup next time turned on\n state.isPushEnabled = false;\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'update') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n }\n\n public async subscribeToPushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n if (this.#pushListenerUnsubscribe) {\n this.#pushListenerUnsubscribe();\n this.#pushListenerUnsubscribe = undefined;\n }\n\n try {\n this.#pushListenerUnsubscribe =\n (await this.#config.pushService.subscribeToPushNotifications(\n this.#env,\n )) ?? undefined;\n } catch {\n // Do nothing, we are silently failing if push notification registration fails\n }\n }\n\n /**\n * Enables push notifications for the application.\n *\n * This method sets up the necessary infrastructure for handling push notifications by:\n * 1. Registering the service worker to listen for messages.\n * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.\n * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.\n *\n * @param addresses - An array of addresses to enable push notifications for.\n */\n public async enablePushNotifications(addresses: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n // Handle creating new reg token (if available)\n try {\n const bearerToken = await this.#getAndAssertBearerToken().catch(\n () => null,\n );\n\n // If there is a bearer token, lets try to refresh/create new reg token\n if (bearerToken) {\n // Activate Push Notifications\n const fcmToken = await activatePushNotifications({\n bearerToken,\n addresses,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n regToken: {\n platform: this.#config.platform,\n locale: this.#config.getLocale?.() ?? 'en',\n oldToken: this.state.fcmToken,\n },\n });\n\n if (fcmToken) {\n this.#updatePushState({ type: 'enable', fcmToken });\n }\n }\n } catch {\n // Do nothing, we are silently failing\n }\n\n // New token created, (re)subscribe to push notifications\n try {\n await this.subscribeToPushNotifications();\n } catch {\n // Do nothing we are silently failing\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n /**\n * Disables push notifications for the application.\n * This removes the registration token on this device, and ensures we unsubscribe from any listeners\n */\n public async disablePushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n // Send a request to the server to unregister the token/device\n await deactivatePushNotifications({\n env: this.#env,\n deleteRegToken: this.#config.pushService.deleteRegToken,\n regToken: this.state.fcmToken,\n });\n } catch (error) {\n const errorMessage = `Failed to disable push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n } finally {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n this.#updatePushState({ type: 'disable' });\n }\n\n /**\n * Updates the triggers for push notifications.\n * This method is responsible for updating the server with the new set of addresses that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param addresses - An array of addresses that should trigger push notifications.\n * @deprecated - this is not used anymore and will most likely be removed\n */\n public async updateTriggerPushNotifications(addresses: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n const bearerToken = await this.#getAndAssertBearerToken();\n const fcmToken = await activatePushNotifications({\n bearerToken,\n addresses,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n regToken: {\n platform: this.#config.platform,\n locale: this.#config.getLocale?.() ?? 'en',\n oldToken: this.state.fcmToken,\n },\n });\n\n // update the state with the new FCM token\n if (fcmToken) {\n this.#updatePushState({ type: 'update', fcmToken });\n }\n } catch (error) {\n const errorMessage = `Failed to update triggers for push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n } finally {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"NotificationServicesPushController.cjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAMA,+DAA2D;AAE3D,wDAA2B;AAE3B,sDAG6B;AAK7B,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAyE/C,QAAA,YAAY,GAA4C;IACnE,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,EAAE;IACZ,kBAAkB,EAAE,KAAK;CAC1B,CAAC;AACF,MAAM,QAAQ,GAA2D;IACvE,aAAa,EAAE;QACb,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;QACf,QAAQ,EAAE,IAAI;KACf;IACD,QAAQ,EAAE;QACR,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;QACf,QAAQ,EAAE,IAAI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AC,KAAK,EAAE,EAAE;YACpB,iDAAiD;YACjD,+EAA+E;YAC/E,KAAK,CAAC,aAAa,GAAG,KAAK,CAAC;YAC5B,KAAK,CAAC,kBAAkB,GAAG,KAAK,CAAC;QACnC,CAAC,CAAC,CAAC;KACJ;IAED,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE;QAC7B,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,aAAa,GAAG,IAAI,CAAC;YAC3B,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;YAClC,KAAK,CAAC,kBAAkB,GAAG,KAAK,CAAC;QACnC,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;kBArGkB,kCAAkC","sourcesContent":["import type {\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\nimport type { PushService } from './types/push-service-interface';\nimport type { Types } from '../NotificationServicesController';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n isPushEnabled: boolean;\n fcmToken: string;\n isUpdatingFCMToken: boolean;\n};\n\nexport type NotificationServicesPushControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\nexport type NotificationServicesPushControllerSubscribeToNotificationsAction = {\n type: `${typeof controllerName}:subscribeToPushNotifications`;\n handler: NotificationServicesPushController['subscribeToPushNotifications'];\n};\n\nexport type Actions =\n | NotificationServicesPushControllerGetStateAction\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | NotificationServicesPushControllerSubscribeToNotificationsAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClickedEvent = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type Events =\n | NotificationServicesPushControllerStateChangeEvent\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClickedEvent;\n\nexport type AllowedEvents = never;\n\nexport type NotificationServicesPushControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\nexport const defaultState: NotificationServicesPushControllerState = {\n isPushEnabled: true,\n fcmToken: '',\n isUpdatingFCMToken: false,\n};\nconst metadata: StateMetadata<NotificationServicesPushControllerState> = {\n isPushEnabled: {\n includeInStateLogs: true,\n persist: true,\n anonymous: true,\n usedInUi: true,\n },\n fcmToken: {\n includeInStateLogs: false,\n persist: true,\n anonymous: true,\n usedInUi: true,\n },\n isUpdatingFCMToken: {\n includeInStateLogs: false,\n persist: false,\n anonymous: true,\n usedInUi: true,\n },\n};\n\nconst defaultPushEnv: PushNotificationEnv = {\n apiKey: '',\n authDomain: '',\n storageBucket: '',\n projectId: '',\n messagingSenderId: '',\n appId: '',\n measurementId: '',\n vapidKey: '',\n};\n\nexport type ControllerConfig = {\n /**\n * User locale for server push notifications\n */\n getLocale?: () => string;\n\n /**\n * Global switch to determine to use push notifications\n * Allows us to control Builds on extension (MV2 vs MV3)\n */\n isPushFeatureEnabled?: boolean;\n\n /**\n * determine the config used for push notification services\n */\n platform: 'extension' | 'mobile';\n\n /**\n * Push Service Interface\n * - create reg token\n * - delete reg token\n * - subscribe to push notifications\n */\n pushService: PushService;\n};\n\ntype StateCommand =\n | { type: 'enable'; fcmToken: string }\n | { type: 'disable' }\n | { type: 'update'; fcmToken: string };\n\n/**\n * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.\n * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.\n * It is responsible for registering and unregistering the service worker that listens for push notifications,\n * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.\n * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.\n */\nexport default class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n readonly #env: PushNotificationEnv;\n\n readonly #config: ControllerConfig;\n\n constructor({\n messenger,\n state,\n env,\n config,\n }: {\n messenger: NotificationServicesPushControllerMessenger;\n state: NotificationServicesPushControllerState;\n /** Push Environment is only required for extension */\n env?: PushNotificationEnv;\n config: ControllerConfig;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n this.#env = env ?? defaultPushEnv;\n this.#config = config;\n\n this.#registerMessageHandlers();\n this.#clearLoadingStates();\n }\n\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:enablePushNotifications',\n this.enablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:disablePushNotifications',\n this.disablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:updateTriggerPushNotifications',\n this.updateTriggerPushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:subscribeToPushNotifications',\n this.subscribeToPushNotifications.bind(this),\n );\n }\n\n #clearLoadingStates(): void {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n async #getAndAssertBearerToken() {\n const bearerToken = await this.messagingSystem.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n log.error(\n 'Failed to enable push notifications: BearerToken token is missing.',\n );\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n #updatePushState(command: StateCommand) {\n if (command.type === 'enable') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'disable') {\n this.update((state) => {\n // Note we do not want to clear the old FCM token\n // We can send it as an old token to our backend to cleanup next time turned on\n state.isPushEnabled = false;\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'update') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n }\n\n public async subscribeToPushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n if (this.#pushListenerUnsubscribe) {\n this.#pushListenerUnsubscribe();\n this.#pushListenerUnsubscribe = undefined;\n }\n\n try {\n this.#pushListenerUnsubscribe =\n (await this.#config.pushService.subscribeToPushNotifications(\n this.#env,\n )) ?? undefined;\n } catch {\n // Do nothing, we are silently failing if push notification registration fails\n }\n }\n\n /**\n * Enables push notifications for the application.\n *\n * This method sets up the necessary infrastructure for handling push notifications by:\n * 1. Registering the service worker to listen for messages.\n * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.\n * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.\n *\n * @param addresses - An array of addresses to enable push notifications for.\n */\n public async enablePushNotifications(addresses: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n // Handle creating new reg token (if available)\n try {\n const bearerToken = await this.#getAndAssertBearerToken().catch(\n () => null,\n );\n\n // If there is a bearer token, lets try to refresh/create new reg token\n if (bearerToken) {\n // Activate Push Notifications\n const fcmToken = await activatePushNotifications({\n bearerToken,\n addresses,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n regToken: {\n platform: this.#config.platform,\n locale: this.#config.getLocale?.() ?? 'en',\n oldToken: this.state.fcmToken,\n },\n });\n\n if (fcmToken) {\n this.#updatePushState({ type: 'enable', fcmToken });\n }\n }\n } catch {\n // Do nothing, we are silently failing\n }\n\n // New token created, (re)subscribe to push notifications\n try {\n await this.subscribeToPushNotifications();\n } catch {\n // Do nothing we are silently failing\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n /**\n * Disables push notifications for the application.\n * This removes the registration token on this device, and ensures we unsubscribe from any listeners\n */\n public async disablePushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n // Send a request to the server to unregister the token/device\n await deactivatePushNotifications({\n env: this.#env,\n deleteRegToken: this.#config.pushService.deleteRegToken,\n regToken: this.state.fcmToken,\n });\n } catch (error) {\n const errorMessage = `Failed to disable push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n } finally {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n this.#updatePushState({ type: 'disable' });\n }\n\n /**\n * Updates the triggers for push notifications.\n * This method is responsible for updating the server with the new set of addresses that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param addresses - An array of addresses that should trigger push notifications.\n * @deprecated - this is not used anymore and will most likely be removed\n */\n public async updateTriggerPushNotifications(addresses: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n const bearerToken = await this.#getAndAssertBearerToken();\n const fcmToken = await activatePushNotifications({\n bearerToken,\n addresses,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n regToken: {\n platform: this.#config.platform,\n locale: this.#config.getLocale?.() ?? 'en',\n oldToken: this.state.fcmToken,\n },\n });\n\n // update the state with the new FCM token\n if (fcmToken) {\n this.#updatePushState({ type: 'update', fcmToken });\n }\n } catch (error) {\n const errorMessage = `Failed to update triggers for push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n } finally {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n }\n}\n"]}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller
|
|
2
|
-
import { BaseController } from "@metamask/base-controller
|
|
3
|
-
import type { Messenger } from "@metamask/messenger";
|
|
1
|
+
import type { RestrictedMessenger, ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
|
+
import { BaseController } from "@metamask/base-controller";
|
|
4
3
|
import type { AuthenticationController } from "@metamask/profile-sync-controller";
|
|
5
4
|
import type { PushNotificationEnv } from "./types/index.cjs";
|
|
6
5
|
import type { PushService } from "./types/push-service-interface.cjs";
|
|
@@ -29,7 +28,7 @@ export type NotificationServicesPushControllerSubscribeToNotificationsAction = {
|
|
|
29
28
|
handler: NotificationServicesPushController['subscribeToPushNotifications'];
|
|
30
29
|
};
|
|
31
30
|
export type Actions = NotificationServicesPushControllerGetStateAction | NotificationServicesPushControllerEnablePushNotificationsAction | NotificationServicesPushControllerDisablePushNotificationsAction | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction | NotificationServicesPushControllerSubscribeToNotificationsAction;
|
|
32
|
-
type AllowedActions = AuthenticationController.AuthenticationControllerGetBearerToken;
|
|
31
|
+
export type AllowedActions = AuthenticationController.AuthenticationControllerGetBearerToken;
|
|
33
32
|
export type NotificationServicesPushControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, NotificationServicesPushControllerState>;
|
|
34
33
|
export type NotificationServicesPushControllerOnNewNotificationEvent = {
|
|
35
34
|
type: `${typeof controllerName}:onNewNotifications`;
|
|
@@ -40,7 +39,8 @@ export type NotificationServicesPushControllerPushNotificationClickedEvent = {
|
|
|
40
39
|
payload: [Types.INotification];
|
|
41
40
|
};
|
|
42
41
|
export type Events = NotificationServicesPushControllerStateChangeEvent | NotificationServicesPushControllerOnNewNotificationEvent | NotificationServicesPushControllerPushNotificationClickedEvent;
|
|
43
|
-
export type
|
|
42
|
+
export type AllowedEvents = never;
|
|
43
|
+
export type NotificationServicesPushControllerMessenger = RestrictedMessenger<typeof controllerName, Actions | AllowedActions, Events | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
44
44
|
export declare const defaultState: NotificationServicesPushControllerState;
|
|
45
45
|
export type ControllerConfig = {
|
|
46
46
|
/**
|
package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NotificationServicesPushController.d.cts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,
|
|
1
|
+
{"version":3,"file":"NotificationServicesPushController.d.cts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;A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AM,EAAE,gBAAgB,CAAC;KAC1B;IAiFY,4BAA4B;IAoBzC;;;;;;;;;OASG;IACU,uBAAuB,CAAC,SAAS,EAAE,MAAM,EAAE;IAkDxD;;;OAGG;IACU,wBAAwB;IAmCrC;;;;;;;OAOG;IACU,8BAA8B,CAAC,SAAS,EAAE,MAAM,EAAE;CAuChE"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller
|
|
2
|
-
import { BaseController } from "@metamask/base-controller
|
|
3
|
-
import type { Messenger } from "@metamask/messenger";
|
|
1
|
+
import type { RestrictedMessenger, ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
|
+
import { BaseController } from "@metamask/base-controller";
|
|
4
3
|
import type { AuthenticationController } from "@metamask/profile-sync-controller";
|
|
5
4
|
import type { PushNotificationEnv } from "./types/index.mjs";
|
|
6
5
|
import type { PushService } from "./types/push-service-interface.mjs";
|
|
@@ -29,7 +28,7 @@ export type NotificationServicesPushControllerSubscribeToNotificationsAction = {
|
|
|
29
28
|
handler: NotificationServicesPushController['subscribeToPushNotifications'];
|
|
30
29
|
};
|
|
31
30
|
export type Actions = NotificationServicesPushControllerGetStateAction | NotificationServicesPushControllerEnablePushNotificationsAction | NotificationServicesPushControllerDisablePushNotificationsAction | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction | NotificationServicesPushControllerSubscribeToNotificationsAction;
|
|
32
|
-
type AllowedActions = AuthenticationController.AuthenticationControllerGetBearerToken;
|
|
31
|
+
export type AllowedActions = AuthenticationController.AuthenticationControllerGetBearerToken;
|
|
33
32
|
export type NotificationServicesPushControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, NotificationServicesPushControllerState>;
|
|
34
33
|
export type NotificationServicesPushControllerOnNewNotificationEvent = {
|
|
35
34
|
type: `${typeof controllerName}:onNewNotifications`;
|
|
@@ -40,7 +39,8 @@ export type NotificationServicesPushControllerPushNotificationClickedEvent = {
|
|
|
40
39
|
payload: [Types.INotification];
|
|
41
40
|
};
|
|
42
41
|
export type Events = NotificationServicesPushControllerStateChangeEvent | NotificationServicesPushControllerOnNewNotificationEvent | NotificationServicesPushControllerPushNotificationClickedEvent;
|
|
43
|
-
export type
|
|
42
|
+
export type AllowedEvents = never;
|
|
43
|
+
export type NotificationServicesPushControllerMessenger = RestrictedMessenger<typeof controllerName, Actions | AllowedActions, Events | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
44
44
|
export declare const defaultState: NotificationServicesPushControllerState;
|
|
45
45
|
export type ControllerConfig = {
|
|
46
46
|
/**
|
package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NotificationServicesPushController.d.mts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,
|
|
1
|
+
{"version":3,"file":"NotificationServicesPushController.d.mts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAOlF,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAAgB;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,2CAAuC;AAClE,OAAO,KAAK,EAAE,KAAK,EAAE,oDAA0C;AAE/D,QAAA,MAAM,cAAc,uCAAuC,CAAC;AAE5D,MAAM,MAAM,uCAAuC,GAAG;IACpD,aAAa,EAAE,OAAO,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,kBAAkB,EAAE,OAAO,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,g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MAAM,GACd,kDAAkD,GAClD,wDAAwD,GACxD,8DAA8D,CAAC;AAEnE,MAAM,MAAM,aAAa,GAAG,KAAK,CAAC;AAElC,MAAM,MAAM,2CAA2C,GAAG,mBAAmB,CAC3E,OAAO,cAAc,EACrB,OAAO,GAAG,cAAc,EACxB,MAAM,GAAG,aAAa,EACtB,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAEF,eAAO,MAAM,YAAY,EAAE,uCAI1B,CAAC;AAiCF,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,MAAM,CAAC;IAEzB;;;OAGG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAE/B;;OAEG;IACH,QAAQ,EAAE,WAAW,GAAG,QAAQ,CAAC;IAEjC;;;;;OAKG;IACH,WAAW,EAAE,WAAW,CAAC;CAC1B,CAAC;AAOF;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,kCAAmC,SAAQ,cAAc,CAC5E,OAAO,cAAc,EACrB,uCAAuC,EACvC,2CAA2C,CAC5C;;gBAOa,EACV,SAAS,EACT,KAAK,EACL,GAAG,EACH,MAAM,GACP,EAAE;QACD,SAAS,EAAE,2CAA2C,CAAC;QACvD,KAAK,EAAE,uCAAuC,CAAC;QAC/C,sDAAsD;QACtD,GAAG,CAAC,EAAE,mBAAmB,CAAC;QAC1B,MAAM,EAAE,gBAAgB,CAAC;KAC1B;IAiFY,4BAA4B;IAoBzC;;;;;;;;;OASG;IACU,uBAAuB,CAAC,SAAS,EAAE,MAAM,EAAE;IAkDxD;;;OAGG;IACU,wBAAwB;IAmCrC;;;;;;;OAOG;IACU,8BAA8B,CAAC,SAAS,EAAE,MAAM,EAAE;CAuChE"}
|
|
@@ -16,7 +16,7 @@ function $importDefault(module) {
|
|
|
16
16
|
}
|
|
17
17
|
return module;
|
|
18
18
|
}
|
|
19
|
-
import { BaseController } from "@metamask/base-controller
|
|
19
|
+
import { BaseController } from "@metamask/base-controller";
|
|
20
20
|
import $log from "loglevel";
|
|
21
21
|
const log = $importDefault($log);
|
|
22
22
|
import { activatePushNotifications, deactivatePushNotifications } from "./services/services.mjs";
|
|
@@ -30,19 +30,19 @@ const metadata = {
|
|
|
30
30
|
isPushEnabled: {
|
|
31
31
|
includeInStateLogs: true,
|
|
32
32
|
persist: true,
|
|
33
|
-
|
|
33
|
+
anonymous: true,
|
|
34
34
|
usedInUi: true,
|
|
35
35
|
},
|
|
36
36
|
fcmToken: {
|
|
37
37
|
includeInStateLogs: false,
|
|
38
38
|
persist: true,
|
|
39
|
-
|
|
39
|
+
anonymous: true,
|
|
40
40
|
usedInUi: true,
|
|
41
41
|
},
|
|
42
42
|
isUpdatingFCMToken: {
|
|
43
43
|
includeInStateLogs: false,
|
|
44
44
|
persist: false,
|
|
45
|
-
|
|
45
|
+
anonymous: true,
|
|
46
46
|
usedInUi: true,
|
|
47
47
|
},
|
|
48
48
|
};
|
|
@@ -228,16 +228,16 @@ class NotificationServicesPushController extends BaseController {
|
|
|
228
228
|
}
|
|
229
229
|
}
|
|
230
230
|
_NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _NotificationServicesPushController_env = new WeakMap(), _NotificationServicesPushController_config = new WeakMap(), _NotificationServicesPushController_instances = new WeakSet(), _NotificationServicesPushController_registerMessageHandlers = function _NotificationServicesPushController_registerMessageHandlers() {
|
|
231
|
-
this.
|
|
232
|
-
this.
|
|
233
|
-
this.
|
|
234
|
-
this.
|
|
231
|
+
this.messagingSystem.registerActionHandler('NotificationServicesPushController:enablePushNotifications', this.enablePushNotifications.bind(this));
|
|
232
|
+
this.messagingSystem.registerActionHandler('NotificationServicesPushController:disablePushNotifications', this.disablePushNotifications.bind(this));
|
|
233
|
+
this.messagingSystem.registerActionHandler('NotificationServicesPushController:updateTriggerPushNotifications', this.updateTriggerPushNotifications.bind(this));
|
|
234
|
+
this.messagingSystem.registerActionHandler('NotificationServicesPushController:subscribeToPushNotifications', this.subscribeToPushNotifications.bind(this));
|
|
235
235
|
}, _NotificationServicesPushController_clearLoadingStates = function _NotificationServicesPushController_clearLoadingStates() {
|
|
236
236
|
this.update((state) => {
|
|
237
237
|
state.isUpdatingFCMToken = false;
|
|
238
238
|
});
|
|
239
239
|
}, _NotificationServicesPushController_getAndAssertBearerToken = async function _NotificationServicesPushController_getAndAssertBearerToken() {
|
|
240
|
-
const bearerToken = await this.
|
|
240
|
+
const bearerToken = await this.messagingSystem.call('AuthenticationController:getBearerToken');
|
|
241
241
|
if (!bearerToken) {
|
|
242
242
|
log.error('Failed to enable push notifications: BearerToken token is missing.');
|
|
243
243
|
throw new Error('BearerToken token is missing');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NotificationServicesPushController.mjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,uCAAuC;AAGhE,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,EACL,yBAAyB,EACzB,2BAA2B,EAC5B,gCAA4B;AAK7B,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAqE5D,MAAM,CAAC,MAAM,YAAY,GAA4C;IACnE,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,EAAE;IACZ,kBAAkB,EAAE,KAAK;CAC1B,CAAC;AACF,MAAM,QAAQ,GAA2D;IACvE,aAAa,EAAE;QACb,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,QAAQ,EAAE;QACR,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,kBAAkB,EAAE;QAClB,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,KAAK;QACd,sBAAs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K,CAAC,kBAAkB,GAAG,KAAK,CAAC;QACnC,CAAC,CAAC,CAAC;KACJ;IAED,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE;QAC7B,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,aAAa,GAAG,IAAI,CAAC;YAC3B,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;YAClC,KAAK,CAAC,kBAAkB,GAAG,KAAK,CAAC;QACnC,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;eArGkB,kCAAkC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller/next';\nimport { BaseController } from '@metamask/base-controller/next';\nimport type { Messenger } from '@metamask/messenger';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\nimport type { PushService } from './types/push-service-interface';\nimport type { Types } from '../NotificationServicesController';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n isPushEnabled: boolean;\n fcmToken: string;\n isUpdatingFCMToken: boolean;\n};\n\nexport type NotificationServicesPushControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\nexport type NotificationServicesPushControllerSubscribeToNotificationsAction = {\n type: `${typeof controllerName}:subscribeToPushNotifications`;\n handler: NotificationServicesPushController['subscribeToPushNotifications'];\n};\n\nexport type Actions =\n | NotificationServicesPushControllerGetStateAction\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | NotificationServicesPushControllerSubscribeToNotificationsAction;\n\ntype AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClickedEvent = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type Events =\n | NotificationServicesPushControllerStateChangeEvent\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClickedEvent;\n\nexport type NotificationServicesPushControllerMessenger = Messenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events\n>;\n\nexport const defaultState: NotificationServicesPushControllerState = {\n isPushEnabled: true,\n fcmToken: '',\n isUpdatingFCMToken: false,\n};\nconst metadata: StateMetadata<NotificationServicesPushControllerState> = {\n isPushEnabled: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n fcmToken: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n isUpdatingFCMToken: {\n includeInStateLogs: false,\n persist: false,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n};\n\nconst defaultPushEnv: PushNotificationEnv = {\n apiKey: '',\n authDomain: '',\n storageBucket: '',\n projectId: '',\n messagingSenderId: '',\n appId: '',\n measurementId: '',\n vapidKey: '',\n};\n\nexport type ControllerConfig = {\n /**\n * User locale for server push notifications\n */\n getLocale?: () => string;\n\n /**\n * Global switch to determine to use push notifications\n * Allows us to control Builds on extension (MV2 vs MV3)\n */\n isPushFeatureEnabled?: boolean;\n\n /**\n * determine the config used for push notification services\n */\n platform: 'extension' | 'mobile';\n\n /**\n * Push Service Interface\n * - create reg token\n * - delete reg token\n * - subscribe to push notifications\n */\n pushService: PushService;\n};\n\ntype StateCommand =\n | { type: 'enable'; fcmToken: string }\n | { type: 'disable' }\n | { type: 'update'; fcmToken: string };\n\n/**\n * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.\n * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.\n * It is responsible for registering and unregistering the service worker that listens for push notifications,\n * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.\n * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.\n */\nexport default class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n readonly #env: PushNotificationEnv;\n\n readonly #config: ControllerConfig;\n\n constructor({\n messenger,\n state,\n env,\n config,\n }: {\n messenger: NotificationServicesPushControllerMessenger;\n state: NotificationServicesPushControllerState;\n /** Push Environment is only required for extension */\n env?: PushNotificationEnv;\n config: ControllerConfig;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n this.#env = env ?? defaultPushEnv;\n this.#config = config;\n\n this.#registerMessageHandlers();\n this.#clearLoadingStates();\n }\n\n #registerMessageHandlers(): void {\n this.messenger.registerActionHandler(\n 'NotificationServicesPushController:enablePushNotifications',\n this.enablePushNotifications.bind(this),\n );\n this.messenger.registerActionHandler(\n 'NotificationServicesPushController:disablePushNotifications',\n this.disablePushNotifications.bind(this),\n );\n this.messenger.registerActionHandler(\n 'NotificationServicesPushController:updateTriggerPushNotifications',\n this.updateTriggerPushNotifications.bind(this),\n );\n this.messenger.registerActionHandler(\n 'NotificationServicesPushController:subscribeToPushNotifications',\n this.subscribeToPushNotifications.bind(this),\n );\n }\n\n #clearLoadingStates(): void {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n async #getAndAssertBearerToken() {\n const bearerToken = await this.messenger.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n log.error(\n 'Failed to enable push notifications: BearerToken token is missing.',\n );\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n #updatePushState(command: StateCommand) {\n if (command.type === 'enable') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'disable') {\n this.update((state) => {\n // Note we do not want to clear the old FCM token\n // We can send it as an old token to our backend to cleanup next time turned on\n state.isPushEnabled = false;\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'update') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n }\n\n public async subscribeToPushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n if (this.#pushListenerUnsubscribe) {\n this.#pushListenerUnsubscribe();\n this.#pushListenerUnsubscribe = undefined;\n }\n\n try {\n this.#pushListenerUnsubscribe =\n (await this.#config.pushService.subscribeToPushNotifications(\n this.#env,\n )) ?? undefined;\n } catch {\n // Do nothing, we are silently failing if push notification registration fails\n }\n }\n\n /**\n * Enables push notifications for the application.\n *\n * This method sets up the necessary infrastructure for handling push notifications by:\n * 1. Registering the service worker to listen for messages.\n * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.\n * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.\n *\n * @param addresses - An array of addresses to enable push notifications for.\n */\n public async enablePushNotifications(addresses: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n // Handle creating new reg token (if available)\n try {\n const bearerToken = await this.#getAndAssertBearerToken().catch(\n () => null,\n );\n\n // If there is a bearer token, lets try to refresh/create new reg token\n if (bearerToken) {\n // Activate Push Notifications\n const fcmToken = await activatePushNotifications({\n bearerToken,\n addresses,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n regToken: {\n platform: this.#config.platform,\n locale: this.#config.getLocale?.() ?? 'en',\n oldToken: this.state.fcmToken,\n },\n });\n\n if (fcmToken) {\n this.#updatePushState({ type: 'enable', fcmToken });\n }\n }\n } catch {\n // Do nothing, we are silently failing\n }\n\n // New token created, (re)subscribe to push notifications\n try {\n await this.subscribeToPushNotifications();\n } catch {\n // Do nothing we are silently failing\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n /**\n * Disables push notifications for the application.\n * This removes the registration token on this device, and ensures we unsubscribe from any listeners\n */\n public async disablePushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n // Send a request to the server to unregister the token/device\n await deactivatePushNotifications({\n env: this.#env,\n deleteRegToken: this.#config.pushService.deleteRegToken,\n regToken: this.state.fcmToken,\n });\n } catch (error) {\n const errorMessage = `Failed to disable push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n } finally {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n this.#updatePushState({ type: 'disable' });\n }\n\n /**\n * Updates the triggers for push notifications.\n * This method is responsible for updating the server with the new set of addresses that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param addresses - An array of addresses that should trigger push notifications.\n * @deprecated - this is not used anymore and will most likely be removed\n */\n public async updateTriggerPushNotifications(addresses: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n const bearerToken = await this.#getAndAssertBearerToken();\n const fcmToken = await activatePushNotifications({\n bearerToken,\n addresses,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n regToken: {\n platform: this.#config.platform,\n locale: this.#config.getLocale?.() ?? 'en',\n oldToken: this.state.fcmToken,\n },\n });\n\n // update the state with the new FCM token\n if (fcmToken) {\n this.#updatePushState({ type: 'update', fcmToken });\n }\n } catch (error) {\n const errorMessage = `Failed to update triggers for push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n } finally {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"NotificationServicesPushController.mjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAMA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,EACL,yBAAyB,EACzB,2BAA2B,EAC5B,gCAA4B;AAK7B,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAyE5D,MAAM,CAAC,MAAM,YAAY,GAA4C;IACnE,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,EAAE;IACZ,kBAAkB,EAAE,KAAK;CAC1B,CAAC;AACF,MAAM,QAAQ,GAA2D;IACvE,aAAa,EAAE;QACb,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;QACf,QAAQ,EAAE,IAAI;KACf;IACD,QAAQ,EAAE;QACR,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;QACf,QAAQ,EAAE,IAAI;KACf;IACD,kBAAkB,EAAE;QAClB,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,IAAI;QACf,QAAQ,EAAE,IAAI;KACf;CACF,CAAC;AAEF,MAAM,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IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE;QAC7B,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,aAAa,GAAG,IAAI,CAAC;YAC3B,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;YAClC,KAAK,CAAC,kBAAkB,GAAG,KAAK,CAAC;QACnC,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;eArGkB,kCAAkC","sourcesContent":["import type {\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\nimport type { PushService } from './types/push-service-interface';\nimport type { Types } from '../NotificationServicesController';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n isPushEnabled: boolean;\n fcmToken: string;\n isUpdatingFCMToken: boolean;\n};\n\nexport type NotificationServicesPushControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\nexport type NotificationServicesPushControllerSubscribeToNotificationsAction = {\n type: `${typeof controllerName}:subscribeToPushNotifications`;\n handler: NotificationServicesPushController['subscribeToPushNotifications'];\n};\n\nexport type Actions =\n | NotificationServicesPushControllerGetStateAction\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | NotificationServicesPushControllerSubscribeToNotificationsAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClickedEvent = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type Events =\n | NotificationServicesPushControllerStateChangeEvent\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClickedEvent;\n\nexport type AllowedEvents = never;\n\nexport type NotificationServicesPushControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\nexport const defaultState: NotificationServicesPushControllerState = {\n isPushEnabled: true,\n fcmToken: '',\n isUpdatingFCMToken: false,\n};\nconst metadata: StateMetadata<NotificationServicesPushControllerState> = {\n isPushEnabled: {\n includeInStateLogs: true,\n persist: true,\n anonymous: true,\n usedInUi: true,\n },\n fcmToken: {\n includeInStateLogs: false,\n persist: true,\n anonymous: true,\n usedInUi: true,\n },\n isUpdatingFCMToken: {\n includeInStateLogs: false,\n persist: false,\n anonymous: true,\n usedInUi: true,\n },\n};\n\nconst defaultPushEnv: PushNotificationEnv = {\n apiKey: '',\n authDomain: '',\n storageBucket: '',\n projectId: '',\n messagingSenderId: '',\n appId: '',\n measurementId: '',\n vapidKey: '',\n};\n\nexport type ControllerConfig = {\n /**\n * User locale for server push notifications\n */\n getLocale?: () => string;\n\n /**\n * Global switch to determine to use push notifications\n * Allows us to control Builds on extension (MV2 vs MV3)\n */\n isPushFeatureEnabled?: boolean;\n\n /**\n * determine the config used for push notification services\n */\n platform: 'extension' | 'mobile';\n\n /**\n * Push Service Interface\n * - create reg token\n * - delete reg token\n * - subscribe to push notifications\n */\n pushService: PushService;\n};\n\ntype StateCommand =\n | { type: 'enable'; fcmToken: string }\n | { type: 'disable' }\n | { type: 'update'; fcmToken: string };\n\n/**\n * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.\n * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.\n * It is responsible for registering and unregistering the service worker that listens for push notifications,\n * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.\n * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.\n */\nexport default class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n readonly #env: PushNotificationEnv;\n\n readonly #config: ControllerConfig;\n\n constructor({\n messenger,\n state,\n env,\n config,\n }: {\n messenger: NotificationServicesPushControllerMessenger;\n state: NotificationServicesPushControllerState;\n /** Push Environment is only required for extension */\n env?: PushNotificationEnv;\n config: ControllerConfig;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n this.#env = env ?? defaultPushEnv;\n this.#config = config;\n\n this.#registerMessageHandlers();\n this.#clearLoadingStates();\n }\n\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:enablePushNotifications',\n this.enablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:disablePushNotifications',\n this.disablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:updateTriggerPushNotifications',\n this.updateTriggerPushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:subscribeToPushNotifications',\n this.subscribeToPushNotifications.bind(this),\n );\n }\n\n #clearLoadingStates(): void {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n async #getAndAssertBearerToken() {\n const bearerToken = await this.messagingSystem.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n log.error(\n 'Failed to enable push notifications: BearerToken token is missing.',\n );\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n #updatePushState(command: StateCommand) {\n if (command.type === 'enable') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'disable') {\n this.update((state) => {\n // Note we do not want to clear the old FCM token\n // We can send it as an old token to our backend to cleanup next time turned on\n state.isPushEnabled = false;\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'update') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n }\n\n public async subscribeToPushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n if (this.#pushListenerUnsubscribe) {\n this.#pushListenerUnsubscribe();\n this.#pushListenerUnsubscribe = undefined;\n }\n\n try {\n this.#pushListenerUnsubscribe =\n (await this.#config.pushService.subscribeToPushNotifications(\n this.#env,\n )) ?? undefined;\n } catch {\n // Do nothing, we are silently failing if push notification registration fails\n }\n }\n\n /**\n * Enables push notifications for the application.\n *\n * This method sets up the necessary infrastructure for handling push notifications by:\n * 1. Registering the service worker to listen for messages.\n * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.\n * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.\n *\n * @param addresses - An array of addresses to enable push notifications for.\n */\n public async enablePushNotifications(addresses: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n // Handle creating new reg token (if available)\n try {\n const bearerToken = await this.#getAndAssertBearerToken().catch(\n () => null,\n );\n\n // If there is a bearer token, lets try to refresh/create new reg token\n if (bearerToken) {\n // Activate Push Notifications\n const fcmToken = await activatePushNotifications({\n bearerToken,\n addresses,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n regToken: {\n platform: this.#config.platform,\n locale: this.#config.getLocale?.() ?? 'en',\n oldToken: this.state.fcmToken,\n },\n });\n\n if (fcmToken) {\n this.#updatePushState({ type: 'enable', fcmToken });\n }\n }\n } catch {\n // Do nothing, we are silently failing\n }\n\n // New token created, (re)subscribe to push notifications\n try {\n await this.subscribeToPushNotifications();\n } catch {\n // Do nothing we are silently failing\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n /**\n * Disables push notifications for the application.\n * This removes the registration token on this device, and ensures we unsubscribe from any listeners\n */\n public async disablePushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n // Send a request to the server to unregister the token/device\n await deactivatePushNotifications({\n env: this.#env,\n deleteRegToken: this.#config.pushService.deleteRegToken,\n regToken: this.state.fcmToken,\n });\n } catch (error) {\n const errorMessage = `Failed to disable push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n } finally {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n this.#updatePushState({ type: 'disable' });\n }\n\n /**\n * Updates the triggers for push notifications.\n * This method is responsible for updating the server with the new set of addresses that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param addresses - An array of addresses that should trigger push notifications.\n * @deprecated - this is not used anymore and will most likely be removed\n */\n public async updateTriggerPushNotifications(addresses: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n const bearerToken = await this.#getAndAssertBearerToken();\n const fcmToken = await activatePushNotifications({\n bearerToken,\n addresses,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n regToken: {\n platform: this.#config.platform,\n locale: this.#config.getLocale?.() ?? 'en',\n oldToken: this.state.fcmToken,\n },\n });\n\n // update the state with the new FCM token\n if (fcmToken) {\n this.#updatePushState({ type: 'update', fcmToken });\n }\n } catch (error) {\n const errorMessage = `Failed to update triggers for push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n } finally {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/notification-services-controller",
|
|
3
|
-
"version": "18.3.0-preview-
|
|
3
|
+
"version": "18.3.0-preview-a649a76b",
|
|
4
4
|
"description": "Manages New MetaMask decentralized Notification system",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -110,9 +110,8 @@
|
|
|
110
110
|
},
|
|
111
111
|
"dependencies": {
|
|
112
112
|
"@contentful/rich-text-html-renderer": "^16.5.2",
|
|
113
|
-
"@metamask/base-controller": "^8.4.
|
|
113
|
+
"@metamask/base-controller": "^8.4.2",
|
|
114
114
|
"@metamask/controller-utils": "^11.14.1",
|
|
115
|
-
"@metamask/messenger": "^0.3.0",
|
|
116
115
|
"@metamask/utils": "^11.8.1",
|
|
117
116
|
"bignumber.js": "^9.1.2",
|
|
118
117
|
"firebase": "^11.2.0",
|