Skip to content

Commit

Permalink
Merge branch 'main' into NOTIFY-847-profile-sync-controller-add-snap-…
Browse files Browse the repository at this point in the history
…caching
  • Loading branch information
Prithpal-Sooriya authored Jul 18, 2024
2 parents 3dee719 + 4bc736a commit 7d8420c
Show file tree
Hide file tree
Showing 38 changed files with 138 additions and 162 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metamask/core-monorepo",
"version": "176.0.0",
"version": "177.0.0",
"private": true,
"description": "Monorepo for packages shared between MetaMask clients",
"repository": {
Expand Down Expand Up @@ -58,7 +58,7 @@
"@metamask/eth-block-tracker": "^9.0.3",
"@metamask/eth-json-rpc-provider": "^4.1.1",
"@metamask/json-rpc-engine": "^9.0.1",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"@types/jest": "^27.4.1",
"@types/node": "^16.18.54",
"@typescript-eslint/eslint-plugin": "^5.62.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/accounts-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@metamask/keyring-controller": "^17.1.1",
"@metamask/snaps-sdk": "^4.2.0",
"@metamask/snaps-utils": "^7.4.0",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"deepmerge": "^4.2.2",
"ethereum-cryptography": "^2.1.2",
"immer": "^9.0.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/address-book-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"dependencies": {
"@metamask/base-controller": "^6.0.1",
"@metamask/controller-utils": "^11.0.1",
"@metamask/utils": "^9.0.0"
"@metamask/utils": "^9.1.0"
},
"devDependencies": {
"@metamask/auto-changelog": "^3.4.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/approval-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"dependencies": {
"@metamask/base-controller": "^6.0.1",
"@metamask/rpc-errors": "^6.3.1",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"nanoid": "^3.1.31"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/assets-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@metamask/polling-controller": "^9.0.0",
"@metamask/preferences-controller": "^13.0.0",
"@metamask/rpc-errors": "^6.3.1",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"@types/bn.js": "^5.1.5",
"@types/uuid": "^8.3.0",
"async-mutex": "^0.5.0",
Expand Down
1 change: 1 addition & 0 deletions packages/assets-controllers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type {
TokenBalancesControllerGetStateAction,
TokenBalancesControllerEvents,
TokenBalancesControllerStateChangeEvent,
TokenBalancesControllerState,
} from './TokenBalancesController';
export { TokenBalancesController } from './TokenBalancesController';
export type {
Expand Down
2 changes: 1 addition & 1 deletion packages/base-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"immer": "^9.0.6"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/build-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"@types/eslint": "^8.44.7"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/chain-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@metamask/snaps-controllers": "^8.1.1",
"@metamask/snaps-sdk": "^4.2.0",
"@metamask/snaps-utils": "^7.4.0",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"uuid": "^8.3.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/controller-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@ethereumjs/util": "^8.1.0",
"@metamask/eth-query": "^4.0.0",
"@metamask/ethjs-unit": "^0.3.0",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"@spruceid/siwe-parser": "2.1.0",
"@types/bn.js": "^5.1.5",
"bn.js": "^5.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ens-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@ethersproject/providers": "^5.7.0",
"@metamask/base-controller": "^6.0.1",
"@metamask/controller-utils": "^11.0.1",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"punycode": "^2.1.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/eth-json-rpc-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@metamask/json-rpc-engine": "^9.0.1",
"@metamask/rpc-errors": "^6.3.1",
"@metamask/safe-event-emitter": "^3.0.0",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"uuid": "^8.3.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/gas-fee-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@metamask/ethjs-unit": "^0.3.0",
"@metamask/network-controller": "^20.0.0",
"@metamask/polling-controller": "^9.0.0",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"@types/bn.js": "^5.1.5",
"@types/uuid": "^8.3.0",
"bn.js": "^5.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/json-rpc-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"dependencies": {
"@metamask/rpc-errors": "^6.3.1",
"@metamask/safe-event-emitter": "^3.0.0",
"@metamask/utils": "^9.0.0"
"@metamask/utils": "^9.1.0"
},
"devDependencies": {
"@lavamoat/allow-scripts": "^3.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/json-rpc-middleware-stream/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"dependencies": {
"@metamask/json-rpc-engine": "^9.0.1",
"@metamask/safe-event-emitter": "^3.0.0",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"readable-stream": "^3.6.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@metamask/eth-simple-keyring": "^6.0.1",
"@metamask/keyring-api": "^8.0.0",
"@metamask/message-manager": "^10.0.1",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"async-mutex": "^0.5.0",
"ethereumjs-wallet": "^1.0.1",
"immer": "^9.0.6"
Expand Down
2 changes: 1 addition & 1 deletion packages/message-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@metamask/base-controller": "^6.0.1",
"@metamask/controller-utils": "^11.0.1",
"@metamask/eth-sig-util": "^7.0.1",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"@types/uuid": "^8.3.0",
"jsonschema": "^1.2.4",
"uuid": "^8.3.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/name-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"dependencies": {
"@metamask/base-controller": "^6.0.1",
"@metamask/controller-utils": "^11.0.1",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"async-mutex": "^0.5.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/network-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@metamask/json-rpc-engine": "^9.0.1",
"@metamask/rpc-errors": "^6.3.1",
"@metamask/swappable-obj-proxy": "^2.2.0",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"async-mutex": "^0.5.0",
"immer": "^9.0.6",
"loglevel": "^1.8.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/notification-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"dependencies": {
"@metamask/base-controller": "^6.0.1",
"@metamask/utils": "^9.0.0",
"@metamask/utils": "^9.1.0",
"nanoid": "^3.1.31"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export function createMockNotificationEthSent(): OnChainRawNotification {
chain_id: 1,
block_number: 17485840,
block_timestamp: '2022-03-01T00:00:00Z',
tx_hash: '0x881D40237659C251811CEC9c364ef91dC08D300C',
tx_hash:
'0xb2256b183f2fb3872f99294ab55fb03e6a479b0d4aca556a3b27568b712505a6',
unread: true,
created_at: '2022-03-01T00:00:00Z',
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
Expand Down Expand Up @@ -48,7 +49,8 @@ export function createMockNotificationEthReceived(): OnChainRawNotification {
chain_id: 1,
block_number: 17485840,
block_timestamp: '2022-03-01T00:00:00Z',
tx_hash: '0x881D40237659C251811CEC9c364ef91dC08D300C',
tx_hash:
'0xb2256b183f2fb3872f99294ab55fb03e6a479b0d4aca556a3b27568b712505a6',
unread: true,
created_at: '2022-03-01T00:00:00Z',
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
Expand Down Expand Up @@ -82,7 +84,8 @@ export function createMockNotificationERC20Sent(): OnChainRawNotification {
chain_id: 1,
block_number: 17485840,
block_timestamp: '2022-03-01T00:00:00Z',
tx_hash: '0x881D40237659C251811CEC9c364ef91dC08D300C',
tx_hash:
'0xb2256b183f2fb3872f99294ab55fb03e6a479b0d4aca556a3b27568b712505a6',
unread: true,
created_at: '2022-03-01T00:00:00Z',
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
Expand Down Expand Up @@ -122,7 +125,8 @@ export function createMockNotificationERC20Received(): OnChainRawNotification {
chain_id: 1,
block_number: 17485840,
block_timestamp: '2022-03-01T00:00:00Z',
tx_hash: '0x881D40237659C251811CEC9c364ef91dC08D300C',
tx_hash:
'0xb2256b183f2fb3872f99294ab55fb03e6a479b0d4aca556a3b27568b712505a6',
unread: true,
created_at: '2022-03-01T00:00:00Z',
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { mockFetchFeatureAnnouncementNotifications } from '../__fixtures__/mockS
import { TRIGGER_TYPES } from '../constants/notification-schema';
import { getFeatureAnnouncementNotifications } from './feature-announcements';

// Mocked type for testing, allows overwriting TS to test erroneous values
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type MockedType = any;

jest.mock('@contentful/rich-text-html-renderer', () => ({
documentToHtmlString: jest
.fn()
Expand All @@ -20,6 +24,27 @@ describe('Feature Announcement Notifications', () => {
jest.clearAllMocks();
});

it('should return an empty array if invalid environment provided', async () => {
mockFetchFeatureAnnouncementNotifications();

const assertEnvEmpty = async (
override: Partial<typeof featureAnnouncementsEnv>,
) => {
const result = await getFeatureAnnouncementNotifications({
...featureAnnouncementsEnv,
...override,
});
expect(result).toHaveLength(0);
};

await assertEnvEmpty({ accessToken: null as MockedType });
await assertEnvEmpty({ platform: null as MockedType });
await assertEnvEmpty({ spaceId: null as MockedType });
await assertEnvEmpty({ accessToken: '' });
await assertEnvEmpty({ platform: '' });
await assertEnvEmpty({ spaceId: '' });
});

it('should return an empty array if fetch fails', async () => {
const mockEndpoint = mockFetchFeatureAnnouncementNotifications({
status: 500,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { documentToHtmlString } from '@contentful/rich-text-html-renderer';
import type { Entry, Asset } from 'contentful';
import log from 'loglevel';
import type { Entry, Asset, EntryCollection } from 'contentful';

import { TRIGGER_TYPES } from '../constants/notification-schema';
import { processFeatureAnnouncement } from '../processors/process-feature-announcement';
Expand Down Expand Up @@ -37,53 +36,30 @@ export type ContentfulResult = {
items?: TypeFeatureAnnouncement[];
};

const fetchFromContentful = async (
url: string,
retries = 3,
retryDelay = 1000,
): Promise<ContentfulResult | null> => {
let lastError: Error | null = null;

for (let i = 0; i < retries; i++) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Fetch failed with status: ${response.status}`);
}
return await response.json();
} catch (error) {
if (error instanceof Error) {
lastError = error;
}
if (i < retries - 1) {
await new Promise((resolve) => setTimeout(resolve, retryDelay));
}
}
}

log.error(
`Error fetching from Contentful after ${retries} retries:`,
lastError,
);
return null;
};
const getFeatureAnnouncementUrl = (env: Env) =>
FEATURE_ANNOUNCEMENT_URL.replace(DEFAULT_SPACE_ID, env.spaceId)
.replace(DEFAULT_ACCESS_TOKEN, env.accessToken)
.replace(DEFAULT_CLIENT_ID, env.platform);

const fetchFeatureAnnouncementNotifications = async (
env: Env,
): Promise<FeatureAnnouncementRawNotification[]> => {
const url = FEATURE_ANNOUNCEMENT_URL.replace(DEFAULT_SPACE_ID, env.spaceId)
.replace(DEFAULT_ACCESS_TOKEN, env.accessToken)
.replace(DEFAULT_CLIENT_ID, env.platform);
const data = await fetchFromContentful(url);
const url = getFeatureAnnouncementUrl(env);

const data = await fetch(url)
.then((r) => r.json())
.catch(() => null);

if (!data) {
return [];
}

const findIncludedItem = (sysId: string) => {
const typedData: EntryCollection<ImageFields | TypeExtensionLinkFields> =
data;
const item =
data?.includes?.Entry?.find((i: Entry) => i?.sys?.id === sysId) ||
data?.includes?.Asset?.find((i: Asset) => i?.sys?.id === sysId);
typedData?.includes?.Entry?.find((i: Entry) => i?.sys?.id === sysId) ||
typedData?.includes?.Asset?.find((i: Asset) => i?.sys?.id === sysId);
return item ? item?.fields : null;
};

Expand All @@ -94,6 +70,7 @@ const fetchFeatureAnnouncementNotifications = async (
const imageFields = fields.image
? (findIncludedItem(fields.image.sys.id) as ImageFields['fields'])
: undefined;

const extensionLinkFields = fields.extensionLink
? (findIncludedItem(
fields.extensionLink.sys.id,
Expand Down Expand Up @@ -135,10 +112,14 @@ const fetchFeatureAnnouncementNotifications = async (
export async function getFeatureAnnouncementNotifications(
env: Env,
): Promise<INotification[]> {
const rawNotifications = await fetchFeatureAnnouncementNotifications(env);
const notifications = rawNotifications.map((notification) =>
processFeatureAnnouncement(notification),
);
if (env?.accessToken && env?.spaceId && env?.platform) {
const rawNotifications = await fetchFeatureAnnouncementNotifications(env);
const notifications = rawNotifications.map((notification) =>
processFeatureAnnouncement(notification),
);

return notifications;
}

return notifications;
return [];
}
Loading

0 comments on commit 7d8420c

Please sign in to comment.