Skip to content

Commit

Permalink
more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Oct 18, 2024
1 parent 3cf79b3 commit e59eeb2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -550,14 +550,16 @@ describe('metamask-notifications - getNotificationsByType', () => {
env: { featureAnnouncements: featureAnnouncementsEnv },
});

const snapNotification = createMockSnapNotification();
const featureAnnouncement = createMockFeatureAnnouncementRaw();

await controller.updateMetamaskNotificationsList(
snapNotification as INotification,
const processedSnapNotification = processSnapNotification(
createMockSnapNotification(),
);
const processedFeatureAnnouncement = processFeatureAnnouncement(
createMockFeatureAnnouncementRaw(),
);

await controller.updateMetamaskNotificationsList(processedSnapNotification);
await controller.updateMetamaskNotificationsList(
featureAnnouncement as INotification,
processedFeatureAnnouncement,
);

expect(controller.state.metamaskNotificationsList).toHaveLength(2);
Expand Down Expand Up @@ -836,10 +838,10 @@ describe('metamask-notifications - updateMetamaskNotificationsList', () => {
env: { featureAnnouncements: featureAnnouncementsEnv },
state: { isNotificationServicesEnabled: true },
});
const snapNotification = createMockSnapNotification();
await controller.updateMetamaskNotificationsList(
snapNotification as INotification,
const processedSnapNotification = processSnapNotification(
createMockSnapNotification(),
);
await controller.updateMetamaskNotificationsList(processedSnapNotification);
expect(controller.state.metamaskNotificationsList).toStrictEqual([
{
type: TRIGGER_TYPES.SNAP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import * as OnChainNotifications from './services/onchain-notifications';
import type {
INotification,
MarkAsReadNotificationsParam,
NotificationUnion,
RawNotificationUnion,
} from './types/notification/notification';
import type { OnChainRawNotification } from './types/on-chain-notification/on-chain-notification';
import type { UserStorage } from './types/user-storage/user-storage';
Expand Down Expand Up @@ -174,7 +174,9 @@ export const defaultState: NotificationServicesControllerState = {
isCheckingAccountsPresence: false,
};

const locallyPersistedNotificationTypes = new Set<string>([TRIGGER_TYPES.SNAP]);
const locallyPersistedNotificationTypes = new Set<TRIGGER_TYPES>([
TRIGGER_TYPES.SNAP,
]);

export type NotificationServicesControllerGetStateAction =
ControllerGetStateAction<
Expand Down Expand Up @@ -1118,7 +1120,7 @@ export default class NotificationServicesController extends BaseController<

// Combined Notifications
const isNotUndefined = <Item>(t?: Item): t is Item => Boolean(t);
const processAndFilter = (ns: NotificationUnion[]) =>
const processAndFilter = (ns: RawNotificationUnion[]) =>
ns
.map((n) => safeProcessNotification(n, readIds))
.filter(isNotUndefined);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TRIGGER_TYPES } from '../constants/notification-schema';
import type { FeatureAnnouncementRawNotification } from '../types/feature-announcement/feature-announcement';
import type {
INotification,
NotificationUnion,
RawNotificationUnion,
} from '../types/notification/notification';
import type { OnChainRawNotification } from '../types/on-chain-notification/on-chain-notification';
import type { RawSnapNotification } from '../types/snaps';
Expand All @@ -14,16 +14,17 @@ import { processOnChainNotification } from './process-onchain-notifications';
import { processSnapNotification } from './process-snap-notifications';

const isOnChainNotification = (
n: NotificationUnion,
n: RawNotificationUnion,
): n is OnChainRawNotification => Object.values(TRIGGER_TYPES).includes(n.type);

const isFeatureAnnouncement = (
n: NotificationUnion,
n: RawNotificationUnion,
): n is FeatureAnnouncementRawNotification =>
n.type === TRIGGER_TYPES.FEATURES_ANNOUNCEMENT;

const isSnapNotification = (n: NotificationUnion): n is RawSnapNotification =>
n.type === TRIGGER_TYPES.SNAP;
const isSnapNotification = (
n: RawNotificationUnion,
): n is RawSnapNotification => n.type === TRIGGER_TYPES.SNAP;

/**
* Process feature announcement and wallet notifications into a shared/normalised notification shape.
Expand All @@ -34,7 +35,7 @@ const isSnapNotification = (n: NotificationUnion): n is RawSnapNotification =>
* @returns a processed notification
*/
export function processNotification(
notification: NotificationUnion,
notification: RawNotificationUnion,
readNotifications: string[] = [],
): INotification {
const exhaustedAllCases = (_: never) => {
Expand All @@ -51,11 +52,11 @@ export function processNotification(
}

if (isSnapNotification(notification)) {
return processSnapNotification(notification as RawSnapNotification);
return processSnapNotification(notification);
}

if (isOnChainNotification(notification)) {
return processOnChainNotification(notification as OnChainRawNotification);
return processOnChainNotification(notification);
}

return exhaustedAllCases(notification as never);
Expand All @@ -69,7 +70,7 @@ export function processNotification(
* @returns a process notification or undefined if failed to process
*/
export function safeProcessNotification(
notification: NotificationUnion,
notification: RawNotificationUnion,
readNotifications: string[] = [],
): INotification | undefined {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import type { OnChainRawNotification } from '../on-chain-notification/on-chain-n
import type { RawSnapNotification } from '../snaps';
import type { Compute } from '../type-utils';

export type NotificationUnion =
| FeatureAnnouncementRawNotification
export type BaseNotification = {
id: string;
createdAt: string;
isRead: boolean;
};

export type RawNotificationUnion =
| OnChainRawNotification
| FeatureAnnouncementRawNotification
| RawSnapNotification;

/**
Expand All @@ -15,11 +21,9 @@ export type NotificationUnion =
* - `data` field (declared in the Raw shapes)
*/
export type INotification = Compute<
NotificationUnion & {
id: string;
createdAt: string;
isRead: boolean;
}
| (FeatureAnnouncementRawNotification & BaseNotification)
| (OnChainRawNotification & BaseNotification)
| (RawSnapNotification & BaseNotification & { readDate?: string | null })
>;

// NFT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ export type ExpandedView = {
footerLink?: { href: string; text: string };
};

export type RawSnapNotificationData =
| {
message: string;
origin: string;
}
| { message: string; origin: string; detailedView: ExpandedView };

export type RawSnapNotification = {
type: TRIGGER_TYPES.SNAP;
readDate: string | null;
data:
| { message: string; origin: string }
| { message: string; origin: string; detailedView: ExpandedView };
data: RawSnapNotificationData;
readDate: null;
};

0 comments on commit e59eeb2

Please sign in to comment.