1
0
Fork 0

Fix policy change not working for push notifications

1. Turns out `policy` needs to be inside `data` hash
2. namedItem(policy) → namedItem('policy')

Super embarrassed that these bugs exist for 7 months since push notifications release.
This commit is contained in:
Lim Chee Aun 2024-03-25 09:20:51 +08:00
parent 27a7bc7627
commit 9d16c6c12a

View file

@ -690,9 +690,10 @@ function PushNotificationsSection({ onClose }) {
) { ) {
setAllowNotifications(true); setAllowNotifications(true);
const { alerts, policy } = backendSubscription; const { alerts, policy } = backendSubscription;
console.log('backendSubscription', backendSubscription);
previousPolicyRef.current = policy; previousPolicyRef.current = policy;
const { elements } = pushFormRef.current; const { elements } = pushFormRef.current;
const policyEl = elements.namedItem(policy); const policyEl = elements.namedItem('policy');
if (policyEl) policyEl.value = policy; if (policyEl) policyEl.value = policy;
// alerts is {}, iterate it // alerts is {}, iterate it
Object.keys(alerts).forEach((alert) => { Object.keys(alerts).forEach((alert) => {
@ -721,65 +722,68 @@ function PushNotificationsSection({ onClose }) {
<form <form
ref={pushFormRef} ref={pushFormRef}
onChange={() => { onChange={() => {
const values = Object.fromEntries(new FormData(pushFormRef.current)); setTimeout(() => {
const allowNotifications = !!values['policy-allow']; const values = Object.fromEntries(new FormData(pushFormRef.current));
const params = { const allowNotifications = !!values['policy-allow'];
policy: values.policy, const params = {
data: { data: {
alerts: { policy: values.policy,
mention: !!values.mention, alerts: {
favourite: !!values.favourite, mention: !!values.mention,
reblog: !!values.reblog, favourite: !!values.favourite,
follow: !!values.follow, reblog: !!values.reblog,
follow_request: !!values.followRequest, follow: !!values.follow,
poll: !!values.poll, follow_request: !!values.followRequest,
update: !!values.update, poll: !!values.poll,
status: !!values.status, update: !!values.update,
status: !!values.status,
},
}, },
}, };
};
let alertsCount = 0; let alertsCount = 0;
// Remove false values from data.alerts // Remove false values from data.alerts
// API defaults to false anyway // API defaults to false anyway
Object.keys(params.data.alerts).forEach((key) => { Object.keys(params.data.alerts).forEach((key) => {
if (!params.data.alerts[key]) { if (!params.data.alerts[key]) {
delete params.data.alerts[key]; delete params.data.alerts[key];
} else { } else {
alertsCount++; alertsCount++;
} }
}); });
const policyChanged = previousPolicyRef.current !== params.policy; const policyChanged =
previousPolicyRef.current !== params.data.policy;
console.log('PN Form', { console.log('PN Form', {
values, values,
allowNotifications: allowNotifications, allowNotifications: allowNotifications,
params, params,
}); });
if (allowNotifications && alertsCount > 0) { if (allowNotifications && alertsCount > 0) {
if (policyChanged) { if (policyChanged) {
console.debug('Policy changed.'); console.debug('Policy changed.');
removeSubscription() removeSubscription()
.then(() => { .then(() => {
updateSubscription(params); updateSubscription(params);
}) })
.catch((err) => { .catch((err) => {
console.warn(err);
alert('Failed to update subscription. Please try again.');
});
} else {
updateSubscription(params).catch((err) => {
console.warn(err); console.warn(err);
alert('Failed to update subscription. Please try again.'); alert('Failed to update subscription. Please try again.');
}); });
}
} else { } else {
updateSubscription(params).catch((err) => { removeSubscription().catch((err) => {
console.warn(err); console.warn(err);
alert('Failed to update subscription. Please try again.'); alert('Failed to remove subscription. Please try again.');
}); });
} }
} else { }, 100);
removeSubscription().catch((err) => {
console.warn(err);
alert('Failed to remove subscription. Please try again.');
});
}
}} }}
> >
<h3>Push Notifications (beta)</h3> <h3>Push Notifications (beta)</h3>