Skip to content

Commit

Permalink
add deleteNotificationById function
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Oct 17, 2024
1 parent 82b07c2 commit 7ab1f78
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,24 @@ describe('metamask-notifications - getNotificationsByType', () => {
});
});

describe('metamask-notifications - deleteNotificationById', () => {
it('will delete a notification by its id', async () => {
const { messenger } = mockNotificationMessenger();
const processedSnapNotification = processSnapNotification(
createMockSnapNotification(),
);
const controller = new NotificationServicesController({
messenger,
env: { featureAnnouncements: featureAnnouncementsEnv },
state: { metamaskNotificationsList: [processedSnapNotification] },
});

await controller.deleteNotificationById(processedSnapNotification.id);

expect(controller.state.metamaskNotificationsList).toHaveLength(0);
});
});

describe('metamask-notifications - markMetamaskNotificationsAsRead()', () => {
const arrangeMocks = (options?: { onChainMarkAsReadFails: boolean }) => {
const messengerMocks = mockNotificationMessenger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,19 @@ export type NotificationServicesControllerGetNotificationsByType = {
handler: NotificationServicesController['getNotificationsByType'];
};

export type NotificationServicesControllerDeleteNotificationById = {
type: `${typeof controllerName}:deleteNotificationById`;
handler: NotificationServicesController['deleteNotificationById'];
};

// Messenger Actions
export type Actions =
| NotificationServicesControllerGetStateAction
| NotificationServicesControllerUpdateMetamaskNotificationsList
| NotificationServicesControllerDisableNotificationServices
| NotificationServicesControllerSelectIsNotificationServicesEnabled
| NotificationServicesControllerGetNotificationsByType;
| NotificationServicesControllerGetNotificationsByType
| NotificationServicesControllerDeleteNotificationById;

// Allowed Actions
export type AllowedActions =
Expand Down Expand Up @@ -584,6 +590,11 @@ export default class NotificationServicesController extends BaseController<
`${controllerName}:getNotificationsByType`,
this.getNotificationsByType.bind(this),
);

this.messagingSystem.registerActionHandler(
`${controllerName}:deleteNotificationById`,
this.deleteNotificationById.bind(this),
);
}

#clearLoadingStates(): void {
Expand Down Expand Up @@ -1165,6 +1176,33 @@ export default class NotificationServicesController extends BaseController<
);
}

/**
* Used to delete a notification by id.
*
* Note: This function should only be used for notifications that are stored
* in this controller directly, currently only snaps notifications.
*
* @param id - The id of the notification to delete.
*/
public async deleteNotificationById(id: string) {
const fetchedNotification = this.state.metamaskNotificationsList.find(
(notification) => notification.id === id,
);

assert(
fetchedNotification,
'The notification to be deleted does not exist.',
);

const newList = this.state.metamaskNotificationsList.filter(
(notification) => notification.id !== id,
);

this.update((state) => {
state.metamaskNotificationsList = newList;
});
}

/**
* Marks specified metamask notifications as read.
*
Expand Down

0 comments on commit 7ab1f78

Please sign in to comment.