feat(notifications): add user-targeted push notifications#15
feat(notifications): add user-targeted push notifications#15francescopausellii wants to merge 11 commits intofeat/web-appfrom
Conversation
Summary of ChangesHello @francescopausellii, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a comprehensive system for user-targeted push notifications. It establishes the necessary database structures and API endpoints to manage user push subscriptions and deliver notifications directly to authenticated users. The frontend has been updated to allow users to subscribe/unsubscribe and for authenticated users to send notifications to other subscribed users. Additionally, new authentication UI components are integrated into the homepage, and foundational fields for user moderation features have been added to the database schema. Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces user-targeted push notifications, including database schema changes, new API endpoints for managing subscriptions and sending notifications, and UI components for authentication and notification management. The changes are well-structured within the feature-based architecture. My review focuses on improving performance and React best practices according to the repository's style guide. I've identified a critical performance issue in the notification sending logic where notifications are sent sequentially instead of in parallel, and a medium-severity issue related to React hook dependencies that could cause unnecessary re-renders. Applying these suggestions will make the feature more robust and performant.
| for (const sub of subscriptions) { | ||
| try { | ||
| await sendNotification( | ||
| { endpoint: sub.endpoint, keys: { auth: sub.auth, p256dh: sub.p256dh } }, | ||
| title, | ||
| message, | ||
| ); | ||
| } catch (error) { | ||
| if (error instanceof webpush.WebPushError && error.statusCode === 410) { | ||
| await prisma.pushSubscription.delete({ where: { id: sub.id } }); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
The current implementation sends notifications to a user's multiple subscriptions sequentially using a for...of loop with await. This creates a network waterfall, where each notification must wait for the previous one to complete, potentially delaying delivery.
To improve performance, these independent network requests should be executed in parallel using Promise.all().
This change aligns with the style guide rule async-parallel (1.4), which is critical for performance.
| for (const sub of subscriptions) { | |
| try { | |
| await sendNotification( | |
| { endpoint: sub.endpoint, keys: { auth: sub.auth, p256dh: sub.p256dh } }, | |
| title, | |
| message, | |
| ); | |
| } catch (error) { | |
| if (error instanceof webpush.WebPushError && error.statusCode === 410) { | |
| await prisma.pushSubscription.delete({ where: { id: sub.id } }); | |
| } | |
| } | |
| } | |
| await Promise.all( | |
| subscriptions.map(async (sub) => { | |
| try { | |
| await sendNotification( | |
| { endpoint: sub.endpoint, keys: { auth: sub.auth, p256dh: sub.p256dh } }, | |
| title, | |
| message, | |
| ); | |
| } catch (error) { | |
| if (error instanceof webpush.WebPushError && error.statusCode === 410) { | |
| await prisma.pushSubscription.delete({ where: { id: sub.id } }); | |
| } else { | |
| console.error(`Failed to send notification to endpoint ${sub.endpoint}:`, error); | |
| } | |
| } | |
| }), | |
| ); |
References
- Rule 1.4
async-parallel: When async operations have no interdependencies, execute them concurrently usingPromise.all(). Sending notifications to different subscriptions for the same user are independent operations and should be parallelized. (link)
| setTargetUserId(session.user.id); | ||
| } | ||
| }); | ||
| }, [isAuthenticated, session?.user]); |
There was a problem hiding this comment.
The useEffect hook's dependency array includes session?.user, which is an object. This can cause the effect to re-run on every render if the session object reference changes, even if its content is the same. This leads to unnecessary re-fetches of the user list.
To avoid this, you should use a primitive value from the object that the effect actually depends on, such as session.user.id.
This follows the style guide rule rerender-dependencies (5.3) to narrow effect dependencies.
| }, [isAuthenticated, session?.user]); | |
| }, [isAuthenticated, session?.user?.id]); |
References
- Rule 5.3
rerender-dependencies: Specify primitive dependencies instead of objects to minimize effect re-runs. The effect depends on the user's ID, sosession?.user?.idshould be used instead of thesession?.userobject. (link)
No description provided.