@@ -15,7 +15,7 @@ var gvbbaseStorage = require("./storage.js"); //Supabase storage module.
1515var cons = require ( "./constants.js" ) ;
1616var bcrypt = require ( "bcryptjs" ) ;
1717var crypto = require ( "crypto" ) ;
18- var webpush = require ( ' web-push' ) ;
18+ var webpush = require ( " web-push" ) ;
1919var scratchCloudWss = require ( "./scratch-cloud.js" ) ;
2020var WebRTCSignaler = require ( "./rtcsignal/req-handler.js" ) ;
2121var botCheck = require ( "./bot-check-manager.js" ) ;
@@ -48,15 +48,17 @@ var WEBPUSH_ENABLED = true;
4848var WEBPUSH_PUBLIC_KEY = process . env . publicPushKey ;
4949var WEBPUSH_PRIVATE_KEY = process . env . privatePushKey ;
5050if ( ! WEBPUSH_PUBLIC_KEY || ! WEBPUSH_PRIVATE_KEY ) {
51- console . warn ( "[WebPush]: No web-push keys or missing one, invite notifications won't work anymore." ) ;
51+ console . warn (
52+ "[WebPush]: No web-push keys or missing one, invite notifications won't work anymore." ,
53+ ) ;
5254 WEBPUSH_ENABLED = false ;
5355}
5456
5557if ( WEBPUSH_ENABLED ) {
5658 webpush . setVapidDetails (
5759 process . env . website || cons . DEFAULT_WEBSITE ,
5860 WEBPUSH_PUBLIC_KEY ,
59- WEBPUSH_PRIVATE_KEY
61+ WEBPUSH_PRIVATE_KEY ,
6062 ) ;
6163}
6264
@@ -2656,11 +2658,13 @@ async function registerPushNotifications(username, newSubscription) {
26562658 // 1. Try to download existing subs
26572659 const buffer = await storage . downloadFile ( filename , false ) ;
26582660 subscriptions = JSON . parse ( buffer . toString ( ) ) ;
2659-
2661+
26602662 // 2. Prevent duplicates (check if endpoint already exists)
2661- const exists = subscriptions . find ( s => s . endpoint === newSubscription . endpoint ) ;
2663+ const exists = subscriptions . find (
2664+ ( s ) => s . endpoint === newSubscription . endpoint ,
2665+ ) ;
26622666 if ( ! exists ) {
2663- subscriptions . push ( newSubscription ) ;
2667+ subscriptions . push ( newSubscription ) ;
26642668 }
26652669 subscriptions = subscriptions . slice ( - cons . MAX_PUSH_SUBSCRIPTIONS ) ;
26662670 } catch ( err ) {
@@ -2669,7 +2673,11 @@ async function registerPushNotifications(username, newSubscription) {
26692673 }
26702674
26712675 // 3. Upload the updated list
2672- await storage . uploadFile ( filename , JSON . stringify ( subscriptions ) , "application/json" ) ;
2676+ await storage . uploadFile (
2677+ filename ,
2678+ JSON . stringify ( subscriptions ) ,
2679+ "application/json" ,
2680+ ) ;
26732681}
26742682
26752683async function removeSubscriptionFromFile ( username , endpointToRemove ) {
@@ -2682,17 +2690,17 @@ async function removeSubscriptionFromFile(username, endpointToRemove) {
26822690
26832691 // 2. Filter out the specific endpoint
26842692 const updatedSubscriptions = subscriptions . filter (
2685- ( sub ) => sub . endpoint !== endpointToRemove
2693+ ( sub ) => sub . endpoint !== endpointToRemove ,
26862694 ) ;
26872695
26882696 // 3. If no subscriptions are left, delete the file; otherwise, update it
26892697 if ( updatedSubscriptions . length === 0 ) {
26902698 await storage . deleteFile ( filename ) ;
26912699 } else {
26922700 await storage . uploadFile (
2693- filename ,
2694- JSON . stringify ( updatedSubscriptions ) ,
2695- "application/json"
2701+ filename ,
2702+ JSON . stringify ( updatedSubscriptions ) ,
2703+ "application/json" ,
26962704 ) ;
26972705 }
26982706 } catch ( err ) {
@@ -2701,13 +2709,13 @@ async function removeSubscriptionFromFile(username, endpointToRemove) {
27012709}
27022710
27032711async function getSubscriptions ( username ) {
2704- try {
2712+ try {
27052713 const filename = `push_sub_${ username . toLowerCase ( ) } .json` ;
27062714 const buffer = await storage . downloadFile ( filename , false ) ;
27072715 let subscriptions = JSON . parse ( buffer . toString ( ) ) ;
27082716
27092717 return subscriptions ;
2710- } catch ( e ) {
2718+ } catch ( e ) {
27112719 return ;
27122720 }
27132721}
@@ -2863,7 +2871,6 @@ const server = http.createServer(async function (req, res) {
28632871 }
28642872
28652873 if ( urlsplit [ 2 ] == "subscribe" && req . method == "POST" ) {
2866-
28672874 if ( ! decryptedUserdata ) {
28682875 res . statusCode = 401 ;
28692876 res . end ( "No user cookie." ) ;
@@ -2876,30 +2883,31 @@ const server = http.createServer(async function (req, res) {
28762883 res . end ( "User cookie is broken/expired." ) ;
28772884 return ;
28782885 }
2879- try {
2886+ try {
28802887 var jsonText = await waitForBody ( req ) ;
2881- } catch ( e ) { return ; }
2882- try {
2888+ } catch ( e ) {
2889+ return ;
2890+ }
2891+ try {
28832892 var json = JSON . parse ( jsonText . toString ( ) ) ;
2884- } catch ( e ) {
2893+ } catch ( e ) {
28852894 res . statusCode = 401 ;
28862895 res . end ( "JSON content is invalid." ) ;
28872896 return ;
28882897 }
2889-
2890- try {
2898+
2899+ try {
28912900 await registerPushNotifications ( decryptedUserdata . username , json ) ;
28922901 res . end ( "" ) ;
28932902 return ;
2894- } catch ( e ) {
2903+ } catch ( e ) {
28952904 res . statusCode = 401 ;
28962905 res . end ( "Unable to register." ) ;
28972906 return ;
28982907 }
28992908 }
29002909
29012910 if ( urlsplit [ 2 ] == "unsubscribe" && req . method == "POST" ) {
2902-
29032911 if ( ! decryptedUserdata ) {
29042912 res . statusCode = 401 ;
29052913 res . end ( "No user cookie." ) ;
@@ -2912,12 +2920,14 @@ const server = http.createServer(async function (req, res) {
29122920 res . end ( "User cookie is broken/expired." ) ;
29132921 return ;
29142922 }
2915- try {
2923+ try {
29162924 var jsonText = await waitForBody ( req ) ;
2917- } catch ( e ) { return ; }
2918- try {
2925+ } catch ( e ) {
2926+ return ;
2927+ }
2928+ try {
29192929 var json = JSON . parse ( jsonText . toString ( ) ) ;
2920- } catch ( e ) {
2930+ } catch ( e ) {
29212931 res . statusCode = 401 ;
29222932 res . end ( "JSON content is invalid." ) ;
29232933 return ;
@@ -2929,16 +2939,18 @@ const server = http.createServer(async function (req, res) {
29292939 return ;
29302940 }
29312941
2932- try {
2933- await removeSubscriptionFromFile ( decryptedUserdata . username , json . endpoint ) ;
2942+ try {
2943+ await removeSubscriptionFromFile (
2944+ decryptedUserdata . username ,
2945+ json . endpoint ,
2946+ ) ;
29342947 res . end ( "" ) ;
29352948 return ;
2936- } catch ( e ) {
2949+ } catch ( e ) {
29372950 res . statusCode = 401 ;
29382951 res . end ( "Unable to deregister." ) ;
29392952 return ;
29402953 }
2941-
29422954 }
29432955 }
29442956
0 commit comments