Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add notification call prevention logic #5081

Merged
merged 3 commits into from
Dec 19, 2024

Conversation

Prithpal-Sooriya
Copy link
Contributor

@Prithpal-Sooriya Prithpal-Sooriya commented Dec 18, 2024

Explanation

We've added logic on platforms to make sure that the global notifications switch is enabled before we fetch. But ideally this should be internal logic.

So we've updated the logic to ensure that Feature Announcements and Wallet Notifications will bail early if the toggle is off. Snap notifications however will not obey this flag (to keep compatibility with the original snap notifications feature).

References

MetaMask/metamask-extension#28173

Changelog

@metamask/notification-services-controller

  • CHANGED: add isNotificationServicesEnabled check inside fetchAndUpdateMetamaskNotifications method.
    • this will prevent fetching feature announcements and wallet notifications if set to false.

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@Prithpal-Sooriya Prithpal-Sooriya requested a review from a team as a code owner December 18, 2024 18:45
Comment on lines +61 to +64
// Testing util to clean up verbose logs when testing errors
const mockErrorLog = () =>
jest.spyOn(log, 'error').mockImplementation(jest.fn());
const mockWarnLog = () => jest.spyOn(log, 'warn').mockImplementation(jest.fn());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

test:verbose is a bit too messy since we are testing a lot of failure cases.
This cleans up the logs only for cases we know we are failing on.

return controller;
};

it('processes and shows all notifications (announcements, wallet, and snap notifications)', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tests for the fetchAndUpdateMetamaskNotifications method were messy, cleaned them up.

@@ -577,7 +577,7 @@ export default class NotificationServicesController extends BaseController<
#registerMessageHandlers(): void {
this.messagingSystem.registerActionHandler(
`${controllerName}:updateMetamaskNotificationsList`,
async (...args) => this.updateMetamaskNotificationsList(...args),
this.updateMetamaskNotificationsList.bind(this),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is equivalent to the previous version. It aligns with the other action handlers.

).catch(() => [])
: [];
const rawFeatureAnnouncementNotifications =
isGlobalNotifsEnabled && this.state.isFeatureAnnouncementsEnabled
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added isGlobalNotifsEnabled check to make sure we fetch announcements if the global flag is enabled.

).catch(() => []);

rawOnChainNotifications.push(...notifications);
if (isGlobalNotifsEnabled) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Surrounded code with this isGlobalNotifsEnabled check to ensure we fetch wallet notifications if the global flag is enabled.

Comment on lines +1131 to +1135
// Snap Notifications (original)
// We do not want to remove them
const snapNotifications = this.state.metamaskNotificationsList.filter(
(notification) => notification.type === TRIGGER_TYPES.SNAP,
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reordering snap notifications, so the code is ordered in steps.

(notification) => notification.type === TRIGGER_TYPES.SNAP,
);

// Combine Notifications
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added better comments to explain each step.

NOTE - this function does to a lot of things.
I think when we do the larger refactor we can think on how to isolate each notifications responsibilities.

Copy link
Contributor

@mathieuartu mathieuartu left a comment

Choose a reason for hiding this comment

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

LGTM apart from the changelog part in the PR description 😛

@Prithpal-Sooriya Prithpal-Sooriya changed the title Fix add notification call prevention logic fix: add notification call prevention logic Dec 19, 2024
@Prithpal-Sooriya Prithpal-Sooriya merged commit 945349b into main Dec 19, 2024
120 checks passed
@Prithpal-Sooriya Prithpal-Sooriya deleted the fix-add-notification-call-prevention-logic branch December 19, 2024 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants