@@ -395,16 +395,19 @@ async function validateUserCookie(decryptedUserdata) {
395395 json . sessions = [ ] ;
396396 }
397397 const currentSessionToken = decryptedUserdata . session ;
398- const isValidSession = json . sessions . some (
399- ( session ) => session . id === currentSessionToken ,
398+ const curSession = json . sessions . find (
399+ ( session ) => session . id == currentSessionToken ,
400400 ) ;
401- if ( isValidSession ) {
401+ if ( curSession ) {
402+ var pushSubscription = curSession . pushSubscription ;
402403 return {
403404 color,
404405 font,
405406 displayName,
406407 success : true ,
407408 valid : true ,
409+ sessionCreated : curSession . created ,
410+ pushEndpoint : pushSubscription ? pushSubscription . endpoint : undefined ,
408411 } ;
409412 } else {
410413 return {
@@ -2653,76 +2656,76 @@ function applyRateLimit(req) {
26532656 return false ;
26542657}
26552658
2656- async function registerPushNotifications ( username , newSubscription ) {
2657- var filename = `push_sub_${ username . trim ( ) . toLowerCase ( ) } .json` ;
2658- var subscriptions = [ ] ;
2659-
2660- try {
2661- // 1. Try to download existing subs
2662- const buffer = await storage . downloadFile ( filename , false ) ;
2663- subscriptions = JSON . parse ( buffer . toString ( ) ) ;
2659+ async function registerPushNotifications ( decryptedUserdata , newSubscription ) {
2660+ var userFile = `user-${ decryptedUserdata . username } .json` ;
2661+ var data = await storage . downloadFile ( userFile ) ;
2662+ var json = JSON . parse ( data ) ;
26642663
2665- // 2. Prevent duplicates (check if endpoint already exists)
2666- const exists = subscriptions . find (
2667- ( s ) => s . endpoint === newSubscription . endpoint ,
2668- ) ;
2669- if ( ! exists ) {
2670- subscriptions . push ( newSubscription ) ;
2671- }
2672- subscriptions = subscriptions . slice ( - cons . MAX_PUSH_SUBSCRIPTIONS ) ;
2673- } catch ( err ) {
2674- // File doesn't exist yet, start a new list
2675- subscriptions = [ newSubscription ] ;
2664+ var sessionID = decryptedUserdata . session ;
2665+ if ( ! json . sessions ) {
2666+ throw new Error ( "User data is missing sessions array" ) ;
2667+ }
2668+ var currentSession = json . sessions . find ( ( s ) => s . id == sessionID ) ;
2669+ if ( ! currentSession ) {
2670+ throw new Error ( "Current session not found in user data" ) ;
26762671 }
26772672
2678- // 3. Upload the updated list
2679- await storage . uploadFile (
2680- filename ,
2681- JSON . stringify ( subscriptions ) ,
2682- "application/json" ,
2683- ) ;
2673+ currentSession . pushSubscription = newSubscription ;
2674+
2675+ await storage . uploadFile ( userFile , JSON . stringify ( json ) , "application/json" ) ;
26842676}
26852677
26862678async function removeSubscriptionFromFile ( username , endpointToRemove ) {
2687- const filename = `push_sub_${ username . toLowerCase ( ) } .json` ;
2688-
2689- try {
2690- // 1. Download the current list of devices
2691- const buffer = await storage . downloadFile ( filename , false ) ;
2692- let subscriptions = JSON . parse ( buffer . toString ( ) ) ;
2693-
2694- // 2. Filter out the specific endpoint
2695- const updatedSubscriptions = subscriptions . filter (
2696- ( sub ) => sub . endpoint !== endpointToRemove ,
2697- ) ;
2679+ var userFile = `user-${ username } .json` ;
2680+ var data = await storage . downloadFile ( userFile ) ;
2681+ var json = JSON . parse ( data ) ;
26982682
2699- // 3. If no subscriptions are left, delete the file; otherwise, update it
2700- if ( updatedSubscriptions . length === 0 ) {
2701- await storage . deleteFile ( filename ) ;
2702- } else {
2703- await storage . uploadFile (
2704- filename ,
2705- JSON . stringify ( updatedSubscriptions ) ,
2706- "application/json" ,
2707- ) ;
2683+ if ( json . sessions ) {
2684+ for ( const session of json . sessions ) {
2685+ if (
2686+ session . pushSubscription &&
2687+ session . pushSubscription . endpoint == endpointToRemove
2688+ ) {
2689+ session . pushSubscription = null ;
2690+ break ;
2691+ }
27082692 }
2709- } catch ( err ) {
2710- console . error ( "Error removing subscription:" , err ) ;
27112693 }
2694+
2695+ await storage . uploadFile ( userFile , JSON . stringify ( json ) , "application/json" ) ;
27122696}
27132697
27142698async function getSubscriptions ( username ) {
27152699 try {
2716- const filename = `push_sub_ ${ username . toLowerCase ( ) } .json` ;
2717- const buffer = await storage . downloadFile ( filename , false ) ;
2718- let subscriptions = JSON . parse ( buffer . toString ( ) ) ;
2700+ var userFile = `user- ${ username } .json` ;
2701+ var data = await storage . downloadFile ( userFile ) ;
2702+ var json = JSON . parse ( data ) ;
27192703
2720- return subscriptions ;
2704+ if ( json . sessions ) {
2705+ var subscriptions = [ ] ;
2706+ for ( const session of json . sessions ) {
2707+ if ( session . pushSubscription ) {
2708+ subscriptions . push ( session . pushSubscription ) ;
2709+ }
2710+ }
2711+ return subscriptions ;
2712+ } else {
2713+ return null ;
2714+ }
27212715 } catch ( e ) {
27222716 return ;
27232717 }
27242718}
27252719
2720+ async function hasSubscription ( username , subscription ) {
2721+ const subs = await getSubscriptions ( username ) ;
2722+ if ( subs ) {
2723+ return subs . some ( ( sub ) => sub . endpoint == subscription . endpoint ) ;
2724+ } else {
2725+ return false ;
2726+ }
2727+ }
2728+
27262729async function sendPushMessage ( username , payload ) {
27272730 const subs = await getSubscriptions ( username ) ;
27282731 if ( ! subs ) return ;
@@ -2733,7 +2736,6 @@ async function sendPushMessage(username, payload) {
27332736 } catch ( err ) {
27342737 // 404: Not Found, 410: Gone (Unsubscribed)
27352738 if ( err . statusCode === 404 || err . statusCode === 410 ) {
2736- console . log ( `Cleaning up dead subscription for ${ username } ` ) ;
27372739 await removeSubscriptionFromFile ( username , sub . endpoint ) ;
27382740 }
27392741 }
@@ -2900,7 +2902,7 @@ const server = http.createServer(async function (req, res) {
29002902 }
29012903
29022904 try {
2903- await registerPushNotifications ( decryptedUserdata . username , json ) ;
2905+ await registerPushNotifications ( decryptedUserdata , json ) ;
29042906 res . end ( "" ) ;
29052907 return ;
29062908 } catch ( e ) {
0 commit comments