@metamask/notification-services-controller 22.1.0 → 23.0.0

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.
Files changed (66) hide show
  1. package/CHANGELOG.md +32 -1
  2. package/dist/NotificationServicesController/NotificationServicesController-method-action-types.cjs +7 -0
  3. package/dist/NotificationServicesController/NotificationServicesController-method-action-types.cjs.map +1 -0
  4. package/dist/NotificationServicesController/NotificationServicesController-method-action-types.d.cts +198 -0
  5. package/dist/NotificationServicesController/NotificationServicesController-method-action-types.d.cts.map +1 -0
  6. package/dist/NotificationServicesController/NotificationServicesController-method-action-types.d.mts +198 -0
  7. package/dist/NotificationServicesController/NotificationServicesController-method-action-types.d.mts.map +1 -0
  8. package/dist/NotificationServicesController/NotificationServicesController-method-action-types.mjs +6 -0
  9. package/dist/NotificationServicesController/NotificationServicesController-method-action-types.mjs.map +1 -0
  10. package/dist/NotificationServicesController/NotificationServicesController.cjs +47 -14
  11. package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
  12. package/dist/NotificationServicesController/NotificationServicesController.d.cts +6 -20
  13. package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
  14. package/dist/NotificationServicesController/NotificationServicesController.d.mts +6 -20
  15. package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
  16. package/dist/NotificationServicesController/NotificationServicesController.mjs +46 -14
  17. package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
  18. package/dist/NotificationServicesController/index.cjs +3 -7
  19. package/dist/NotificationServicesController/index.cjs.map +1 -1
  20. package/dist/NotificationServicesController/index.d.cts +3 -3
  21. package/dist/NotificationServicesController/index.d.cts.map +1 -1
  22. package/dist/NotificationServicesController/index.d.mts +3 -3
  23. package/dist/NotificationServicesController/index.d.mts.map +1 -1
  24. package/dist/NotificationServicesController/index.mjs +2 -3
  25. package/dist/NotificationServicesController/index.mjs.map +1 -1
  26. package/dist/NotificationServicesPushController/NotificationServicesPushController-method-action-types.cjs +7 -0
  27. package/dist/NotificationServicesPushController/NotificationServicesPushController-method-action-types.cjs.map +1 -0
  28. package/dist/NotificationServicesPushController/NotificationServicesPushController-method-action-types.d.cts +60 -0
  29. package/dist/NotificationServicesPushController/NotificationServicesPushController-method-action-types.d.cts.map +1 -0
  30. package/dist/NotificationServicesPushController/NotificationServicesPushController-method-action-types.d.mts +60 -0
  31. package/dist/NotificationServicesPushController/NotificationServicesPushController-method-action-types.d.mts.map +1 -0
  32. package/dist/NotificationServicesPushController/NotificationServicesPushController-method-action-types.mjs +6 -0
  33. package/dist/NotificationServicesPushController/NotificationServicesPushController-method-action-types.mjs.map +1 -0
  34. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs +40 -10
  35. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
  36. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +12 -18
  37. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
  38. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +12 -18
  39. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
  40. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +40 -11
  41. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
  42. package/dist/NotificationServicesPushController/index.cjs +3 -7
  43. package/dist/NotificationServicesPushController/index.cjs.map +1 -1
  44. package/dist/NotificationServicesPushController/index.d.cts +3 -3
  45. package/dist/NotificationServicesPushController/index.d.cts.map +1 -1
  46. package/dist/NotificationServicesPushController/index.d.mts +3 -3
  47. package/dist/NotificationServicesPushController/index.d.mts.map +1 -1
  48. package/dist/NotificationServicesPushController/index.mjs +2 -3
  49. package/dist/NotificationServicesPushController/index.mjs.map +1 -1
  50. package/dist/NotificationServicesPushController/mocks/mockResponse.cjs +9 -1
  51. package/dist/NotificationServicesPushController/mocks/mockResponse.cjs.map +1 -1
  52. package/dist/NotificationServicesPushController/mocks/mockResponse.d.cts +2 -1
  53. package/dist/NotificationServicesPushController/mocks/mockResponse.d.cts.map +1 -1
  54. package/dist/NotificationServicesPushController/mocks/mockResponse.d.mts +2 -1
  55. package/dist/NotificationServicesPushController/mocks/mockResponse.d.mts.map +1 -1
  56. package/dist/NotificationServicesPushController/mocks/mockResponse.mjs +7 -0
  57. package/dist/NotificationServicesPushController/mocks/mockResponse.mjs.map +1 -1
  58. package/dist/NotificationServicesPushController/services/services.cjs +31 -1
  59. package/dist/NotificationServicesPushController/services/services.cjs.map +1 -1
  60. package/dist/NotificationServicesPushController/services/services.d.cts +22 -0
  61. package/dist/NotificationServicesPushController/services/services.d.cts.map +1 -1
  62. package/dist/NotificationServicesPushController/services/services.d.mts +22 -0
  63. package/dist/NotificationServicesPushController/services/services.d.mts.map +1 -1
  64. package/dist/NotificationServicesPushController/services/services.mjs +29 -0
  65. package/dist/NotificationServicesPushController/services/services.mjs.map +1 -1
  66. package/package.json +5 -1
@@ -0,0 +1,60 @@
1
+ /**
2
+ * This file is auto generated by `scripts/generate-method-action-types.ts`.
3
+ * Do not edit manually.
4
+ */
5
+ import type { NotificationServicesPushController } from "./NotificationServicesPushController.cjs";
6
+ export type NotificationServicesPushControllerSubscribeToPushNotificationsAction = {
7
+ type: `NotificationServicesPushController:subscribeToPushNotifications`;
8
+ handler: NotificationServicesPushController['subscribeToPushNotifications'];
9
+ };
10
+ /**
11
+ * Enables push notifications for the application.
12
+ *
13
+ * This method sets up the necessary infrastructure for handling push notifications by:
14
+ * 1. Registering the service worker to listen for messages.
15
+ * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.
16
+ * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
17
+ *
18
+ * @param addresses - An array of addresses to enable push notifications for.
19
+ */
20
+ export type NotificationServicesPushControllerEnablePushNotificationsAction = {
21
+ type: `NotificationServicesPushController:enablePushNotifications`;
22
+ handler: NotificationServicesPushController['enablePushNotifications'];
23
+ };
24
+ /**
25
+ * Disables push notifications for the application.
26
+ * This removes the registration token on this device, and ensures we unsubscribe from any listeners
27
+ */
28
+ export type NotificationServicesPushControllerDisablePushNotificationsAction = {
29
+ type: `NotificationServicesPushController:disablePushNotifications`;
30
+ handler: NotificationServicesPushController['disablePushNotifications'];
31
+ };
32
+ /**
33
+ * Deletes backend push notification links for the given addresses on the current platform.
34
+ * This is used when accounts are removed (for example SRP removal), so backend can remove
35
+ * all associated FCM tokens for those address/platform pairs.
36
+ *
37
+ * @param addresses - Addresses that should be unlinked from push notifications.
38
+ * @returns Whether the delete request succeeded.
39
+ */
40
+ export type NotificationServicesPushControllerDeletePushNotificationLinksAction = {
41
+ type: `NotificationServicesPushController:deletePushNotificationLinks`;
42
+ handler: NotificationServicesPushController['deletePushNotificationLinks'];
43
+ };
44
+ /**
45
+ * Updates the triggers for push notifications.
46
+ * This method is responsible for updating the server with the new set of addresses that should trigger push notifications.
47
+ * It uses the current FCM token and a BearerToken for authentication.
48
+ *
49
+ * @param addresses - An array of addresses that should trigger push notifications.
50
+ * @deprecated - this is not used anymore and will most likely be removed
51
+ */
52
+ export type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction = {
53
+ type: `NotificationServicesPushController:updateTriggerPushNotifications`;
54
+ handler: NotificationServicesPushController['updateTriggerPushNotifications'];
55
+ };
56
+ /**
57
+ * Union of all NotificationServicesPushController action types.
58
+ */
59
+ export type NotificationServicesPushControllerMethodActions = NotificationServicesPushControllerSubscribeToPushNotificationsAction | NotificationServicesPushControllerEnablePushNotificationsAction | NotificationServicesPushControllerDisablePushNotificationsAction | NotificationServicesPushControllerDeletePushNotificationLinksAction | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction;
60
+ //# sourceMappingURL=NotificationServicesPushController-method-action-types.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NotificationServicesPushController-method-action-types.d.cts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,kCAAkC,EAAE,iDAA6C;AAE/F,MAAM,MAAM,oEAAoE,GAC9E;IACE,IAAI,EAAE,iEAAiE,CAAC;IACxE,OAAO,EAAE,kCAAkC,CAAC,8BAA8B,CAAC,CAAC;CAC7E,CAAC;AAEJ;;;;;;;;;GASG;AACH,MAAM,MAAM,+DAA+D,GAAG;IAC5E,IAAI,EAAE,4DAA4D,CAAC;IACnE,OAAO,EAAE,kCAAkC,CAAC,yBAAyB,CAAC,CAAC;CACxE,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,gEAAgE,GAAG;IAC7E,IAAI,EAAE,6DAA6D,CAAC;IACpE,OAAO,EAAE,kCAAkC,CAAC,0BAA0B,CAAC,CAAC;CACzE,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,mEAAmE,GAC7E;IACE,IAAI,EAAE,gEAAgE,CAAC;IACvE,OAAO,EAAE,kCAAkC,CAAC,6BAA6B,CAAC,CAAC;CAC5E,CAAC;AAEJ;;;;;;;GAOG;AACH,MAAM,MAAM,sEAAsE,GAChF;IACE,IAAI,EAAE,mEAAmE,CAAC;IAC1E,OAAO,EAAE,kCAAkC,CAAC,gCAAgC,CAAC,CAAC;CAC/E,CAAC;AAEJ;;GAEG;AACH,MAAM,MAAM,+CAA+C,GACvD,oEAAoE,GACpE,+DAA+D,GAC/D,gEAAgE,GAChE,mEAAmE,GACnE,sEAAsE,CAAC"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * This file is auto generated by `scripts/generate-method-action-types.ts`.
3
+ * Do not edit manually.
4
+ */
5
+ import type { NotificationServicesPushController } from "./NotificationServicesPushController.mjs";
6
+ export type NotificationServicesPushControllerSubscribeToPushNotificationsAction = {
7
+ type: `NotificationServicesPushController:subscribeToPushNotifications`;
8
+ handler: NotificationServicesPushController['subscribeToPushNotifications'];
9
+ };
10
+ /**
11
+ * Enables push notifications for the application.
12
+ *
13
+ * This method sets up the necessary infrastructure for handling push notifications by:
14
+ * 1. Registering the service worker to listen for messages.
15
+ * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.
16
+ * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
17
+ *
18
+ * @param addresses - An array of addresses to enable push notifications for.
19
+ */
20
+ export type NotificationServicesPushControllerEnablePushNotificationsAction = {
21
+ type: `NotificationServicesPushController:enablePushNotifications`;
22
+ handler: NotificationServicesPushController['enablePushNotifications'];
23
+ };
24
+ /**
25
+ * Disables push notifications for the application.
26
+ * This removes the registration token on this device, and ensures we unsubscribe from any listeners
27
+ */
28
+ export type NotificationServicesPushControllerDisablePushNotificationsAction = {
29
+ type: `NotificationServicesPushController:disablePushNotifications`;
30
+ handler: NotificationServicesPushController['disablePushNotifications'];
31
+ };
32
+ /**
33
+ * Deletes backend push notification links for the given addresses on the current platform.
34
+ * This is used when accounts are removed (for example SRP removal), so backend can remove
35
+ * all associated FCM tokens for those address/platform pairs.
36
+ *
37
+ * @param addresses - Addresses that should be unlinked from push notifications.
38
+ * @returns Whether the delete request succeeded.
39
+ */
40
+ export type NotificationServicesPushControllerDeletePushNotificationLinksAction = {
41
+ type: `NotificationServicesPushController:deletePushNotificationLinks`;
42
+ handler: NotificationServicesPushController['deletePushNotificationLinks'];
43
+ };
44
+ /**
45
+ * Updates the triggers for push notifications.
46
+ * This method is responsible for updating the server with the new set of addresses that should trigger push notifications.
47
+ * It uses the current FCM token and a BearerToken for authentication.
48
+ *
49
+ * @param addresses - An array of addresses that should trigger push notifications.
50
+ * @deprecated - this is not used anymore and will most likely be removed
51
+ */
52
+ export type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction = {
53
+ type: `NotificationServicesPushController:updateTriggerPushNotifications`;
54
+ handler: NotificationServicesPushController['updateTriggerPushNotifications'];
55
+ };
56
+ /**
57
+ * Union of all NotificationServicesPushController action types.
58
+ */
59
+ export type NotificationServicesPushControllerMethodActions = NotificationServicesPushControllerSubscribeToPushNotificationsAction | NotificationServicesPushControllerEnablePushNotificationsAction | NotificationServicesPushControllerDisablePushNotificationsAction | NotificationServicesPushControllerDeletePushNotificationLinksAction | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction;
60
+ //# sourceMappingURL=NotificationServicesPushController-method-action-types.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NotificationServicesPushController-method-action-types.d.mts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,kCAAkC,EAAE,iDAA6C;AAE/F,MAAM,MAAM,oEAAoE,GAC9E;IACE,IAAI,EAAE,iEAAiE,CAAC;IACxE,OAAO,EAAE,kCAAkC,CAAC,8BAA8B,CAAC,CAAC;CAC7E,CAAC;AAEJ;;;;;;;;;GASG;AACH,MAAM,MAAM,+DAA+D,GAAG;IAC5E,IAAI,EAAE,4DAA4D,CAAC;IACnE,OAAO,EAAE,kCAAkC,CAAC,yBAAyB,CAAC,CAAC;CACxE,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,gEAAgE,GAAG;IAC7E,IAAI,EAAE,6DAA6D,CAAC;IACpE,OAAO,EAAE,kCAAkC,CAAC,0BAA0B,CAAC,CAAC;CACzE,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,mEAAmE,GAC7E;IACE,IAAI,EAAE,gEAAgE,CAAC;IACvE,OAAO,EAAE,kCAAkC,CAAC,6BAA6B,CAAC,CAAC;CAC5E,CAAC;AAEJ;;;;;;;GAOG;AACH,MAAM,MAAM,sEAAsE,GAChF;IACE,IAAI,EAAE,mEAAmE,CAAC;IAC1E,OAAO,EAAE,kCAAkC,CAAC,gCAAgC,CAAC,CAAC;CAC/E,CAAC;AAEJ;;GAEG;AACH,MAAM,MAAM,+CAA+C,GACvD,oEAAoE,GACpE,+DAA+D,GAC/D,gEAAgE,GAChE,mEAAmE,GACnE,sEAAsE,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * This file is auto generated by `scripts/generate-method-action-types.ts`.
3
+ * Do not edit manually.
4
+ */
5
+ export {};
6
+ //# sourceMappingURL=NotificationServicesPushController-method-action-types.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NotificationServicesPushController-method-action-types.mjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { NotificationServicesPushController } from './NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerSubscribeToPushNotificationsAction =\n {\n type: `NotificationServicesPushController:subscribeToPushNotifications`;\n handler: NotificationServicesPushController['subscribeToPushNotifications'];\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 */\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `NotificationServicesPushController:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\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 */\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `NotificationServicesPushController:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\n\n/**\n * Deletes backend push notification links for the given addresses on the current platform.\n * This is used when accounts are removed (for example SRP removal), so backend can remove\n * all associated FCM tokens for those address/platform pairs.\n *\n * @param addresses - Addresses that should be unlinked from push notifications.\n * @returns Whether the delete request succeeded.\n */\nexport type NotificationServicesPushControllerDeletePushNotificationLinksAction =\n {\n type: `NotificationServicesPushController:deletePushNotificationLinks`;\n handler: NotificationServicesPushController['deletePushNotificationLinks'];\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 */\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `NotificationServicesPushController:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\n\n/**\n * Union of all NotificationServicesPushController action types.\n */\nexport type NotificationServicesPushControllerMethodActions =\n | NotificationServicesPushControllerSubscribeToPushNotificationsAction\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerDeletePushNotificationLinksAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction;\n"]}
@@ -13,13 +13,20 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
13
13
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
15
  };
16
- var _NotificationServicesPushController_instances, _NotificationServicesPushController_pushListenerUnsubscribe, _NotificationServicesPushController_env, _NotificationServicesPushController_config, _NotificationServicesPushController_registerMessageHandlers, _NotificationServicesPushController_clearLoadingStates, _NotificationServicesPushController_getAndAssertBearerToken, _NotificationServicesPushController_updatePushState;
16
+ var _NotificationServicesPushController_instances, _NotificationServicesPushController_pushListenerUnsubscribe, _NotificationServicesPushController_env, _NotificationServicesPushController_config, _NotificationServicesPushController_clearLoadingStates, _NotificationServicesPushController_getAndAssertBearerToken, _NotificationServicesPushController_updatePushState;
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.defaultState = void 0;
18
+ exports.NotificationServicesPushController = exports.defaultState = void 0;
19
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';
23
+ const MESSENGER_EXPOSED_METHODS = [
24
+ 'subscribeToPushNotifications',
25
+ 'enablePushNotifications',
26
+ 'disablePushNotifications',
27
+ 'updateTriggerPushNotifications',
28
+ 'deletePushNotificationLinks',
29
+ ];
23
30
  exports.defaultState = {
24
31
  isPushEnabled: true,
25
32
  fcmToken: '',
@@ -76,7 +83,7 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
76
83
  _NotificationServicesPushController_config.set(this, void 0);
77
84
  __classPrivateFieldSet(this, _NotificationServicesPushController_env, env ?? defaultPushEnv, "f");
78
85
  __classPrivateFieldSet(this, _NotificationServicesPushController_config, config, "f");
79
- __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_registerMessageHandlers).call(this);
86
+ this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
80
87
  __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_clearLoadingStates).call(this);
81
88
  }
82
89
  async subscribeToPushNotifications() {
@@ -182,6 +189,34 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
182
189
  // Update State
183
190
  __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_updatePushState).call(this, { type: 'disable' });
184
191
  }
192
+ /**
193
+ * Deletes backend push notification links for the given addresses on the current platform.
194
+ * This is used when accounts are removed (for example SRP removal), so backend can remove
195
+ * all associated FCM tokens for those address/platform pairs.
196
+ *
197
+ * @param addresses - Addresses that should be unlinked from push notifications.
198
+ * @returns Whether the delete request succeeded.
199
+ */
200
+ async deletePushNotificationLinks(addresses) {
201
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled ||
202
+ addresses.length === 0 ||
203
+ !this.state.fcmToken) {
204
+ return false;
205
+ }
206
+ try {
207
+ const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
208
+ return await (0, services_1.deleteLinksAPI)({
209
+ bearerToken,
210
+ addresses,
211
+ platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
212
+ token: this.state.fcmToken,
213
+ env: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").env ?? 'prd',
214
+ });
215
+ }
216
+ catch {
217
+ return false;
218
+ }
219
+ }
185
220
  /**
186
221
  * Updates the triggers for push notifications.
187
222
  * This method is responsible for updating the server with the new set of addresses that should trigger push notifications.
@@ -228,12 +263,8 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
228
263
  }
229
264
  }
230
265
  }
231
- _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _NotificationServicesPushController_env = new WeakMap(), _NotificationServicesPushController_config = new WeakMap(), _NotificationServicesPushController_instances = new WeakSet(), _NotificationServicesPushController_registerMessageHandlers = function _NotificationServicesPushController_registerMessageHandlers() {
232
- this.messenger.registerActionHandler('NotificationServicesPushController:enablePushNotifications', this.enablePushNotifications.bind(this));
233
- this.messenger.registerActionHandler('NotificationServicesPushController:disablePushNotifications', this.disablePushNotifications.bind(this));
234
- this.messenger.registerActionHandler('NotificationServicesPushController:updateTriggerPushNotifications', this.updateTriggerPushNotifications.bind(this));
235
- this.messenger.registerActionHandler('NotificationServicesPushController:subscribeToPushNotifications', this.subscribeToPushNotifications.bind(this));
236
- }, _NotificationServicesPushController_clearLoadingStates = function _NotificationServicesPushController_clearLoadingStates() {
266
+ exports.NotificationServicesPushController = NotificationServicesPushController;
267
+ _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _NotificationServicesPushController_env = new WeakMap(), _NotificationServicesPushController_config = new WeakMap(), _NotificationServicesPushController_instances = new WeakSet(), _NotificationServicesPushController_clearLoadingStates = function _NotificationServicesPushController_clearLoadingStates() {
237
268
  this.update((state) => {
238
269
  state.isUpdatingFCMToken = false;
239
270
  });
@@ -267,5 +298,4 @@ _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _No
267
298
  });
268
299
  }
269
300
  };
270
- exports.default = NotificationServicesPushController;
271
301
  //# sourceMappingURL=NotificationServicesPushController.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationServicesPushController.cjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAKA,+DAA2D;AAG3D,wDAA2B;AAG3B,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,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,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;CACF,CAAC;AAEF,MAAM,cAAc,GAAwB;IAC1C,MAAM,EAAE,EAAE;IACV,UAAU,EAAE,EAAE;IACd,aAAa,EAAE,EAAE;IACjB,SAAS,EAAE,EAAE;IACb,iBAAiB,EAAE,EAAE;IACrB,KAAK,EAAE,EAAE;IACT,aAAa,EAAE,EAAE;IACjB,QAAQ,EAAE,EAAE;CACb,CAAC;AAmCF;;;;;;GAMG;AACH,MAAqB,kCAAmC,SAAQ,gCAI/D;IAOC,YAAY,EACV,SAAS,EACT,KAAK,EACL,GAAG,EACH,MAAM,GAOP;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ;YACR,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE,EAA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C;QAC9B,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;IACL,CAAC;AACH,CAAC;kBAlGkB,kCAAkC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport type { ENV } from './services/endpoints';\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.AuthenticationControllerGetBearerTokenAction;\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 env?: ENV;\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(): Promise<string> {\n const bearerToken = await this.messenger.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n #updatePushState(command: StateCommand): void {\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(): Promise<void> {\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[]): Promise<void> {\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 controllerEnv: this.#config.env ?? 'prd',\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(): Promise<void> {\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(\n addresses: string[],\n ): Promise<void> {\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 controllerEnv: this.#config.env ?? 'prd',\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":";;;;;;;;;;;;;;;;;;AAKA,+DAA2D;AAG3D,wDAA2B;AAI3B,sDAI6B;AAK7B,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAQ5D,MAAM,yBAAyB,GAAG;IAChC,8BAA8B;IAC9B,yBAAyB;IACzB,0BAA0B;IAC1B,gCAAgC;IAChC,6BAA6B;CACrB,CAAC;AA0CE,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,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,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;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AW,CAAC;AACrB,CAAC,qHAEgB,OAAqB;IACpC,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9B,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;IACL,CAAC;IAED,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;QAC/B,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;IACL,CAAC;IAED,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9B,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;IACL,CAAC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport type { NotificationServicesPushControllerMethodActions } from './NotificationServicesPushController-method-action-types';\nimport type { ENV } from './services/endpoints';\nimport {\n activatePushNotifications,\n deleteLinksAPI,\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\nconst MESSENGER_EXPOSED_METHODS = [\n 'subscribeToPushNotifications',\n 'enablePushNotifications',\n 'disablePushNotifications',\n 'updateTriggerPushNotifications',\n 'deletePushNotificationLinks',\n] as const;\n\nexport type NotificationServicesPushControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type Actions =\n | NotificationServicesPushControllerGetStateAction\n | NotificationServicesPushControllerMethodActions;\n\ntype AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerTokenAction;\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 env?: ENV;\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 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.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.#clearLoadingStates();\n }\n\n #clearLoadingStates(): void {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n async #getAndAssertBearerToken(): Promise<string> {\n const bearerToken = await this.messenger.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n #updatePushState(command: StateCommand): void {\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(): Promise<void> {\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[]): Promise<void> {\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 controllerEnv: this.#config.env ?? 'prd',\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(): Promise<void> {\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 * Deletes backend push notification links for the given addresses on the current platform.\n * This is used when accounts are removed (for example SRP removal), so backend can remove\n * all associated FCM tokens for those address/platform pairs.\n *\n * @param addresses - Addresses that should be unlinked from push notifications.\n * @returns Whether the delete request succeeded.\n */\n public async deletePushNotificationLinks(\n addresses: string[],\n ): Promise<boolean> {\n if (\n !this.#config.isPushFeatureEnabled ||\n addresses.length === 0 ||\n !this.state.fcmToken\n ) {\n return false;\n }\n\n try {\n const bearerToken = await this.#getAndAssertBearerToken();\n return await deleteLinksAPI({\n bearerToken,\n addresses,\n platform: this.#config.platform,\n token: this.state.fcmToken,\n env: this.#config.env ?? 'prd',\n });\n } catch {\n return false;\n }\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(\n addresses: string[],\n ): Promise<void> {\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 controllerEnv: this.#config.env ?? 'prd',\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"]}
@@ -2,6 +2,7 @@ import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@meta
2
2
  import { BaseController } from "@metamask/base-controller";
3
3
  import type { Messenger } from "@metamask/messenger";
4
4
  import type { AuthenticationController } from "@metamask/profile-sync-controller";
5
+ import type { NotificationServicesPushControllerMethodActions } from "./NotificationServicesPushController-method-action-types.cjs";
5
6
  import type { ENV } from "./services/endpoints.cjs";
6
7
  import type { PushNotificationEnv } from "./types/index.cjs";
7
8
  import type { PushService } from "./types/push-service-interface.cjs";
@@ -13,23 +14,7 @@ export type NotificationServicesPushControllerState = {
13
14
  isUpdatingFCMToken: boolean;
14
15
  };
15
16
  export type NotificationServicesPushControllerGetStateAction = ControllerGetStateAction<typeof controllerName, NotificationServicesPushControllerState>;
16
- export type NotificationServicesPushControllerEnablePushNotificationsAction = {
17
- type: `${typeof controllerName}:enablePushNotifications`;
18
- handler: NotificationServicesPushController['enablePushNotifications'];
19
- };
20
- export type NotificationServicesPushControllerDisablePushNotificationsAction = {
21
- type: `${typeof controllerName}:disablePushNotifications`;
22
- handler: NotificationServicesPushController['disablePushNotifications'];
23
- };
24
- export type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction = {
25
- type: `${typeof controllerName}:updateTriggerPushNotifications`;
26
- handler: NotificationServicesPushController['updateTriggerPushNotifications'];
27
- };
28
- export type NotificationServicesPushControllerSubscribeToNotificationsAction = {
29
- type: `${typeof controllerName}:subscribeToPushNotifications`;
30
- handler: NotificationServicesPushController['subscribeToPushNotifications'];
31
- };
32
- export type Actions = NotificationServicesPushControllerGetStateAction | NotificationServicesPushControllerEnablePushNotificationsAction | NotificationServicesPushControllerDisablePushNotificationsAction | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction | NotificationServicesPushControllerSubscribeToNotificationsAction;
17
+ export type Actions = NotificationServicesPushControllerGetStateAction | NotificationServicesPushControllerMethodActions;
33
18
  type AllowedActions = AuthenticationController.AuthenticationControllerGetBearerTokenAction;
34
19
  export type NotificationServicesPushControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, NotificationServicesPushControllerState>;
35
20
  export type NotificationServicesPushControllerOnNewNotificationEvent = {
@@ -73,7 +58,7 @@ export type ControllerConfig = {
73
58
  * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.
74
59
  * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.
75
60
  */
76
- export default class NotificationServicesPushController extends BaseController<typeof controllerName, NotificationServicesPushControllerState, NotificationServicesPushControllerMessenger> {
61
+ export declare class NotificationServicesPushController extends BaseController<typeof controllerName, NotificationServicesPushControllerState, NotificationServicesPushControllerMessenger> {
77
62
  #private;
78
63
  constructor({ messenger, state, env, config, }: {
79
64
  messenger: NotificationServicesPushControllerMessenger;
@@ -99,6 +84,15 @@ export default class NotificationServicesPushController extends BaseController<t
99
84
  * This removes the registration token on this device, and ensures we unsubscribe from any listeners
100
85
  */
101
86
  disablePushNotifications(): Promise<void>;
87
+ /**
88
+ * Deletes backend push notification links for the given addresses on the current platform.
89
+ * This is used when accounts are removed (for example SRP removal), so backend can remove
90
+ * all associated FCM tokens for those address/platform pairs.
91
+ *
92
+ * @param addresses - Addresses that should be unlinked from push notifications.
93
+ * @returns Whether the delete request succeeded.
94
+ */
95
+ deletePushNotificationLinks(addresses: string[]): Promise<boolean>;
102
96
  /**
103
97
  * Updates the triggers for push notifications.
104
98
  * This method is responsible for updating the server with the new set of addresses that should trigger push notifications.
@@ -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,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAGlF,OAAO,KAAK,EAAE,GAAG,EAAE,iCAA6B;AAKhD,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,gDAAgD,GAC1D,wBAAwB,CACtB,OAAO,cAAc,EACrB,uCAAuC,CACxC,CAAC;AAEJ,MAAM,MAAM,+DAA+D,GAA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,cAAc,EACrB,OAAO,GAAG,cAAc,EACxB,MAAM,CACP,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;IAEzB,GAAG,CAAC,EAAE,GAAG,CAAC;CACX,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;IA8EY,4BAA4B,IAAI,OAAO,CAAC,IAAI,CAAC;IAoB1D;;;;;;;;;OASG;IACU,uBAAuB,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAmDxE;;;OAGG;IACU,wBAAwB,IAAI,OAAO,CAAC,IAAI,CAAC;IAmCtD;;;;;;;OAOG;IACU,8BAA8B,CACzC,SAAS,EAAE,MAAM,EAAE,GAClB,OAAO,CAAC,IAAI,CAAC;CAwCjB"}
1
+ {"version":3,"file":"NotificationServicesPushController.d.cts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc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Ac,EACrB,OAAO,GAAG,cAAc,EACxB,MAAM,CACP,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;IAEzB,GAAG,CAAC,EAAE,GAAG,CAAC;CACX,CAAC;AAOF;;;;;;GAMG;AACH,qBAAa,kCAAmC,SAAQ,cAAc,CACpE,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;IA+DY,4BAA4B,IAAI,OAAO,CAAC,IAAI,CAAC;IAoB1D;;;;;;;;;OASG;IACU,uBAAuB,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAmDxE;;;OAGG;IACU,wBAAwB,IAAI,OAAO,CAAC,IAAI,CAAC;IAmCtD;;;;;;;OAOG;IACU,2BAA2B,CACtC,SAAS,EAAE,MAAM,EAAE,GAClB,OAAO,CAAC,OAAO,CAAC;IAuBnB;;;;;;;OAOG;IACU,8BAA8B,CACzC,SAAS,EAAE,MAAM,EAAE,GAClB,OAAO,CAAC,IAAI,CAAC;CAwCjB"}
@@ -2,6 +2,7 @@ import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@meta
2
2
  import { BaseController } from "@metamask/base-controller";
3
3
  import type { Messenger } from "@metamask/messenger";
4
4
  import type { AuthenticationController } from "@metamask/profile-sync-controller";
5
+ import type { NotificationServicesPushControllerMethodActions } from "./NotificationServicesPushController-method-action-types.mjs";
5
6
  import type { ENV } from "./services/endpoints.mjs";
6
7
  import type { PushNotificationEnv } from "./types/index.mjs";
7
8
  import type { PushService } from "./types/push-service-interface.mjs";
@@ -13,23 +14,7 @@ export type NotificationServicesPushControllerState = {
13
14
  isUpdatingFCMToken: boolean;
14
15
  };
15
16
  export type NotificationServicesPushControllerGetStateAction = ControllerGetStateAction<typeof controllerName, NotificationServicesPushControllerState>;
16
- export type NotificationServicesPushControllerEnablePushNotificationsAction = {
17
- type: `${typeof controllerName}:enablePushNotifications`;
18
- handler: NotificationServicesPushController['enablePushNotifications'];
19
- };
20
- export type NotificationServicesPushControllerDisablePushNotificationsAction = {
21
- type: `${typeof controllerName}:disablePushNotifications`;
22
- handler: NotificationServicesPushController['disablePushNotifications'];
23
- };
24
- export type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction = {
25
- type: `${typeof controllerName}:updateTriggerPushNotifications`;
26
- handler: NotificationServicesPushController['updateTriggerPushNotifications'];
27
- };
28
- export type NotificationServicesPushControllerSubscribeToNotificationsAction = {
29
- type: `${typeof controllerName}:subscribeToPushNotifications`;
30
- handler: NotificationServicesPushController['subscribeToPushNotifications'];
31
- };
32
- export type Actions = NotificationServicesPushControllerGetStateAction | NotificationServicesPushControllerEnablePushNotificationsAction | NotificationServicesPushControllerDisablePushNotificationsAction | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction | NotificationServicesPushControllerSubscribeToNotificationsAction;
17
+ export type Actions = NotificationServicesPushControllerGetStateAction | NotificationServicesPushControllerMethodActions;
33
18
  type AllowedActions = AuthenticationController.AuthenticationControllerGetBearerTokenAction;
34
19
  export type NotificationServicesPushControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, NotificationServicesPushControllerState>;
35
20
  export type NotificationServicesPushControllerOnNewNotificationEvent = {
@@ -73,7 +58,7 @@ export type ControllerConfig = {
73
58
  * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.
74
59
  * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.
75
60
  */
76
- export default class NotificationServicesPushController extends BaseController<typeof controllerName, NotificationServicesPushControllerState, NotificationServicesPushControllerMessenger> {
61
+ export declare class NotificationServicesPushController extends BaseController<typeof controllerName, NotificationServicesPushControllerState, NotificationServicesPushControllerMessenger> {
77
62
  #private;
78
63
  constructor({ messenger, state, env, config, }: {
79
64
  messenger: NotificationServicesPushControllerMessenger;
@@ -99,6 +84,15 @@ export default class NotificationServicesPushController extends BaseController<t
99
84
  * This removes the registration token on this device, and ensures we unsubscribe from any listeners
100
85
  */
101
86
  disablePushNotifications(): Promise<void>;
87
+ /**
88
+ * Deletes backend push notification links for the given addresses on the current platform.
89
+ * This is used when accounts are removed (for example SRP removal), so backend can remove
90
+ * all associated FCM tokens for those address/platform pairs.
91
+ *
92
+ * @param addresses - Addresses that should be unlinked from push notifications.
93
+ * @returns Whether the delete request succeeded.
94
+ */
95
+ deletePushNotificationLinks(addresses: string[]): Promise<boolean>;
102
96
  /**
103
97
  * Updates the triggers for push notifications.
104
98
  * This method is responsible for updating the server with the new set of addresses that should trigger push notifications.
@@ -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,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAGlF,OAAO,KAAK,EAAE,GAAG,EAAE,iCAA6B;AAKhD,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAAgB;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,2CAAuC;AAClE,OAAO,KAAK,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wB,IAAI,OAAO,CAAC,IAAI,CAAC;IAmCtD;;;;;;;OAOG;IACU,8BAA8B,CACzC,SAAS,EAAE,MAAM,EAAE,GAClB,OAAO,CAAC,IAAI,CAAC;CAwCjB"}
1
+ {"version":3,"file":"NotificationServicesPushController.d.mts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAGlF,OAAO,KAAK,EAAE,+CAA+C,EAAE,qEAAiE;AAChI,OAAO,KAAK,EAAE,GAAG,EAAE,iCAA6B;AAMhD,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;AAUF,MAAM,MAAM,gDAAgD,GAC1D,wBAAwB,CACtB,OAAO,cAAc,EACrB,uCAAuC,CACxC,CAAC;AAEJ,MAAM,MAAM,OAAO,GACf,gDAAgD,GAChD,+CAA+C,CAAC;AAEpD,KAAK,cAAc,GACjB,wBAAwB,CAAC,4CAA4C,CAAC;AAExE,MAAM,MAAM,kDAAkD,GAC5D,0BAA0B,CACxB,OAAO,cAAc,EACrB,uCAAuC,CACxC,CAAC;AAEJ,MAAM,MAAM,wDAAwD,GAAG;IACrE,IAAI,EAAE,GAAG,OAAO,cAAc,qBAAqB,CAAC;IACpD,OAAO,EAAE,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IAAI,CAAC;IAmCtD;;;;;;;OAOG;IACU,2BAA2B,CACtC,SAAS,EAAE,MAAM,EAAE,GAClB,OAAO,CAAC,OAAO,CAAC;IAuBnB;;;;;;;OAOG;IACU,8BAA8B,CACzC,SAAS,EAAE,MAAM,EAAE,GAClB,OAAO,CAAC,IAAI,CAAC;CAwCjB"}
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _NotificationServicesPushController_instances, _NotificationServicesPushController_pushListenerUnsubscribe, _NotificationServicesPushController_env, _NotificationServicesPushController_config, _NotificationServicesPushController_registerMessageHandlers, _NotificationServicesPushController_clearLoadingStates, _NotificationServicesPushController_getAndAssertBearerToken, _NotificationServicesPushController_updatePushState;
12
+ var _NotificationServicesPushController_instances, _NotificationServicesPushController_pushListenerUnsubscribe, _NotificationServicesPushController_env, _NotificationServicesPushController_config, _NotificationServicesPushController_clearLoadingStates, _NotificationServicesPushController_getAndAssertBearerToken, _NotificationServicesPushController_updatePushState;
13
13
  function $importDefault(module) {
14
14
  if (module?.__esModule) {
15
15
  return module.default;
@@ -19,8 +19,15 @@ function $importDefault(module) {
19
19
  import { BaseController } from "@metamask/base-controller";
20
20
  import $log from "loglevel";
21
21
  const log = $importDefault($log);
22
- import { activatePushNotifications, deactivatePushNotifications } from "./services/services.mjs";
22
+ import { activatePushNotifications, deleteLinksAPI, deactivatePushNotifications } from "./services/services.mjs";
23
23
  const controllerName = 'NotificationServicesPushController';
24
+ const MESSENGER_EXPOSED_METHODS = [
25
+ 'subscribeToPushNotifications',
26
+ 'enablePushNotifications',
27
+ 'disablePushNotifications',
28
+ 'updateTriggerPushNotifications',
29
+ 'deletePushNotificationLinks',
30
+ ];
24
31
  export const defaultState = {
25
32
  isPushEnabled: true,
26
33
  fcmToken: '',
@@ -63,7 +70,7 @@ const defaultPushEnv = {
63
70
  * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.
64
71
  * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.
65
72
  */
66
- class NotificationServicesPushController extends BaseController {
73
+ export class NotificationServicesPushController extends BaseController {
67
74
  constructor({ messenger, state, env, config, }) {
68
75
  super({
69
76
  messenger,
@@ -77,7 +84,7 @@ class NotificationServicesPushController extends BaseController {
77
84
  _NotificationServicesPushController_config.set(this, void 0);
78
85
  __classPrivateFieldSet(this, _NotificationServicesPushController_env, env ?? defaultPushEnv, "f");
79
86
  __classPrivateFieldSet(this, _NotificationServicesPushController_config, config, "f");
80
- __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_registerMessageHandlers).call(this);
87
+ this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
81
88
  __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_clearLoadingStates).call(this);
82
89
  }
83
90
  async subscribeToPushNotifications() {
@@ -183,6 +190,34 @@ class NotificationServicesPushController extends BaseController {
183
190
  // Update State
184
191
  __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_updatePushState).call(this, { type: 'disable' });
185
192
  }
193
+ /**
194
+ * Deletes backend push notification links for the given addresses on the current platform.
195
+ * This is used when accounts are removed (for example SRP removal), so backend can remove
196
+ * all associated FCM tokens for those address/platform pairs.
197
+ *
198
+ * @param addresses - Addresses that should be unlinked from push notifications.
199
+ * @returns Whether the delete request succeeded.
200
+ */
201
+ async deletePushNotificationLinks(addresses) {
202
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled ||
203
+ addresses.length === 0 ||
204
+ !this.state.fcmToken) {
205
+ return false;
206
+ }
207
+ try {
208
+ const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
209
+ return await deleteLinksAPI({
210
+ bearerToken,
211
+ addresses,
212
+ platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
213
+ token: this.state.fcmToken,
214
+ env: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").env ?? 'prd',
215
+ });
216
+ }
217
+ catch {
218
+ return false;
219
+ }
220
+ }
186
221
  /**
187
222
  * Updates the triggers for push notifications.
188
223
  * This method is responsible for updating the server with the new set of addresses that should trigger push notifications.
@@ -229,12 +264,7 @@ class NotificationServicesPushController extends BaseController {
229
264
  }
230
265
  }
231
266
  }
232
- _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _NotificationServicesPushController_env = new WeakMap(), _NotificationServicesPushController_config = new WeakMap(), _NotificationServicesPushController_instances = new WeakSet(), _NotificationServicesPushController_registerMessageHandlers = function _NotificationServicesPushController_registerMessageHandlers() {
233
- this.messenger.registerActionHandler('NotificationServicesPushController:enablePushNotifications', this.enablePushNotifications.bind(this));
234
- this.messenger.registerActionHandler('NotificationServicesPushController:disablePushNotifications', this.disablePushNotifications.bind(this));
235
- this.messenger.registerActionHandler('NotificationServicesPushController:updateTriggerPushNotifications', this.updateTriggerPushNotifications.bind(this));
236
- this.messenger.registerActionHandler('NotificationServicesPushController:subscribeToPushNotifications', this.subscribeToPushNotifications.bind(this));
237
- }, _NotificationServicesPushController_clearLoadingStates = function _NotificationServicesPushController_clearLoadingStates() {
267
+ _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _NotificationServicesPushController_env = new WeakMap(), _NotificationServicesPushController_config = new WeakMap(), _NotificationServicesPushController_instances = new WeakSet(), _NotificationServicesPushController_clearLoadingStates = function _NotificationServicesPushController_clearLoadingStates() {
238
268
  this.update((state) => {
239
269
  state.isUpdatingFCMToken = false;
240
270
  });
@@ -268,5 +298,4 @@ _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _No
268
298
  });
269
299
  }
270
300
  };
271
- export default NotificationServicesPushController;
272
301
  //# sourceMappingURL=NotificationServicesPushController.mjs.map