1
0
mirror of https://github.com/tooot-app/app synced 2024-12-23 16:20:08 +01: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

@ -8,12 +8,7 @@ import { isDevelopment } from '@utils/checkEnvironment'
import { useAppsQuery } from '@utils/queryHooks/apps'
import { useProfileQuery } from '@utils/queryHooks/profile'
import { getExpoToken, retrieveExpoToken } from '@utils/slices/appSlice'
import {
PUSH_ADMIN,
PUSH_DEFAULT,
setChannels,
usePushFeatures
} from '@utils/slices/instances/push/utils'
import { PUSH_ADMIN, PUSH_DEFAULT, usePushFeatures } from '@utils/slices/instances/push/utils'
import { updateInstancePush } from '@utils/slices/instances/updatePush'
import { updateInstancePushAlert } from '@utils/slices/instances/updatePushAlert'
import { updateInstancePushDecode } from '@utils/slices/instances/updatePushDecode'
@ -25,7 +20,7 @@ import * as Notifications from 'expo-notifications'
import * as WebBrowser from 'expo-web-browser'
import React, { useState, useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import { AppState, Linking, Platform, ScrollView, View } from 'react-native'
import { AppState, Linking, ScrollView, View } from 'react-native'
import { useSelector } from 'react-redux'
const TabMePush: React.FC = () => {
@ -50,11 +45,7 @@ const TabMePush: React.FC = () => {
setPushEnabled(permissions.granted)
setPushCanAskAgain(permissions.canAskAgain)
layoutAnimation()
if (Platform.OS === 'android') {
await setChannels(instance)
dispatch(retrieveExpoToken())
}
dispatch(retrieveExpoToken())
}
if (appsQuery.data?.vapid_key) {

View File

@ -27,7 +27,7 @@ const TabMeSettingsLanguage: React.FC<TabMeStackScreenProps<'Tab-Me-Settings-Lan
// Update Android notification channel language
if (Platform.OS === 'android') {
instances.forEach(setChannels)
instances.forEach(instance => setChannels(instance, true))
}
navigation.pop(1)

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 })