@metamask-previews/notification-services-controller 0.21.0-preview-5575cd05 → 0.21.0-preview-a1eb992b

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 (119) hide show
  1. package/dist/NotificationServicesController/NotificationServicesController.cjs +74 -62
  2. package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
  3. package/dist/NotificationServicesController/NotificationServicesController.d.cts +34 -10
  4. package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
  5. package/dist/NotificationServicesController/NotificationServicesController.d.mts +34 -10
  6. package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
  7. package/dist/NotificationServicesController/NotificationServicesController.mjs +74 -62
  8. package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
  9. package/dist/NotificationServicesController/index.cjs +0 -1
  10. package/dist/NotificationServicesController/index.cjs.map +1 -1
  11. package/dist/NotificationServicesController/index.d.cts +0 -1
  12. package/dist/NotificationServicesController/index.d.cts.map +1 -1
  13. package/dist/NotificationServicesController/index.d.mts +0 -1
  14. package/dist/NotificationServicesController/index.d.mts.map +1 -1
  15. package/dist/NotificationServicesController/index.mjs +0 -1
  16. package/dist/NotificationServicesController/index.mjs.map +1 -1
  17. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs +56 -56
  18. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
  19. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +24 -18
  20. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
  21. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +24 -18
  22. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
  23. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +57 -57
  24. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
  25. package/dist/NotificationServicesPushController/__fixtures__/mockServices.cjs +13 -1
  26. package/dist/NotificationServicesPushController/__fixtures__/mockServices.cjs.map +1 -1
  27. package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.cts +1 -0
  28. package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.cts.map +1 -1
  29. package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.mts +1 -0
  30. package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.mts.map +1 -1
  31. package/dist/NotificationServicesPushController/__fixtures__/mockServices.mjs +12 -1
  32. package/dist/NotificationServicesPushController/__fixtures__/mockServices.mjs.map +1 -1
  33. package/dist/NotificationServicesPushController/constants.cjs +14 -0
  34. package/dist/NotificationServicesPushController/constants.cjs.map +1 -0
  35. package/dist/NotificationServicesPushController/constants.d.cts +11 -0
  36. package/dist/NotificationServicesPushController/constants.d.cts.map +1 -0
  37. package/dist/NotificationServicesPushController/constants.d.mts +11 -0
  38. package/dist/NotificationServicesPushController/constants.d.mts.map +1 -0
  39. package/dist/NotificationServicesPushController/constants.mjs +11 -0
  40. package/dist/NotificationServicesPushController/constants.mjs.map +1 -0
  41. package/dist/NotificationServicesPushController/{types/push-service-interface.cjs → services/push/index.cjs} +1 -1
  42. package/dist/NotificationServicesPushController/services/push/index.cjs.map +1 -0
  43. package/dist/NotificationServicesPushController/services/push/index.d.cts +6 -0
  44. package/dist/NotificationServicesPushController/services/push/index.d.cts.map +1 -0
  45. package/dist/NotificationServicesPushController/services/push/index.d.mts +6 -0
  46. package/dist/NotificationServicesPushController/services/push/index.d.mts.map +1 -0
  47. package/dist/NotificationServicesPushController/services/push/index.mjs +2 -0
  48. package/dist/NotificationServicesPushController/services/push/index.mjs.map +1 -0
  49. package/dist/NotificationServicesPushController/services/services.cjs +93 -15
  50. package/dist/NotificationServicesPushController/services/services.cjs.map +1 -1
  51. package/dist/NotificationServicesPushController/services/services.d.cts +27 -4
  52. package/dist/NotificationServicesPushController/services/services.d.cts.map +1 -1
  53. package/dist/NotificationServicesPushController/services/services.d.mts +27 -4
  54. package/dist/NotificationServicesPushController/services/services.d.mts.map +1 -1
  55. package/dist/NotificationServicesPushController/services/services.mjs +94 -14
  56. package/dist/NotificationServicesPushController/services/services.mjs.map +1 -1
  57. package/dist/NotificationServicesPushController/types/index.cjs.map +1 -1
  58. package/dist/NotificationServicesPushController/types/index.d.cts +0 -1
  59. package/dist/NotificationServicesPushController/types/index.d.cts.map +1 -1
  60. package/dist/NotificationServicesPushController/types/index.d.mts +0 -1
  61. package/dist/NotificationServicesPushController/types/index.d.mts.map +1 -1
  62. package/dist/NotificationServicesPushController/types/index.mjs.map +1 -1
  63. package/dist/NotificationServicesPushController/utils/get-notification-message.cjs +19 -1
  64. package/dist/NotificationServicesPushController/utils/get-notification-message.cjs.map +1 -1
  65. package/dist/NotificationServicesPushController/utils/get-notification-message.d.cts +7 -0
  66. package/dist/NotificationServicesPushController/utils/get-notification-message.d.cts.map +1 -1
  67. package/dist/NotificationServicesPushController/utils/get-notification-message.d.mts +7 -0
  68. package/dist/NotificationServicesPushController/utils/get-notification-message.d.mts.map +1 -1
  69. package/dist/NotificationServicesPushController/utils/get-notification-message.mjs +17 -0
  70. package/dist/NotificationServicesPushController/utils/get-notification-message.mjs.map +1 -1
  71. package/package.json +1 -11
  72. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.cjs +0 -14
  73. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.cjs.map +0 -1
  74. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.cts +0 -3
  75. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.cts.map +0 -1
  76. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.mts +0 -3
  77. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.mts.map +0 -1
  78. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.mjs +0 -10
  79. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.mjs.map +0 -1
  80. package/dist/NotificationServicesPushController/types/push-service-interface.cjs.map +0 -1
  81. package/dist/NotificationServicesPushController/types/push-service-interface.d.cts +0 -30
  82. package/dist/NotificationServicesPushController/types/push-service-interface.d.cts.map +0 -1
  83. package/dist/NotificationServicesPushController/types/push-service-interface.d.mts +0 -30
  84. package/dist/NotificationServicesPushController/types/push-service-interface.d.mts.map +0 -1
  85. package/dist/NotificationServicesPushController/types/push-service-interface.mjs +0 -2
  86. package/dist/NotificationServicesPushController/types/push-service-interface.mjs.map +0 -1
  87. package/dist/NotificationServicesPushController/web/index.cjs +0 -8
  88. package/dist/NotificationServicesPushController/web/index.cjs.map +0 -1
  89. package/dist/NotificationServicesPushController/web/index.d.cts +0 -2
  90. package/dist/NotificationServicesPushController/web/index.d.cts.map +0 -1
  91. package/dist/NotificationServicesPushController/web/index.d.mts +0 -2
  92. package/dist/NotificationServicesPushController/web/index.d.mts.map +0 -1
  93. package/dist/NotificationServicesPushController/web/index.mjs +0 -2
  94. package/dist/NotificationServicesPushController/web/index.mjs.map +0 -1
  95. package/dist/NotificationServicesPushController/web/push-utils.cjs +0 -170
  96. package/dist/NotificationServicesPushController/web/push-utils.cjs.map +0 -1
  97. package/dist/NotificationServicesPushController/web/push-utils.d.cts +0 -37
  98. package/dist/NotificationServicesPushController/web/push-utils.d.cts.map +0 -1
  99. package/dist/NotificationServicesPushController/web/push-utils.d.mts +0 -37
  100. package/dist/NotificationServicesPushController/web/push-utils.d.mts.map +0 -1
  101. package/dist/NotificationServicesPushController/web/push-utils.mjs +0 -168
  102. package/dist/NotificationServicesPushController/web/push-utils.mjs.map +0 -1
  103. package/dist/shared/index.cjs +0 -19
  104. package/dist/shared/index.cjs.map +0 -1
  105. package/dist/shared/index.d.cts +0 -3
  106. package/dist/shared/index.d.cts.map +0 -1
  107. package/dist/shared/index.d.mts +0 -3
  108. package/dist/shared/index.d.mts.map +0 -1
  109. package/dist/shared/index.mjs +0 -3
  110. package/dist/shared/index.mjs.map +0 -1
  111. package/dist/shared/is-onchain-notification.cjs +0 -22
  112. package/dist/shared/is-onchain-notification.cjs.map +0 -1
  113. package/dist/shared/is-onchain-notification.d.cts +0 -9
  114. package/dist/shared/is-onchain-notification.d.cts.map +0 -1
  115. package/dist/shared/is-onchain-notification.d.mts +0 -9
  116. package/dist/shared/is-onchain-notification.d.mts.map +0 -1
  117. package/dist/shared/is-onchain-notification.mjs +0 -18
  118. package/dist/shared/is-onchain-notification.mjs.map +0 -1
  119. package/push-services/web/package.json +0 -150
@@ -22,9 +22,37 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
25
28
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.updateTriggerPushNotifications = exports.deactivatePushNotifications = exports.activatePushNotifications = exports.updateLinksAPI = void 0;
29
+ exports.listenToPushNotifications = exports.updateTriggerPushNotifications = exports.deactivatePushNotifications = exports.activatePushNotifications = exports.updateLinksAPI = exports.getPushNotificationLinks = void 0;
30
+ const loglevel_1 = __importDefault(require("loglevel"));
27
31
  const endpoints = __importStar(require("./endpoints.cjs"));
32
+ const push_web_1 = require("./push/push-web.cjs");
33
+ /**
34
+ * Fetches push notification links from a remote endpoint using a BearerToken for authorization.
35
+ *
36
+ * @param bearerToken - The JSON Web Token used for authorization.
37
+ * @returns A promise that resolves with the links result or null if an error occurs.
38
+ */
39
+ async function getPushNotificationLinks(bearerToken) {
40
+ try {
41
+ const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {
42
+ headers: { Authorization: `Bearer ${bearerToken}` },
43
+ });
44
+ if (!response.ok) {
45
+ loglevel_1.default.error('Failed to fetch the push notification links');
46
+ throw new Error('Failed to fetch the push notification links');
47
+ }
48
+ return response.json();
49
+ }
50
+ catch (error) {
51
+ loglevel_1.default.error('Failed to fetch the push notification links', error);
52
+ return null;
53
+ }
54
+ }
55
+ exports.getPushNotificationLinks = getPushNotificationLinks;
28
56
  /**
29
57
  * Updates the push notification links on a remote API.
30
58
  *
@@ -61,28 +89,42 @@ exports.updateLinksAPI = updateLinksAPI;
61
89
  * @returns A promise that resolves with an object containing the success status and the BearerToken token.
62
90
  */
63
91
  async function activatePushNotifications(params) {
64
- const { bearerToken, triggers, env, createRegToken, platform } = params;
65
- const regToken = await createRegToken(env).catch(() => null);
92
+ const { bearerToken, triggers, env, createRegToken, platform, fcmToken } = params;
93
+ const notificationLinks = await getPushNotificationLinks(bearerToken);
94
+ if (!notificationLinks) {
95
+ return null;
96
+ }
97
+ const regToken = fcmToken ?? (await createRegToken(env).catch(() => null));
66
98
  if (!regToken) {
67
99
  return null;
68
100
  }
69
- await updateLinksAPI(bearerToken, triggers, [{ token: regToken, platform }]);
101
+ const newRegTokens = new Set(notificationLinks.registration_tokens);
102
+ newRegTokens.add({ token: regToken, platform });
103
+ await updateLinksAPI(bearerToken, triggers, Array.from(newRegTokens));
70
104
  return regToken;
71
105
  }
72
106
  exports.activatePushNotifications = activatePushNotifications;
73
107
  /**
74
- * Disables push notifications by removing the registration token
75
- * We do not need to unlink triggers, and remove old reg tokens (this is cleaned up in the back-end)
108
+ * Disables push notifications by removing the registration token and unlinking triggers.
76
109
  *
77
110
  * @param params - Deactivate Push Params
78
- * @returns A promise that resolves with true if push notifications were successfully disabled, false otherwise.
111
+ * @returns A promise that resolves with true if notifications were successfully disabled, false otherwise.
79
112
  */
80
113
  async function deactivatePushNotifications(params) {
81
- const { regToken, env, deleteRegToken } = params;
114
+ const { regToken, bearerToken, triggers, env, deleteRegToken } = params;
82
115
  // if we don't have a reg token, then we can early return
83
116
  if (!regToken) {
84
117
  return true;
85
118
  }
119
+ const notificationLinks = await getPushNotificationLinks(bearerToken);
120
+ if (!notificationLinks) {
121
+ return false;
122
+ }
123
+ const filteredRegTokens = notificationLinks.registration_tokens.filter((r) => r.token !== regToken);
124
+ const isTokenRemovedFromAPI = await updateLinksAPI(bearerToken, triggers, filteredRegTokens);
125
+ if (!isTokenRemovedFromAPI) {
126
+ return false;
127
+ }
86
128
  const isTokenRemovedFromFCM = await deleteRegToken(env);
87
129
  if (!isTokenRemovedFromFCM) {
88
130
  return false;
@@ -101,17 +143,53 @@ exports.deactivatePushNotifications = deactivatePushNotifications;
101
143
  * - fcmToken: the new or existing Firebase Cloud Messaging token used for the update, if applicable.
102
144
  */
103
145
  async function updateTriggerPushNotifications(params) {
104
- const { bearerToken, triggers, createRegToken, platform, deleteRegToken, env, } = params;
105
- await deleteRegToken(env);
106
- const newRegToken = await createRegToken(env);
107
- if (!newRegToken) {
108
- throw new Error('Failed to create a new registration token');
146
+ const { bearerToken, regToken, triggers, createRegToken, platform, deleteRegToken, env, } = params;
147
+ const notificationLinks = await getPushNotificationLinks(bearerToken);
148
+ if (!notificationLinks) {
149
+ return { isTriggersLinkedToPushNotifications: false };
150
+ }
151
+ // Create new registration token if doesn't exist
152
+ const hasRegToken = Boolean(regToken &&
153
+ notificationLinks.registration_tokens.some((r) => r.token === regToken));
154
+ let newRegToken = null;
155
+ if (!hasRegToken) {
156
+ await deleteRegToken(env);
157
+ newRegToken = await createRegToken(env);
158
+ if (!newRegToken) {
159
+ throw new Error('Failed to create a new registration token');
160
+ }
161
+ notificationLinks.registration_tokens.push({
162
+ token: newRegToken,
163
+ platform,
164
+ });
109
165
  }
110
- const isTriggersLinkedToPushNotifications = await updateLinksAPI(bearerToken, triggers, [{ token: newRegToken, platform }]);
166
+ const isTriggersLinkedToPushNotifications = await updateLinksAPI(bearerToken, triggers, notificationLinks.registration_tokens);
111
167
  return {
112
168
  isTriggersLinkedToPushNotifications,
113
- fcmToken: newRegToken,
169
+ fcmToken: newRegToken ?? null,
114
170
  };
115
171
  }
116
172
  exports.updateTriggerPushNotifications = updateTriggerPushNotifications;
173
+ /**
174
+ * Listens to push notifications and invokes the provided callback function with the received notification data.
175
+ *
176
+ * @param params - listen params
177
+ * @returns A promise that resolves to an unsubscribe function to stop listening to push notifications.
178
+ */
179
+ async function listenToPushNotifications(params) {
180
+ const { env, listenToPushReceived, listenToPushClicked } = params;
181
+ /*
182
+ Push notifications require 2 listeners that need tracking (when creating and for tearing down):
183
+ 1. handling receiving a push notification (and the content we want to display)
184
+ 2. handling when a user clicks on a push notification
185
+ */
186
+ const unsubscribePushNotifications = await (0, push_web_1.listenToPushNotificationsReceived)(env, listenToPushReceived);
187
+ const unsubscribeNotificationClicks = (0, push_web_1.listenToPushNotificationsClicked)(listenToPushClicked);
188
+ const unsubscribe = () => {
189
+ unsubscribePushNotifications?.();
190
+ unsubscribeNotificationClicks();
191
+ };
192
+ return unsubscribe;
193
+ }
194
+ exports.listenToPushNotifications = listenToPushNotifications;
117
195
  //# sourceMappingURL=services.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"services.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2DAAyC;AAoBzC;;;;;;;GAOG;AACI,KAAK,UAAU,cAAc,CAClC,WAAmB,EACnB,QAAkB,EAClB,SAAqB;IAErB,IAAI;QACF,MAAM,IAAI,GAAgB;YACxB,WAAW,EAAE,QAAQ;YACrB,mBAAmB,EAAE,SAAS;SAC/B,CAAC;QACF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,4BAA4B,EAAE;YACnE,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,aAAa,EAAE,UAAU,WAAW,EAAE;gBACtC,cAAc,EAAE,kBAAkB;aACnC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC,CAAC;QACH,OAAO,QAAQ,CAAC,MAAM,KAAK,GAAG,CAAC;KAChC;IAAC,MAAM;QACN,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAtBD,wCAsBC;AAaD;;;;;GAKG;AACI,KAAK,UAAU,yBAAyB,CAC7C,MAAuC;IAEvC,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,GAAG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AAQ,EAAE,CAAC,CACnC,CAAC;IAEF,OAAO;QACL,mCAAmC;QACnC,QAAQ,EAAE,WAAW;KACtB,CAAC;AACJ,CAAC;AA/BD,wEA+BC","sourcesContent":["import * as endpoints from './endpoints';\nimport type { PushNotificationEnv } from '../types';\nimport type {\n CreateRegToken,\n DeleteRegToken,\n} from '../types/push-service-interface';\n\nexport type RegToken = {\n token: string;\n platform: 'extension' | 'mobile' | 'portfolio';\n};\n\n/**\n * Links API Response Shape\n */\nexport type LinksResult = {\n trigger_ids: string[];\n registration_tokens: RegToken[];\n};\n\n/**\n * Updates the push notification links on a remote API.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @param triggers - An array of trigger identifiers.\n * @param regTokens - An array of registration tokens.\n * @returns A promise that resolves with true if the update was successful, false otherwise.\n */\nexport async function updateLinksAPI(\n bearerToken: string,\n triggers: string[],\n regTokens: RegToken[],\n): Promise<boolean> {\n try {\n const body: LinksResult = {\n trigger_ids: triggers,\n registration_tokens: regTokens,\n };\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${bearerToken}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(body),\n });\n return response.status === 200;\n } catch {\n return false;\n }\n}\n\ntype ActivatePushNotificationsParams = {\n // Push Links\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n};\n\n/**\n * Enables push notifications by registering the device and linking triggers.\n *\n * @param params - Activate Push Params\n * @returns A promise that resolves with an object containing the success status and the BearerToken token.\n */\nexport async function activatePushNotifications(\n params: ActivatePushNotificationsParams,\n): Promise<string | null> {\n const { bearerToken, triggers, env, createRegToken, platform } = params;\n\n const regToken = await createRegToken(env).catch(() => null);\n if (!regToken) {\n return null;\n }\n\n await updateLinksAPI(bearerToken, triggers, [{ token: regToken, platform }]);\n return regToken;\n}\n\ntype DeactivatePushNotificationsParams = {\n // Push Links\n regToken: string;\n\n // Push Un-registration\n env: PushNotificationEnv;\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Disables push notifications by removing the registration token\n * We do not need to unlink triggers, and remove old reg tokens (this is cleaned up in the back-end)\n *\n * @param params - Deactivate Push Params\n * @returns A promise that resolves with true if push notifications were successfully disabled, false otherwise.\n */\nexport async function deactivatePushNotifications(\n params: DeactivatePushNotificationsParams,\n): Promise<boolean> {\n const { regToken, env, deleteRegToken } = params;\n\n // if we don't have a reg token, then we can early return\n if (!regToken) {\n return true;\n }\n\n const isTokenRemovedFromFCM = await deleteRegToken(env);\n if (!isTokenRemovedFromFCM) {\n return false;\n }\n\n return true;\n}\n\ntype UpdateTriggerPushNotificationsParams = {\n // Push Links\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n\n // Push Un-registration\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Updates the triggers linked to push notifications for a given registration token.\n * If the provided registration token does not exist or is not in the current set of registration tokens,\n * a new registration token is created and used for the update.\n *\n * @param params - Update Push Params\n * @returns A promise that resolves with an object containing:\n * - isTriggersLinkedToPushNotifications: boolean indicating if the triggers were successfully updated.\n * - fcmToken: the new or existing Firebase Cloud Messaging token used for the update, if applicable.\n */\nexport async function updateTriggerPushNotifications(\n params: UpdateTriggerPushNotificationsParams,\n): Promise<{\n isTriggersLinkedToPushNotifications: boolean;\n fcmToken?: string | null;\n}> {\n const {\n bearerToken,\n triggers,\n createRegToken,\n platform,\n deleteRegToken,\n env,\n } = params;\n\n await deleteRegToken(env);\n const newRegToken = await createRegToken(env);\n if (!newRegToken) {\n throw new Error('Failed to create a new registration token');\n }\n\n const isTriggersLinkedToPushNotifications = await updateLinksAPI(\n bearerToken,\n triggers,\n [{ token: newRegToken, platform }],\n );\n\n return {\n isTriggersLinkedToPushNotifications,\n fcmToken: newRegToken,\n };\n}\n"]}
1
+ {"version":3,"file":"services.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wDAA2B;AAE3B,2DAAyC;AAEzC,kDAGyB;AAkBzB;;;;;GAKG;AACI,KAAK,UAAU,wBAAwB,CAC5C,WAAmB;IAEnB,IAAI;QACF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,4BAA4B,EAAE;YACnE,OAAO,EAAE,EAAE,aAAa,EAAE,UAAU,WAAW,EAAE,EAAE;SACpD,CAAC,CAAC;QACH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;YAChB,kBAAG,CAAC,KAAK,CAAC,6CAA6C,CAAC,CAAC;YACzD,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;SAChE;QACD,OAAO,QAAQ,CAAC,IAAI,EAA0B,CAAC;KAChD;IAAC,OAAO,KAAK,EAAE;QACd,kBAAG,CAAC,KAAK,CAAC,6CAA6C,EAAE,KAAK,CAAC,CAAC;QAChE,OAAO,IAAI,CAAC;KACb;AACH,CAAC;AAhBD,4DAgBC;AAED;;;;;;;GAOG;AACI,KAAK,UAAU,cAAc,CAClC,WAAmB,EACnB,QAAkB,EAClB,SAAqB;IAErB,IAAI;QACF,MAAM,IAAI,GAAgB;YACxB,WAAW,EAAE,QAAQ;YACrB,mBAAmB,EAAE,SAAS;SAC/B,CAAC;QACF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,4BAA4B,EAAE;YACnE,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,aAAa,EAAE,UAAU,WAAW,EAAE;gBACtC,cAAc,EAAE,kBAAkB;aACnC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC,CAAC;QACH,OAAO,QAAQ,CAAC,MAAM,KAAK,GAAG,CAAC;KAChC;IAAC,MAAM;Q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AAmB,CAAC,IAAI,CAAC;YACzC,KAAK,EAAE,WAAW;YAClB,QAAQ;SACT,CAAC,CAAC;KACJ;IAED,MAAM,mCAAmC,GAAG,MAAM,cAAc,CAC9D,WAAW,EACX,QAAQ,EACR,iBAAiB,CAAC,mBAAmB,CACtC,CAAC;IAEF,OAAO;QACL,mCAAmC;QACnC,QAAQ,EAAE,WAAW,IAAI,IAAI;KAC9B,CAAC;AACJ,CAAC;AAjDD,wEAiDC;AAaD;;;;;GAKG;AACI,KAAK,UAAU,yBAAyB,CAC7C,MAAuC;IAEvC,MAAM,EAAE,GAAG,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,GAAG,MAAM,CAAC;IAElE;;;;MAIE;IACF,MAAM,4BAA4B,GAAG,MAAM,IAAA,4CAAiC,EAC1E,GAAG,EACH,oBAAoB,CACrB,CAAC;IACF,MAAM,6BAA6B,GACjC,IAAA,2CAAgC,EAAC,mBAAmB,CAAC,CAAC;IAExD,MAAM,WAAW,GAAG,GAAG,EAAE;QACvB,4BAA4B,EAAE,EAAE,CAAC;QACjC,6BAA6B,EAAE,CAAC;IAClC,CAAC,CAAC;IAEF,OAAO,WAAW,CAAC;AACrB,CAAC;AAvBD,8DAuBC","sourcesContent":["import log from 'loglevel';\n\nimport * as endpoints from './endpoints';\nimport type { CreateRegToken, DeleteRegToken } from './push';\nimport {\n listenToPushNotificationsClicked,\n listenToPushNotificationsReceived,\n} from './push/push-web';\nimport type { Types } from '../../NotificationServicesController';\nimport type { PushNotificationEnv } from '../types';\n\nexport type RegToken = {\n token: string;\n platform: 'extension' | 'mobile' | 'portfolio';\n};\n\n/**\n * Links API Response Shape\n */\nexport type LinksResult = {\n trigger_ids: string[];\n\n registration_tokens: RegToken[];\n};\n\n/**\n * Fetches push notification links from a remote endpoint using a BearerToken for authorization.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @returns A promise that resolves with the links result or null if an error occurs.\n */\nexport async function getPushNotificationLinks(\n bearerToken: string,\n): Promise<LinksResult | null> {\n try {\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n headers: { Authorization: `Bearer ${bearerToken}` },\n });\n if (!response.ok) {\n log.error('Failed to fetch the push notification links');\n throw new Error('Failed to fetch the push notification links');\n }\n return response.json() as Promise<LinksResult>;\n } catch (error) {\n log.error('Failed to fetch the push notification links', error);\n return null;\n }\n}\n\n/**\n * Updates the push notification links on a remote API.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @param triggers - An array of trigger identifiers.\n * @param regTokens - An array of registration tokens.\n * @returns A promise that resolves with true if the update was successful, false otherwise.\n */\nexport async function updateLinksAPI(\n bearerToken: string,\n triggers: string[],\n regTokens: RegToken[],\n): Promise<boolean> {\n try {\n const body: LinksResult = {\n trigger_ids: triggers,\n registration_tokens: regTokens,\n };\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${bearerToken}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(body),\n });\n return response.status === 200;\n } catch {\n return false;\n }\n}\n\ntype ActivatePushNotificationsParams = {\n // Push Links\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n fcmToken?: string;\n};\n\n/**\n * Enables push notifications by registering the device and linking triggers.\n *\n * @param params - Activate Push Params\n * @returns A promise that resolves with an object containing the success status and the BearerToken token.\n */\nexport async function activatePushNotifications(\n params: ActivatePushNotificationsParams,\n): Promise<string | null> {\n const { bearerToken, triggers, env, createRegToken, platform, fcmToken } =\n params;\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n\n if (!notificationLinks) {\n return null;\n }\n\n const regToken = fcmToken ?? (await createRegToken(env).catch(() => null));\n if (!regToken) {\n return null;\n }\n\n const newRegTokens = new Set(notificationLinks.registration_tokens);\n newRegTokens.add({ token: regToken, platform });\n\n await updateLinksAPI(bearerToken, triggers, Array.from(newRegTokens));\n return regToken;\n}\n\ntype DeactivatePushNotificationsParams = {\n // Push Links\n regToken: string;\n bearerToken: string;\n triggers: string[];\n\n // Push Un-registration\n env: PushNotificationEnv;\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Disables push notifications by removing the registration token and unlinking triggers.\n *\n * @param params - Deactivate Push Params\n * @returns A promise that resolves with true if notifications were successfully disabled, false otherwise.\n */\nexport async function deactivatePushNotifications(\n params: DeactivatePushNotificationsParams,\n): Promise<boolean> {\n const { regToken, bearerToken, triggers, env, deleteRegToken } = params;\n\n // if we don't have a reg token, then we can early return\n if (!regToken) {\n return true;\n }\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n if (!notificationLinks) {\n return false;\n }\n\n const filteredRegTokens = notificationLinks.registration_tokens.filter(\n (r) => r.token !== regToken,\n );\n\n const isTokenRemovedFromAPI = await updateLinksAPI(\n bearerToken,\n triggers,\n filteredRegTokens,\n );\n if (!isTokenRemovedFromAPI) {\n return false;\n }\n\n const isTokenRemovedFromFCM = await deleteRegToken(env);\n if (!isTokenRemovedFromFCM) {\n return false;\n }\n\n return true;\n}\n\ntype UpdateTriggerPushNotificationsParams = {\n // Push Links\n regToken: string;\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n\n // Push Un-registration\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Updates the triggers linked to push notifications for a given registration token.\n * If the provided registration token does not exist or is not in the current set of registration tokens,\n * a new registration token is created and used for the update.\n *\n * @param params - Update Push Params\n * @returns A promise that resolves with an object containing:\n * - isTriggersLinkedToPushNotifications: boolean indicating if the triggers were successfully updated.\n * - fcmToken: the new or existing Firebase Cloud Messaging token used for the update, if applicable.\n */\nexport async function updateTriggerPushNotifications(\n params: UpdateTriggerPushNotificationsParams,\n): Promise<{\n isTriggersLinkedToPushNotifications: boolean;\n fcmToken?: string | null;\n}> {\n const {\n bearerToken,\n regToken,\n triggers,\n createRegToken,\n platform,\n deleteRegToken,\n env,\n } = params;\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n if (!notificationLinks) {\n return { isTriggersLinkedToPushNotifications: false };\n }\n // Create new registration token if doesn't exist\n const hasRegToken = Boolean(\n regToken &&\n notificationLinks.registration_tokens.some((r) => r.token === regToken),\n );\n\n let newRegToken: string | null = null;\n if (!hasRegToken) {\n await deleteRegToken(env);\n newRegToken = await createRegToken(env);\n if (!newRegToken) {\n throw new Error('Failed to create a new registration token');\n }\n notificationLinks.registration_tokens.push({\n token: newRegToken,\n platform,\n });\n }\n\n const isTriggersLinkedToPushNotifications = await updateLinksAPI(\n bearerToken,\n triggers,\n notificationLinks.registration_tokens,\n );\n\n return {\n isTriggersLinkedToPushNotifications,\n fcmToken: newRegToken ?? null,\n };\n}\n\ntype ListenToPushNotificationsParams = {\n env: PushNotificationEnv;\n listenToPushReceived: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n listenToPushClicked: (\n event: NotificationEvent,\n notification?: Types.INotification,\n ) => void;\n};\n\n/**\n * Listens to push notifications and invokes the provided callback function with the received notification data.\n *\n * @param params - listen params\n * @returns A promise that resolves to an unsubscribe function to stop listening to push notifications.\n */\nexport async function listenToPushNotifications(\n params: ListenToPushNotificationsParams,\n): Promise<() => void> {\n const { env, listenToPushReceived, listenToPushClicked } = params;\n\n /*\n Push notifications require 2 listeners that need tracking (when creating and for tearing down):\n 1. handling receiving a push notification (and the content we want to display)\n 2. handling when a user clicks on a push notification\n */\n const unsubscribePushNotifications = await listenToPushNotificationsReceived(\n env,\n listenToPushReceived,\n );\n const unsubscribeNotificationClicks =\n listenToPushNotificationsClicked(listenToPushClicked);\n\n const unsubscribe = () => {\n unsubscribePushNotifications?.();\n unsubscribeNotificationClicks();\n };\n\n return unsubscribe;\n}\n"]}
@@ -1,5 +1,6 @@
1
+ import type { CreateRegToken, DeleteRegToken } from "./push/index.cjs";
2
+ import type { Types } from "../../NotificationServicesController/index.cjs";
1
3
  import type { PushNotificationEnv } from "../types/index.cjs";
2
- import type { CreateRegToken, DeleteRegToken } from "../types/push-service-interface.cjs";
3
4
  export type RegToken = {
4
5
  token: string;
5
6
  platform: 'extension' | 'mobile' | 'portfolio';
@@ -11,6 +12,13 @@ export type LinksResult = {
11
12
  trigger_ids: string[];
12
13
  registration_tokens: RegToken[];
13
14
  };
15
+ /**
16
+ * Fetches push notification links from a remote endpoint using a BearerToken for authorization.
17
+ *
18
+ * @param bearerToken - The JSON Web Token used for authorization.
19
+ * @returns A promise that resolves with the links result or null if an error occurs.
20
+ */
21
+ export declare function getPushNotificationLinks(bearerToken: string): Promise<LinksResult | null>;
14
22
  /**
15
23
  * Updates the push notification links on a remote API.
16
24
  *
@@ -26,6 +34,7 @@ type ActivatePushNotificationsParams = {
26
34
  env: PushNotificationEnv;
27
35
  createRegToken: CreateRegToken;
28
36
  platform: 'extension' | 'mobile' | 'portfolio';
37
+ fcmToken?: string;
29
38
  };
30
39
  /**
31
40
  * Enables push notifications by registering the device and linking triggers.
@@ -36,18 +45,20 @@ type ActivatePushNotificationsParams = {
36
45
  export declare function activatePushNotifications(params: ActivatePushNotificationsParams): Promise<string | null>;
37
46
  type DeactivatePushNotificationsParams = {
38
47
  regToken: string;
48
+ bearerToken: string;
49
+ triggers: string[];
39
50
  env: PushNotificationEnv;
40
51
  deleteRegToken: DeleteRegToken;
41
52
  };
42
53
  /**
43
- * Disables push notifications by removing the registration token
44
- * We do not need to unlink triggers, and remove old reg tokens (this is cleaned up in the back-end)
54
+ * Disables push notifications by removing the registration token and unlinking triggers.
45
55
  *
46
56
  * @param params - Deactivate Push Params
47
- * @returns A promise that resolves with true if push notifications were successfully disabled, false otherwise.
57
+ * @returns A promise that resolves with true if notifications were successfully disabled, false otherwise.
48
58
  */
49
59
  export declare function deactivatePushNotifications(params: DeactivatePushNotificationsParams): Promise<boolean>;
50
60
  type UpdateTriggerPushNotificationsParams = {
61
+ regToken: string;
51
62
  bearerToken: string;
52
63
  triggers: string[];
53
64
  env: PushNotificationEnv;
@@ -69,5 +80,17 @@ export declare function updateTriggerPushNotifications(params: UpdateTriggerPush
69
80
  isTriggersLinkedToPushNotifications: boolean;
70
81
  fcmToken?: string | null;
71
82
  }>;
83
+ type ListenToPushNotificationsParams = {
84
+ env: PushNotificationEnv;
85
+ listenToPushReceived: (notification: Types.INotification) => void | Promise<void>;
86
+ listenToPushClicked: (event: NotificationEvent, notification?: Types.INotification) => void;
87
+ };
88
+ /**
89
+ * Listens to push notifications and invokes the provided callback function with the received notification data.
90
+ *
91
+ * @param params - listen params
92
+ * @returns A promise that resolves to an unsubscribe function to stop listening to push notifications.
93
+ */
94
+ export declare function listenToPushNotifications(params: ListenToPushNotificationsParams): Promise<() => void>;
72
95
  export {};
73
96
  //# sourceMappingURL=services.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"services.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,2BAAiB;AACpD,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACf,4CAAwC;AAEzC,MAAM,MAAM,QAAQ,GAAG;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;CAChD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,mBAAmB,EAAE,QAAQ,EAAE,CAAC;CACjC,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAClC,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,MAAM,EAAE,EAClB,SAAS,EAAE,QAAQ,EAAE,GACpB,OAAO,CAAC,OAAO,CAAC,CAkBlB;AAED,KAAK,+BAA+B,GAAG;IAErC,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,EAAE,CAAC;IAGnB,GAAG,EAAE,mBAAmB,CAAC;IACzB,cAAc,EAAE,cAAc,CAAC;IAC/B,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;CAChD,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,yBAAyB,CAC7C,MAAM,EAAE,+BAA+B,GACtC,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAUxB;AAED,KAAK,iCAAiC,GAAG;IAEvC,QAAQ,EAAE,MAAM,CAAC;IAGjB,GAAG,EAAE,mBAAmB,CAAC;IACzB,cAAc,EAAE,cAAc,CAAC;CAChC,CAAC;AAEF;;;;;;GAMG;AACH,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,iCAAiC,GACxC,OAAO,CAAC,OAAO,CAAC,CAclB;AAED,KAAK,oCAAoC,GAAG;IAE1C,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,EAAE,CAAC;IAGnB,GAAG,EAAE,mBAAmB,CAAC;IACzB,cAAc,EAAE,cAAc,CAAC;IAC/B,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;IAG/C,cAAc,EAAE,cAAc,CAAC;CAChC,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAsB,8BAA8B,CAClD,MAAM,EAAE,oCAAoC,GAC3C,OAAO,CAAC;IACT,mCAAmC,EAAE,OAAO,CAAC;IAC7C,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B,CAAC,CA0BD"}
1
+ {"version":3,"file":"services.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,yBAAe;AAK7D,OAAO,KAAK,EAAE,KAAK,EAAE,uDAA6C;AAClE,OAAO,KAAK,EAAE,mBAAmB,EAAE,2BAAiB;AAEpD,MAAM,MAAM,QAAQ,GAAG;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,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AAM,CAAC;IACpB,QAAQ,EAAE,MAAM,EAAE,CAAC;IAGnB,GAAG,EAAE,mBAAmB,CAAC;IACzB,cAAc,EAAE,cAAc,CAAC;IAC/B,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;IAG/C,cAAc,EAAE,cAAc,CAAC;CAChC,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAsB,8BAA8B,CAClD,MAAM,EAAE,oCAAoC,GAC3C,OAAO,CAAC;IACT,mCAAmC,EAAE,OAAO,CAAC;IAC7C,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B,CAAC,CA4CD;AAED,KAAK,+BAA+B,GAAG;IACrC,GAAG,EAAE,mBAAmB,CAAC;IACzB,oBAAoB,EAAE,CACpB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,mBAAmB,EAAE,CACnB,KAAK,EAAE,iBAAiB,EACxB,YAAY,CAAC,EAAE,KAAK,CAAC,aAAa,KAC/B,IAAI,CAAC;CACX,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,yBAAyB,CAC7C,MAAM,EAAE,+BAA+B,GACtC,OAAO,CAAC,MAAM,IAAI,CAAC,CAqBrB"}
@@ -1,5 +1,6 @@
1
+ import type { CreateRegToken, DeleteRegToken } from "./push/index.mjs";
2
+ import type { Types } from "../../NotificationServicesController/index.mjs";
1
3
  import type { PushNotificationEnv } from "../types/index.mjs";
2
- import type { CreateRegToken, DeleteRegToken } from "../types/push-service-interface.mjs";
3
4
  export type RegToken = {
4
5
  token: string;
5
6
  platform: 'extension' | 'mobile' | 'portfolio';
@@ -11,6 +12,13 @@ export type LinksResult = {
11
12
  trigger_ids: string[];
12
13
  registration_tokens: RegToken[];
13
14
  };
15
+ /**
16
+ * Fetches push notification links from a remote endpoint using a BearerToken for authorization.
17
+ *
18
+ * @param bearerToken - The JSON Web Token used for authorization.
19
+ * @returns A promise that resolves with the links result or null if an error occurs.
20
+ */
21
+ export declare function getPushNotificationLinks(bearerToken: string): Promise<LinksResult | null>;
14
22
  /**
15
23
  * Updates the push notification links on a remote API.
16
24
  *
@@ -26,6 +34,7 @@ type ActivatePushNotificationsParams = {
26
34
  env: PushNotificationEnv;
27
35
  createRegToken: CreateRegToken;
28
36
  platform: 'extension' | 'mobile' | 'portfolio';
37
+ fcmToken?: string;
29
38
  };
30
39
  /**
31
40
  * Enables push notifications by registering the device and linking triggers.
@@ -36,18 +45,20 @@ type ActivatePushNotificationsParams = {
36
45
  export declare function activatePushNotifications(params: ActivatePushNotificationsParams): Promise<string | null>;
37
46
  type DeactivatePushNotificationsParams = {
38
47
  regToken: string;
48
+ bearerToken: string;
49
+ triggers: string[];
39
50
  env: PushNotificationEnv;
40
51
  deleteRegToken: DeleteRegToken;
41
52
  };
42
53
  /**
43
- * Disables push notifications by removing the registration token
44
- * We do not need to unlink triggers, and remove old reg tokens (this is cleaned up in the back-end)
54
+ * Disables push notifications by removing the registration token and unlinking triggers.
45
55
  *
46
56
  * @param params - Deactivate Push Params
47
- * @returns A promise that resolves with true if push notifications were successfully disabled, false otherwise.
57
+ * @returns A promise that resolves with true if notifications were successfully disabled, false otherwise.
48
58
  */
49
59
  export declare function deactivatePushNotifications(params: DeactivatePushNotificationsParams): Promise<boolean>;
50
60
  type UpdateTriggerPushNotificationsParams = {
61
+ regToken: string;
51
62
  bearerToken: string;
52
63
  triggers: string[];
53
64
  env: PushNotificationEnv;
@@ -69,5 +80,17 @@ export declare function updateTriggerPushNotifications(params: UpdateTriggerPush
69
80
  isTriggersLinkedToPushNotifications: boolean;
70
81
  fcmToken?: string | null;
71
82
  }>;
83
+ type ListenToPushNotificationsParams = {
84
+ env: PushNotificationEnv;
85
+ listenToPushReceived: (notification: Types.INotification) => void | Promise<void>;
86
+ listenToPushClicked: (event: NotificationEvent, notification?: Types.INotification) => void;
87
+ };
88
+ /**
89
+ * Listens to push notifications and invokes the provided callback function with the received notification data.
90
+ *
91
+ * @param params - listen params
92
+ * @returns A promise that resolves to an unsubscribe function to stop listening to push notifications.
93
+ */
94
+ export declare function listenToPushNotifications(params: ListenToPushNotificationsParams): Promise<() => void>;
72
95
  export {};
73
96
  //# sourceMappingURL=services.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"services.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,2BAAiB;AACpD,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACf,4CAAwC;AAEzC,MAAM,MAAM,QAAQ,GAAG;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;CAChD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,mBAAmB,EAAE,QAAQ,EAAE,CAAC;CACjC,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAClC,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,MAAM,EAAE,EAClB,SAAS,EAAE,QAAQ,EAAE,GACpB,OAAO,CAAC,OAAO,CAAC,CAkBlB;AAED,KAAK,+BAA+B,GAAG;IAErC,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,EAAE,CAAC;IAGnB,GAAG,EAAE,mBAAmB,CAAC;IACzB,cAAc,EAAE,cAAc,CAAC;IAC/B,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;CAChD,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,yBAAyB,CAC7C,MAAM,EAAE,+BAA+B,GACtC,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAUxB;AAED,KAAK,iCAAiC,GAAG;IAEvC,QAAQ,EAAE,MAAM,CAAC;IAGjB,GAAG,EAAE,mBAAmB,CAAC;IACzB,cAAc,EAAE,cAAc,CAAC;CAChC,CAAC;AAEF;;;;;;GAMG;AACH,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,iCAAiC,GACxC,OAAO,CAAC,OAAO,CAAC,CAclB;AAED,KAAK,oCAAoC,GAAG;IAE1C,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,EAAE,CAAC;IAGnB,GAAG,EAAE,mBAAmB,CAAC;IACzB,cAAc,EAAE,cAAc,CAAC;IAC/B,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;IAG/C,cAAc,EAAE,cAAc,CAAC;CAChC,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAsB,8BAA8B,CAClD,MAAM,EAAE,oCAAoC,GAC3C,OAAO,CAAC;IACT,mCAAmC,EAAE,OAAO,CAAC;IAC7C,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B,CAAC,CA0BD"}
1
+ {"version":3,"file":"services.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,yBAAe;AAK7D,OAAO,KAAK,EAAE,KAAK,EAAE,uDAA6C;AAClE,OAAO,KAAK,EAAE,mBAAmB,EAAE,2BAAiB;AAEpD,MAAM,MAAM,QAAQ,GAAG;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAC;CAChD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,WAAW,EAAE,MAAM,EAAE,CAAC;IAEtB,mBAAmB,EAAE,QAAQ,EAAE,CAAC;CACjC,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,wBAAwB,CAC5C,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAc7B;AAED;;;;;;;GAOG;AACH,wBAAsB,cAAc,CA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CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B,CAAC,CA4CD;AAED,KAAK,+BAA+B,GAAG;IACrC,GAAG,EAAE,mBAAmB,CAAC;IACzB,oBAAoB,EAAE,CACpB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,mBAAmB,EAAE,CACnB,KAAK,EAAE,iBAAiB,EACxB,YAAY,CAAC,EAAE,KAAK,CAAC,aAAa,KAC/B,IAAI,CAAC;CACX,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,yBAAyB,CAC7C,MAAM,EAAE,+BAA+B,GACtC,OAAO,CAAC,MAAM,IAAI,CAAC,CAqBrB"}
@@ -1,4 +1,35 @@
1
+ function $importDefault(module) {
2
+ if (module?.__esModule) {
3
+ return module.default;
4
+ }
5
+ return module;
6
+ }
7
+ import $log from "loglevel";
8
+ const log = $importDefault($log);
1
9
  import * as endpoints from "./endpoints.mjs";
10
+ import { listenToPushNotificationsClicked, listenToPushNotificationsReceived } from "./push/push-web.mjs";
11
+ /**
12
+ * Fetches push notification links from a remote endpoint using a BearerToken for authorization.
13
+ *
14
+ * @param bearerToken - The JSON Web Token used for authorization.
15
+ * @returns A promise that resolves with the links result or null if an error occurs.
16
+ */
17
+ export async function getPushNotificationLinks(bearerToken) {
18
+ try {
19
+ const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {
20
+ headers: { Authorization: `Bearer ${bearerToken}` },
21
+ });
22
+ if (!response.ok) {
23
+ log.error('Failed to fetch the push notification links');
24
+ throw new Error('Failed to fetch the push notification links');
25
+ }
26
+ return response.json();
27
+ }
28
+ catch (error) {
29
+ log.error('Failed to fetch the push notification links', error);
30
+ return null;
31
+ }
32
+ }
2
33
  /**
3
34
  * Updates the push notification links on a remote API.
4
35
  *
@@ -34,27 +65,41 @@ export async function updateLinksAPI(bearerToken, triggers, regTokens) {
34
65
  * @returns A promise that resolves with an object containing the success status and the BearerToken token.
35
66
  */
36
67
  export async function activatePushNotifications(params) {
37
- const { bearerToken, triggers, env, createRegToken, platform } = params;
38
- const regToken = await createRegToken(env).catch(() => null);
68
+ const { bearerToken, triggers, env, createRegToken, platform, fcmToken } = params;
69
+ const notificationLinks = await getPushNotificationLinks(bearerToken);
70
+ if (!notificationLinks) {
71
+ return null;
72
+ }
73
+ const regToken = fcmToken ?? (await createRegToken(env).catch(() => null));
39
74
  if (!regToken) {
40
75
  return null;
41
76
  }
42
- await updateLinksAPI(bearerToken, triggers, [{ token: regToken, platform }]);
77
+ const newRegTokens = new Set(notificationLinks.registration_tokens);
78
+ newRegTokens.add({ token: regToken, platform });
79
+ await updateLinksAPI(bearerToken, triggers, Array.from(newRegTokens));
43
80
  return regToken;
44
81
  }
45
82
  /**
46
- * Disables push notifications by removing the registration token
47
- * We do not need to unlink triggers, and remove old reg tokens (this is cleaned up in the back-end)
83
+ * Disables push notifications by removing the registration token and unlinking triggers.
48
84
  *
49
85
  * @param params - Deactivate Push Params
50
- * @returns A promise that resolves with true if push notifications were successfully disabled, false otherwise.
86
+ * @returns A promise that resolves with true if notifications were successfully disabled, false otherwise.
51
87
  */
52
88
  export async function deactivatePushNotifications(params) {
53
- const { regToken, env, deleteRegToken } = params;
89
+ const { regToken, bearerToken, triggers, env, deleteRegToken } = params;
54
90
  // if we don't have a reg token, then we can early return
55
91
  if (!regToken) {
56
92
  return true;
57
93
  }
94
+ const notificationLinks = await getPushNotificationLinks(bearerToken);
95
+ if (!notificationLinks) {
96
+ return false;
97
+ }
98
+ const filteredRegTokens = notificationLinks.registration_tokens.filter((r) => r.token !== regToken);
99
+ const isTokenRemovedFromAPI = await updateLinksAPI(bearerToken, triggers, filteredRegTokens);
100
+ if (!isTokenRemovedFromAPI) {
101
+ return false;
102
+ }
58
103
  const isTokenRemovedFromFCM = await deleteRegToken(env);
59
104
  if (!isTokenRemovedFromFCM) {
60
105
  return false;
@@ -72,16 +117,51 @@ export async function deactivatePushNotifications(params) {
72
117
  * - fcmToken: the new or existing Firebase Cloud Messaging token used for the update, if applicable.
73
118
  */
74
119
  export async function updateTriggerPushNotifications(params) {
75
- const { bearerToken, triggers, createRegToken, platform, deleteRegToken, env, } = params;
76
- await deleteRegToken(env);
77
- const newRegToken = await createRegToken(env);
78
- if (!newRegToken) {
79
- throw new Error('Failed to create a new registration token');
120
+ const { bearerToken, regToken, triggers, createRegToken, platform, deleteRegToken, env, } = params;
121
+ const notificationLinks = await getPushNotificationLinks(bearerToken);
122
+ if (!notificationLinks) {
123
+ return { isTriggersLinkedToPushNotifications: false };
80
124
  }
81
- const isTriggersLinkedToPushNotifications = await updateLinksAPI(bearerToken, triggers, [{ token: newRegToken, platform }]);
125
+ // Create new registration token if doesn't exist
126
+ const hasRegToken = Boolean(regToken &&
127
+ notificationLinks.registration_tokens.some((r) => r.token === regToken));
128
+ let newRegToken = null;
129
+ if (!hasRegToken) {
130
+ await deleteRegToken(env);
131
+ newRegToken = await createRegToken(env);
132
+ if (!newRegToken) {
133
+ throw new Error('Failed to create a new registration token');
134
+ }
135
+ notificationLinks.registration_tokens.push({
136
+ token: newRegToken,
137
+ platform,
138
+ });
139
+ }
140
+ const isTriggersLinkedToPushNotifications = await updateLinksAPI(bearerToken, triggers, notificationLinks.registration_tokens);
82
141
  return {
83
142
  isTriggersLinkedToPushNotifications,
84
- fcmToken: newRegToken,
143
+ fcmToken: newRegToken ?? null,
144
+ };
145
+ }
146
+ /**
147
+ * Listens to push notifications and invokes the provided callback function with the received notification data.
148
+ *
149
+ * @param params - listen params
150
+ * @returns A promise that resolves to an unsubscribe function to stop listening to push notifications.
151
+ */
152
+ export async function listenToPushNotifications(params) {
153
+ const { env, listenToPushReceived, listenToPushClicked } = params;
154
+ /*
155
+ Push notifications require 2 listeners that need tracking (when creating and for tearing down):
156
+ 1. handling receiving a push notification (and the content we want to display)
157
+ 2. handling when a user clicks on a push notification
158
+ */
159
+ const unsubscribePushNotifications = await listenToPushNotificationsReceived(env, listenToPushReceived);
160
+ const unsubscribeNotificationClicks = listenToPushNotificationsClicked(listenToPushClicked);
161
+ const unsubscribe = () => {
162
+ unsubscribePushNotifications?.();
163
+ unsubscribeNotificationClicks();
85
164
  };
165
+ return unsubscribe;
86
166
  }
87
167
  //# sourceMappingURL=services.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"services.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,wBAAoB;AAoBzC;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,WAAmB,EACnB,QAAkB,EAClB,SAAqB;IAErB,IAAI;QACF,MAAM,IAAI,GAAgB;YACxB,WAAW,EAAE,QAAQ;YACrB,mBAAmB,EAAE,SAAS;SAC/B,CAAC;QACF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,4BAA4B,EAAE;YACnE,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,aAAa,EAAE,UAAU,WAAW,EAAE;gBACtC,cAAc,EAAE,kBAAkB;aACnC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC,CAAC;QACH,OAAO,QAAQ,CAAC,MAAM,KAAK,GAAG,CAAC;KAChC;IAAC,MAAM;QACN,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAaD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,yBAAyB,CAC7C,MAAuC;IAEvC,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,GAAG,EAAE,cAAc,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC;IAExE,MAAM,QAAQ,GAAG,MAAM,cAAc,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC;IAC7D,IAAI,CAAC,QAAQ,EAAE;QACb,OAAO,IAAI,CAAC;KACb;IAED,MAAM,cAAc,CAAC,WAAW,EAAE,QAAQ,EAAE,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC;IAC7E,OAAO,QAAQ,CAAC;AAClB,CAAC;AAWD;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,2BAA2B,CAC/C,MAAyC;IAEzC,MAAM,EAAE,QAAQ,EAAE,GAAG,EAAE,cAAc,EAAE,GAAG,MAAM,CAAC;IAEjD,yDAAyD;IACzD,IAAI,CAAC,QAAQ,EAAE;QACb,OAAO,IAAI,CAAC;KACb;IAED,MAAM,qBAAqB,GAAG,MAAM,cAAc,CAAC,GAAG,CAAC,CAAC;IACxD,IAAI,CAAC,qBAAqB,EAAE;QAC1B,OAAO,KAAK,CAAC;KACd;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAgBD;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,8BAA8B,CAClD,MAA4C;IAK5C,MAAM,EACJ,WAAW,EACX,QAAQ,EACR,cAAc,EACd,QAAQ,EACR,cAAc,EACd,GAAG,GACJ,GAAG,MAAM,CAAC;IAEX,MAAM,cAAc,CAAC,GAAG,CAAC,CAAC;IAC1B,MAAM,WAAW,GAAG,MAAM,cAAc,CAAC,GAAG,CAAC,CAAC;IAC9C,IAAI,CAAC,WAAW,EAAE;QAChB,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;KAC9D;IAED,MAAM,mCAAmC,GAAG,MAAM,cAAc,CAC9D,WAAW,EACX,QAAQ,EACR,CAAC,EAAE,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,CAAC,CACnC,CAAC;IAEF,OAAO;QACL,mCAAmC;QACnC,QAAQ,EAAE,WAAW;KACtB,CAAC;AACJ,CAAC","sourcesContent":["import * as endpoints from './endpoints';\nimport type { PushNotificationEnv } from '../types';\nimport type {\n CreateRegToken,\n DeleteRegToken,\n} from '../types/push-service-interface';\n\nexport type RegToken = {\n token: string;\n platform: 'extension' | 'mobile' | 'portfolio';\n};\n\n/**\n * Links API Response Shape\n */\nexport type LinksResult = {\n trigger_ids: string[];\n registration_tokens: RegToken[];\n};\n\n/**\n * Updates the push notification links on a remote API.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @param triggers - An array of trigger identifiers.\n * @param regTokens - An array of registration tokens.\n * @returns A promise that resolves with true if the update was successful, false otherwise.\n */\nexport async function updateLinksAPI(\n bearerToken: string,\n triggers: string[],\n regTokens: RegToken[],\n): Promise<boolean> {\n try {\n const body: LinksResult = {\n trigger_ids: triggers,\n registration_tokens: regTokens,\n };\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${bearerToken}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(body),\n });\n return response.status === 200;\n } catch {\n return false;\n }\n}\n\ntype ActivatePushNotificationsParams = {\n // Push Links\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n};\n\n/**\n * Enables push notifications by registering the device and linking triggers.\n *\n * @param params - Activate Push Params\n * @returns A promise that resolves with an object containing the success status and the BearerToken token.\n */\nexport async function activatePushNotifications(\n params: ActivatePushNotificationsParams,\n): Promise<string | null> {\n const { bearerToken, triggers, env, createRegToken, platform } = params;\n\n const regToken = await createRegToken(env).catch(() => null);\n if (!regToken) {\n return null;\n }\n\n await updateLinksAPI(bearerToken, triggers, [{ token: regToken, platform }]);\n return regToken;\n}\n\ntype DeactivatePushNotificationsParams = {\n // Push Links\n regToken: string;\n\n // Push Un-registration\n env: PushNotificationEnv;\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Disables push notifications by removing the registration token\n * We do not need to unlink triggers, and remove old reg tokens (this is cleaned up in the back-end)\n *\n * @param params - Deactivate Push Params\n * @returns A promise that resolves with true if push notifications were successfully disabled, false otherwise.\n */\nexport async function deactivatePushNotifications(\n params: DeactivatePushNotificationsParams,\n): Promise<boolean> {\n const { regToken, env, deleteRegToken } = params;\n\n // if we don't have a reg token, then we can early return\n if (!regToken) {\n return true;\n }\n\n const isTokenRemovedFromFCM = await deleteRegToken(env);\n if (!isTokenRemovedFromFCM) {\n return false;\n }\n\n return true;\n}\n\ntype UpdateTriggerPushNotificationsParams = {\n // Push Links\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n\n // Push Un-registration\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Updates the triggers linked to push notifications for a given registration token.\n * If the provided registration token does not exist or is not in the current set of registration tokens,\n * a new registration token is created and used for the update.\n *\n * @param params - Update Push Params\n * @returns A promise that resolves with an object containing:\n * - isTriggersLinkedToPushNotifications: boolean indicating if the triggers were successfully updated.\n * - fcmToken: the new or existing Firebase Cloud Messaging token used for the update, if applicable.\n */\nexport async function updateTriggerPushNotifications(\n params: UpdateTriggerPushNotificationsParams,\n): Promise<{\n isTriggersLinkedToPushNotifications: boolean;\n fcmToken?: string | null;\n}> {\n const {\n bearerToken,\n triggers,\n createRegToken,\n platform,\n deleteRegToken,\n env,\n } = params;\n\n await deleteRegToken(env);\n const newRegToken = await createRegToken(env);\n if (!newRegToken) {\n throw new Error('Failed to create a new registration token');\n }\n\n const isTriggersLinkedToPushNotifications = await updateLinksAPI(\n bearerToken,\n triggers,\n [{ token: newRegToken, platform }],\n );\n\n return {\n isTriggersLinkedToPushNotifications,\n fcmToken: newRegToken,\n };\n}\n"]}
1
+ {"version":3,"file":"services.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/services/services.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,KAAK,SAAS,wBAAoB;AAEzC,OAAO,EACL,gCAAgC,EAChC,iCAAiC,EAClC,4BAAwB;AAkBzB;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAC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,IAAI,IAAI;KAC9B,CAAC;AACJ,CAAC;AAaD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,yBAAyB,CAC7C,MAAuC;IAEvC,MAAM,EAAE,GAAG,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,GAAG,MAAM,CAAC;IAElE;;;;MAIE;IACF,MAAM,4BAA4B,GAAG,MAAM,iCAAiC,CAC1E,GAAG,EACH,oBAAoB,CACrB,CAAC;IACF,MAAM,6BAA6B,GACjC,gCAAgC,CAAC,mBAAmB,CAAC,CAAC;IAExD,MAAM,WAAW,GAAG,GAAG,EAAE;QACvB,4BAA4B,EAAE,EAAE,CAAC;QACjC,6BAA6B,EAAE,CAAC;IAClC,CAAC,CAAC;IAEF,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import log from 'loglevel';\n\nimport * as endpoints from './endpoints';\nimport type { CreateRegToken, DeleteRegToken } from './push';\nimport {\n listenToPushNotificationsClicked,\n listenToPushNotificationsReceived,\n} from './push/push-web';\nimport type { Types } from '../../NotificationServicesController';\nimport type { PushNotificationEnv } from '../types';\n\nexport type RegToken = {\n token: string;\n platform: 'extension' | 'mobile' | 'portfolio';\n};\n\n/**\n * Links API Response Shape\n */\nexport type LinksResult = {\n trigger_ids: string[];\n\n registration_tokens: RegToken[];\n};\n\n/**\n * Fetches push notification links from a remote endpoint using a BearerToken for authorization.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @returns A promise that resolves with the links result or null if an error occurs.\n */\nexport async function getPushNotificationLinks(\n bearerToken: string,\n): Promise<LinksResult | null> {\n try {\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n headers: { Authorization: `Bearer ${bearerToken}` },\n });\n if (!response.ok) {\n log.error('Failed to fetch the push notification links');\n throw new Error('Failed to fetch the push notification links');\n }\n return response.json() as Promise<LinksResult>;\n } catch (error) {\n log.error('Failed to fetch the push notification links', error);\n return null;\n }\n}\n\n/**\n * Updates the push notification links on a remote API.\n *\n * @param bearerToken - The JSON Web Token used for authorization.\n * @param triggers - An array of trigger identifiers.\n * @param regTokens - An array of registration tokens.\n * @returns A promise that resolves with true if the update was successful, false otherwise.\n */\nexport async function updateLinksAPI(\n bearerToken: string,\n triggers: string[],\n regTokens: RegToken[],\n): Promise<boolean> {\n try {\n const body: LinksResult = {\n trigger_ids: triggers,\n registration_tokens: regTokens,\n };\n const response = await fetch(endpoints.REGISTRATION_TOKENS_ENDPOINT, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${bearerToken}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(body),\n });\n return response.status === 200;\n } catch {\n return false;\n }\n}\n\ntype ActivatePushNotificationsParams = {\n // Push Links\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n fcmToken?: string;\n};\n\n/**\n * Enables push notifications by registering the device and linking triggers.\n *\n * @param params - Activate Push Params\n * @returns A promise that resolves with an object containing the success status and the BearerToken token.\n */\nexport async function activatePushNotifications(\n params: ActivatePushNotificationsParams,\n): Promise<string | null> {\n const { bearerToken, triggers, env, createRegToken, platform, fcmToken } =\n params;\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n\n if (!notificationLinks) {\n return null;\n }\n\n const regToken = fcmToken ?? (await createRegToken(env).catch(() => null));\n if (!regToken) {\n return null;\n }\n\n const newRegTokens = new Set(notificationLinks.registration_tokens);\n newRegTokens.add({ token: regToken, platform });\n\n await updateLinksAPI(bearerToken, triggers, Array.from(newRegTokens));\n return regToken;\n}\n\ntype DeactivatePushNotificationsParams = {\n // Push Links\n regToken: string;\n bearerToken: string;\n triggers: string[];\n\n // Push Un-registration\n env: PushNotificationEnv;\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Disables push notifications by removing the registration token and unlinking triggers.\n *\n * @param params - Deactivate Push Params\n * @returns A promise that resolves with true if notifications were successfully disabled, false otherwise.\n */\nexport async function deactivatePushNotifications(\n params: DeactivatePushNotificationsParams,\n): Promise<boolean> {\n const { regToken, bearerToken, triggers, env, deleteRegToken } = params;\n\n // if we don't have a reg token, then we can early return\n if (!regToken) {\n return true;\n }\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n if (!notificationLinks) {\n return false;\n }\n\n const filteredRegTokens = notificationLinks.registration_tokens.filter(\n (r) => r.token !== regToken,\n );\n\n const isTokenRemovedFromAPI = await updateLinksAPI(\n bearerToken,\n triggers,\n filteredRegTokens,\n );\n if (!isTokenRemovedFromAPI) {\n return false;\n }\n\n const isTokenRemovedFromFCM = await deleteRegToken(env);\n if (!isTokenRemovedFromFCM) {\n return false;\n }\n\n return true;\n}\n\ntype UpdateTriggerPushNotificationsParams = {\n // Push Links\n regToken: string;\n bearerToken: string;\n triggers: string[];\n\n // Push Registration\n env: PushNotificationEnv;\n createRegToken: CreateRegToken;\n platform: 'extension' | 'mobile' | 'portfolio';\n\n // Push Un-registration\n deleteRegToken: DeleteRegToken;\n};\n\n/**\n * Updates the triggers linked to push notifications for a given registration token.\n * If the provided registration token does not exist or is not in the current set of registration tokens,\n * a new registration token is created and used for the update.\n *\n * @param params - Update Push Params\n * @returns A promise that resolves with an object containing:\n * - isTriggersLinkedToPushNotifications: boolean indicating if the triggers were successfully updated.\n * - fcmToken: the new or existing Firebase Cloud Messaging token used for the update, if applicable.\n */\nexport async function updateTriggerPushNotifications(\n params: UpdateTriggerPushNotificationsParams,\n): Promise<{\n isTriggersLinkedToPushNotifications: boolean;\n fcmToken?: string | null;\n}> {\n const {\n bearerToken,\n regToken,\n triggers,\n createRegToken,\n platform,\n deleteRegToken,\n env,\n } = params;\n\n const notificationLinks = await getPushNotificationLinks(bearerToken);\n if (!notificationLinks) {\n return { isTriggersLinkedToPushNotifications: false };\n }\n // Create new registration token if doesn't exist\n const hasRegToken = Boolean(\n regToken &&\n notificationLinks.registration_tokens.some((r) => r.token === regToken),\n );\n\n let newRegToken: string | null = null;\n if (!hasRegToken) {\n await deleteRegToken(env);\n newRegToken = await createRegToken(env);\n if (!newRegToken) {\n throw new Error('Failed to create a new registration token');\n }\n notificationLinks.registration_tokens.push({\n token: newRegToken,\n platform,\n });\n }\n\n const isTriggersLinkedToPushNotifications = await updateLinksAPI(\n bearerToken,\n triggers,\n notificationLinks.registration_tokens,\n );\n\n return {\n isTriggersLinkedToPushNotifications,\n fcmToken: newRegToken ?? null,\n };\n}\n\ntype ListenToPushNotificationsParams = {\n env: PushNotificationEnv;\n listenToPushReceived: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n listenToPushClicked: (\n event: NotificationEvent,\n notification?: Types.INotification,\n ) => void;\n};\n\n/**\n * Listens to push notifications and invokes the provided callback function with the received notification data.\n *\n * @param params - listen params\n * @returns A promise that resolves to an unsubscribe function to stop listening to push notifications.\n */\nexport async function listenToPushNotifications(\n params: ListenToPushNotificationsParams,\n): Promise<() => void> {\n const { env, listenToPushReceived, listenToPushClicked } = params;\n\n /*\n Push notifications require 2 listeners that need tracking (when creating and for tearing down):\n 1. handling receiving a push notification (and the content we want to display)\n 2. handling when a user clicks on a push notification\n */\n const unsubscribePushNotifications = await listenToPushNotificationsReceived(\n env,\n listenToPushReceived,\n );\n const unsubscribeNotificationClicks =\n listenToPushNotificationsClicked(listenToPushClicked);\n\n const unsubscribe = () => {\n unsubscribePushNotifications?.();\n unsubscribeNotificationClicks();\n };\n\n return unsubscribe;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"","sourcesContent":["export type * from './firebase';\nexport type * from './push-service-interface';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"","sourcesContent":["export type * from './firebase';\n"]}
@@ -1,3 +1,2 @@
1
1
  export type * from "./firebase.cjs";
2
- export type * from "./push-service-interface.cjs";
3
2
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"AAAA,oCAAgC;AAChC,kDAA8C"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"AAAA,oCAAgC"}
@@ -1,3 +1,2 @@
1
1
  export type * from "./firebase.mjs";
2
- export type * from "./push-service-interface.mjs";
3
2
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"AAAA,oCAAgC;AAChC,kDAA8C"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"AAAA,oCAAgC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"","sourcesContent":["export type * from './firebase';\nexport type * from './push-service-interface';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/types/index.ts"],"names":[],"mappings":"","sourcesContent":["export type * from './firebase';\n"]}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createOnChainPushNotificationMessage = exports.createOnChainPushNotificationMessages = void 0;
3
+ exports.createOnChainPushNotificationMessage = exports.isOnChainNotification = exports.createOnChainPushNotificationMessages = void 0;
4
4
  const get_notification_data_1 = require("./get-notification-data.cjs");
5
5
  const NotificationServicesController_1 = require("../../NotificationServicesController/index.cjs");
6
6
  /**
@@ -137,6 +137,24 @@ exports.createOnChainPushNotificationMessages = createOnChainPushNotificationMes
137
137
  function getChainSymbol(chainId) {
138
138
  return NotificationServicesController_1.Constants.CHAIN_SYMBOLS[chainId] ?? null;
139
139
  }
140
+ /**
141
+ * Checks if the given value is an OnChainRawNotification object.
142
+ *
143
+ * @param n - The value to check.
144
+ * @returns True if the value is an OnChainRawNotification object, false otherwise.
145
+ */
146
+ function isOnChainNotification(n) {
147
+ const assumed = n;
148
+ // We don't have a validation/parsing library to check all possible types of an on chain notification
149
+ // It is safe enough just to check "some" fields, and catch any errors down the line if the shape is bad.
150
+ const isValidEnoughToBeOnChainNotification = [
151
+ assumed?.id,
152
+ assumed?.data,
153
+ assumed?.trigger_id,
154
+ ].every((field) => field !== undefined);
155
+ return isValidEnoughToBeOnChainNotification;
156
+ }
157
+ exports.isOnChainNotification = isOnChainNotification;
140
158
  /**
141
159
  * Creates a push notification message based on the given on-chain raw notification.
142
160
  *