Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 52 additions & 33 deletions composables/push-notifications/usePushManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { mastodon } from 'masto'

import type {
CreatePushNotification,
PushNotificationPolicy,
Expand Down Expand Up @@ -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
Expand All @@ -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 (
Expand Down Expand Up @@ -121,35 +120,41 @@ 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
else
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,
Expand Down Expand Up @@ -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',
}
}