@@ -30,7 +30,7 @@ const deleteSubscriptionFromIndex = async (subcriptionId: string) => {
30
30
}
31
31
32
32
const saveSubscription = ( server , subscriptionId ) => {
33
- sbp ( 'chelonia/db/set' , `_private_webpush_${ subscriptionId } ` , JSON . stringify ( {
33
+ return sbp ( 'chelonia/db/set' , `_private_webpush_${ subscriptionId } ` , JSON . stringify ( {
34
34
subscription : server . pushSubscriptions [ subscriptionId ] ,
35
35
channelIDs : [ ...server . pushSubscriptions [ subscriptionId ] . subscriptions ]
36
36
} ) ) . catch ( e => {
@@ -163,22 +163,25 @@ export const subscriptionInfoWrapper = (subcriptionId: string, subscriptionInfo:
163
163
return subscriptionInfo
164
164
}
165
165
166
- const removeSubscription = ( server , subscriptionId ) => {
167
- const subscription = server . pushSubscriptions [ subscriptionId ]
168
- delete server . pushSubscriptions [ subscriptionId ]
169
- if ( server . subscribersByChannelID ) {
170
- subscription . subscriptions . forEach ( ( channelID ) => {
171
- server . subscribersByChannelID [ channelID ] . delete ( subscription )
172
- } )
166
+ const removeSubscription = async ( server , subscriptionId ) => {
167
+ try {
168
+ const subscription = server . pushSubscriptions [ subscriptionId ]
169
+ delete server . pushSubscriptions [ subscriptionId ]
170
+ if ( server . subscribersByChannelID ) {
171
+ subscription . subscriptions . forEach ( ( channelID ) => {
172
+ server . subscribersByChannelID [ channelID ] . delete ( subscription )
173
+ } )
174
+ }
175
+ await deleteSubscriptionFromIndex ( subscriptionId )
176
+ await sbp ( 'chelonia/db/delete' , `_private_webpush_${ subscriptionId } ` )
177
+ } catch ( e ) {
178
+ console . error ( e , 'Error removing subscription' , subscriptionId )
173
179
}
174
- deleteSubscriptionFromIndex ( subscriptionId ) . then ( ( ) => {
175
- return sbp ( 'chelonia/db/delete' , `_private_webpush_${ subscriptionId } ` )
176
- } ) . catch ( ( e ) => console . error ( e , 'Error removing subscription' , subscriptionId ) )
177
180
}
178
181
179
182
const deleteClient = ( subscriptionId ) => {
180
183
const server = sbp ( 'okTurtles.data/get' , PUBSUB_INSTANCE )
181
- removeSubscription ( server , subscriptionId )
184
+ return removeSubscription ( server , subscriptionId )
182
185
}
183
186
184
187
// Web push subscriptions (that contain a body) are mandatorily encrypted. The
@@ -239,6 +242,7 @@ export const postEvent = async (subscription: Object, event: ?string): Promise<v
239
242
} )
240
243
241
244
if ( ! req . ok ) {
245
+ console . warn ( 'Error sending push notification' , subscription . id , req . status )
242
246
// If the response was 401 (Unauthorized), 404 (Not found) or 410 (Gone),
243
247
// it likely means that the subscription no longer exists.
244
248
if ( [ 401 , 404 , 410 ] . includes ( req . status ) ) {
@@ -268,11 +272,17 @@ export const pushServerActionhandlers: any = {
268
272
const subscriptionId = await getSubscriptionId ( subscription )
269
273
270
274
if ( ! server . pushSubscriptions [ subscriptionId ] ) {
275
+ console . debug ( `saving new push subscription '${ subscriptionId } ':` , subscription )
271
276
// If this is a new subscription, we call `subscriptionInfoWrapper` and
272
277
// store it in memory.
273
278
server . pushSubscriptions [ subscriptionId ] = subscriptionInfoWrapper ( subscriptionId , subscription )
274
279
addSubscriptionToIndex ( subscriptionId ) . then ( ( ) => {
275
280
return sbp ( 'chelonia/db/set' , `_private_webpush_${ subscriptionId } ` , JSON . stringify ( { subscription : subscription , channelIDs : [ ] } ) )
281
+ . catch ( async e => {
282
+ console . error ( e , 'removing subscription from index because of error saving subscription' , subscriptionId )
283
+ await deleteSubscriptionFromIndex ( subscriptionId )
284
+ throw e
285
+ } )
276
286
} ) . catch ( ( e ) => console . error ( e , 'Error saving subscription' , subscriptionId ) )
277
287
// Send an initial push notification to verify that the endpoint works
278
288
// This is mostly for testing to be able to auto-remove invalid or expired
0 commit comments