fix: undo changes on push notifications settings stops working (#1599)

zio/stable
Joaquín Sánchez 2023-02-03 17:01:43 +01:00 committed by GitHub
parent eb5748ac65
commit 6784f3a090
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 52 additions and 33 deletions

View File

@ -1,4 +1,5 @@
import type { mastodon } from 'masto'
import type {
CreatePushNotification,
PushNotificationPolicy,
@ -27,19 +28,17 @@ export const usePushManager = () => {
const isSupported = $computed(() => supportsPushNotifications)
const hiddenNotification = useLocalStorage<PushNotificationRequest>(STORAGE_KEY_NOTIFICATION, {})
const configuredPolicy = useLocalStorage<PushNotificationPolicy>(STORAGE_KEY_NOTIFICATION_POLICY, {})
const pushNotificationData = ref({
follow: currentUser.value?.pushSubscription?.alerts.follow ?? true,
favourite: currentUser.value?.pushSubscription?.alerts.favourite ?? true,
reblog: currentUser.value?.pushSubscription?.alerts.reblog ?? true,
mention: currentUser.value?.pushSubscription?.alerts.mention ?? true,
poll: currentUser.value?.pushSubscription?.alerts.poll ?? true,
policy: configuredPolicy.value[currentUser.value?.account?.acct ?? ''] ?? 'all',
})
// don't clone, we're using indexeddb
const { history, commit, clear } = useManualRefHistory(pushNotificationData)
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 ?? ''],
))
const saveEnabled = computed(() => {
const current = pushNotificationData.value
const previous = history.value?.[0]?.snapshot
const previous = oldPushNotificationData.value
return current.favourite !== previous.favourite
|| current.reblog !== previous.reblog
|| current.mention !== previous.mention
@ -50,14 +49,14 @@ export const usePushManager = () => {
watch(() => currentUser.value?.pushSubscription, (subscription) => {
isSubscribed.value = !!subscription
pushNotificationData.value = {
follow: subscription?.alerts.follow ?? false,
favourite: subscription?.alerts.favourite ?? false,
reblog: subscription?.alerts.reblog ?? false,
mention: subscription?.alerts.mention ?? false,
poll: subscription?.alerts.poll ?? false,
policy: configuredPolicy.value[currentUser.value?.account?.acct ?? ''] ?? 'all',
}
pushNotificationData.value = createRawSettings(
subscription,
configuredPolicy.value[currentUser.value?.account?.acct ?? ''],
)
oldPushNotificationData.value = createRawSettings(
subscription,
configuredPolicy.value[currentUser.value?.account?.acct ?? ''],
)
}, { immediate: true, flush: 'post' })
const subscribe = async (
@ -121,7 +120,15 @@ export const usePushManager = () => {
if (policy)
pushNotificationData.value.policy = policy
commit()
const current = pushNotificationData.value
oldPushNotificationData.value = {
favourite: current.favourite,
reblog: current.reblog,
mention: current.mention,
follow: current.follow,
poll: current.poll,
policy: current.policy,
}
if (policy)
configuredPolicy.value[currentUser.value!.account.acct ?? ''] = policy
@ -129,27 +136,25 @@ export const usePushManager = () => {
configuredPolicy.value[currentUser.value!.account.acct ?? ''] = pushNotificationData.value.policy
await nextTick()
clear()
await nextTick()
}
const undoChanges = () => {
const current = pushNotificationData.value
const previous = history.value[0].snapshot
current.favourite = previous.favourite
current.reblog = previous.reblog
current.mention = previous.mention
current.follow = previous.follow
current.poll = previous.poll
current.policy = previous.policy
const previous = oldPushNotificationData.value
pushNotificationData.value = {
favourite: previous.favourite,
reblog: previous.reblog,
mention: previous.mention,
follow: previous.follow,
poll: previous.poll,
policy: previous.policy,
}
configuredPolicy.value[currentUser.value!.account.acct ?? ''] = previous.policy
commit()
clear()
}
const updateSubscription = async () => {
if (currentUser.value) {
const previous = history.value[0].snapshot
const previous = oldPushNotificationData.value
// const previous = history.value[0].snapshot
const data = {
alerts: {
follow: pushNotificationData.value.follow,
@ -190,3 +195,17 @@ export const usePushManager = () => {
unsubscribe,
}
}
function createRawSettings(
pushSubscription?: mastodon.v1.WebPushSubscription,
subscriptionPolicy?: mastodon.v1.SubscriptionPolicy,
) {
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',
}
}