From 7ab1f78f2478cac5b87384fe024afaab77e25962 Mon Sep 17 00:00:00 2001 From: Hassan Malik Date: Thu, 17 Oct 2024 15:48:07 -0400 Subject: [PATCH] add deleteNotificationById function --- .../NotificationServicesController.test.ts | 18 +++++++++ .../NotificationServicesController.ts | 40 ++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.test.ts b/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.test.ts index 329f8835a4..99849c9730 100644 --- a/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.test.ts +++ b/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.test.ts @@ -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(); diff --git a/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts b/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts index 64e3740128..86a9c3345b 100644 --- a/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts +++ b/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts @@ -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 = @@ -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 { @@ -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. *