fix: multiple push notifications susbscriptions on multiple account servers (#1069)

This commit is contained in:
Joaquín Sánchez 2023-01-13 13:54:30 +01:00 committed by GitHub
parent a733fbba08
commit 1d151c53c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 7 deletions

View file

@ -4,6 +4,7 @@ import type {
PushManagerSubscriptionInfo,
RequiredUserLogin,
} from '~/composables/push-notifications/types'
import { PushSubscriptionError } from '~/composables/push-notifications/types'
export const createPushSubscription = async (
user: RequiredUserLogin,
@ -41,8 +42,11 @@ export const createPushSubscription = async (
)
})
.catch((error) => {
if (error.code === 20 && error.name === 'AbortError')
console.warn('Your browser supports Web Push Notifications, but does not seem to implement the VAPID protocol.')
let useError: PushSubscriptionError | Error = error
if (error.code === 11 && error.name === 'InvalidStateError')
useError = new PushSubscriptionError('too_many_registrations', 'Too many registrations')
else if (error.code === 20 && error.name === 'AbortError')
console.error('Your browser supports Web Push Notifications, but does not seem to implement the VAPID protocol.')
else if (error.code === 5 && error.name === 'InvalidCharacterError')
console.error('The VAPID public key seems to be invalid:', vapidKey)
@ -54,6 +58,9 @@ export const createPushSubscription = async (
console.error(e)
return Promise.resolve(undefined)
})
.finally(() => {
return Promise.reject(useError)
})
})
}

View file

@ -24,3 +24,12 @@ export interface CustomEmojisInfo {
lastUpdate: number
emojis: mastodon.v1.CustomEmoji[]
}
export type PushSubscriptionErrorCode = 'too_many_registrations'
export class PushSubscriptionError extends Error {
code: PushSubscriptionErrorCode
constructor(code: PushSubscriptionErrorCode, message?: string) {
super(message)
this.code = code
}
}