@metamask-previews/notification-services-controller 26.0.1-preview-b63fbdb38 → 26.0.1-preview-e0510042f

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 (24) hide show
  1. package/CHANGELOG.md +0 -18
  2. package/dist/NotificationServicesController/NotificationServicesController.cjs +112 -137
  3. package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
  4. package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
  5. package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
  6. package/dist/NotificationServicesController/NotificationServicesController.mjs +113 -138
  7. package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
  8. package/dist/NotificationServicesController/mocks/mockResponses.cjs +1 -9
  9. package/dist/NotificationServicesController/mocks/mockResponses.cjs.map +1 -1
  10. package/dist/NotificationServicesController/mocks/mockResponses.d.cts +0 -1
  11. package/dist/NotificationServicesController/mocks/mockResponses.d.cts.map +1 -1
  12. package/dist/NotificationServicesController/mocks/mockResponses.d.mts +0 -1
  13. package/dist/NotificationServicesController/mocks/mockResponses.d.mts.map +1 -1
  14. package/dist/NotificationServicesController/mocks/mockResponses.mjs +1 -8
  15. package/dist/NotificationServicesController/mocks/mockResponses.mjs.map +1 -1
  16. package/dist/NotificationServicesController/services/api-notifications.cjs +4 -43
  17. package/dist/NotificationServicesController/services/api-notifications.cjs.map +1 -1
  18. package/dist/NotificationServicesController/services/api-notifications.d.cts +2 -23
  19. package/dist/NotificationServicesController/services/api-notifications.d.cts.map +1 -1
  20. package/dist/NotificationServicesController/services/api-notifications.d.mts +2 -23
  21. package/dist/NotificationServicesController/services/api-notifications.d.mts.map +1 -1
  22. package/dist/NotificationServicesController/services/api-notifications.mjs +3 -40
  23. package/dist/NotificationServicesController/services/api-notifications.mjs.map +1 -1
  24. package/package.json +1 -1
@@ -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 _NotificationServicesController_instances, _NotificationServicesController_keyringController, _NotificationServicesController_auth, _NotificationServicesController_pushNotifications, _NotificationServicesController_accounts, _NotificationServicesController_locale, _NotificationServicesController_featureAnnouncementEnv, _NotificationServicesController_env, _NotificationServicesController_clearLoadingStates, _NotificationServicesController_assertAuthEnabled, _NotificationServicesController_enableAuth, _NotificationServicesController_getBearerToken, _NotificationServicesController_isWalletActivityPushEnabled, _NotificationServicesController_registerPushNotifications, _NotificationServicesController_setIsUpdatingMetamaskNotifications, _NotificationServicesController_setIsFetchingMetamaskNotifications, _NotificationServicesController_setIsCheckingAccountsPresence, _NotificationServicesController_updateUpdatingAccountsState, _NotificationServicesController_clearUpdatingAccountsState;
12
+ var _NotificationServicesController_instances, _NotificationServicesController_keyringController, _NotificationServicesController_auth, _NotificationServicesController_pushNotifications, _NotificationServicesController_accounts, _NotificationServicesController_locale, _NotificationServicesController_featureAnnouncementEnv, _NotificationServicesController_env, _NotificationServicesController_clearLoadingStates, _NotificationServicesController_assertAuthEnabled, _NotificationServicesController_enableAuth, _NotificationServicesController_getBearerToken, _NotificationServicesController_registerWalletActivityAddresses, _NotificationServicesController_setIsUpdatingMetamaskNotifications, _NotificationServicesController_setIsFetchingMetamaskNotifications, _NotificationServicesController_setIsCheckingAccountsPresence, _NotificationServicesController_updateUpdatingAccountsState, _NotificationServicesController_clearUpdatingAccountsState;
13
13
  function $importDefault(module) {
14
14
  if (module?.__esModule) {
15
15
  return module.default;
@@ -26,7 +26,7 @@ import $log from "loglevel";
26
26
  const log = $importDefault($log);
27
27
  import { TRIGGER_TYPES } from "./constants/notification-schema.mjs";
28
28
  import { processAndFilterNotifications, safeProcessNotification } from "./processors/process-notifications.mjs";
29
- import { getAPINotifications, getNotificationsApiConfigCached, markNotificationsAsRead, updateOnChainNotifications } from "./services/api-notifications.mjs";
29
+ import { getAPINotifications, getNotificationsApiConfigCached, markNotificationsAsRead } from "./services/api-notifications.mjs";
30
30
  import { getFeatureAnnouncementNotifications } from "./services/feature-announcements.mjs";
31
31
  import { createPerpOrderNotification } from "./services/perp-notifications.mjs";
32
32
  // Unique name for the controller
@@ -128,48 +128,58 @@ export const DEFAULT_SOCIAL_AI_PREFERENCES = {
128
128
  mutedTraderProfileIds: [],
129
129
  };
130
130
  export { DEFAULT_AGENTIC_CLI_PREFERENCES, DEFAULT_PRICE_ALERT_PREFERENCES } from "@metamask/authenticated-user-storage";
131
- const filterEnabledAccounts = (triggerConfig) => triggerConfig
132
- .filter((addressConfig) => Boolean(addressConfig.enabled))
133
- .map((addressConfig) => addressConfig.address);
134
131
  /**
135
- * Returns the subset of `accounts` that has a wallet-activity subscription in
136
- * the Trigger API, which is the source of truth for the per-address enabled bit.
132
+ * Builds wallet-activity preferences from the keyring's current accounts.
137
133
  *
138
- * The address universe is always supplied by the caller from the keyring, so the
139
- * result cannot contain an address this installation does not hold.
134
+ * @param accounts - The keyring accounts to build wallet-activity entries for.
135
+ * @returns An array of wallet-activity account entries (lower-cased addresses).
136
+ */
137
+ const buildWalletActivityAccounts = (accounts) => accounts.map(({ address, enabled }) => {
138
+ const lowercased = address.toLowerCase();
139
+ return {
140
+ address: lowercased,
141
+ enabled,
142
+ };
143
+ });
144
+ /**
145
+ * Builds wallet-activity initialization from the Trigger API. If Trigger has no
146
+ * enabled entries for the current keyring accounts, this is a first-time
147
+ * notification setup and all current accounts should start enabled.
140
148
  *
141
- * @param bearerToken - JWT used to query the Trigger API.
142
- * @param accounts - The keyring accounts to check.
149
+ * @param bearerToken - JWT used to query Trigger API.
150
+ * @param accounts - The keyring accounts to initialize.
143
151
  * @param env - The environment to use for the Trigger API call.
144
- * @returns The enabled addresses, as returned by the Trigger API, or `null` if
145
- * the Trigger API could not be read.
152
+ * @returns Wallet-activity account initialization entries.
146
153
  */
147
- const getEnabledAccounts = async (bearerToken, accounts, env) => {
154
+ const buildWalletActivityAccountsFromTriggerConfig = async (bearerToken, accounts, env) => {
148
155
  const triggerConfig = await getNotificationsApiConfigCached(bearerToken, accounts, env);
149
- if (triggerConfig === null) {
150
- return null;
151
- }
152
- return filterEnabledAccounts(triggerConfig);
156
+ const triggerConfigByAddress = new Map(triggerConfig.map(({ address, enabled }) => [
157
+ address.toLowerCase(),
158
+ enabled,
159
+ ]));
160
+ const hasEnabledTriggerAccount = accounts.some((address) => triggerConfigByAddress.get(address.toLowerCase()) === true);
161
+ return accounts.map((address) => ({
162
+ address,
163
+ enabled: hasEnabledTriggerAccount
164
+ ? (triggerConfigByAddress.get(address.toLowerCase()) ?? false)
165
+ : true,
166
+ }));
153
167
  };
154
168
  /**
155
169
  * Builds a fresh `NotificationPreferences` blob using hardcoded defaults for
156
- * Perps, Social AI, and Agentic CLI and the user's marketing/product-announcement
157
- * flags.
158
- *
159
- * `walletActivity.accounts` is deliberately left empty. Addresses are scoped to a
160
- * keyring, but this blob is keyed by canonical profile ID, which pairing shares
161
- * across every SRP belonging to the same user — so storing them here pools the
162
- * addresses of unrelated SRPs. Subscriptions live in the Trigger API instead.
170
+ * Perps, Social AI, and Agentic CLI, the supplied wallet-activity accounts and
171
+ * the user's marketing/product-announcement flags.
163
172
  *
173
+ * @param walletActivityAccounts - The wallet-activity account config to initialize.
164
174
  * @param hasMarketingConsent - Whether marketing push notifications should be enabled.
165
175
  * @param productAnnouncementEnabled - Whether marketing in-app notifications should be enabled.
166
176
  * @returns A complete `NotificationPreferences` object.
167
177
  */
168
- const buildFreshPreferences = (hasMarketingConsent, productAnnouncementEnabled) => ({
178
+ const buildFreshPreferences = (walletActivityAccounts, hasMarketingConsent, productAnnouncementEnabled) => ({
169
179
  walletActivity: {
170
180
  inAppNotificationsEnabled: true,
171
181
  pushNotificationsEnabled: true,
172
- accounts: [],
182
+ accounts: buildWalletActivityAccounts(walletActivityAccounts),
173
183
  },
174
184
  marketing: {
175
185
  inAppNotificationsEnabled: productAnnouncementEnabled,
@@ -471,20 +481,13 @@ export class NotificationServicesController extends BaseController {
471
481
  */
472
482
  async enablePushNotifications() {
473
483
  try {
474
- if (!(await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_isWalletActivityPushEnabled).call(this))) {
475
- await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").disablePushNotifications();
476
- return;
477
- }
478
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
479
- const { accounts } = __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
480
- const enabledAddresses = await getEnabledAccounts(bearerToken, accounts, __classPrivateFieldGet(this, _NotificationServicesController_env, "f"));
481
- if (enabledAddresses === null) {
482
- // "No addresses" unregisters the device, so an unreadable subscription
483
- // list must not be treated as an empty one: leave the existing links
484
- // alone until we can read the real state.
485
- return;
484
+ const preferences = await this.messenger.call('AuthenticatedUserStorageService:getNotificationPreferences');
485
+ const enabledAddresses = (preferences?.walletActivity.accounts ?? [])
486
+ .filter((account) => account.enabled)
487
+ .map((account) => account.address);
488
+ if (enabledAddresses.length > 0) {
489
+ await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").enablePushNotifications(enabledAddresses);
486
490
  }
487
- await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_registerPushNotifications).call(this, enabledAddresses);
488
491
  }
489
492
  catch {
490
493
  // Do nothing, failing silently.
@@ -499,16 +502,10 @@ export class NotificationServicesController extends BaseController {
499
502
  async checkAccountsPresence(accounts) {
500
503
  try {
501
504
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_setIsCheckingAccountsPresence).call(this, true);
502
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
503
- const triggerConfig = await getNotificationsApiConfigCached(bearerToken, accounts, __classPrivateFieldGet(this, _NotificationServicesController_env, "f"));
504
- if (triggerConfig === null) {
505
- // Reporting every account as disabled would misrepresent the user's
506
- // settings, so surface the failure instead.
507
- throw new Error('Failed to read wallet-activity subscriptions');
508
- }
509
- const enabledByAddress = new Map(triggerConfig.map((addressConfig) => [
510
- addressConfig.address.toLowerCase(),
511
- Boolean(addressConfig.enabled),
505
+ const preferences = await this.messenger.call('AuthenticatedUserStorageService:getNotificationPreferences');
506
+ const enabledByAddress = new Map((preferences?.walletActivity.accounts ?? []).map((account) => [
507
+ account.address.toLowerCase(),
508
+ account.enabled,
512
509
  ]));
513
510
  const result = {};
514
511
  for (const address of accounts) {
@@ -567,54 +564,27 @@ export class NotificationServicesController extends BaseController {
567
564
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_setIsUpdatingMetamaskNotifications).call(this, true);
568
565
  const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
569
566
  const { accounts } = __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
570
- // 1. Read existing AUS notification preferences. A missing blob is
571
- // `isMissingAUSConfig` and is initialized in step 3. It is not, on its
572
- // own, first-time subscribe — that also needs an empty Trigger API list
573
- // (step 2).
567
+ // 1. Read existing AUS notification preferences and initialize only if absent.
574
568
  const preferences = await this.messenger.call('AuthenticatedUserStorageService:getNotificationPreferences');
575
569
  const hasMarketingConsent = Boolean(opts?.hasMarketingConsent);
576
570
  const productAnnouncementEnabled = Boolean(opts?.productAnnouncementEnabled);
577
- const isMissingAUSConfig = preferences === null;
578
- const isPushEnabled = preferences?.walletActivity.pushNotificationsEnabled ?? true;
579
- // 2. Subscribe the keyring's accounts only on first-time setup: no AUS
580
- // blob and the Trigger API returned an empty list.
581
- //
582
- // This method also runs on the daily re-subscribe. An existing AUS blob
583
- // already rules that path out, including when the user has turned every
584
- // account off — so we do not treat "no enabled addresses" as "never set
585
- // up".
586
- //
587
- // An empty Trigger API list (not a list of disabled addresses) is what
588
- // distinguishes "never subscribed" from "subscribed and then disabled".
589
- // A user upgrading from a client that never wrote a preferences blob
590
- // therefore keeps whichever accounts they had already disabled.
591
- const notificationsApiPreferences = await getNotificationsApiConfigCached(bearerToken, accounts, __classPrivateFieldGet(this, _NotificationServicesController_env, "f"));
592
- if (notificationsApiPreferences === null) {
593
- // An unreadable subscription list is not an empty one. Subscribing
594
- // every account here would re-enable ones the user had turned off, and
595
- // registering push for that guessed list would send activity for them,
596
- // so fail and let the caller retry.
597
- throw new Error('Failed to read wallet-activity subscriptions');
598
- }
599
- let accountsWithWalletActivityNotificationsEnabled = filterEnabledAccounts(notificationsApiPreferences);
600
- const isFirstTimeSetup = isMissingAUSConfig && notificationsApiPreferences.length === 0;
601
- if (isFirstTimeSetup) {
602
- await updateOnChainNotifications(bearerToken, accounts.map((address) => ({ address, enabled: true })), __classPrivateFieldGet(this, _NotificationServicesController_env, "f"));
603
- // Match the lower-case form the Trigger API echoes back, which is what
604
- // every other path feeding the push API uses.
605
- accountsWithWalletActivityNotificationsEnabled = accounts.map((address) => address.toLowerCase());
571
+ let nextPreferences;
572
+ if (preferences === null) {
573
+ const walletActivityAccounts = await buildWalletActivityAccountsFromTriggerConfig(bearerToken, accounts, __classPrivateFieldGet(this, _NotificationServicesController_env, "f"));
574
+ nextPreferences = buildFreshPreferences(walletActivityAccounts, hasMarketingConsent, productAnnouncementEnabled);
606
575
  }
607
- // 3. Initialize the preferences blob, only once the subscriptions above
608
- // are in place. Its existence is what makes the next run a re-subscribe
609
- // rather than a first-time setup, so writing it earlier would let a
610
- // failed subscribe leave the user enabled with no accounts subscribed
611
- // and no second chance to seed them.
612
- if (isMissingAUSConfig) {
613
- await this.messenger.call('AuthenticatedUserStorageService:putNotificationPreferences', buildFreshPreferences(hasMarketingConsent, productAnnouncementEnabled), __classPrivateFieldGet(this, _NotificationServicesController_featureAnnouncementEnv, "f").platform);
576
+ if (nextPreferences) {
577
+ await this.messenger.call('AuthenticatedUserStorageService:putNotificationPreferences', nextPreferences, __classPrivateFieldGet(this, _NotificationServicesController_featureAnnouncementEnv, "f").platform);
614
578
  }
579
+ const effectivePreferences = nextPreferences ?? preferences;
580
+ const accountsWithNotifications = (effectivePreferences?.walletActivity.accounts ?? [])
581
+ .filter((account) => account.enabled)
582
+ .map((account) => account.address);
615
583
  if (opts.registerPushNotifications ?? true) {
616
584
  // Attempt FCM/device registration only; clients must request OS permission separately.
617
- __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_registerPushNotifications).call(this, isPushEnabled ? accountsWithWalletActivityNotificationsEnabled : []).catch(() => {
585
+ __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f")
586
+ .enablePushNotifications(accountsWithNotifications)
587
+ .catch(() => {
618
588
  // Do Nothing
619
589
  });
620
590
  }
@@ -707,8 +677,9 @@ export class NotificationServicesController extends BaseController {
707
677
  async disableAccounts(accounts) {
708
678
  try {
709
679
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_updateUpdatingAccountsState).call(this, accounts);
710
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
711
- await updateOnChainNotifications(bearerToken, accounts.map((address) => ({ address, enabled: false })), __classPrivateFieldGet(this, _NotificationServicesController_env, "f"));
680
+ // Sign-in gate.
681
+ await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
682
+ await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_registerWalletActivityAddresses).call(this, accounts.map((address) => ({ address, enabled: false })));
712
683
  await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").deletePushNotificationLinks(accounts);
713
684
  }
714
685
  catch {
@@ -736,8 +707,9 @@ export class NotificationServicesController extends BaseController {
736
707
  async enableAccounts(accounts) {
737
708
  try {
738
709
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_updateUpdatingAccountsState).call(this, accounts);
739
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
740
- await updateOnChainNotifications(bearerToken, accounts.map((address) => ({ address, enabled: true })), __classPrivateFieldGet(this, _NotificationServicesController_env, "f"));
710
+ // Sign-in gate.
711
+ await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
712
+ await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_registerWalletActivityAddresses).call(this, accounts.map((address) => ({ address, enabled: true })));
741
713
  await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").addPushNotificationLinks(accounts);
742
714
  }
743
715
  catch (error) {
@@ -776,20 +748,14 @@ export class NotificationServicesController extends BaseController {
776
748
  : [];
777
749
  // Raw On Chain Notifications
778
750
  const rawOnChainNotifications = [];
779
- const isWalletActivityInAppEnabled = notificationPreferences?.walletActivity.inAppNotificationsEnabled ??
780
- true;
781
- if (isGlobalNotifsEnabled && isWalletActivityInAppEnabled) {
751
+ if (isGlobalNotifsEnabled) {
782
752
  try {
783
753
  const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
784
- // Addresses come from the keyring, so this installation can only ever
785
- // ask for activity on accounts it actually holds. The Trigger API
786
- // narrows that to the ones the user has enabled.
787
- const { accounts } = __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
788
- const addressesWithNotifications = await getEnabledAccounts(bearerToken, accounts, __classPrivateFieldGet(this, _NotificationServicesController_env, "f"));
789
- if (addressesWithNotifications !== null) {
790
- const notifications = await getAPINotifications(bearerToken, addressesWithNotifications, __classPrivateFieldGet(this, _NotificationServicesController_locale, "f").call(this), __classPrivateFieldGet(this, _NotificationServicesController_featureAnnouncementEnv, "f").platform, __classPrivateFieldGet(this, _NotificationServicesController_env, "f")).catch(() => []);
791
- rawOnChainNotifications.push(...notifications);
792
- }
754
+ const addressesWithNotifications = (notificationPreferences?.walletActivity.accounts ?? [])
755
+ .filter((account) => account.enabled)
756
+ .map((account) => account.address);
757
+ const notifications = await getAPINotifications(bearerToken, addressesWithNotifications, __classPrivateFieldGet(this, _NotificationServicesController_locale, "f").call(this), __classPrivateFieldGet(this, _NotificationServicesController_featureAnnouncementEnv, "f").platform, __classPrivateFieldGet(this, _NotificationServicesController_env, "f")).catch(() => []);
758
+ rawOnChainNotifications.push(...notifications);
793
759
  }
794
760
  catch {
795
761
  // Do nothing
@@ -1012,44 +978,53 @@ _NotificationServicesController_keyringController = new WeakMap(), _Notification
1012
978
  throw new Error('Missing BearerToken');
1013
979
  }
1014
980
  return { bearerToken };
1015
- }, _NotificationServicesController_isWalletActivityPushEnabled =
981
+ }, _NotificationServicesController_registerWalletActivityAddresses =
1016
982
  /**
1017
- * Reads the global wallet-activity push toggle from
1018
- * {@link AuthenticatedUserStorageService}.
1019
- *
1020
- * Only the channel boolean is read. It is a user-level setting that contains
1021
- * no addresses, so sharing it across a paired profile is correct. The address
1022
- * list in the same blob is deliberately ignored — see
1023
- * {@link buildFreshPreferences}.
983
+ * Updates the `walletActivity.accounts` entries in the user's
984
+ * notification-preferences blob in {@link AuthenticatedUserStorageService}.
1024
985
  *
1025
- * A missing or unreadable blob counts as enabled, matching the API's "every
1026
- * toggle is on unless stated otherwise" default, so that a storage outage does
1027
- * not silently stop notifications.
986
+ * `putNotificationPreferences` replaces the entire blob, so we read the
987
+ * current preferences, merge the supplied updates into
988
+ * `walletActivity.accounts`, and write the result back. This helper is only
989
+ * meant to be used for incremental updates (enable/disable individual
990
+ * accounts) after the preferences blob has already been initialized via
991
+ * {@link createOnChainTriggers}; callers should not rely on it to perform
992
+ * first-time initialization.
1028
993
  *
1029
- * @returns Whether wallet-activity push notifications are enabled.
994
+ * @param updates - Addresses to register, each with the desired `enabled` flag
1030
995
  */
1031
- async function _NotificationServicesController_isWalletActivityPushEnabled() {
1032
- const preferences = await this.messenger
996
+ async function _NotificationServicesController_registerWalletActivityAddresses(updates) {
997
+ if (updates.length === 0) {
998
+ return;
999
+ }
1000
+ const currentPreferences = await this.messenger
1033
1001
  .call('AuthenticatedUserStorageService:getNotificationPreferences')
1034
- .catch(() => null);
1035
- return preferences?.walletActivity.pushNotificationsEnabled ?? true;
1036
- }, _NotificationServicesController_registerPushNotifications =
1037
- /**
1038
- * Registers this device for push notifications on the given addresses.
1039
- *
1040
- * An empty list cannot be sent: the push API rejects a registration with no
1041
- * addresses, and because that request is what performs the delete-and-reinsert
1042
- * of the device's links, a rejection leaves the previous links in place and
1043
- * push keeps arriving. So "no addresses" has to mean unregistering the device.
1044
- *
1045
- * @param addresses - The addresses to receive push notifications for.
1046
- */
1047
- async function _NotificationServicesController_registerPushNotifications(addresses) {
1048
- if (addresses.length === 0) {
1049
- await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").disablePushNotifications();
1002
+ .catch((error) => {
1003
+ log.error('Failed to get notification preferences. Re-initializing them instead.', error);
1004
+ // TODO: return null once validation error captured
1005
+ // return null;
1006
+ throw error;
1007
+ });
1008
+ if (!currentPreferences) {
1009
+ log.warn('Preferences blob not yet initialized; run `createOnChainTriggers` first.');
1050
1010
  return;
1051
1011
  }
1052
- await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").enablePushNotifications(addresses);
1012
+ const accountsByAddress = new Map(currentPreferences.walletActivity.accounts.map((account) => [
1013
+ account.address.toLowerCase(),
1014
+ { ...account, address: account.address.toLowerCase() },
1015
+ ]));
1016
+ for (const update of updates) {
1017
+ const address = update.address.toLowerCase();
1018
+ accountsByAddress.set(address, { address, enabled: update.enabled });
1019
+ }
1020
+ const nextPreferences = {
1021
+ ...currentPreferences,
1022
+ walletActivity: {
1023
+ ...currentPreferences.walletActivity,
1024
+ accounts: [...accountsByAddress.values()],
1025
+ },
1026
+ };
1027
+ await this.messenger.call('AuthenticatedUserStorageService:putNotificationPreferences', nextPreferences, __classPrivateFieldGet(this, _NotificationServicesController_featureAnnouncementEnv, "f").platform);
1053
1028
  }, _NotificationServicesController_setIsUpdatingMetamaskNotifications = function _NotificationServicesController_setIsUpdatingMetamaskNotifications(isUpdatingMetamaskNotifications) {
1054
1029
  this.update((state) => {
1055
1030
  state.isUpdatingMetamaskNotifications = isUpdatingMetamaskNotifications;