@@ -81,22 +81,12 @@ export class NotificationService {
8181 configureVapid ( config : VapidConfig ) : void {
8282 this . vapidConfig = config ;
8383 webpush . setVapidDetails ( config . subject , config . publicKey , config . privateKey ) ;
84- logger . info ( `VAPID configured — subject="${ config . subject } " publicKeyLength=${ config . publicKey . length } privateKeyLength=${ config . privateKey . length } ` ) ;
8584 }
8685
8786 getVapidPublicKey ( ) : string | null {
8887 return this . vapidConfig ?. publicKey ?? null ;
8988 }
9089
91- getVapidDetails ( ) : { publicKey : string ; privateKeyLength : number ; subject : string } | null {
92- if ( ! this . vapidConfig ) return null ;
93- return {
94- publicKey : this . vapidConfig . publicKey ,
95- privateKeyLength : this . vapidConfig . privateKey . length ,
96- subject : this . vapidConfig . subject ,
97- } ;
98- }
99-
10090 isConfigured ( ) : boolean {
10191 return this . vapidConfig !== null ;
10292 }
@@ -136,8 +126,6 @@ export class NotificationService {
136126 last_used_at : number | null ;
137127 } ;
138128
139- logger . info ( `Saved push subscription for user ${ userId } ` ) ;
140-
141129 return {
142130 id : row . id ,
143131 userId : row . user_id ,
@@ -212,39 +200,21 @@ export class NotificationService {
212200 event : SSEEvent
213201 ) : Promise < void > {
214202 const config = EVENT_CONFIG [ event . type ] ;
215- if ( ! config ) {
216- logger . debug ( `[push] Ignoring SSE event type="${ event . type } " (no notification config)` ) ;
217- return ;
218- }
203+ if ( ! config ) return ;
219204
220- logger . info ( `[push] Processing SSE event type=" ${ event . type } " for directory=" ${ _directory } "` ) ;
205+ if ( this . hasActiveSSEClients ( ) ) return ;
221206
222- if ( this . hasActiveSSEClients ( ) ) {
223- logger . info ( `[push] Skipping push — active visible SSE clients detected` ) ;
224- return ;
225- }
226-
227- if ( ! this . isConfigured ( ) ) {
228- logger . warn ( `[push] Skipping push — VAPID not configured` ) ;
229- return ;
230- }
207+ if ( ! this . isConfigured ( ) ) return ;
231208
232209 const userIds = this . getAllUserIds ( ) ;
233- logger . info ( `[push] Found ${ userIds . length } user(s) with push subscriptions` ) ;
234210
235211 for ( const userId of userIds ) {
236212 const settings = this . settingsService . getSettings ( userId ) ;
237213 const notifPrefs =
238214 settings . preferences . notifications ?? DEFAULT_NOTIFICATION_PREFERENCES ;
239215
240- if ( ! notifPrefs . enabled ) {
241- logger . info ( `[push] Skipping user="${ userId } " — notifications disabled` ) ;
242- continue ;
243- }
244- if ( ! notifPrefs . events [ config . preferencesKey ] ) {
245- logger . info ( `[push] Skipping user="${ userId } " — event "${ config . preferencesKey } " disabled` ) ;
246- continue ;
247- }
216+ if ( ! notifPrefs . enabled ) continue ;
217+ if ( ! notifPrefs . events [ config . preferencesKey ] ) continue ;
248218
249219 const sessionId = event . properties . sessionID as string | undefined ;
250220
@@ -271,86 +241,56 @@ export class NotificationService {
271241 } ,
272242 } ;
273243
274- logger . info ( `[push] Sending push to user="${ userId } " title="${ payload . title } "` ) ;
275244 await this . sendToUser ( userId , payload ) ;
276245 }
277246 }
278247
279- async sendTestNotification ( userId : string ) : Promise < { sent : number ; failed : number ; results : Array < { endpoint : string ; status : string ; statusCode ?: number ; error ?: string } > } > {
280- const subscriptions = this . getSubscriptions ( userId ) ;
281- logger . info ( `[push:test] Sending test notification to user="${ userId } " (${ subscriptions . length } subscription(s))` ) ;
282- for ( const sub of subscriptions ) {
283- logger . info ( `[push:test] Subscription endpoint: ${ sub . endpoint } ` ) ;
284- }
285- const results = await this . sendToUserWithResults ( userId , {
248+ async sendTestNotification ( userId : string ) : Promise < void > {
249+ await this . sendToUser ( userId , {
286250 title : "Test Notification" ,
287251 body : "Push notifications are working correctly" ,
288252 tag : "test" ,
289253 data : { eventType : "test" , url : "/" } ,
290254 } ) ;
291- logger . info ( `[push:test] Results: ${ JSON . stringify ( results ) } ` ) ;
292- return results ;
293255 }
294256
295257 private async sendToUser (
296258 userId : string ,
297259 payload : PushNotificationPayload
298260 ) : Promise < void > {
299- await this . sendToUserWithResults ( userId , payload ) ;
300- }
301-
302- private async sendToUserWithResults (
303- userId : string ,
304- payload : PushNotificationPayload
305- ) : Promise < { sent : number ; failed : number ; results : Array < { endpoint : string ; status : string ; statusCode ?: number ; error ?: string } > } > {
306261 const subscriptions = this . getSubscriptions ( userId ) ;
307262 const expiredEndpoints : string [ ] = [ ] ;
308- const results : Array < { endpoint : string ; status : string ; statusCode ?: number ; error ?: string } > = [ ] ;
309-
310- logger . info ( `[push] Delivering to ${ subscriptions . length } subscription(s) for user="${ userId } "` ) ;
311263
312264 await Promise . allSettled (
313265 subscriptions . map ( async ( sub ) => {
314- const endpointPreview = sub . endpoint . slice ( 0 , 80 ) ;
315266 try {
316- const response = await webpush . sendNotification (
267+ await webpush . sendNotification (
317268 {
318269 endpoint : sub . endpoint ,
319270 keys : { p256dh : sub . p256dh , auth : sub . auth } ,
320271 } ,
321272 JSON . stringify ( payload )
322273 ) ;
323274
324- logger . info ( `[push] Success for ${ endpointPreview } ... — statusCode=${ response . statusCode } headers=${ JSON . stringify ( response . headers ) } ` ) ;
325- results . push ( { endpoint : endpointPreview , status : "success" , statusCode : response . statusCode } ) ;
326-
327275 this . db
328276 . prepare (
329277 "UPDATE push_subscriptions SET last_used_at = ? WHERE id = ?"
330278 )
331279 . run ( Date . now ( ) , sub . id ) ;
332280 } catch ( error ) {
333281 const statusCode = ( error as { statusCode ?: number } ) . statusCode ;
334- const body = ( error as { body ?: string } ) . body ;
335- const message = ( error as Error ) . message ;
336-
337- logger . error ( `[push] Failed for ${ endpointPreview } ... — statusCode=${ statusCode } body="${ body } " message="${ message } "` ) ;
338- results . push ( { endpoint : endpointPreview , status : "failed" , statusCode, error : body ?? message } ) ;
339282
340283 if ( statusCode === 404 || statusCode === 410 ) {
341284 expiredEndpoints . push ( sub . endpoint ) ;
285+ } else {
286+ logger . error ( `Push delivery failed for ${ sub . endpoint . slice ( 0 , 50 ) } :` , error ) ;
342287 }
343288 }
344289 } )
345290 ) ;
346291
347292 for ( const endpoint of expiredEndpoints ) {
348293 this . removeSubscription ( endpoint ) ;
349- logger . info ( `[push] Removed expired push subscription: ${ endpoint . slice ( 0 , 50 ) } ...` ) ;
350294 }
351-
352- const sent = results . filter ( r => r . status === "success" ) . length ;
353- const failed = results . filter ( r => r . status === "failed" ) . length ;
354- return { sent, failed, results } ;
355295 }
356296}
0 commit comments