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