chore: update masto (#506)
Co-authored-by: userquin <userquin@gmail.com>
This commit is contained in:
parent
8d57cfc886
commit
bae4ad7d4a
6 changed files with 105 additions and 52 deletions
|
@ -1,6 +1,7 @@
|
|||
import type {
|
||||
CreatePushSubscriptionParams,
|
||||
PushSubscription as MastoPushSubscription,
|
||||
SubscriptionPolicy,
|
||||
} from 'masto'
|
||||
import type {
|
||||
CreatePushNotification,
|
||||
|
@ -8,11 +9,13 @@ import type {
|
|||
RequiredUserLogin,
|
||||
} from '~/composables/push-notifications/types'
|
||||
import { useMasto } from '~/composables/masto'
|
||||
import { currentUser, removePushNotifications } from '~/composables/users'
|
||||
import { currentUser, removePushNotificationData, removePushNotifications } from '~/composables/users'
|
||||
|
||||
export const createPushSubscription = async (
|
||||
user: RequiredUserLogin,
|
||||
notificationData: CreatePushNotification,
|
||||
policy: SubscriptionPolicy = 'all',
|
||||
force = false,
|
||||
): Promise<MastoPushSubscription | undefined> => {
|
||||
const { server: serverEndpoint, vapidKey } = user
|
||||
|
||||
|
@ -26,19 +29,21 @@ export const createPushSubscription = async (
|
|||
// If the VAPID public key did not change and the endpoint corresponds
|
||||
// to the endpoint saved in the backend, the subscription is valid
|
||||
// If push subscription is not there, we need to create it: it is fetched on login
|
||||
if (subscriptionServerKey === currentServerKey && subscription.endpoint === serverEndpoint && user.pushSubscription) {
|
||||
if (subscriptionServerKey === currentServerKey && subscription.endpoint === serverEndpoint && (!force && user.pushSubscription)) {
|
||||
return Promise.resolve(user.pushSubscription)
|
||||
}
|
||||
else if (user.pushSubscription) {
|
||||
// if we have a subscription, but it is not valid, we need to remove it
|
||||
return unsubscribeFromBackend(false)
|
||||
// if we have a subscription, but it is not valid or forcing renew, we need to remove it
|
||||
// we need to prevent removing push notification data
|
||||
return unsubscribeFromBackend(false, false)
|
||||
.catch(removePushNotificationDataOnError)
|
||||
.then(() => subscribe(registration, vapidKey))
|
||||
.then(subscription => sendSubscriptionToBackend(subscription, notificationData))
|
||||
.then(subscription => sendSubscriptionToBackend(subscription, notificationData, policy))
|
||||
}
|
||||
}
|
||||
|
||||
return subscribe(registration, vapidKey).then(
|
||||
subscription => sendSubscriptionToBackend(subscription, notificationData),
|
||||
subscription => sendSubscriptionToBackend(subscription, notificationData, policy),
|
||||
)
|
||||
})
|
||||
.catch((error) => {
|
||||
|
@ -92,18 +97,30 @@ async function subscribe(
|
|||
})
|
||||
}
|
||||
|
||||
async function unsubscribeFromBackend(fromSWPushManager: boolean) {
|
||||
async function unsubscribeFromBackend(fromSWPushManager: boolean, removePushNotification = true) {
|
||||
const cu = currentUser.value
|
||||
if (cu) {
|
||||
await removePushNotifications(cu)
|
||||
removePushNotification && await removePushNotificationData(cu, fromSWPushManager)
|
||||
}
|
||||
}
|
||||
|
||||
async function removePushNotificationDataOnError(e: Error) {
|
||||
const cu = currentUser.value
|
||||
if (cu)
|
||||
await removePushNotifications(cu, fromSWPushManager)
|
||||
await removePushNotificationData(cu, true)
|
||||
|
||||
throw e
|
||||
}
|
||||
|
||||
async function sendSubscriptionToBackend(
|
||||
subscription: PushSubscription,
|
||||
data: CreatePushNotification,
|
||||
policy: SubscriptionPolicy,
|
||||
): Promise<MastoPushSubscription> {
|
||||
const { endpoint, keys } = subscription.toJSON()
|
||||
const params: CreatePushSubscriptionParams = {
|
||||
policy,
|
||||
subscription: {
|
||||
endpoint: endpoint!,
|
||||
keys: {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import type { SubscriptionPolicy } from 'masto'
|
||||
import type {
|
||||
CreatePushNotification,
|
||||
PushNotificationPolicy,
|
||||
|
@ -14,6 +15,7 @@ const supportsPushNotifications = typeof window !== 'undefined'
|
|||
&& 'getKey' in PushSubscription.prototype
|
||||
|
||||
export const usePushManager = () => {
|
||||
const masto = useMasto()
|
||||
const isSubscribed = ref(false)
|
||||
const notificationPermission = ref<PermissionState | undefined>(
|
||||
Notification.permission === 'denied'
|
||||
|
@ -59,7 +61,7 @@ export const usePushManager = () => {
|
|||
}
|
||||
}, { immediate: true, flush: 'post' })
|
||||
|
||||
const subscribe = async (notificationData?: CreatePushNotification): Promise<SubscriptionResult> => {
|
||||
const subscribe = async (notificationData?: CreatePushNotification, policy?: SubscriptionPolicy, force?: boolean): Promise<SubscriptionResult> => {
|
||||
if (!isSupported || !currentUser.value)
|
||||
return 'invalid-state'
|
||||
|
||||
|
@ -90,18 +92,22 @@ export const usePushManager = () => {
|
|||
return 'notification-denied'
|
||||
}
|
||||
|
||||
currentUser.value.pushSubscription = await createPushSubscription({
|
||||
pushSubscription, server, token, vapidKey,
|
||||
}, notificationData ?? {
|
||||
alerts: {
|
||||
follow: true,
|
||||
favourite: true,
|
||||
reblog: true,
|
||||
mention: true,
|
||||
poll: true,
|
||||
currentUser.value.pushSubscription = await createPushSubscription(
|
||||
{
|
||||
pushSubscription, server, token, vapidKey,
|
||||
},
|
||||
policy: 'all',
|
||||
})
|
||||
notificationData ?? {
|
||||
alerts: {
|
||||
follow: true,
|
||||
favourite: true,
|
||||
reblog: true,
|
||||
mention: true,
|
||||
poll: true,
|
||||
},
|
||||
},
|
||||
policy ?? 'all',
|
||||
force,
|
||||
)
|
||||
await nextTick()
|
||||
notificationPermission.value = permission
|
||||
hiddenNotification.value[acct] = true
|
||||
|
@ -116,9 +122,17 @@ export const usePushManager = () => {
|
|||
await removePushNotifications(currentUser.value)
|
||||
}
|
||||
|
||||
const saveSettings = async () => {
|
||||
const saveSettings = async (policy?: SubscriptionPolicy) => {
|
||||
if (policy)
|
||||
pushNotificationData.value.policy = policy
|
||||
|
||||
commit()
|
||||
configuredPolicy.value[currentUser.value!.account.acct ?? ''] = pushNotificationData.value.policy
|
||||
|
||||
if (policy)
|
||||
configuredPolicy.value[currentUser.value!.account.acct ?? ''] = policy
|
||||
else
|
||||
configuredPolicy.value[currentUser.value!.account.acct ?? ''] = pushNotificationData.value.policy
|
||||
|
||||
await nextTick()
|
||||
clear()
|
||||
await nextTick()
|
||||
|
@ -140,19 +154,31 @@ export const usePushManager = () => {
|
|||
|
||||
const updateSubscription = async () => {
|
||||
if (currentUser.value) {
|
||||
currentUser.value.pushSubscription = await useMasto().pushSubscriptions.update({
|
||||
data: {
|
||||
alerts: {
|
||||
follow: pushNotificationData.value.follow,
|
||||
favourite: pushNotificationData.value.favourite,
|
||||
reblog: pushNotificationData.value.reblog,
|
||||
mention: pushNotificationData.value.mention,
|
||||
poll: pushNotificationData.value.poll,
|
||||
},
|
||||
policy: pushNotificationData.value.policy,
|
||||
const previous = history.value[0].snapshot
|
||||
const data = {
|
||||
alerts: {
|
||||
follow: pushNotificationData.value.follow,
|
||||
favourite: pushNotificationData.value.favourite,
|
||||
reblog: pushNotificationData.value.reblog,
|
||||
mention: pushNotificationData.value.mention,
|
||||
poll: pushNotificationData.value.poll,
|
||||
},
|
||||
})
|
||||
await saveSettings()
|
||||
}
|
||||
|
||||
const policy = pushNotificationData.value.policy
|
||||
|
||||
const policyChanged = previous.policy !== policy
|
||||
|
||||
// to change policy we need to resubscribe
|
||||
if (policyChanged)
|
||||
await subscribe(data, policy, true)
|
||||
else
|
||||
currentUser.value.pushSubscription = await masto.pushSubscriptions.update({ data })
|
||||
|
||||
policyChanged && await nextTick()
|
||||
|
||||
// force change policy when changed: watch is resetting it on push subscription update
|
||||
await saveSettings(policyChanged ? policy : undefined)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue