2021-02-27 16:33:54 +01:00
|
|
|
import apiInstance from '@api/instance'
|
2021-06-21 11:59:29 +02:00
|
|
|
import apiTooot from '@api/tooot'
|
2021-02-27 16:33:54 +01:00
|
|
|
import { RootState } from '@root/store'
|
2021-06-21 11:59:29 +02:00
|
|
|
import { getInstance } from '@utils/slices/instancesSlice'
|
2021-03-02 01:17:06 +01:00
|
|
|
import * as Notifications from 'expo-notifications'
|
|
|
|
import { Platform } from 'react-native'
|
2021-02-27 16:33:54 +01:00
|
|
|
|
|
|
|
const pushUnregister = async (state: RootState, expoToken: string) => {
|
|
|
|
const instance = getInstance(state)
|
2021-03-02 01:17:06 +01:00
|
|
|
const instanceUri = instance?.uri
|
|
|
|
const instanceAccount = instance?.account
|
2021-02-27 16:33:54 +01:00
|
|
|
|
|
|
|
if (!instance?.url || !instance.account.id) {
|
|
|
|
return Promise.reject()
|
|
|
|
}
|
|
|
|
|
|
|
|
await apiInstance<{}>({
|
|
|
|
method: 'delete',
|
|
|
|
url: 'push/subscription'
|
|
|
|
})
|
|
|
|
|
2021-06-21 11:59:29 +02:00
|
|
|
await apiTooot<{ endpoint: string; publicKey: string; auth: string }>({
|
2021-12-06 21:25:09 +01:00
|
|
|
method: 'delete',
|
2022-01-04 09:09:45 +01:00
|
|
|
url: `/push/unsubscribe/${expoToken}/${instance.url}/${instance.account.id}`
|
2021-02-27 16:33:54 +01:00
|
|
|
})
|
|
|
|
|
2021-03-02 01:17:06 +01:00
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
const accountFull = `@${instanceAccount?.acct}@${instanceUri}`
|
|
|
|
Notifications.deleteNotificationChannelGroupAsync(accountFull)
|
|
|
|
}
|
|
|
|
|
2021-02-27 16:33:54 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
export default pushUnregister
|