@@ -77,7 +77,6 @@ fastify.post('/public/:publicKey', async (request, reply) => {
7777 if ( helper . parseKey ( publicKey , { validate : false } ) . type !== 'public' ) throw new Error ( 'Unauthorized' ) ;
7878
7979 const webhook = await helper . cacheGet ( publicKey , 'hook' ) ; // Also validates key
80- let webhookUsed = false ;
8180
8281 const app = request . query . app ;
8382
@@ -92,9 +91,11 @@ fastify.post('/public/:publicKey', async (request, reply) => {
9291 if ( ip ) meta . ip = ip ;
9392 const data = helper . decoratePayload ( request . body , meta ) ;
9493
95- // Try posting the data to webhook, if any, with timeout. On fail, store/bin data for later retrieval.
94+ // Try posting the data to webhook, if any, with timeout.
95+ // On fail, store data and send web-push notifications to owner.
9696 try {
9797 if ( ! webhook ) throw new Error ( 'No webhook' ) ;
98+ data . webhook = true ; // Data passed via webhook should also have webhook-usage metadata
9899
99100 await fetch ( webhook , {
100101 method : "POST" ,
@@ -107,19 +108,15 @@ fastify.post('/public/:publicKey', async (request, reply) => {
107108 return response . text ( ) ;
108109 } )
109110
110- webhookUsed = true ;
111111 } catch ( err ) {
112+ data . webhook = false ;
112113 await helper . publicProduce ( publicKey , data ) ;
113114 if ( webhook ) waitUntil ( helper . cacheDel ( publicKey , 'hook' ) . catch ( ( err ) => { } ) ) ;
114- }
115-
116- if ( app ) {
117- data . webhook = webhookUsed ; // Adding webhook info to metadata
118- waitUntil ( helper . OneSignalSendPush ( app , publicKey , data ) . catch ( ( err ) => { } ) ) ;
115+ if ( app ) waitUntil ( helper . OneSignalSendPush ( app , publicKey , data ) . catch ( ( err ) => { } ) ) ;
119116 }
120117
121118 if ( redirectOnOk == null ) {
122- reply . send ( { message : "Done" , error : "Ok" , statusCode : reply . statusCode , webhook : webhookUsed } ) ;
119+ reply . send ( { message : "Done" , error : "Ok" , statusCode : reply . statusCode , webhook : data . webhook } ) ;
123120 } else {
124121 reply . redirect ( redirectOnOk , 303 ) ;
125122 }
0 commit comments