2023-01-08 09:21:09 +03:00
|
|
|
import type { mastodon } from 'masto'
|
2023-02-03 19:01:43 +03:00
|
|
|
|
2022-12-18 02:29:16 +03:00
|
|
|
import type {
|
|
|
|
CreatePushNotification,
|
|
|
|
PushNotificationPolicy,
|
|
|
|
PushNotificationRequest,
|
|
|
|
SubscriptionResult,
|
|
|
|
} from '~/composables/push-notifications/types'
|
|
|
|
import { STORAGE_KEY_NOTIFICATION, STORAGE_KEY_NOTIFICATION_POLICY } from '~/constants'
|
|
|
|
|
|
|
|
const supportsPushNotifications = typeof window !== 'undefined'
|
2022-12-28 17:57:06 +03:00
|
|
|
&& 'serviceWorker' in navigator
|
|
|
|
&& 'PushManager' in window
|
|
|
|
&& 'getKey' in PushSubscription.prototype
|
2022-12-18 02:29:16 +03:00
|
|
|
|
2023-03-30 22:01:24 +03:00
|
|
|
export function usePushManager() {
|
2024-02-21 18:20:08 +03:00
|
|
|
const { client } = useMasto()
|
2022-12-18 02:29:16 +03:00
|
|
|
const isSubscribed = ref(false)
|
|
|
|
const notificationPermission = ref<PermissionState | undefined>(
|
|
|
|
Notification.permission === 'denied'
|
|
|
|
? 'denied'
|
|
|
|
: Notification.permission === 'granted'
|
|
|
|
? 'granted'
|
|
|
|
: Notification.permission === 'default'
|
|
|
|
? 'prompt'
|
|
|
|
: undefined,
|
|
|
|
)
|
2024-02-21 18:20:08 +03:00
|
|
|
const isSupported = computed(() => supportsPushNotifications)
|
2022-12-18 02:29:16 +03:00
|
|
|
const hiddenNotification = useLocalStorage<PushNotificationRequest>(STORAGE_KEY_NOTIFICATION, {})
|
|
|
|
const configuredPolicy = useLocalStorage<PushNotificationPolicy>(STORAGE_KEY_NOTIFICATION_POLICY, {})
|
2023-02-03 19:01:43 +03:00
|
|
|
const pushNotificationData = ref(createRawSettings(
|
|
|
|
currentUser.value?.pushSubscription,
|
|
|
|
configuredPolicy.value[currentUser.value?.account?.acct ?? ''],
|
|
|
|
))
|
|
|
|
const oldPushNotificationData = ref(createRawSettings(
|
|
|
|
currentUser.value?.pushSubscription,
|
|
|
|
configuredPolicy.value[currentUser.value?.account?.acct ?? ''],
|
|
|
|
))
|
2022-12-18 02:29:16 +03:00
|
|
|
const saveEnabled = computed(() => {
|
|
|
|
const current = pushNotificationData.value
|
2023-02-03 19:01:43 +03:00
|
|
|
const previous = oldPushNotificationData.value
|
2022-12-18 02:29:16 +03:00
|
|
|
return current.favourite !== previous.favourite
|
|
|
|
|| current.reblog !== previous.reblog
|
|
|
|
|| current.mention !== previous.mention
|
|
|
|
|| current.follow !== previous.follow
|
|
|
|
|| current.poll !== previous.poll
|
|
|
|
|| current.policy !== previous.policy
|
|
|
|
})
|
|
|
|
|
|
|
|
watch(() => currentUser.value?.pushSubscription, (subscription) => {
|
|
|
|
isSubscribed.value = !!subscription
|
2023-02-03 19:01:43 +03:00
|
|
|
pushNotificationData.value = createRawSettings(
|
|
|
|
subscription,
|
|
|
|
configuredPolicy.value[currentUser.value?.account?.acct ?? ''],
|
|
|
|
)
|
|
|
|
oldPushNotificationData.value = createRawSettings(
|
|
|
|
subscription,
|
|
|
|
configuredPolicy.value[currentUser.value?.account?.acct ?? ''],
|
|
|
|
)
|
2022-12-18 02:29:16 +03:00
|
|
|
}, { immediate: true, flush: 'post' })
|
|
|
|
|
2023-01-01 22:24:22 +03:00
|
|
|
const subscribe = async (
|
|
|
|
notificationData?: CreatePushNotification,
|
2024-01-09 11:56:15 +03:00
|
|
|
policy?: mastodon.v1.WebPushSubscriptionPolicy,
|
2023-01-01 22:24:22 +03:00
|
|
|
force?: boolean,
|
|
|
|
): Promise<SubscriptionResult> => {
|
2024-03-11 13:53:25 +03:00
|
|
|
if (!isSupported.value)
|
2023-01-01 22:24:22 +03:00
|
|
|
return 'not-supported'
|
|
|
|
|
|
|
|
if (!currentUser.value)
|
|
|
|
return 'no-user'
|
2022-12-18 02:29:16 +03:00
|
|
|
|
|
|
|
const { pushSubscription, server, token, vapidKey, account: { acct } } = currentUser.value
|
|
|
|
|
|
|
|
if (!token || !server || !vapidKey)
|
2023-01-01 22:24:22 +03:00
|
|
|
return 'invalid-vapid-key'
|
2022-12-18 02:29:16 +03:00
|
|
|
|
2023-01-01 22:24:22 +03:00
|
|
|
// always request permission, browsers should remember user decision
|
|
|
|
const permission = await Promise.resolve(Notification.requestPermission()).then((p) => {
|
|
|
|
return p === 'default' ? 'prompt' : p
|
|
|
|
})
|
2022-12-18 02:29:16 +03:00
|
|
|
|
2023-01-01 22:24:22 +03:00
|
|
|
if (permission === 'denied') {
|
2022-12-18 02:29:16 +03:00
|
|
|
notificationPermission.value = permission
|
|
|
|
return 'notification-denied'
|
|
|
|
}
|
|
|
|
|
2022-12-22 16:48:20 +03:00
|
|
|
currentUser.value.pushSubscription = await createPushSubscription(
|
|
|
|
{
|
2024-03-05 17:48:58 +03:00
|
|
|
pushSubscription,
|
|
|
|
server,
|
|
|
|
token,
|
|
|
|
vapidKey,
|
2022-12-18 02:29:16 +03:00
|
|
|
},
|
2022-12-22 16:48:20 +03:00
|
|
|
notificationData ?? {
|
|
|
|
alerts: {
|
|
|
|
follow: true,
|
|
|
|
favourite: true,
|
|
|
|
reblog: true,
|
|
|
|
mention: true,
|
|
|
|
poll: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
policy ?? 'all',
|
|
|
|
force,
|
|
|
|
)
|
2022-12-18 02:29:16 +03:00
|
|
|
await nextTick()
|
|
|
|
notificationPermission.value = permission
|
|
|
|
hiddenNotification.value[acct] = true
|
|
|
|
|
|
|
|
return 'subscribed'
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsubscribe = async () => {
|
2024-03-11 13:53:25 +03:00
|
|
|
if (!isSupported.value || !isSubscribed.value || !currentUser.value)
|
2022-12-18 02:29:16 +03:00
|
|
|
return false
|
|
|
|
|
|
|
|
await removePushNotifications(currentUser.value)
|
2022-12-22 20:22:00 +03:00
|
|
|
await removePushNotificationData(currentUser.value)
|
2022-12-18 02:29:16 +03:00
|
|
|
}
|
|
|
|
|
2024-01-09 11:56:15 +03:00
|
|
|
const saveSettings = async (policy?: mastodon.v1.WebPushSubscriptionPolicy) => {
|
2022-12-22 16:48:20 +03:00
|
|
|
if (policy)
|
|
|
|
pushNotificationData.value.policy = policy
|
|
|
|
|
2023-02-03 19:01:43 +03:00
|
|
|
const current = pushNotificationData.value
|
|
|
|
oldPushNotificationData.value = {
|
|
|
|
favourite: current.favourite,
|
|
|
|
reblog: current.reblog,
|
|
|
|
mention: current.mention,
|
|
|
|
follow: current.follow,
|
|
|
|
poll: current.poll,
|
|
|
|
policy: current.policy,
|
|
|
|
}
|
2022-12-22 16:48:20 +03:00
|
|
|
|
|
|
|
if (policy)
|
|
|
|
configuredPolicy.value[currentUser.value!.account.acct ?? ''] = policy
|
|
|
|
else
|
|
|
|
configuredPolicy.value[currentUser.value!.account.acct ?? ''] = pushNotificationData.value.policy
|
|
|
|
|
2022-12-18 02:29:16 +03:00
|
|
|
await nextTick()
|
|
|
|
}
|
|
|
|
|
|
|
|
const undoChanges = () => {
|
2023-02-03 19:01:43 +03:00
|
|
|
const previous = oldPushNotificationData.value
|
|
|
|
pushNotificationData.value = {
|
|
|
|
favourite: previous.favourite,
|
|
|
|
reblog: previous.reblog,
|
|
|
|
mention: previous.mention,
|
|
|
|
follow: previous.follow,
|
|
|
|
poll: previous.poll,
|
|
|
|
policy: previous.policy,
|
|
|
|
}
|
2022-12-18 02:29:16 +03:00
|
|
|
configuredPolicy.value[currentUser.value!.account.acct ?? ''] = previous.policy
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateSubscription = async () => {
|
|
|
|
if (currentUser.value) {
|
2023-02-03 19:01:43 +03:00
|
|
|
const previous = oldPushNotificationData.value
|
|
|
|
// const previous = history.value[0].snapshot
|
2022-12-22 16:48:20 +03:00
|
|
|
const data = {
|
|
|
|
alerts: {
|
|
|
|
follow: pushNotificationData.value.follow,
|
|
|
|
favourite: pushNotificationData.value.favourite,
|
|
|
|
reblog: pushNotificationData.value.reblog,
|
|
|
|
mention: pushNotificationData.value.mention,
|
|
|
|
poll: pushNotificationData.value.poll,
|
2022-12-18 02:29:16 +03:00
|
|
|
},
|
2022-12-22 16:48:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-02-21 18:20:08 +03:00
|
|
|
currentUser.value.pushSubscription = await client.value.v1.push.subscription.update({ data })
|
2022-12-22 16:48:20 +03:00
|
|
|
|
2024-08-16 17:52:08 +03:00
|
|
|
if (policyChanged)
|
|
|
|
await nextTick()
|
2022-12-22 16:48:20 +03:00
|
|
|
|
|
|
|
// force change policy when changed: watch is resetting it on push subscription update
|
|
|
|
await saveSettings(policyChanged ? policy : undefined)
|
2022-12-18 02:29:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
pushNotificationData,
|
|
|
|
saveEnabled,
|
|
|
|
undoChanges,
|
|
|
|
hiddenNotification,
|
|
|
|
isSupported,
|
|
|
|
isSubscribed,
|
|
|
|
notificationPermission,
|
|
|
|
updateSubscription,
|
|
|
|
subscribe,
|
|
|
|
unsubscribe,
|
|
|
|
}
|
|
|
|
}
|
2023-02-03 19:01:43 +03:00
|
|
|
|
|
|
|
function createRawSettings(
|
|
|
|
pushSubscription?: mastodon.v1.WebPushSubscription,
|
2024-01-09 11:56:15 +03:00
|
|
|
subscriptionPolicy?: mastodon.v1.WebPushSubscriptionPolicy,
|
2023-02-03 19:01:43 +03:00
|
|
|
) {
|
|
|
|
return {
|
|
|
|
follow: pushSubscription?.alerts.follow ?? true,
|
|
|
|
favourite: pushSubscription?.alerts.favourite ?? true,
|
|
|
|
reblog: pushSubscription?.alerts.reblog ?? true,
|
|
|
|
mention: pushSubscription?.alerts.mention ?? true,
|
|
|
|
poll: pushSubscription?.alerts.poll ?? true,
|
|
|
|
policy: subscriptionPolicy ?? 'all',
|
|
|
|
}
|
|
|
|
}
|