Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions eslint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -1575,30 +1575,6 @@
"count": 1
}
},
"packages/notification-services-controller/src/NotificationServicesController/services/feature-announcements.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 2
},
"@typescript-eslint/naming-convention": {
"count": 2
},
"@typescript-eslint/prefer-nullish-coalescing": {
"count": 2
},
"id-length": {
"count": 3
}
},
"packages/notification-services-controller/src/NotificationServicesController/services/notification-config-cache.ts": {
"@typescript-eslint/naming-convention": {
"count": 1
}
},
"packages/notification-services-controller/src/NotificationServicesController/services/perp-notifications.test.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 1
}
},
"packages/notification-services-controller/src/NotificationServicesController/services/perp-notifications.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,25 @@ type Env = {
*/
export type ContentfulResult = {
includes?: {
// Property names match Contentful API response structure
// eslint-disable-next-line @typescript-eslint/naming-convention
Entry?: Entry[];
// eslint-disable-next-line @typescript-eslint/naming-convention
Asset?: Asset[];
};
items?: TypeFeatureAnnouncement[];
};

export const getFeatureAnnouncementUrl = (env: Env, previewToken?: string) => {
export const getFeatureAnnouncementUrl = (
env: Env,
previewToken?: string,
): string => {
const domain = previewToken ? PREVIEW_DOMAIN : DEFAULT_DOMAIN;
const replacedUrl = FEATURE_ANNOUNCEMENT_URL.replace(
DEFAULT_SPACE_ID,
env.spaceId,
)
.replace(DEFAULT_ACCESS_TOKEN, previewToken || env.accessToken)
.replace(DEFAULT_ACCESS_TOKEN, previewToken ?? env.accessToken)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Inconsistent empty string handling between domain and token

The change from || to ?? on line 58 creates an inconsistency with the truthiness check on line 53. If previewToken is an empty string, line 53's previewToken ? PREVIEW_DOMAIN : DEFAULT_DOMAIN evaluates to DEFAULT_DOMAIN (treating empty string as falsy), but line 58's previewToken ?? env.accessToken returns the empty string instead of falling back to env.accessToken. This could result in API calls to the production domain with an empty access token, causing authentication failures.

Additional Locations (1)

Fix in Cursor Fix in Web

.replace(DEFAULT_CLIENT_ID, env.platform)
.replace(DEFAULT_DOMAIN, domain);
return encodeURI(replacedUrl);
Expand All @@ -62,14 +68,22 @@ const fetchFeatureAnnouncementNotifications = async (
const url = getFeatureAnnouncementUrl(env, previewToken);

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

if (!data) {
return [];
}

const findIncludedItem = (sysId: string) => {
const findIncludedItem = (
sysId: string,
):
| ImageFields['fields']
| TypeExtensionLinkFields['fields']
| TypePortfolioLinkFields['fields']
| TypeMobileLinkFields['fields']
| TypeExternalLinkFields['fields']
| null => {
const typedData: EntryCollection<
| ImageFields
| TypeExtensionLinkFields
Expand All @@ -78,15 +92,19 @@ const fetchFeatureAnnouncementNotifications = async (
| TypeExternalLinkFields
> = data;
const item =
typedData?.includes?.Entry?.find((i: Entry) => i?.sys?.id === sysId) ||
typedData?.includes?.Asset?.find((i: Asset) => i?.sys?.id === sysId);
typedData?.includes?.Entry?.find(
(entry: Entry) => entry?.sys?.id === sysId,
) ??
typedData?.includes?.Asset?.find(
(asset: Asset) => asset?.sys?.id === sysId,
);
return item ? item?.fields : null;
};

const contentfulNotifications = data?.items ?? [];
const rawNotifications: FeatureAnnouncementRawNotification[] =
contentfulNotifications.map((n: TypeFeatureAnnouncement) => {
const { fields } = n;
contentfulNotifications.map((item: TypeFeatureAnnouncement) => {
const { fields } = item;
const imageFields = fields.image
? (findIncludedItem(fields.image.sys.id) as ImageFields['fields'])
: undefined;
Expand Down Expand Up @@ -114,7 +132,7 @@ const fetchFeatureAnnouncementNotifications = async (

const notification: FeatureAnnouncementRawNotification = {
type: TRIGGER_TYPES.FEATURES_ANNOUNCEMENT,
createdAt: new Date(n.sys.createdAt).toString(),
createdAt: new Date(item.sys.createdAt).toString(),
data: {
id: fields.id,
category: fields.category,
Expand Down Expand Up @@ -163,15 +181,17 @@ const fetchFeatureAnnouncementNotifications = async (
},
} as const;

const filteredRawNotifications = rawNotifications.filter((n) => {
const minVersion = n.data?.[versionKeys[env.platform].min];
const maxVersion = n.data?.[versionKeys[env.platform].max];
return isVersionInBounds({
currentVersion: env.platformVersion,
minVersion,
maxVersion,
});
});
const filteredRawNotifications = rawNotifications.filter(
(rawNotification) => {
const minVersion = rawNotification.data?.[versionKeys[env.platform].min];
const maxVersion = rawNotification.data?.[versionKeys[env.platform].max];
return isVersionInBounds({
currentVersion: env.platformVersion,
minVersion,
maxVersion,
});
},
);

return filteredRawNotifications;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export const NotificationConfigCacheTTL = 1000 * 60; // 60 seconds
export class OnChainNotificationsCache {
#cache: NotificationConfigCache | null = null;

readonly #TTL = NotificationConfigCacheTTL;
readonly #ttl = NotificationConfigCacheTTL;

#isExpired(): boolean {
return !this.#cache || Date.now() - this.#cache.timestamp > this.#TTL;
return !this.#cache || Date.now() - this.#cache.timestamp > this.#ttl;
}

#hasAllAddresses(addresses: string[]): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ describe('Perps Service - createPerpOrderNotification', () => {
jest.clearAllMocks();
});

const arrangeMocks = () => {
const arrangeMocks = (): {
consoleErrorSpy: jest.SpyInstance<void, Parameters<typeof console.error>>;
} => {
const consoleErrorSpy = jest
.spyOn(console, 'error')
.mockImplementation(jest.fn());
Expand Down
Loading