-
-
Notifications
You must be signed in to change notification settings - Fork 256
feat: migration notification API from v2 to v3 #7102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3acea9b
952b49c
0f5d336
3b4c69e
86d1f2b
ba01621
38e9730
29ed5f1
3562e62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,19 +21,24 @@ import type { AuthenticationController } from '@metamask/profile-sync-controller | |
| import { assert } from '@metamask/utils'; | ||
| import log from 'loglevel'; | ||
|
|
||
| import type { NormalisedAPINotification } from '.'; | ||
| import { TRIGGER_TYPES } from './constants/notification-schema'; | ||
| import { | ||
| processAndFilterNotifications, | ||
| safeProcessNotification, | ||
| } from './processors/process-notifications'; | ||
| import * as FeatureNotifications from './services/feature-announcements'; | ||
| import * as OnChainNotifications from './services/onchain-notifications'; | ||
| import { | ||
| getAPINotifications, | ||
| getNotificationsApiConfigCached, | ||
| markNotificationsAsRead, | ||
| updateOnChainNotifications, | ||
| } from './services/api-notifications'; | ||
| import { getFeatureAnnouncementNotifications } from './services/feature-announcements'; | ||
| import { createPerpOrderNotification } from './services/perp-notifications'; | ||
| import type { | ||
| INotification, | ||
| MarkAsReadNotificationsParam, | ||
| } from './types/notification/notification'; | ||
| import type { OnChainRawNotification } from './types/on-chain-notification/on-chain-notification'; | ||
| import type { OrderInput } from './types/perps'; | ||
| import type { | ||
| NotificationServicesPushControllerEnablePushNotificationsAction, | ||
|
|
@@ -500,6 +505,8 @@ export default class NotificationServicesController extends BaseController< | |
| }, | ||
| }; | ||
|
|
||
| readonly #locale: () => string; | ||
|
|
||
| readonly #featureAnnouncementEnv: FeatureAnnouncementEnv; | ||
|
|
||
| /** | ||
|
|
@@ -510,7 +517,7 @@ export default class NotificationServicesController extends BaseController< | |
| * @param args.state - Initial state to set on this controller. | ||
| * @param args.env - environment variables for a given controller. | ||
| * @param args.env.featureAnnouncements - env variables for feature announcements. | ||
| * @param args.env.isPushIntegrated - toggle push notifications on/off if client has integrated them. | ||
| * @param args.env.locale - users locale for better dynamic server notifications | ||
| */ | ||
| constructor({ | ||
| messenger, | ||
|
|
@@ -521,7 +528,7 @@ export default class NotificationServicesController extends BaseController< | |
| state?: Partial<NotificationServicesControllerState>; | ||
| env: { | ||
| featureAnnouncements: FeatureAnnouncementEnv; | ||
| isPushIntegrated?: boolean; | ||
| locale?: () => string; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We now pass in locale to handle server side in-app platform notifications |
||
| }; | ||
| }) { | ||
| super({ | ||
|
|
@@ -532,6 +539,7 @@ export default class NotificationServicesController extends BaseController< | |
| }); | ||
|
|
||
| this.#featureAnnouncementEnv = env.featureAnnouncements; | ||
| this.#locale = env.locale ?? (() => 'en'); | ||
| this.#registerMessageHandlers(); | ||
| this.#clearLoadingStates(); | ||
| } | ||
|
|
@@ -694,11 +702,10 @@ export default class NotificationServicesController extends BaseController< | |
| try { | ||
| const { bearerToken } = await this.#getBearerToken(); | ||
| const { accounts } = this.#accounts.listAccounts(); | ||
| const addressesWithNotifications = | ||
| await OnChainNotifications.getOnChainNotificationsConfigCached( | ||
| bearerToken, | ||
| accounts, | ||
| ); | ||
| const addressesWithNotifications = await getNotificationsApiConfigCached( | ||
| bearerToken, | ||
| accounts, | ||
| ); | ||
| const addresses = addressesWithNotifications | ||
| .filter((a) => Boolean(a.enabled)) | ||
| .map((a) => a.address); | ||
|
|
@@ -725,11 +732,10 @@ export default class NotificationServicesController extends BaseController< | |
|
|
||
| // Retrieve user storage | ||
| const { bearerToken } = await this.#getBearerToken(); | ||
| const addressesWithNotifications = | ||
| await OnChainNotifications.getOnChainNotificationsConfigCached( | ||
| bearerToken, | ||
| accounts, | ||
| ); | ||
| const addressesWithNotifications = await getNotificationsApiConfigCached( | ||
| bearerToken, | ||
| accounts, | ||
| ); | ||
|
|
||
| const result: Record<string, boolean> = {}; | ||
| addressesWithNotifications.forEach((a) => { | ||
|
|
@@ -788,11 +794,10 @@ export default class NotificationServicesController extends BaseController< | |
| const { accounts } = this.#accounts.listAccounts(); | ||
|
|
||
| // 1. See if has enabled notifications before | ||
| const addressesWithNotifications = | ||
| await OnChainNotifications.getOnChainNotificationsConfigCached( | ||
| bearerToken, | ||
| accounts, | ||
| ); | ||
| const addressesWithNotifications = await getNotificationsApiConfigCached( | ||
| bearerToken, | ||
| accounts, | ||
| ); | ||
|
|
||
| // Notifications API can return array with addresses set to false | ||
| // So assert that at least one address is enabled | ||
|
|
@@ -802,7 +807,7 @@ export default class NotificationServicesController extends BaseController< | |
|
|
||
| // 2. Enable Notifications (if no accounts subscribed or we are resetting) | ||
| if (accountsWithNotifications.length === 0 || opts?.resetNotifications) { | ||
| await OnChainNotifications.updateOnChainNotifications( | ||
| await updateOnChainNotifications( | ||
| bearerToken, | ||
| accounts.map((address) => ({ address, enabled: true })), | ||
| ); | ||
|
|
@@ -903,7 +908,7 @@ export default class NotificationServicesController extends BaseController< | |
| const { bearerToken } = await this.#getBearerToken(); | ||
|
|
||
| // Delete these UUIDs (Mutates User Storage) | ||
| await OnChainNotifications.updateOnChainNotifications( | ||
| await updateOnChainNotifications( | ||
| bearerToken, | ||
| accounts.map((address) => ({ address, enabled: false })), | ||
| ); | ||
|
|
@@ -935,7 +940,7 @@ export default class NotificationServicesController extends BaseController< | |
| this.#updateUpdatingAccountsState(accounts); | ||
|
|
||
| const { bearerToken } = await this.#getBearerToken(); | ||
| await OnChainNotifications.updateOnChainNotifications( | ||
| await updateOnChainNotifications( | ||
| bearerToken, | ||
| accounts.map((address) => ({ address, enabled: true })), | ||
| ); | ||
|
|
@@ -970,31 +975,29 @@ export default class NotificationServicesController extends BaseController< | |
| // Raw Feature Notifications | ||
| const rawAnnouncements = | ||
| isGlobalNotifsEnabled && this.state.isFeatureAnnouncementsEnabled | ||
| ? await FeatureNotifications.getFeatureAnnouncementNotifications( | ||
| ? await getFeatureAnnouncementNotifications( | ||
| this.#featureAnnouncementEnv, | ||
| previewToken, | ||
| ).catch(() => []) | ||
| : []; | ||
|
|
||
| // Raw On Chain Notifications | ||
| const rawOnChainNotifications: OnChainRawNotification[] = []; | ||
| const rawOnChainNotifications: NormalisedAPINotification[] = []; | ||
| if (isGlobalNotifsEnabled) { | ||
| try { | ||
| const { bearerToken } = await this.#getBearerToken(); | ||
| const { accounts } = this.#accounts.listAccounts(); | ||
| const addressesWithNotifications = ( | ||
| await OnChainNotifications.getOnChainNotificationsConfigCached( | ||
| bearerToken, | ||
| accounts, | ||
| ) | ||
| await getNotificationsApiConfigCached(bearerToken, accounts) | ||
| ) | ||
| .filter((a) => Boolean(a.enabled)) | ||
| .map((a) => a.address); | ||
| const notifications = | ||
| await OnChainNotifications.getOnChainNotifications( | ||
| bearerToken, | ||
| addressesWithNotifications, | ||
| ).catch(() => []); | ||
| const notifications = await getAPINotifications( | ||
| bearerToken, | ||
| addressesWithNotifications, | ||
| this.#locale(), | ||
| this.#featureAnnouncementEnv.platform, | ||
| ).catch(() => []); | ||
| rawOnChainNotifications.push(...notifications); | ||
| } catch { | ||
| // Do nothing | ||
|
|
@@ -1165,7 +1168,7 @@ export default class NotificationServicesController extends BaseController< | |
| onchainNotificationIds = onChainNotifications.map( | ||
| (notification) => notification.id, | ||
| ); | ||
| await OnChainNotifications.markNotificationsAsRead( | ||
| await markNotificationsAsRead( | ||
| bearerToken, | ||
| onchainNotificationIds, | ||
| ).catch(() => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,16 +15,11 @@ export enum TRIGGER_TYPES { | |
| ERC721_RECEIVED = 'erc721_received', | ||
| ERC1155_SENT = 'erc1155_sent', | ||
| ERC1155_RECEIVED = 'erc1155_received', | ||
| AAVE_V3_HEALTH_FACTOR = 'aave_v3_health_factor', | ||
| ENS_EXPIRATION = 'ens_expiration', | ||
| LIDO_STAKING_REWARDS = 'lido_staking_rewards', | ||
| ROCKETPOOL_STAKING_REWARDS = 'rocketpool_staking_rewards', | ||
| NOTIONAL_LOAN_EXPIRATION = 'notional_loan_expiration', | ||
| SPARK_FI_HEALTH_FACTOR = 'spark_fi_health_factor', | ||
| SNAP = 'snap', | ||
| PLATFORM = 'platform', | ||
| } | ||
|
|
||
| export const TRIGGER_TYPES_WALLET_SET: Set<string> = new Set([ | ||
| export const NOTIFICATION_API_TRIGGER_TYPES_SET: Set<string> = new Set([ | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update set and add new types supported by our API |
||
| TRIGGER_TYPES.METAMASK_SWAP_COMPLETED, | ||
| TRIGGER_TYPES.ERC20_SENT, | ||
| TRIGGER_TYPES.ERC20_RECEIVED, | ||
|
|
@@ -40,13 +35,8 @@ export const TRIGGER_TYPES_WALLET_SET: Set<string> = new Set([ | |
| TRIGGER_TYPES.ERC721_RECEIVED, | ||
| TRIGGER_TYPES.ERC1155_SENT, | ||
| TRIGGER_TYPES.ERC1155_RECEIVED, | ||
| ]) satisfies Set<Exclude<TRIGGER_TYPES, TRIGGER_TYPES.FEATURES_ANNOUNCEMENT>>; | ||
|
|
||
| export enum TRIGGER_TYPES_GROUPS { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. never used, removed |
||
| RECEIVED = 'received', | ||
| SENT = 'sent', | ||
| DEFI = 'defi', | ||
| } | ||
| TRIGGER_TYPES.PLATFORM, | ||
| ]); | ||
|
|
||
| export const NOTIFICATION_CHAINS_ID = { | ||
| ETHEREUM: '1', | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed mock util