feat: push notifications new controller#23137
Conversation
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
ea7d9a4 to
ddb0703
Compare
| @@ -0,0 +1,2735 @@ | |||
| /* eslint-disable */ | |||
There was a problem hiding this comment.
@matteoscurati could you include a link to where you copied the code from in this code comment?
There was a problem hiding this comment.
You're right. I'll include the license and the source of these libraries 👍
There was a problem hiding this comment.
Can we not use the npm package for this? https://www.npmjs.com/package/firebase
There was a problem hiding this comment.
Great question. Unfortunately, Lavamoat node doesn't support ESM, and Firebase does not expose an entry point in CJS. For this reason, at least for now, we had to import Firebase in the way indicated. If I understand correctly, this problem will be solved with the release of Endomoat
There was a problem hiding this comment.
Can we transpile Firebase using Babel instead?
There is an example of how we did this in the past here: https://github.com/MetaMask/metamask-extension/pull/17251/files#diff-a05d1f077953d24c0d8ddecf64163da2653ba3b728e1ba1f18af6d770767b127R831-R841
There was a problem hiding this comment.
Ok, thanks to @FrederikBolding 's suggestion, I'm able to import Firebase using npm instead of importing the libraries directly!
| @@ -0,0 +1,4 @@ | |||
| /* eslint-disable */ | |||
There was a problem hiding this comment.
A few questions/suggestions:
- why does this file need to be minified?
- is it copied from somewhere? if so where? and then does a license need to be included?
- can we include a comment at the top of this file explaining what it is?
There was a problem hiding this comment.
Ok, see the comment above.
The library is minified because it is published that way here https://www.gstatic.com/firebasejs/10.8.0/firebase-messaging-sw.js.
In any case, I have proceeded to expand it.
There was a problem hiding this comment.
Sure. See the comment above.
The library is minified because it is published that way here https://www.gstatic.com/firebasejs/10.8.0/firebase-messaging-sw.js.
In any case, I have proceeded to expand it.
| decimalPlaces: 4, | ||
| }; | ||
|
|
||
| export function calcTokenAmount(value: string, decimals: string) { |
There was a problem hiding this comment.
I see you added a code comment for the same function in shared/lib/transactions-controller-utils.js. Did you intend to import that function here?
There was a problem hiding this comment.
Yes, I'm duplicating this function to keep the SW as light as possible. This way, I don't have to import the entire util file. I know duplication isn't a desirable pattern, but our SW is already quite heavy, and I would like to avoid unnecessary imports.
I added a comment directly in the file!
| ERC1155_RECEIVED = 'erc1155_received', | ||
| } | ||
|
|
||
| const chains = { |
There was a problem hiding this comment.
Nit: because we refer to ETHEREUM as MAINNET throughout the shared/constants/network file, and that is used throughout the codebase, it might be best to name this chains.ETHEREUM property as chains.MAINNET
app/scripts/controllers/push-platform-notifications/firebase/utils/get-notification-message.ts
Outdated
Show resolved
Hide resolved
| }, | ||
| }; | ||
|
|
||
| export class PushPlatformNotificationsController extends BaseController< |
There was a problem hiding this comment.
Could you add a brief code comment that describes the purpose of the controller?
| @@ -0,0 +1,300 @@ | |||
| /** | |||
| * This file was auto-generated by openapi-typescript. | |||
There was a problem hiding this comment.
Perhaps include one more line here on how to generate the file, maybe even just the specific command that was used?
There was a problem hiding this comment.
Yep, in fact this was in a previous PR so will remove this and make sure the generated file is documented
| public async enablePushNotifications(UUIDs: string[]) { | ||
| const bearerToken = await this.getBearerToken(); | ||
| if (!bearerToken) { | ||
| log.error('Failed to enable push notifications: BearerToken token is missing.'); |
There was a problem hiding this comment.
Any reason not to include the log.error message in the new Error thrown below? I think that with the current implementation, Sentry won't capture the specific error message.
There was a problem hiding this comment.
No, you're right. I've corrected it using this pattern:
const errorMessage = `Failed to update triggers for push notifications: ${error}`;
log.error(errorMessage);
throw new Error(errorMessage);
Can it work?
danjm
left a comment
There was a problem hiding this comment.
Okay, I have completed a first review of this PR. Looks really good overall. My comments are relatively minor
4744f9f to
b660747
Compare
|
@danjm thank you very much! I've reviewed your comments and modified the code as requested. I hope everything is ok. If so, I'll wait to follow the order for merging the PRs we discussed to:
|
app/scripts/controllers/push-platform-notifications/push-platform-notifications.ts
Show resolved
Hide resolved
f52d1fd to
efbb9b3
Compare
46b861d to
162355b
Compare
Builds ready [8e304f6]
Page Load Metrics (1138 ± 635 ms)
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
Builds ready [51a7968]
Page Load Metrics (1597 ± 725 ms)
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
app/scripts/metamask-controller.js
Outdated
| delete flatState.vault; | ||
|
|
||
| // The FCM should not be exposed to the UI | ||
| delete flatState.fcmToken; |
There was a problem hiding this comment.
I think we can avoid having to do this. If the PushPlatformNotificationsController is added to this.store in the metamask-controller but not this.memStore, then it's state won't be exposed to the UI, but it will still be persisted.
There was a problem hiding this comment.
Ok, thanks for the comment, now everything is much clearer. Removed. Thx!
Builds ready [f8a4b25]
Page Load Metrics (1613 ± 675 ms)
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
Prithpal-Sooriya
left a comment
There was a problem hiding this comment.
Some comments - will resolve them
app/scripts/controllers/push-platform-notifications/push-platform-notifications.test.ts
Show resolved
Hide resolved
app/scripts/controllers/push-platform-notifications/push-platform-notifications.ts
Outdated
Show resolved
Hide resolved
app/scripts/controllers/push-platform-notifications/push-platform-notifications.ts
Outdated
Show resolved
Hide resolved
app/scripts/controllers/push-platform-notifications/types/__mocks__/mockedHalNotifications.ts
Outdated
Show resolved
Hide resolved
...controllers/push-platform-notifications/types/on-chain-notification/on-chain-notification.ts
Outdated
Show resolved
Hide resolved
app/scripts/controllers/push-platform-notifications/types/type-utils.ts
Outdated
Show resolved
Hide resolved
remove duplicate files and files that should not impact this build
| export const CHAIN_SYMBOLS = { | ||
| [NOTIFICATION_CHAINS.ETHEREUM]: 'ETH', | ||
| [NOTIFICATION_CHAINS.OPTIMISM]: 'ETH', | ||
| [NOTIFICATION_CHAINS.BSC]: 'BNB', | ||
| [NOTIFICATION_CHAINS.POLYGON]: 'MATIC', | ||
| [NOTIFICATION_CHAINS.ARBITRUM]: 'ETH', | ||
| [NOTIFICATION_CHAINS.AVALANCHE]: 'AVAX', | ||
| [NOTIFICATION_CHAINS.LINEA]: 'ETH', | ||
| }; |
There was a problem hiding this comment.
Hardcoded Chain Symbols since we need Platform agnostic values (not tied to extension).
If there are shared values then sweet, otherwise we'll keep as is for now.
this isn't really needed - we can add a feature comment once everything is merged
Prithpal-Sooriya
left a comment
There was a problem hiding this comment.
LGTM! We will wait for an extension review (and also lets merge this after next RC to avoid additional MV3 debugging)
Builds ready [b0e6312]
Page Load Metrics (1958 ± 806 ms)
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
Builds ready [a0dc43f]
Page Load Metrics (190 ± 269 ms)
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
Description
This PR introduces a new SW (Service Worker) and a new controller for managing push notifications.
This integration, due to the differences between MV2 and MV3 regarding the use of background scripts and the SW, is only related to the manifest V2. For the manifest V3, part of this logic will be modified, using the extension's own service worker as a starting point. With manifest V2, on the contrary, it is necessary to initialize a dedicated service worker so that FCM (Firebase Cloud Messaging) can handle messages received in the background.
The final UI will allow the user to enable/disable the receipt of notifications. However, this PR concerns only the
new controller and the build management for the creation of the SW.
The new controller will work in accordance with two other controllers for managing authentication and for managing the triggers necessary to receive notifications.
Related issues
No related issues.
Fixes:
Manual testing steps
At the moment, the necessary environment variables have not been inserted into the build process. To test the branch locally, feel free to ask me for the env to insert into your local .metamaskrc file
Screenshots/Recordings
In this video, I show the interaction between a test server for sending notifications and the extension:
https://www.loom.com/share/702fc6a8aea74510acdb1bfac614fa4a
Before
After
Check the entire flow here https://www.figma.com/file/c7GgNw2kScGrVyRGAPhwEd/Notifications?type=design&node-id=484%3A44713&mode=design&t=QsKrmdqZnx42IrMt-1
Pre-merge author checklist
Pre-merge reviewer checklist