1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00

Attempt to fix #596

This commit is contained in:
xmflsct
2022-12-21 15:24:23 +01:00
parent a0bfb6cb24
commit 5e90a0d8f3
7 changed files with 40 additions and 29 deletions

View File

@ -2,18 +2,35 @@ import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import { RootState } from '@root/store'
import { isDevelopment } from '@utils/checkEnvironment'
import * as Notifications from 'expo-notifications'
import { Platform } from 'react-native'
import { setChannels } from './instances/push/utils'
import { getInstance } from './instancesSlice'
export const retrieveExpoToken = createAsyncThunk('app/expoToken', async (): Promise<string> => {
if (isDevelopment) {
return 'ExponentPushToken[DEVELOPMENT_1]'
export const retrieveExpoToken = createAsyncThunk(
'app/expoToken',
async (_, { getState }): Promise<string> => {
const instance = getInstance(getState() as RootState)
const expoToken = getExpoToken(getState() as RootState)
if (Platform.OS === 'android') {
await setChannels(instance)
}
if (expoToken?.length) {
return expoToken
} else {
if (isDevelopment) {
return 'ExponentPushToken[DEVELOPMENT_1]'
}
const res = await Notifications.getExpoPushTokenAsync({
experienceId: '@xmflsct/tooot',
applicationId: 'com.xmflsct.app.tooot'
})
return res.data
}
}
const res = await Notifications.getExpoPushTokenAsync({
experienceId: '@xmflsct/tooot',
applicationId: 'com.xmflsct.app.tooot'
})
return res.data
})
)
export type AppState = {
expoToken?: string

View File

@ -28,7 +28,7 @@ const subscribe = async ({
}) => {
return apiTooot({
method: 'post',
url: `/push/subscribe/${expoToken}/${instanceUrl}/${accountId}`,
url: `push/subscribe/${expoToken}/${instanceUrl}/${accountId}`,
body: { accountFull, serverKey, auth }
})
}
@ -97,7 +97,7 @@ const pushRegister = async (
})
if (Platform.OS === 'android') {
setChannels(instance)
setChannels(instance, true)
}
return Promise.resolve(auth)

View File

@ -21,7 +21,7 @@ const pushUnregister = async (state: RootState, expoToken: string) => {
await apiTooot<{ endpoint: string; publicKey: string; auth: string }>({
method: 'delete',
url: `/push/unsubscribe/${expoToken}/${instance.url}/${instance.account.id}`
url: `push/unsubscribe/${expoToken}/${instance.url}/${instance.account.id}`
})
if (Platform.OS === 'android') {

View File

@ -62,7 +62,7 @@ export const PUSH_ADMIN = (
}
}) as { type: 'admin.sign_up' | 'admin.report'; permission: number }[]
export const setChannels = async (instance: InstanceLatest) => {
export const setChannels = async (instance: InstanceLatest, reset: boolean | undefined = false) => {
const account = `@${instance.account.acct}@${instance.uri}`
const deleteChannel = async (type: string) =>
@ -82,6 +82,9 @@ export const setChannels = async (instance: InstanceLatest) => {
const profileQuery = await queryClient.fetchQuery(queryKey, queryFunctionProfile)
const channelGroup = await Notifications.getNotificationChannelGroupAsync(account)
if (channelGroup && !reset) {
return
}
if (!channelGroup) {
await Notifications.setNotificationChannelGroupAsync(account, { name: account })
}

View File

@ -25,14 +25,14 @@ export const updateInstancePushDecode = createAsyncThunk(
await apiTooot({
method: 'put',
url: `/push/update-decode/${expoToken}/${instance.url}/${instance.account.id}`,
url: `push/update-decode/${expoToken}/${instance.url}/${instance.account.id}`,
body: {
auth: !disable ? null : instance.push.keys.auth
}
})
if (Platform.OS === 'android') {
setChannels(instance)
setChannels(instance, true)
}
return Promise.resolve({ disable })