@metamask-previews/notification-services-controller 0.21.0-preview-dbdf1da → 1.0.0-preview-95a6811

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 (254) hide show
  1. package/CHANGELOG.md +22 -3
  2. package/README.md +35 -1
  3. package/dist/NotificationServicesController/NotificationServicesController.cjs +66 -77
  4. package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
  5. package/dist/NotificationServicesController/NotificationServicesController.d.cts +10 -34
  6. package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
  7. package/dist/NotificationServicesController/NotificationServicesController.d.mts +10 -34
  8. package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
  9. package/dist/NotificationServicesController/NotificationServicesController.mjs +66 -77
  10. package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
  11. package/dist/NotificationServicesController/index.cjs +2 -1
  12. package/dist/NotificationServicesController/index.cjs.map +1 -1
  13. package/dist/NotificationServicesController/index.d.cts +2 -1
  14. package/dist/NotificationServicesController/index.d.cts.map +1 -1
  15. package/dist/NotificationServicesController/index.d.mts +2 -1
  16. package/dist/NotificationServicesController/index.d.mts.map +1 -1
  17. package/dist/NotificationServicesController/index.mjs +2 -1
  18. package/dist/NotificationServicesController/index.mjs.map +1 -1
  19. package/dist/NotificationServicesController/mocks/index.cjs.map +1 -0
  20. package/dist/NotificationServicesController/mocks/index.d.cts.map +1 -0
  21. package/dist/NotificationServicesController/mocks/index.d.mts.map +1 -0
  22. package/dist/NotificationServicesController/mocks/index.mjs.map +1 -0
  23. package/dist/NotificationServicesController/mocks/mock-feature-announcements.cjs.map +1 -0
  24. package/dist/NotificationServicesController/mocks/mock-feature-announcements.d.cts.map +1 -0
  25. package/dist/NotificationServicesController/mocks/mock-feature-announcements.d.mts.map +1 -0
  26. package/dist/NotificationServicesController/mocks/mock-feature-announcements.mjs.map +1 -0
  27. package/dist/NotificationServicesController/mocks/mock-notification-trigger.cjs.map +1 -0
  28. package/dist/NotificationServicesController/mocks/mock-notification-trigger.d.cts.map +1 -0
  29. package/dist/NotificationServicesController/mocks/mock-notification-trigger.d.mts.map +1 -0
  30. package/dist/NotificationServicesController/mocks/mock-notification-trigger.mjs.map +1 -0
  31. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.cjs.map +1 -0
  32. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.d.cts.map +1 -0
  33. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.d.mts.map +1 -0
  34. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.mjs.map +1 -0
  35. package/dist/NotificationServicesController/mocks/mock-raw-notifications.cjs.map +1 -0
  36. package/dist/NotificationServicesController/mocks/mock-raw-notifications.d.cts.map +1 -0
  37. package/dist/NotificationServicesController/mocks/mock-raw-notifications.d.mts.map +1 -0
  38. package/dist/NotificationServicesController/mocks/mock-raw-notifications.mjs.map +1 -0
  39. package/dist/NotificationServicesController/mocks/mock-snap-notification.cjs.map +1 -0
  40. package/dist/NotificationServicesController/mocks/mock-snap-notification.d.cts.map +1 -0
  41. package/dist/NotificationServicesController/mocks/mock-snap-notification.d.mts.map +1 -0
  42. package/dist/NotificationServicesController/mocks/mock-snap-notification.mjs.map +1 -0
  43. package/dist/NotificationServicesController/mocks/mockResponses.cjs.map +1 -0
  44. package/dist/NotificationServicesController/mocks/mockResponses.d.cts.map +1 -0
  45. package/dist/NotificationServicesController/mocks/mockResponses.d.mts.map +1 -0
  46. package/dist/NotificationServicesController/mocks/mockResponses.mjs.map +1 -0
  47. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs +95 -46
  48. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
  49. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +17 -17
  50. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
  51. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +17 -17
  52. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
  53. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +96 -47
  54. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
  55. package/dist/NotificationServicesPushController/index.cjs +1 -1
  56. package/dist/NotificationServicesPushController/index.cjs.map +1 -1
  57. package/dist/NotificationServicesPushController/index.d.cts +1 -1
  58. package/dist/NotificationServicesPushController/index.d.cts.map +1 -1
  59. package/dist/NotificationServicesPushController/index.d.mts +1 -1
  60. package/dist/NotificationServicesPushController/index.d.mts.map +1 -1
  61. package/dist/NotificationServicesPushController/index.mjs +1 -1
  62. package/dist/NotificationServicesPushController/index.mjs.map +1 -1
  63. package/dist/NotificationServicesPushController/mocks/index.cjs.map +1 -0
  64. package/dist/NotificationServicesPushController/mocks/index.d.cts.map +1 -0
  65. package/dist/NotificationServicesPushController/mocks/index.d.mts.map +1 -0
  66. package/dist/NotificationServicesPushController/mocks/index.mjs.map +1 -0
  67. package/dist/NotificationServicesPushController/{__fixtures__/mockResponse.mjs.map → mocks/mockResponse.cjs.map} +1 -1
  68. package/dist/NotificationServicesPushController/mocks/mockResponse.d.cts.map +1 -0
  69. package/dist/NotificationServicesPushController/mocks/mockResponse.d.mts.map +1 -0
  70. package/dist/NotificationServicesPushController/mocks/mockResponse.mjs.map +1 -0
  71. package/dist/NotificationServicesPushController/services/services.cjs +9 -28
  72. package/dist/NotificationServicesPushController/services/services.cjs.map +1 -1
  73. package/dist/NotificationServicesPushController/services/services.d.cts +2 -17
  74. package/dist/NotificationServicesPushController/services/services.d.cts.map +1 -1
  75. package/dist/NotificationServicesPushController/services/services.d.mts +2 -17
  76. package/dist/NotificationServicesPushController/services/services.d.mts.map +1 -1
  77. package/dist/NotificationServicesPushController/services/services.mjs +8 -26
  78. package/dist/NotificationServicesPushController/services/services.mjs.map +1 -1
  79. package/dist/NotificationServicesPushController/types/index.cjs.map +1 -1
  80. package/dist/NotificationServicesPushController/types/index.d.cts +1 -0
  81. package/dist/NotificationServicesPushController/types/index.d.cts.map +1 -1
  82. package/dist/NotificationServicesPushController/types/index.d.mts +1 -0
  83. package/dist/NotificationServicesPushController/types/index.d.mts.map +1 -1
  84. package/dist/NotificationServicesPushController/types/index.mjs.map +1 -1
  85. package/dist/NotificationServicesPushController/{services/push/index.cjs → types/push-service-interface.cjs} +1 -1
  86. package/dist/NotificationServicesPushController/types/push-service-interface.cjs.map +1 -0
  87. package/dist/NotificationServicesPushController/types/push-service-interface.d.cts +30 -0
  88. package/dist/NotificationServicesPushController/types/push-service-interface.d.cts.map +1 -0
  89. package/dist/NotificationServicesPushController/types/push-service-interface.d.mts +30 -0
  90. package/dist/NotificationServicesPushController/types/push-service-interface.d.mts.map +1 -0
  91. package/dist/NotificationServicesPushController/types/push-service-interface.mjs +2 -0
  92. package/dist/NotificationServicesPushController/types/push-service-interface.mjs.map +1 -0
  93. package/dist/NotificationServicesPushController/utils/get-notification-message.cjs +1 -19
  94. package/dist/NotificationServicesPushController/utils/get-notification-message.cjs.map +1 -1
  95. package/dist/NotificationServicesPushController/utils/get-notification-message.d.cts +0 -7
  96. package/dist/NotificationServicesPushController/utils/get-notification-message.d.cts.map +1 -1
  97. package/dist/NotificationServicesPushController/utils/get-notification-message.d.mts +0 -7
  98. package/dist/NotificationServicesPushController/utils/get-notification-message.d.mts.map +1 -1
  99. package/dist/NotificationServicesPushController/utils/get-notification-message.mjs +0 -17
  100. package/dist/NotificationServicesPushController/utils/get-notification-message.mjs.map +1 -1
  101. package/dist/NotificationServicesPushController/web/index.cjs +8 -0
  102. package/dist/NotificationServicesPushController/web/index.cjs.map +1 -0
  103. package/dist/NotificationServicesPushController/web/index.d.cts +2 -0
  104. package/dist/NotificationServicesPushController/web/index.d.cts.map +1 -0
  105. package/dist/NotificationServicesPushController/web/index.d.mts +2 -0
  106. package/dist/NotificationServicesPushController/web/index.d.mts.map +1 -0
  107. package/dist/NotificationServicesPushController/web/index.mjs +2 -0
  108. package/dist/NotificationServicesPushController/web/index.mjs.map +1 -0
  109. package/dist/NotificationServicesPushController/web/push-utils.cjs +170 -0
  110. package/dist/NotificationServicesPushController/web/push-utils.cjs.map +1 -0
  111. package/dist/NotificationServicesPushController/web/push-utils.d.cts +37 -0
  112. package/dist/NotificationServicesPushController/web/push-utils.d.cts.map +1 -0
  113. package/dist/NotificationServicesPushController/web/push-utils.d.mts +37 -0
  114. package/dist/NotificationServicesPushController/web/push-utils.d.mts.map +1 -0
  115. package/dist/NotificationServicesPushController/web/push-utils.mjs +168 -0
  116. package/dist/NotificationServicesPushController/web/push-utils.mjs.map +1 -0
  117. package/dist/shared/index.cjs +19 -0
  118. package/dist/shared/index.cjs.map +1 -0
  119. package/dist/shared/index.d.cts +3 -0
  120. package/dist/shared/index.d.cts.map +1 -0
  121. package/dist/shared/index.d.mts +3 -0
  122. package/dist/shared/index.d.mts.map +1 -0
  123. package/dist/shared/index.mjs +3 -0
  124. package/dist/shared/index.mjs.map +1 -0
  125. package/dist/shared/is-onchain-notification.cjs +22 -0
  126. package/dist/shared/is-onchain-notification.cjs.map +1 -0
  127. package/dist/shared/is-onchain-notification.d.cts +9 -0
  128. package/dist/shared/is-onchain-notification.d.cts.map +1 -0
  129. package/dist/shared/is-onchain-notification.d.mts +9 -0
  130. package/dist/shared/is-onchain-notification.d.mts.map +1 -0
  131. package/dist/shared/is-onchain-notification.mjs +18 -0
  132. package/dist/shared/is-onchain-notification.mjs.map +1 -0
  133. package/notification-services/mocks/package.json +2 -2
  134. package/package.json +21 -11
  135. package/push-services/mocks/package.json +2 -2
  136. package/push-services/web/package.json +9 -0
  137. package/dist/NotificationServicesController/__fixtures__/index.cjs.map +0 -1
  138. package/dist/NotificationServicesController/__fixtures__/index.d.cts.map +0 -1
  139. package/dist/NotificationServicesController/__fixtures__/index.d.mts.map +0 -1
  140. package/dist/NotificationServicesController/__fixtures__/index.mjs.map +0 -1
  141. package/dist/NotificationServicesController/__fixtures__/mock-feature-announcements.cjs.map +0 -1
  142. package/dist/NotificationServicesController/__fixtures__/mock-feature-announcements.d.cts.map +0 -1
  143. package/dist/NotificationServicesController/__fixtures__/mock-feature-announcements.d.mts.map +0 -1
  144. package/dist/NotificationServicesController/__fixtures__/mock-feature-announcements.mjs.map +0 -1
  145. package/dist/NotificationServicesController/__fixtures__/mock-notification-trigger.cjs.map +0 -1
  146. package/dist/NotificationServicesController/__fixtures__/mock-notification-trigger.d.cts.map +0 -1
  147. package/dist/NotificationServicesController/__fixtures__/mock-notification-trigger.d.mts.map +0 -1
  148. package/dist/NotificationServicesController/__fixtures__/mock-notification-trigger.mjs.map +0 -1
  149. package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.cjs.map +0 -1
  150. package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.d.cts.map +0 -1
  151. package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.d.mts.map +0 -1
  152. package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.mjs.map +0 -1
  153. package/dist/NotificationServicesController/__fixtures__/mock-raw-notifications.cjs.map +0 -1
  154. package/dist/NotificationServicesController/__fixtures__/mock-raw-notifications.d.cts.map +0 -1
  155. package/dist/NotificationServicesController/__fixtures__/mock-raw-notifications.d.mts.map +0 -1
  156. package/dist/NotificationServicesController/__fixtures__/mock-raw-notifications.mjs.map +0 -1
  157. package/dist/NotificationServicesController/__fixtures__/mock-snap-notification.cjs.map +0 -1
  158. package/dist/NotificationServicesController/__fixtures__/mock-snap-notification.d.cts.map +0 -1
  159. package/dist/NotificationServicesController/__fixtures__/mock-snap-notification.d.mts.map +0 -1
  160. package/dist/NotificationServicesController/__fixtures__/mock-snap-notification.mjs.map +0 -1
  161. package/dist/NotificationServicesController/__fixtures__/mockAddresses.cjs +0 -6
  162. package/dist/NotificationServicesController/__fixtures__/mockAddresses.cjs.map +0 -1
  163. package/dist/NotificationServicesController/__fixtures__/mockAddresses.d.cts +0 -3
  164. package/dist/NotificationServicesController/__fixtures__/mockAddresses.d.cts.map +0 -1
  165. package/dist/NotificationServicesController/__fixtures__/mockAddresses.d.mts +0 -3
  166. package/dist/NotificationServicesController/__fixtures__/mockAddresses.d.mts.map +0 -1
  167. package/dist/NotificationServicesController/__fixtures__/mockAddresses.mjs +0 -3
  168. package/dist/NotificationServicesController/__fixtures__/mockAddresses.mjs.map +0 -1
  169. package/dist/NotificationServicesController/__fixtures__/mockResponses.cjs.map +0 -1
  170. package/dist/NotificationServicesController/__fixtures__/mockResponses.d.cts.map +0 -1
  171. package/dist/NotificationServicesController/__fixtures__/mockResponses.d.mts.map +0 -1
  172. package/dist/NotificationServicesController/__fixtures__/mockResponses.mjs.map +0 -1
  173. package/dist/NotificationServicesController/__fixtures__/mockServices.cjs +0 -56
  174. package/dist/NotificationServicesController/__fixtures__/mockServices.cjs.map +0 -1
  175. package/dist/NotificationServicesController/__fixtures__/mockServices.d.cts +0 -12
  176. package/dist/NotificationServicesController/__fixtures__/mockServices.d.cts.map +0 -1
  177. package/dist/NotificationServicesController/__fixtures__/mockServices.d.mts +0 -12
  178. package/dist/NotificationServicesController/__fixtures__/mockServices.d.mts.map +0 -1
  179. package/dist/NotificationServicesController/__fixtures__/mockServices.mjs +0 -52
  180. package/dist/NotificationServicesController/__fixtures__/mockServices.mjs.map +0 -1
  181. package/dist/NotificationServicesController/__fixtures__/test-utils.cjs +0 -31
  182. package/dist/NotificationServicesController/__fixtures__/test-utils.cjs.map +0 -1
  183. package/dist/NotificationServicesController/__fixtures__/test-utils.d.cts +0 -14
  184. package/dist/NotificationServicesController/__fixtures__/test-utils.d.cts.map +0 -1
  185. package/dist/NotificationServicesController/__fixtures__/test-utils.d.mts +0 -14
  186. package/dist/NotificationServicesController/__fixtures__/test-utils.d.mts.map +0 -1
  187. package/dist/NotificationServicesController/__fixtures__/test-utils.mjs +0 -27
  188. package/dist/NotificationServicesController/__fixtures__/test-utils.mjs.map +0 -1
  189. package/dist/NotificationServicesPushController/__fixtures__/index.cjs.map +0 -1
  190. package/dist/NotificationServicesPushController/__fixtures__/index.d.cts.map +0 -1
  191. package/dist/NotificationServicesPushController/__fixtures__/index.d.mts.map +0 -1
  192. package/dist/NotificationServicesPushController/__fixtures__/index.mjs.map +0 -1
  193. package/dist/NotificationServicesPushController/__fixtures__/mockResponse.cjs.map +0 -1
  194. package/dist/NotificationServicesPushController/__fixtures__/mockResponse.d.cts.map +0 -1
  195. package/dist/NotificationServicesPushController/__fixtures__/mockResponse.d.mts.map +0 -1
  196. package/dist/NotificationServicesPushController/__fixtures__/mockServices.cjs +0 -19
  197. package/dist/NotificationServicesPushController/__fixtures__/mockServices.cjs.map +0 -1
  198. package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.cts +0 -8
  199. package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.cts.map +0 -1
  200. package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.mts +0 -8
  201. package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.mts.map +0 -1
  202. package/dist/NotificationServicesPushController/__fixtures__/mockServices.mjs +0 -19
  203. package/dist/NotificationServicesPushController/__fixtures__/mockServices.mjs.map +0 -1
  204. package/dist/NotificationServicesPushController/constants.cjs +0 -14
  205. package/dist/NotificationServicesPushController/constants.cjs.map +0 -1
  206. package/dist/NotificationServicesPushController/constants.d.cts +0 -11
  207. package/dist/NotificationServicesPushController/constants.d.cts.map +0 -1
  208. package/dist/NotificationServicesPushController/constants.d.mts +0 -11
  209. package/dist/NotificationServicesPushController/constants.d.mts.map +0 -1
  210. package/dist/NotificationServicesPushController/constants.mjs +0 -11
  211. package/dist/NotificationServicesPushController/constants.mjs.map +0 -1
  212. package/dist/NotificationServicesPushController/services/push/index.cjs.map +0 -1
  213. package/dist/NotificationServicesPushController/services/push/index.d.cts +0 -6
  214. package/dist/NotificationServicesPushController/services/push/index.d.cts.map +0 -1
  215. package/dist/NotificationServicesPushController/services/push/index.d.mts +0 -6
  216. package/dist/NotificationServicesPushController/services/push/index.d.mts.map +0 -1
  217. package/dist/NotificationServicesPushController/services/push/index.mjs +0 -2
  218. package/dist/NotificationServicesPushController/services/push/index.mjs.map +0 -1
  219. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/index.cjs +0 -0
  220. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/index.d.cts +0 -0
  221. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/index.d.mts +0 -0
  222. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/index.mjs +0 -0
  223. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-feature-announcements.cjs +0 -0
  224. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-feature-announcements.d.cts +0 -0
  225. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-feature-announcements.d.mts +0 -0
  226. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-feature-announcements.mjs +0 -0
  227. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-trigger.cjs +0 -0
  228. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-trigger.d.cts +0 -0
  229. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-trigger.d.mts +0 -0
  230. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-trigger.mjs +0 -0
  231. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-user-storage.cjs +0 -0
  232. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-user-storage.d.cts +0 -0
  233. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-user-storage.d.mts +0 -0
  234. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-user-storage.mjs +0 -0
  235. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-raw-notifications.cjs +0 -0
  236. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-raw-notifications.d.cts +0 -0
  237. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-raw-notifications.d.mts +0 -0
  238. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-raw-notifications.mjs +0 -0
  239. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-snap-notification.cjs +0 -0
  240. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-snap-notification.d.cts +0 -0
  241. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-snap-notification.d.mts +0 -0
  242. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-snap-notification.mjs +0 -0
  243. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mockResponses.cjs +0 -0
  244. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mockResponses.d.cts +0 -0
  245. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mockResponses.d.mts +0 -0
  246. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mockResponses.mjs +0 -0
  247. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/index.cjs +0 -0
  248. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/index.d.cts +0 -0
  249. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/index.d.mts +0 -0
  250. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/index.mjs +0 -0
  251. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/mockResponse.cjs +0 -0
  252. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/mockResponse.d.cts +0 -0
  253. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/mockResponse.d.mts +0 -0
  254. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/mockResponse.mjs +0 -0
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationServicesPushController.cjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAMA,+DAA2D;AAE3D,wDAA2B;AAE3B,2DAA0E;AAC1E,sDAK6B;AAI7B,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAuE/C,QAAA,YAAY,GAA4C;IACnE,QAAQ,E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C,IAAI,QAAQ,EAAE;gBACZ,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;oBACpB,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC;gBAC5B,CAAC,CAAC,CAAC;aACJ;SACF;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,YAAY,GAAG,qDACnB,KACF,EAAE,CAAC;YACH,kBAAG,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;SAC/B;IACH,CAAC;CACF;;IAhMG,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,4DAA4D,EAC5D,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,IAAI,CAAC,CACxC,CAAC;IACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,6DAA6D,EAC7D,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,IAAI,CAAC,CACzC,CAAC;IACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,mEAAmE,EACnE,IAAI,CAAC,8BAA8B,CAAC,IAAI,CAAC,IAAI,CAAC,CAC/C,CAAC;IACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,iEAAiE,EACjE,IAAI,CAAC,4BAA4B,CAAC,IAAI,CAAC,IAAI,CAAC,CAC7C,CAAC;AACJ,CAAC,gEAED,KAAK;IACH,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CACjD,yCAAyC,CAC1C,CAAC;IACF,IAAI,CAAC,WAAW,EAAE;QAChB,kBAAG,CAAC,KAAK,CACP,oEAAoE,CACrE,CAAC;QACF,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;IAED,OAAO,WAAW,CAAC;AACrB,CAAC;kBAlEkB,kCAAkC","sourcesContent":["import type {\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport { createRegToken, deleteRegToken } from './services/push/push-web';\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n listenToPushNotifications,\n updateTriggerPushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\nimport type { Types } from '../NotificationServicesController';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n fcmToken: string;\n};\n\nexport type NotificationServicesPushControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\nexport type NotificationServicesPushControllerSubscribeToNotificationsAction = {\n type: `${typeof controllerName}:subscribeToPushNotifications`;\n handler: NotificationServicesPushController['subscribeToPushNotifications'];\n};\n\nexport type Actions =\n | NotificationServicesPushControllerGetStateAction\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | NotificationServicesPushControllerSubscribeToNotificationsAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClickedEvent = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type Events =\n | NotificationServicesPushControllerStateChangeEvent\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClickedEvent;\n\nexport type AllowedEvents = never;\n\nexport type NotificationServicesPushControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\nexport const defaultState: NotificationServicesPushControllerState = {\n fcmToken: '',\n};\nconst metadata: StateMetadata<NotificationServicesPushControllerState> = {\n fcmToken: {\n persist: true,\n anonymous: true,\n },\n};\n\ntype ControllerConfig = {\n /**\n * Config to turn on/off push notifications.\n * This is currently linked to MV3 builds on extension.\n */\n isPushEnabled: boolean;\n\n /**\n * Must handle when a push notification is received.\n * You must call `registration.showNotification` or equivalent to show the notification on web/mobile\n */\n onPushNotificationReceived: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n\n /**\n * Must handle when a push notification is clicked.\n * You must call `event.notification.close();` or equivalent for closing and opening notification in a new window.\n */\n onPushNotificationClicked: (\n event: NotificationEvent,\n notification?: Types.INotification,\n ) => void;\n\n /**\n * determine the config used for push notification services\n */\n platform: 'extension' | 'mobile';\n};\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 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;\n this.#config = config;\n\n this.#registerMessageHandlers();\n }\n\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:enablePushNotifications',\n this.enablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:disablePushNotifications',\n this.disablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:updateTriggerPushNotifications',\n this.updateTriggerPushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:subscribeToPushNotifications',\n this.subscribeToPushNotifications.bind(this),\n );\n }\n\n async #getAndAssertBearerToken() {\n const bearerToken = await this.messagingSystem.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n log.error(\n 'Failed to enable push notifications: BearerToken token is missing.',\n );\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n async subscribeToPushNotifications() {\n if (this.#pushListenerUnsubscribe) {\n this.#pushListenerUnsubscribe();\n this.#pushListenerUnsubscribe = undefined;\n }\n\n try {\n this.#pushListenerUnsubscribe = await listenToPushNotifications({\n env: this.#env,\n listenToPushReceived: async (n) => {\n this.messagingSystem.publish(\n 'NotificationServicesPushController:onNewNotifications',\n n,\n );\n await this.#config.onPushNotificationReceived(n);\n },\n listenToPushClicked: (e, n) => {\n if (n) {\n this.messagingSystem.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n n,\n );\n }\n\n this.#config.onPushNotificationClicked(e, n);\n },\n });\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 UUIDs - An array of UUIDs to enable push notifications for.\n * @param fcmToken - The optional FCM token to use for push notifications.\n */\n async enablePushNotifications(UUIDs: string[], fcmToken?: string) {\n if (!this.#config.isPushEnabled) {\n return;\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 regToken = await activatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n fcmToken,\n createRegToken,\n platform: this.#config.platform,\n }).catch(() => null);\n\n if (regToken) {\n this.update((state) => {\n state.fcmToken = regToken;\n });\n }\n }\n } catch {\n // Do nothing, we are silently failing\n }\n\n // New token created, (re)subscribe to push notifications\n await this.subscribeToPushNotifications();\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 async disablePushNotifications() {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n let isPushNotificationsDisabled: boolean;\n\n try {\n // Send a request to the server to unregister the token/device\n isPushNotificationsDisabled = await deactivatePushNotifications({\n env: this.#env,\n 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 }\n\n // Remove the FCM token from the state\n if (!isPushNotificationsDisabled) {\n return;\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n if (isPushNotificationsDisabled) {\n this.update((state) => {\n state.fcmToken = '';\n });\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 UUIDs that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param UUIDs - An array of UUIDs that should trigger push notifications.\n */\n async updateTriggerPushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n const bearerToken = await this.#getAndAssertBearerToken();\n\n try {\n const { fcmToken } = await updateTriggerPushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken,\n deleteRegToken,\n platform: this.#config.platform,\n });\n\n // update the state with the new FCM token\n if (fcmToken) {\n this.update((state) => {\n state.fcmToken = fcmToken;\n });\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 }\n }\n}\n"]}
1
+ {"version":3,"file":"NotificationServicesPushController.cjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAMA,+DAA2D;AAE3D,wDAA2B;AAE3B,sDAI6B;AAK7B,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAyE/C,QAAA,YAAY,GAA4C;IACnE,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,EAAE;IACZ,kBAAkB,EAAE,KAAK;CAC1B,CAAC;AACF,MAAM,QAAQ,GAA2D;IACvE,aAAa,EAAE;QACb,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;KAChB;IACD,QAAQ,EAAE;QACR,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;KAChB;IACD,kBAAkB,EAAE;QAClB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,IAAI;KAChB;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;AA4BF;;;;;;GAMG;AACH,MAAqB,kCAAmC,SAAQ,gCAI/D;IAO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AAK;IACH,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CACjD,yCAAyC,CAC1C,CAAC;IACF,IAAI,CAAC,WAAW,EAAE;QAChB,kBAAG,CAAC,KAAK,CACP,oEAAoE,CACrE,CAAC;QACF,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;IAED,OAAO,WAAW,CAAC;AACrB,CAAC,qHAEgB,OAAqB;IACpC,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE;QAC7B,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,aAAa,GAAG,IAAI,CAAC;YAC3B,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;YAClC,KAAK,CAAC,kBAAkB,GAAG,KAAK,CAAC;QACnC,CAAC,CAAC,CAAC;KACJ;IAED,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS,EAAE;QAC9B,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,aAAa,GAAG,KAAK,CAAC;YAC5B,KAAK,CAAC,QAAQ,GAAG,EAAE,CAAC;YACpB,KAAK,CAAC,kBAAkB,GAAG,KAAK,CAAC;QACnC,CAAC,CAAC,CAAC;KACJ;IAED,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE;QAC7B,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,aAAa,GAAG,IAAI,CAAC;YAC3B,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;YAClC,KAAK,CAAC,kBAAkB,GAAG,KAAK,CAAC;QACnC,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;kBApGkB,kCAAkC","sourcesContent":["import type {\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n updateTriggerPushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\nimport type { PushService } from './types/push-service-interface';\nimport type { Types } from '../NotificationServicesController';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n isPushEnabled: boolean;\n fcmToken: string;\n isUpdatingFCMToken: boolean;\n};\n\nexport type NotificationServicesPushControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\nexport type NotificationServicesPushControllerSubscribeToNotificationsAction = {\n type: `${typeof controllerName}:subscribeToPushNotifications`;\n handler: NotificationServicesPushController['subscribeToPushNotifications'];\n};\n\nexport type Actions =\n | NotificationServicesPushControllerGetStateAction\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | NotificationServicesPushControllerSubscribeToNotificationsAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClickedEvent = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type Events =\n | NotificationServicesPushControllerStateChangeEvent\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClickedEvent;\n\nexport type AllowedEvents = never;\n\nexport type NotificationServicesPushControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\nexport const defaultState: NotificationServicesPushControllerState = {\n isPushEnabled: true,\n fcmToken: '',\n isUpdatingFCMToken: false,\n};\nconst metadata: StateMetadata<NotificationServicesPushControllerState> = {\n isPushEnabled: {\n persist: true,\n anonymous: true,\n },\n fcmToken: {\n persist: true,\n anonymous: true,\n },\n isUpdatingFCMToken: {\n persist: false,\n anonymous: 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 * Global switch to determine to use push notifications\n * Allows us to control Builds on extension (MV2 vs MV3)\n */\n isPushFeatureEnabled?: boolean;\n\n /**\n * determine the config used for push notification services\n */\n platform: 'extension' | 'mobile';\n\n /**\n * Push Service Interface\n * - create reg token\n * - delete reg token\n * - subscribe to push notifications\n */\n pushService: PushService;\n};\n\ntype StateCommand =\n | { type: 'enable'; fcmToken: string }\n | { type: 'disable' }\n | { type: 'update'; fcmToken: string };\n\n/**\n * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.\n * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.\n * It is responsible for registering and unregistering the service worker that listens for push notifications,\n * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.\n * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.\n */\nexport default class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n readonly #env: PushNotificationEnv;\n\n readonly #config: ControllerConfig;\n\n constructor({\n messenger,\n state,\n env,\n config,\n }: {\n messenger: NotificationServicesPushControllerMessenger;\n state: NotificationServicesPushControllerState;\n /** Push Environment is only required for extension */\n env?: PushNotificationEnv;\n config: ControllerConfig;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n this.#env = env ?? defaultPushEnv;\n this.#config = config;\n\n this.#registerMessageHandlers();\n this.#clearLoadingStates();\n }\n\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:enablePushNotifications',\n this.enablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:disablePushNotifications',\n this.disablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:updateTriggerPushNotifications',\n this.updateTriggerPushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:subscribeToPushNotifications',\n this.subscribeToPushNotifications.bind(this),\n );\n }\n\n #clearLoadingStates(): void {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n async #getAndAssertBearerToken() {\n const bearerToken = await this.messagingSystem.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n log.error(\n 'Failed to enable push notifications: BearerToken token is missing.',\n );\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n #updatePushState(command: StateCommand) {\n if (command.type === 'enable') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'disable') {\n this.update((state) => {\n state.isPushEnabled = false;\n state.fcmToken = '';\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'update') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n }\n\n public async subscribeToPushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n if (this.#pushListenerUnsubscribe) {\n this.#pushListenerUnsubscribe();\n this.#pushListenerUnsubscribe = undefined;\n }\n\n try {\n this.#pushListenerUnsubscribe =\n (await this.#config.pushService.subscribeToPushNotifications(\n this.#env,\n )) ?? undefined;\n } catch {\n // Do nothing, we are silently failing if push notification registration fails\n }\n }\n\n /**\n * Enables push notifications for the application.\n *\n * This method sets up the necessary infrastructure for handling push notifications by:\n * 1. Registering the service worker to listen for messages.\n * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.\n * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.\n *\n * @param UUIDs - An array of UUIDs to enable push notifications for.\n */\n public async enablePushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n // Handle creating new reg token (if available)\n try {\n const bearerToken = await this.#getAndAssertBearerToken().catch(\n () => null,\n );\n\n // If there is a bearer token, lets try to refresh/create new reg token\n if (bearerToken) {\n // Activate Push Notifications\n const fcmToken = await activatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n platform: this.#config.platform,\n }).catch(() => null);\n\n if (fcmToken) {\n this.#updatePushState({ type: 'enable', fcmToken });\n }\n }\n } catch {\n // Do nothing, we are silently failing\n }\n\n // New token created, (re)subscribe to push notifications\n try {\n await this.subscribeToPushNotifications();\n } catch {\n // Do nothing we are silently failing\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n /**\n * Disables push notifications for the application.\n * This removes the registration token on this device, and ensures we unsubscribe from any listeners\n */\n public async disablePushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n // Send a request to the server to unregister the token/device\n await deactivatePushNotifications({\n env: this.#env,\n deleteRegToken: this.#config.pushService.deleteRegToken,\n regToken: this.state.fcmToken,\n });\n } catch (error) {\n const errorMessage = `Failed to disable push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n } finally {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n this.#updatePushState({ type: 'disable' });\n }\n\n /**\n * Updates the triggers for push notifications.\n * This method is responsible for updating the server with the new set of UUIDs that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param UUIDs - An array of UUIDs that should trigger push notifications.\n */\n public async updateTriggerPushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n const bearerToken = await this.#getAndAssertBearerToken();\n const { fcmToken } = await updateTriggerPushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n deleteRegToken: this.#config.pushService.deleteRegToken,\n platform: this.#config.platform,\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,10 +2,13 @@ import type { RestrictedMessenger, ControllerGetStateAction, ControllerStateChan
2
2
  import { BaseController } from "@metamask/base-controller";
3
3
  import type { AuthenticationController } from "@metamask/profile-sync-controller";
4
4
  import type { PushNotificationEnv } from "./types/index.cjs";
5
+ import type { PushService } from "./types/push-service-interface.cjs";
5
6
  import type { Types } from "../NotificationServicesController/index.cjs";
6
7
  declare const controllerName = "NotificationServicesPushController";
7
8
  export type NotificationServicesPushControllerState = {
9
+ isPushEnabled: boolean;
8
10
  fcmToken: string;
11
+ isUpdatingFCMToken: boolean;
9
12
  };
10
13
  export type NotificationServicesPushControllerGetStateAction = ControllerGetStateAction<typeof controllerName, NotificationServicesPushControllerState>;
11
14
  export type NotificationServicesPushControllerEnablePushNotificationsAction = {
@@ -39,26 +42,23 @@ export type Events = NotificationServicesPushControllerStateChangeEvent | Notifi
39
42
  export type AllowedEvents = never;
40
43
  export type NotificationServicesPushControllerMessenger = RestrictedMessenger<typeof controllerName, Actions | AllowedActions, Events | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
41
44
  export declare const defaultState: NotificationServicesPushControllerState;
42
- type ControllerConfig = {
43
- /**
44
- * Config to turn on/off push notifications.
45
- * This is currently linked to MV3 builds on extension.
46
- */
47
- isPushEnabled: boolean;
45
+ export type ControllerConfig = {
48
46
  /**
49
- * Must handle when a push notification is received.
50
- * You must call `registration.showNotification` or equivalent to show the notification on web/mobile
47
+ * Global switch to determine to use push notifications
48
+ * Allows us to control Builds on extension (MV2 vs MV3)
51
49
  */
52
- onPushNotificationReceived: (notification: Types.INotification) => void | Promise<void>;
53
- /**
54
- * Must handle when a push notification is clicked.
55
- * You must call `event.notification.close();` or equivalent for closing and opening notification in a new window.
56
- */
57
- onPushNotificationClicked: (event: NotificationEvent, notification?: Types.INotification) => void;
50
+ isPushFeatureEnabled?: boolean;
58
51
  /**
59
52
  * determine the config used for push notification services
60
53
  */
61
54
  platform: 'extension' | 'mobile';
55
+ /**
56
+ * Push Service Interface
57
+ * - create reg token
58
+ * - delete reg token
59
+ * - subscribe to push notifications
60
+ */
61
+ pushService: PushService;
62
62
  };
63
63
  /**
64
64
  * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
@@ -72,7 +72,8 @@ export default class NotificationServicesPushController extends BaseController<t
72
72
  constructor({ messenger, state, env, config, }: {
73
73
  messenger: NotificationServicesPushControllerMessenger;
74
74
  state: NotificationServicesPushControllerState;
75
- env: PushNotificationEnv;
75
+ /** Push Environment is only required for extension */
76
+ env?: PushNotificationEnv;
76
77
  config: ControllerConfig;
77
78
  });
78
79
  subscribeToPushNotifications(): Promise<void>;
@@ -85,9 +86,8 @@ export default class NotificationServicesPushController extends BaseController<t
85
86
  * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
86
87
  *
87
88
  * @param UUIDs - An array of UUIDs to enable push notifications for.
88
- * @param fcmToken - The optional FCM token to use for push notifications.
89
89
  */
90
- enablePushNotifications(UUIDs: string[], fcmToken?: string): Promise<void>;
90
+ enablePushNotifications(UUIDs: string[]): Promise<void>;
91
91
  /**
92
92
  * Disables push notifications for the application.
93
93
  * This removes the registration token on this device, and ensures we unsubscribe from any listeners
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationServicesPushController.d.cts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAUlF,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAAgB;AACnD,OAAO,KAAK,EAAE,KAAK,EAAE,oDAA0C;AAE/D,QAAA,MAAM,cAAc,uCAAuC,CAAC;AAE5D,MAAM,MAAM,uCAAuC,GAAG;IACpD,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,gDAAgD,GAC1D,wBAAwB,CACtB,OAAO,cAAc,EACrB,uCAAuC,CACxC,CAAC;AAEJ,MAAM,MAAM,+DAA+D,GAAG;IAC5E,IAAI,EAAE,GAAG,OAAO,cAAc,0BAA0B,CAAC;IACzD,OAAO,EAAE,kCAAkC,CAAC,yBAAyB,CAAC,CAAC;CACxE,CAAC;AACF,MAAM,MAAM,gEAAgE,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AAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAEF,eAAO,MAAM,YAAY,EAAE,uCAE1B,CAAC;AAQF,KAAK,gBAAgB,GAAG;IACtB;;;OAGG;IACH,aAAa,EAAE,OAAO,CAAC;IAEvB;;;OAGG;IACH,0BAA0B,EAAE,CAC1B,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1B;;;OAGG;IACH,yBAAyB,EAAE,CACzB,KAAK,EAAE,iBAAiB,EACxB,YAAY,CAAC,EAAE,KAAK,CAAC,aAAa,KAC/B,IAAI,CAAC;IAEV;;OAEG;IACH,QAAQ,EAAE,WAAW,GAAG,QAAQ,CAAC;CAClC,CAAC;AAEF;;;;;;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,GAAG,EAAE,mBAAmB,CAAC;QACzB,MAAM,EAAE,gBAAgB,CAAC;KAC1B;IA+CK,4BAA4B;IAgClC;;;;;;;;;;OAUG;IACG,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,QAAQ,CAAC,EAAE,MAAM;IAqChE;;;OAGG;IACG,wBAAwB;IAsC9B;;;;;;OAMG;IACG,8BAA8B,CAAC,KAAK,EAAE,MAAM,EAAE;CA+BrD"}
1
+ {"version":3,"file":"NotificationServicesPushController.d.cts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,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AAC;KAC1B;IAgFY,4BAA4B;IAoBzC;;;;;;;;;OASG;IACU,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE;IA8CpD;;;OAGG;IACU,wBAAwB;IAmCrC;;;;;;OAMG;IACU,8BAA8B,CAAC,KAAK,EAAE,MAAM,EAAE;CAoC5D"}
@@ -2,10 +2,13 @@ import type { RestrictedMessenger, ControllerGetStateAction, ControllerStateChan
2
2
  import { BaseController } from "@metamask/base-controller";
3
3
  import type { AuthenticationController } from "@metamask/profile-sync-controller";
4
4
  import type { PushNotificationEnv } from "./types/index.mjs";
5
+ import type { PushService } from "./types/push-service-interface.mjs";
5
6
  import type { Types } from "../NotificationServicesController/index.mjs";
6
7
  declare const controllerName = "NotificationServicesPushController";
7
8
  export type NotificationServicesPushControllerState = {
9
+ isPushEnabled: boolean;
8
10
  fcmToken: string;
11
+ isUpdatingFCMToken: boolean;
9
12
  };
10
13
  export type NotificationServicesPushControllerGetStateAction = ControllerGetStateAction<typeof controllerName, NotificationServicesPushControllerState>;
11
14
  export type NotificationServicesPushControllerEnablePushNotificationsAction = {
@@ -39,26 +42,23 @@ export type Events = NotificationServicesPushControllerStateChangeEvent | Notifi
39
42
  export type AllowedEvents = never;
40
43
  export type NotificationServicesPushControllerMessenger = RestrictedMessenger<typeof controllerName, Actions | AllowedActions, Events | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
41
44
  export declare const defaultState: NotificationServicesPushControllerState;
42
- type ControllerConfig = {
43
- /**
44
- * Config to turn on/off push notifications.
45
- * This is currently linked to MV3 builds on extension.
46
- */
47
- isPushEnabled: boolean;
45
+ export type ControllerConfig = {
48
46
  /**
49
- * Must handle when a push notification is received.
50
- * You must call `registration.showNotification` or equivalent to show the notification on web/mobile
47
+ * Global switch to determine to use push notifications
48
+ * Allows us to control Builds on extension (MV2 vs MV3)
51
49
  */
52
- onPushNotificationReceived: (notification: Types.INotification) => void | Promise<void>;
53
- /**
54
- * Must handle when a push notification is clicked.
55
- * You must call `event.notification.close();` or equivalent for closing and opening notification in a new window.
56
- */
57
- onPushNotificationClicked: (event: NotificationEvent, notification?: Types.INotification) => void;
50
+ isPushFeatureEnabled?: boolean;
58
51
  /**
59
52
  * determine the config used for push notification services
60
53
  */
61
54
  platform: 'extension' | 'mobile';
55
+ /**
56
+ * Push Service Interface
57
+ * - create reg token
58
+ * - delete reg token
59
+ * - subscribe to push notifications
60
+ */
61
+ pushService: PushService;
62
62
  };
63
63
  /**
64
64
  * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
@@ -72,7 +72,8 @@ export default class NotificationServicesPushController extends BaseController<t
72
72
  constructor({ messenger, state, env, config, }: {
73
73
  messenger: NotificationServicesPushControllerMessenger;
74
74
  state: NotificationServicesPushControllerState;
75
- env: PushNotificationEnv;
75
+ /** Push Environment is only required for extension */
76
+ env?: PushNotificationEnv;
76
77
  config: ControllerConfig;
77
78
  });
78
79
  subscribeToPushNotifications(): Promise<void>;
@@ -85,9 +86,8 @@ export default class NotificationServicesPushController extends BaseController<t
85
86
  * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
86
87
  *
87
88
  * @param UUIDs - An array of UUIDs to enable push notifications for.
88
- * @param fcmToken - The optional FCM token to use for push notifications.
89
89
  */
90
- enablePushNotifications(UUIDs: string[], fcmToken?: string): Promise<void>;
90
+ enablePushNotifications(UUIDs: string[]): Promise<void>;
91
91
  /**
92
92
  * Disables push notifications for the application.
93
93
  * This removes the registration token on this device, and ensures we unsubscribe from any listeners
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationServicesPushController.d.mts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAUlF,OAAO,KAAK,EAAE,mBAAm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,4BAA4B;IAgClC;;;;;;;;;;OAUG;IACG,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,QAAQ,CAAC,EAAE,MAAM;IAqChE;;;OAGG;IACG,wBAAwB;IAsC9B;;;;;;OAMG;IACG,8BAA8B,CAAC,KAAK,EAAE,MAAM,EAAE;CA+BrD"}
1
+ {"version":3,"file":"NotificationServicesPushController.d.mts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAQlF,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,GAAG;IAC5E,IAAI,EAAE,GAAG,OAAO,cAAc,0BAA0B,CAAC;IACzD,OAAO,EAAE,kCAAkC,CAAC,yBAAyB,CAAC,CAAC;CACxE,CAAC;AACF,MAAM,MAAM,gEAAgE,GAAG;IAC7E,IAAI,EAAE,GAAG,OAAO,cAAc,2BAA2B,CAAC;IAC1D,OAAO,EAAE,kCAAkC,CAAC,0BAA0B,CAAC,CAAC;CACzE,CAAC;AACF,MAAM,MAAM,sEAAsE,GAChF;IACE,IAAI,EAAE,GAAG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EG;IACH,QAAQ,EAAE,WAAW,GAAG,QAAQ,CAAC;IAEjC;;;;;OAKG;IACH,WAAW,EAAE,WAAW,CAAC;CAC1B,CAAC;AAOF;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,kCAAmC,SAAQ,cAAc,CAC5E,OAAO,cAAc,EACrB,uCAAuC,EACvC,2CAA2C,CAC5C;;gBAOa,EACV,SAAS,EACT,KAAK,EACL,GAAG,EACH,MAAM,GACP,EAAE;QACD,SAAS,EAAE,2CAA2C,CAAC;QACvD,KAAK,EAAE,uCAAuC,CAAC;QAC/C,sDAAsD;QACtD,GAAG,CAAC,EAAE,mBAAmB,CAAC;QAC1B,MAAM,EAAE,gBAAgB,CAAC;KAC1B;IAgFY,4BAA4B;IAoBzC;;;;;;;;;OASG;IACU,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE;IA8CpD;;;OAGG;IACU,wBAAwB;IAmCrC;;;;;;OAMG;IACU,8BAA8B,CAAC,KAAK,EAAE,MAAM,EAAE;CAoC5D"}
@@ -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_getAndAssertBearerToken;
12
+ var _NotificationServicesPushController_instances, _NotificationServicesPushController_pushListenerUnsubscribe, _NotificationServicesPushController_env, _NotificationServicesPushController_config, _NotificationServicesPushController_registerMessageHandlers, _NotificationServicesPushController_clearLoadingStates, _NotificationServicesPushController_getAndAssertBearerToken, _NotificationServicesPushController_updatePushState;
13
13
  function $importDefault(module) {
14
14
  if (module?.__esModule) {
15
15
  return module.default;
@@ -19,17 +19,36 @@ 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 { createRegToken, deleteRegToken } from "./services/push/push-web.mjs";
23
- import { activatePushNotifications, deactivatePushNotifications, listenToPushNotifications, updateTriggerPushNotifications } from "./services/services.mjs";
22
+ import { activatePushNotifications, deactivatePushNotifications, updateTriggerPushNotifications } from "./services/services.mjs";
24
23
  const controllerName = 'NotificationServicesPushController';
25
24
  export const defaultState = {
25
+ isPushEnabled: true,
26
26
  fcmToken: '',
27
+ isUpdatingFCMToken: false,
27
28
  };
28
29
  const metadata = {
30
+ isPushEnabled: {
31
+ persist: true,
32
+ anonymous: true,
33
+ },
29
34
  fcmToken: {
30
35
  persist: true,
31
36
  anonymous: true,
32
37
  },
38
+ isUpdatingFCMToken: {
39
+ persist: false,
40
+ anonymous: true,
41
+ },
42
+ };
43
+ const defaultPushEnv = {
44
+ apiKey: '',
45
+ authDomain: '',
46
+ storageBucket: '',
47
+ projectId: '',
48
+ messagingSenderId: '',
49
+ appId: '',
50
+ measurementId: '',
51
+ vapidKey: '',
33
52
  };
34
53
  /**
35
54
  * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
@@ -50,29 +69,21 @@ class NotificationServicesPushController extends BaseController {
50
69
  _NotificationServicesPushController_pushListenerUnsubscribe.set(this, undefined);
51
70
  _NotificationServicesPushController_env.set(this, void 0);
52
71
  _NotificationServicesPushController_config.set(this, void 0);
53
- __classPrivateFieldSet(this, _NotificationServicesPushController_env, env, "f");
72
+ __classPrivateFieldSet(this, _NotificationServicesPushController_env, env ?? defaultPushEnv, "f");
54
73
  __classPrivateFieldSet(this, _NotificationServicesPushController_config, config, "f");
55
74
  __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_registerMessageHandlers).call(this);
75
+ __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_clearLoadingStates).call(this);
56
76
  }
57
77
  async subscribeToPushNotifications() {
78
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
79
+ return;
80
+ }
58
81
  if (__classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")) {
59
82
  __classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f").call(this);
60
83
  __classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, undefined, "f");
61
84
  }
62
85
  try {
63
- __classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, await listenToPushNotifications({
64
- env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
65
- listenToPushReceived: async (n) => {
66
- this.messagingSystem.publish('NotificationServicesPushController:onNewNotifications', n);
67
- await __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").onPushNotificationReceived(n);
68
- },
69
- listenToPushClicked: (e, n) => {
70
- if (n) {
71
- this.messagingSystem.publish('NotificationServicesPushController:pushNotificationClicked', n);
72
- }
73
- __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").onPushNotificationClicked(e, n);
74
- },
75
- }), "f");
86
+ __classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, (await __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.subscribeToPushNotifications(__classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"))) ?? undefined, "f");
76
87
  }
77
88
  catch {
78
89
  // Do nothing, we are silently failing if push notification registration fails
@@ -87,30 +98,29 @@ class NotificationServicesPushController extends BaseController {
87
98
  * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
88
99
  *
89
100
  * @param UUIDs - An array of UUIDs to enable push notifications for.
90
- * @param fcmToken - The optional FCM token to use for push notifications.
91
101
  */
92
- async enablePushNotifications(UUIDs, fcmToken) {
93
- if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
102
+ async enablePushNotifications(UUIDs) {
103
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
94
104
  return;
95
105
  }
106
+ this.update((state) => {
107
+ state.isUpdatingFCMToken = true;
108
+ });
96
109
  // Handle creating new reg token (if available)
97
110
  try {
98
111
  const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this).catch(() => null);
99
112
  // If there is a bearer token, lets try to refresh/create new reg token
100
113
  if (bearerToken) {
101
114
  // Activate Push Notifications
102
- const regToken = await activatePushNotifications({
115
+ const fcmToken = await activatePushNotifications({
103
116
  bearerToken,
104
117
  triggers: UUIDs,
105
118
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
106
- fcmToken,
107
- createRegToken,
119
+ createRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.createRegToken,
108
120
  platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
109
121
  }).catch(() => null);
110
- if (regToken) {
111
- this.update((state) => {
112
- state.fcmToken = regToken;
113
- });
122
+ if (fcmToken) {
123
+ __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_updatePushState).call(this, { type: 'enable', fcmToken });
114
124
  }
115
125
  }
116
126
  }
@@ -118,22 +128,32 @@ class NotificationServicesPushController extends BaseController {
118
128
  // Do nothing, we are silently failing
119
129
  }
120
130
  // New token created, (re)subscribe to push notifications
121
- await this.subscribeToPushNotifications();
131
+ try {
132
+ await this.subscribeToPushNotifications();
133
+ }
134
+ catch {
135
+ // Do nothing we are silently failing
136
+ }
137
+ this.update((state) => {
138
+ state.isUpdatingFCMToken = false;
139
+ });
122
140
  }
123
141
  /**
124
142
  * Disables push notifications for the application.
125
143
  * This removes the registration token on this device, and ensures we unsubscribe from any listeners
126
144
  */
127
145
  async disablePushNotifications() {
128
- if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
146
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
129
147
  return;
130
148
  }
131
- let isPushNotificationsDisabled;
149
+ this.update((state) => {
150
+ state.isUpdatingFCMToken = true;
151
+ });
132
152
  try {
133
153
  // Send a request to the server to unregister the token/device
134
- isPushNotificationsDisabled = await deactivatePushNotifications({
154
+ await deactivatePushNotifications({
135
155
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
136
- deleteRegToken,
156
+ deleteRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.deleteRegToken,
137
157
  regToken: this.state.fcmToken,
138
158
  });
139
159
  }
@@ -142,18 +162,15 @@ class NotificationServicesPushController extends BaseController {
142
162
  log.error(errorMessage);
143
163
  throw new Error(errorMessage);
144
164
  }
145
- // Remove the FCM token from the state
146
- if (!isPushNotificationsDisabled) {
147
- return;
165
+ finally {
166
+ this.update((state) => {
167
+ state.isUpdatingFCMToken = false;
168
+ });
148
169
  }
149
170
  // Unsubscribe from push notifications
150
171
  __classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")?.call(this);
151
172
  // Update State
152
- if (isPushNotificationsDisabled) {
153
- this.update((state) => {
154
- state.fcmToken = '';
155
- });
156
- }
173
+ __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_updatePushState).call(this, { type: 'disable' });
157
174
  }
158
175
  /**
159
176
  * Updates the triggers for push notifications.
@@ -163,24 +180,25 @@ class NotificationServicesPushController extends BaseController {
163
180
  * @param UUIDs - An array of UUIDs that should trigger push notifications.
164
181
  */
165
182
  async updateTriggerPushNotifications(UUIDs) {
166
- if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
183
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
167
184
  return;
168
185
  }
169
- const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
186
+ this.update((state) => {
187
+ state.isUpdatingFCMToken = true;
188
+ });
170
189
  try {
190
+ const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
171
191
  const { fcmToken } = await updateTriggerPushNotifications({
172
192
  bearerToken,
173
193
  triggers: UUIDs,
174
194
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
175
- createRegToken,
176
- deleteRegToken,
195
+ createRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.createRegToken,
196
+ deleteRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.deleteRegToken,
177
197
  platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
178
198
  });
179
199
  // update the state with the new FCM token
180
200
  if (fcmToken) {
181
- this.update((state) => {
182
- state.fcmToken = fcmToken;
183
- });
201
+ __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_updatePushState).call(this, { type: 'update', fcmToken });
184
202
  }
185
203
  }
186
204
  catch (error) {
@@ -188,6 +206,11 @@ class NotificationServicesPushController extends BaseController {
188
206
  log.error(errorMessage);
189
207
  throw new Error(errorMessage);
190
208
  }
209
+ finally {
210
+ this.update((state) => {
211
+ state.isUpdatingFCMToken = false;
212
+ });
213
+ }
191
214
  }
192
215
  }
193
216
  _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _NotificationServicesPushController_env = new WeakMap(), _NotificationServicesPushController_config = new WeakMap(), _NotificationServicesPushController_instances = new WeakSet(), _NotificationServicesPushController_registerMessageHandlers = function _NotificationServicesPushController_registerMessageHandlers() {
@@ -195,6 +218,10 @@ _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _No
195
218
  this.messagingSystem.registerActionHandler('NotificationServicesPushController:disablePushNotifications', this.disablePushNotifications.bind(this));
196
219
  this.messagingSystem.registerActionHandler('NotificationServicesPushController:updateTriggerPushNotifications', this.updateTriggerPushNotifications.bind(this));
197
220
  this.messagingSystem.registerActionHandler('NotificationServicesPushController:subscribeToPushNotifications', this.subscribeToPushNotifications.bind(this));
221
+ }, _NotificationServicesPushController_clearLoadingStates = function _NotificationServicesPushController_clearLoadingStates() {
222
+ this.update((state) => {
223
+ state.isUpdatingFCMToken = false;
224
+ });
198
225
  }, _NotificationServicesPushController_getAndAssertBearerToken = async function _NotificationServicesPushController_getAndAssertBearerToken() {
199
226
  const bearerToken = await this.messagingSystem.call('AuthenticationController:getBearerToken');
200
227
  if (!bearerToken) {
@@ -202,6 +229,28 @@ _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _No
202
229
  throw new Error('BearerToken token is missing');
203
230
  }
204
231
  return bearerToken;
232
+ }, _NotificationServicesPushController_updatePushState = function _NotificationServicesPushController_updatePushState(command) {
233
+ if (command.type === 'enable') {
234
+ this.update((state) => {
235
+ state.isPushEnabled = true;
236
+ state.fcmToken = command.fcmToken;
237
+ state.isUpdatingFCMToken = false;
238
+ });
239
+ }
240
+ if (command.type === 'disable') {
241
+ this.update((state) => {
242
+ state.isPushEnabled = false;
243
+ state.fcmToken = '';
244
+ state.isUpdatingFCMToken = false;
245
+ });
246
+ }
247
+ if (command.type === 'update') {
248
+ this.update((state) => {
249
+ state.isPushEnabled = true;
250
+ state.fcmToken = command.fcmToken;
251
+ state.isUpdatingFCMToken = false;
252
+ });
253
+ }
205
254
  };
206
255
  export default NotificationServicesPushController;
207
256
  //# sourceMappingURL=NotificationServicesPushController.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationServicesPushController.mjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAMA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,qCAAiC;AAC1E,OAAO,EACL,yBAAyB,EACz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\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport { createRegToken, deleteRegToken } from './services/push/push-web';\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n listenToPushNotifications,\n updateTriggerPushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\nimport type { Types } from '../NotificationServicesController';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n fcmToken: string;\n};\n\nexport type NotificationServicesPushControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\nexport type NotificationServicesPushControllerSubscribeToNotificationsAction = {\n type: `${typeof controllerName}:subscribeToPushNotifications`;\n handler: NotificationServicesPushController['subscribeToPushNotifications'];\n};\n\nexport type Actions =\n | NotificationServicesPushControllerGetStateAction\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | NotificationServicesPushControllerSubscribeToNotificationsAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClickedEvent = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type Events =\n | NotificationServicesPushControllerStateChangeEvent\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClickedEvent;\n\nexport type AllowedEvents = never;\n\nexport type NotificationServicesPushControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\nexport const defaultState: NotificationServicesPushControllerState = {\n fcmToken: '',\n};\nconst metadata: StateMetadata<NotificationServicesPushControllerState> = {\n fcmToken: {\n persist: true,\n anonymous: true,\n },\n};\n\ntype ControllerConfig = {\n /**\n * Config to turn on/off push notifications.\n * This is currently linked to MV3 builds on extension.\n */\n isPushEnabled: boolean;\n\n /**\n * Must handle when a push notification is received.\n * You must call `registration.showNotification` or equivalent to show the notification on web/mobile\n */\n onPushNotificationReceived: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n\n /**\n * Must handle when a push notification is clicked.\n * You must call `event.notification.close();` or equivalent for closing and opening notification in a new window.\n */\n onPushNotificationClicked: (\n event: NotificationEvent,\n notification?: Types.INotification,\n ) => void;\n\n /**\n * determine the config used for push notification services\n */\n platform: 'extension' | 'mobile';\n};\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 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;\n this.#config = config;\n\n this.#registerMessageHandlers();\n }\n\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:enablePushNotifications',\n this.enablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:disablePushNotifications',\n this.disablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:updateTriggerPushNotifications',\n this.updateTriggerPushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:subscribeToPushNotifications',\n this.subscribeToPushNotifications.bind(this),\n );\n }\n\n async #getAndAssertBearerToken() {\n const bearerToken = await this.messagingSystem.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n log.error(\n 'Failed to enable push notifications: BearerToken token is missing.',\n );\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n async subscribeToPushNotifications() {\n if (this.#pushListenerUnsubscribe) {\n this.#pushListenerUnsubscribe();\n this.#pushListenerUnsubscribe = undefined;\n }\n\n try {\n this.#pushListenerUnsubscribe = await listenToPushNotifications({\n env: this.#env,\n listenToPushReceived: async (n) => {\n this.messagingSystem.publish(\n 'NotificationServicesPushController:onNewNotifications',\n n,\n );\n await this.#config.onPushNotificationReceived(n);\n },\n listenToPushClicked: (e, n) => {\n if (n) {\n this.messagingSystem.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n n,\n );\n }\n\n this.#config.onPushNotificationClicked(e, n);\n },\n });\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 UUIDs - An array of UUIDs to enable push notifications for.\n * @param fcmToken - The optional FCM token to use for push notifications.\n */\n async enablePushNotifications(UUIDs: string[], fcmToken?: string) {\n if (!this.#config.isPushEnabled) {\n return;\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 regToken = await activatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n fcmToken,\n createRegToken,\n platform: this.#config.platform,\n }).catch(() => null);\n\n if (regToken) {\n this.update((state) => {\n state.fcmToken = regToken;\n });\n }\n }\n } catch {\n // Do nothing, we are silently failing\n }\n\n // New token created, (re)subscribe to push notifications\n await this.subscribeToPushNotifications();\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 async disablePushNotifications() {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n let isPushNotificationsDisabled: boolean;\n\n try {\n // Send a request to the server to unregister the token/device\n isPushNotificationsDisabled = await deactivatePushNotifications({\n env: this.#env,\n 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 }\n\n // Remove the FCM token from the state\n if (!isPushNotificationsDisabled) {\n return;\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n if (isPushNotificationsDisabled) {\n this.update((state) => {\n state.fcmToken = '';\n });\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 UUIDs that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param UUIDs - An array of UUIDs that should trigger push notifications.\n */\n async updateTriggerPushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n const bearerToken = await this.#getAndAssertBearerToken();\n\n try {\n const { fcmToken } = await updateTriggerPushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken,\n deleteRegToken,\n platform: this.#config.platform,\n });\n\n // update the state with the new FCM token\n if (fcmToken) {\n this.update((state) => {\n state.fcmToken = fcmToken;\n });\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 }\n }\n}\n"]}
1
+ {"version":3,"file":"NotificationServicesPushController.mjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAMA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,EACL,yBAAyB,EACzB,2BAA2B,EAC3B,8BAA8B,EAC/B,gCAA4B;AAK7B,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAyE5D,MAAM,CAAC,MAAM,YAAY,GAA4C;IACnE,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,EAAE;IACZ,kBAAkB,EAAE,KAAK;CAC1B,CAAC;AACF,MAAM,QAAQ,GAA2D;IACvE,aAAa,EAAE;QACb,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;KAChB;IACD,QAAQ,EAAE;QACR,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;KAChB;IACD,kBAAkB,EAAE;QAClB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,IAAI;KAChB;CACF,CAAC;AAEF,MAAM,cAAc,GAAwB;IAC1C,MAAM,EAAE,EAAE;IACV,UAAU,EAAE,EAAE;IACd,aAAa,EAAE,EAAE;IACjB,SAAS,EAAE,E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,CAAC;KACJ;AACH,CAAC;eApGkB,kCAAkC","sourcesContent":["import type {\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n updateTriggerPushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\nimport type { PushService } from './types/push-service-interface';\nimport type { Types } from '../NotificationServicesController';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n isPushEnabled: boolean;\n fcmToken: string;\n isUpdatingFCMToken: boolean;\n};\n\nexport type NotificationServicesPushControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\nexport type NotificationServicesPushControllerSubscribeToNotificationsAction = {\n type: `${typeof controllerName}:subscribeToPushNotifications`;\n handler: NotificationServicesPushController['subscribeToPushNotifications'];\n};\n\nexport type Actions =\n | NotificationServicesPushControllerGetStateAction\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | NotificationServicesPushControllerSubscribeToNotificationsAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClickedEvent = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type Events =\n | NotificationServicesPushControllerStateChangeEvent\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClickedEvent;\n\nexport type AllowedEvents = never;\n\nexport type NotificationServicesPushControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\nexport const defaultState: NotificationServicesPushControllerState = {\n isPushEnabled: true,\n fcmToken: '',\n isUpdatingFCMToken: false,\n};\nconst metadata: StateMetadata<NotificationServicesPushControllerState> = {\n isPushEnabled: {\n persist: true,\n anonymous: true,\n },\n fcmToken: {\n persist: true,\n anonymous: true,\n },\n isUpdatingFCMToken: {\n persist: false,\n anonymous: 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 * Global switch to determine to use push notifications\n * Allows us to control Builds on extension (MV2 vs MV3)\n */\n isPushFeatureEnabled?: boolean;\n\n /**\n * determine the config used for push notification services\n */\n platform: 'extension' | 'mobile';\n\n /**\n * Push Service Interface\n * - create reg token\n * - delete reg token\n * - subscribe to push notifications\n */\n pushService: PushService;\n};\n\ntype StateCommand =\n | { type: 'enable'; fcmToken: string }\n | { type: 'disable' }\n | { type: 'update'; fcmToken: string };\n\n/**\n * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.\n * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.\n * It is responsible for registering and unregistering the service worker that listens for push notifications,\n * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.\n * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.\n */\nexport default class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n readonly #env: PushNotificationEnv;\n\n readonly #config: ControllerConfig;\n\n constructor({\n messenger,\n state,\n env,\n config,\n }: {\n messenger: NotificationServicesPushControllerMessenger;\n state: NotificationServicesPushControllerState;\n /** Push Environment is only required for extension */\n env?: PushNotificationEnv;\n config: ControllerConfig;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n this.#env = env ?? defaultPushEnv;\n this.#config = config;\n\n this.#registerMessageHandlers();\n this.#clearLoadingStates();\n }\n\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:enablePushNotifications',\n this.enablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:disablePushNotifications',\n this.disablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:updateTriggerPushNotifications',\n this.updateTriggerPushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:subscribeToPushNotifications',\n this.subscribeToPushNotifications.bind(this),\n );\n }\n\n #clearLoadingStates(): void {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n async #getAndAssertBearerToken() {\n const bearerToken = await this.messagingSystem.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n log.error(\n 'Failed to enable push notifications: BearerToken token is missing.',\n );\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n #updatePushState(command: StateCommand) {\n if (command.type === 'enable') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'disable') {\n this.update((state) => {\n state.isPushEnabled = false;\n state.fcmToken = '';\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'update') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n }\n\n public async subscribeToPushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n if (this.#pushListenerUnsubscribe) {\n this.#pushListenerUnsubscribe();\n this.#pushListenerUnsubscribe = undefined;\n }\n\n try {\n this.#pushListenerUnsubscribe =\n (await this.#config.pushService.subscribeToPushNotifications(\n this.#env,\n )) ?? undefined;\n } catch {\n // Do nothing, we are silently failing if push notification registration fails\n }\n }\n\n /**\n * Enables push notifications for the application.\n *\n * This method sets up the necessary infrastructure for handling push notifications by:\n * 1. Registering the service worker to listen for messages.\n * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.\n * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.\n *\n * @param UUIDs - An array of UUIDs to enable push notifications for.\n */\n public async enablePushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n // Handle creating new reg token (if available)\n try {\n const bearerToken = await this.#getAndAssertBearerToken().catch(\n () => null,\n );\n\n // If there is a bearer token, lets try to refresh/create new reg token\n if (bearerToken) {\n // Activate Push Notifications\n const fcmToken = await activatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n platform: this.#config.platform,\n }).catch(() => null);\n\n if (fcmToken) {\n this.#updatePushState({ type: 'enable', fcmToken });\n }\n }\n } catch {\n // Do nothing, we are silently failing\n }\n\n // New token created, (re)subscribe to push notifications\n try {\n await this.subscribeToPushNotifications();\n } catch {\n // Do nothing we are silently failing\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n /**\n * Disables push notifications for the application.\n * This removes the registration token on this device, and ensures we unsubscribe from any listeners\n */\n public async disablePushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n // Send a request to the server to unregister the token/device\n await deactivatePushNotifications({\n env: this.#env,\n deleteRegToken: this.#config.pushService.deleteRegToken,\n regToken: this.state.fcmToken,\n });\n } catch (error) {\n const errorMessage = `Failed to disable push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n } finally {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n this.#updatePushState({ type: 'disable' });\n }\n\n /**\n * Updates the triggers for push notifications.\n * This method is responsible for updating the server with the new set of UUIDs that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param UUIDs - An array of UUIDs that should trigger push notifications.\n */\n public async updateTriggerPushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n const bearerToken = await this.#getAndAssertBearerToken();\n const { fcmToken } = await updateTriggerPushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n deleteRegToken: this.#config.pushService.deleteRegToken,\n platform: this.#config.platform,\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"]}