2018-10-28 23:28:22 +01:00
|
|
|
<div class="push-notifications">
|
|
|
|
{#if pushNotificationsSupport === false}
|
2019-06-20 08:00:27 +02:00
|
|
|
<p>Your browser doesn't support push notifications.</p>
|
2018-10-28 23:28:22 +01:00
|
|
|
{:elseif $notificationPermission === "denied"}
|
2019-06-20 08:00:27 +02:00
|
|
|
<p role="alert">You have denied permission to show notifications.</p>
|
|
|
|
{:elseif $loggedInInstancesInOrder.length > 1}
|
|
|
|
<p>Note that you can only have push notifications for one instance at a time.</p>
|
2018-10-28 23:28:22 +01:00
|
|
|
{/if}
|
2019-05-26 00:20:09 +02:00
|
|
|
<form id="push-notification-settings"
|
|
|
|
disabled="{!pushNotificationsSupport}"
|
|
|
|
ref:form
|
|
|
|
aria-label="Push notification settings">
|
|
|
|
{#each options as option, i (option.key)}
|
|
|
|
{#if i > 0}
|
|
|
|
<br>
|
|
|
|
{/if}
|
|
|
|
<input type="checkbox"
|
|
|
|
id="push-notifications-{option.key}"
|
|
|
|
name="{option.key}"
|
|
|
|
disabled="{!pushNotificationsSupport}"
|
|
|
|
on:change="onPushSettingsChange(event)">
|
|
|
|
<label for="push-notifications-{option.key}">{option.label}</label>
|
|
|
|
{/each}
|
2018-10-28 23:28:22 +01:00
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
<style>
|
|
|
|
.push-notifications {
|
|
|
|
background: var(--form-bg);
|
|
|
|
border: 1px solid var(--main-border);
|
|
|
|
border-radius: 4px;
|
|
|
|
display: block;
|
|
|
|
padding: 20px;
|
|
|
|
line-height: 2em;
|
|
|
|
}
|
2019-05-26 00:20:09 +02:00
|
|
|
form[disabled="true"] {
|
2018-10-28 23:28:22 +01:00
|
|
|
opacity: 0.5;
|
|
|
|
}
|
2019-05-26 00:20:09 +02:00
|
|
|
p {
|
|
|
|
margin: 0 0 10px 0;
|
2018-10-28 23:28:22 +01:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<script>
|
|
|
|
import { store } from '../../../_store/store'
|
2019-10-30 02:58:49 +01:00
|
|
|
import { importShowTextConfirmationDialog } from '../../dialog/asyncDialogs/importShowTextConfirmationDialog.js'
|
2018-10-28 23:28:22 +01:00
|
|
|
import { logOutOfInstance } from '../../../_actions/instances'
|
|
|
|
import { updatePushSubscriptionForInstance, updateAlerts } from '../../../_actions/pushSubscription'
|
2018-12-23 00:37:51 +01:00
|
|
|
import { toast } from '../../toast/toast'
|
2019-05-25 22:21:17 +02:00
|
|
|
import { get } from '../../../_utils/lodash-lite'
|
2018-10-28 23:28:22 +01:00
|
|
|
|
|
|
|
export default {
|
|
|
|
async oncreate () {
|
2019-08-03 22:49:37 +02:00
|
|
|
const { instanceName, options } = this.get()
|
2018-10-28 23:28:22 +01:00
|
|
|
await updatePushSubscriptionForInstance(instanceName)
|
|
|
|
|
2019-05-26 00:20:09 +02:00
|
|
|
const { form } = this.refs
|
2019-06-20 08:00:27 +02:00
|
|
|
const pushSubscription = this.store.getInstanceData(instanceName, 'pushSubscriptions')
|
2018-10-28 23:28:22 +01:00
|
|
|
|
2019-08-03 22:49:37 +02:00
|
|
|
for (const { key } of options) {
|
2019-05-26 00:20:09 +02:00
|
|
|
form.elements[key].checked = get(pushSubscription, ['alerts', key])
|
|
|
|
}
|
2018-10-28 23:28:22 +01:00
|
|
|
},
|
|
|
|
store: () => store,
|
2019-05-26 00:20:09 +02:00
|
|
|
data: () => ({
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
key: 'follow',
|
|
|
|
label: 'New Followers'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'favourite',
|
|
|
|
label: 'Favorites'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'reblog',
|
|
|
|
label: 'Boosts'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'mention',
|
|
|
|
label: 'Mentions'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'poll',
|
|
|
|
label: 'Poll results'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}),
|
2018-10-28 23:28:22 +01:00
|
|
|
computed: {
|
|
|
|
pushNotificationsSupport: ({ $pushNotificationsSupport }) => $pushNotificationsSupport
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
async onPushSettingsChange (e) {
|
2019-05-26 00:20:09 +02:00
|
|
|
const { instanceName, options } = this.get()
|
|
|
|
const { form } = this.refs
|
|
|
|
const alerts = {}
|
|
|
|
|
2019-08-03 22:49:37 +02:00
|
|
|
for (const { key } of options) {
|
2019-05-26 00:20:09 +02:00
|
|
|
alerts[key] = form.elements[key].checked
|
2018-10-28 23:28:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await updateAlerts(instanceName, alerts)
|
|
|
|
} catch (err) {
|
|
|
|
e.target.checked = !e.target.checked
|
|
|
|
|
|
|
|
// TODO: Better way to detect missing authorization scope
|
|
|
|
if (err.message.startsWith('403:')) {
|
2019-08-03 22:49:37 +02:00
|
|
|
const showTextConfirmationDialog = await importShowTextConfirmationDialog()
|
2019-02-19 00:43:41 +01:00
|
|
|
showTextConfirmationDialog({
|
|
|
|
text: `You need to reauthenticate in order to enable push notification. Log out of ${instanceName}?`
|
|
|
|
}).on('positive', () => {
|
|
|
|
/* no await */ logOutOfInstance(instanceName)
|
2018-10-28 23:28:22 +01:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
toast.say(`Failed to update push notification settings: ${err.message}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-23 00:37:51 +01:00
|
|
|
</script>
|