mirror of
https://github.com/tooot-app/app
synced 2025-04-24 23:18:47 +02:00
Fix #615
This commit is contained in:
parent
e5744aba06
commit
0efb7e5b70
@ -4,7 +4,7 @@ import { getGlobalStorage, setGlobalStorage } from '@utils/storage/actions'
|
|||||||
import * as Notifications from 'expo-notifications'
|
import * as Notifications from 'expo-notifications'
|
||||||
import { Platform } from 'react-native'
|
import { Platform } from 'react-native'
|
||||||
|
|
||||||
export const updateExpoToken = async () => {
|
export const updateExpoToken = async (): Promise<string> => {
|
||||||
const expoToken = getGlobalStorage.string('app.expo_token')
|
const expoToken = getGlobalStorage.string('app.expo_token')
|
||||||
|
|
||||||
if (Platform.OS === 'android') {
|
if (Platform.OS === 'android') {
|
||||||
@ -12,16 +12,19 @@ export const updateExpoToken = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (expoToken?.length) {
|
if (expoToken?.length) {
|
||||||
return Promise.resolve()
|
return Promise.resolve(expoToken)
|
||||||
} else {
|
} else {
|
||||||
if (isDevelopment) {
|
if (isDevelopment) {
|
||||||
setGlobalStorage('app.expo_token', 'ExponentPushToken[DEVELOPMENT_1]')
|
setGlobalStorage('app.expo_token', 'ExponentPushToken[DEVELOPMENT_1]')
|
||||||
return Promise.resolve()
|
return Promise.resolve('ExponentPushToken[DEVELOPMENT_1]')
|
||||||
}
|
}
|
||||||
|
|
||||||
return await Notifications.getExpoPushTokenAsync({
|
return await Notifications.getExpoPushTokenAsync({
|
||||||
experienceId: '@xmflsct/tooot',
|
experienceId: '@xmflsct/tooot',
|
||||||
applicationId: 'com.xmflsct.app.tooot'
|
applicationId: 'com.xmflsct.app.tooot'
|
||||||
}).then(({ data }) => setGlobalStorage('app.expo_token', data))
|
}).then(({ data }) => {
|
||||||
|
setGlobalStorage('app.expo_token', data)
|
||||||
|
return data
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import apiGeneral from '@utils/api/general'
|
|||||||
import apiTooot from '@utils/api/tooot'
|
import apiTooot from '@utils/api/tooot'
|
||||||
import navigationRef from '@utils/navigation/navigationRef'
|
import navigationRef from '@utils/navigation/navigationRef'
|
||||||
import {
|
import {
|
||||||
|
generateAccountKey,
|
||||||
getAccountDetails,
|
getAccountDetails,
|
||||||
getGlobalStorage,
|
getGlobalStorage,
|
||||||
setAccountStorage,
|
setAccountStorage,
|
||||||
@ -12,7 +13,7 @@ import {
|
|||||||
} from '@utils/storage/actions'
|
} from '@utils/storage/actions'
|
||||||
import { AxiosError } from 'axios'
|
import { AxiosError } from 'axios'
|
||||||
import * as Notifications from 'expo-notifications'
|
import * as Notifications from 'expo-notifications'
|
||||||
import { useEffect } from 'react'
|
import { useEffect, useRef } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import { AppState } from 'react-native'
|
import { AppState } from 'react-native'
|
||||||
import { updateExpoToken } from './updateExpoToken'
|
import { updateExpoToken } from './updateExpoToken'
|
||||||
@ -20,22 +21,32 @@ import { updateExpoToken } from './updateExpoToken'
|
|||||||
const pushUseConnect = () => {
|
const pushUseConnect = () => {
|
||||||
const { t } = useTranslation('screens')
|
const { t } = useTranslation('screens')
|
||||||
|
|
||||||
useEffect(() => {
|
const startupChecked = useRef<boolean>(false)
|
||||||
updateExpoToken()
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
const [expoToken] = useGlobalStorage.string('app.expo_token')
|
const [expoToken] = useGlobalStorage.string('app.expo_token')
|
||||||
const pushEnabledCount = getGlobalStorage.object('accounts')?.filter(account => {
|
const accounts = getGlobalStorage.object('accounts')
|
||||||
return getAccountDetails(['push'], account)?.push?.global
|
const pushEnabled = accounts
|
||||||
}).length
|
?.map(account => {
|
||||||
|
const details = getAccountDetails(['push', 'auth.domain', 'auth.account.id'], account)
|
||||||
|
if (details?.push?.global) {
|
||||||
|
return {
|
||||||
|
accountKey: generateAccountKey({
|
||||||
|
domain: details['auth.domain'],
|
||||||
|
id: details['auth.account.id']
|
||||||
|
}),
|
||||||
|
push: details.push
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter(d => !!d)
|
||||||
|
|
||||||
const connectQuery = useQuery<any, AxiosError>(
|
const connectQuery = useQuery<{ accounts: string[] } | undefined, AxiosError>(
|
||||||
['tooot', { endpoint: 'push/connect' }],
|
['tooot', { endpoint: 'push/connect' }],
|
||||||
() =>
|
() =>
|
||||||
apiTooot<Mastodon.Status>({
|
apiTooot<{ accounts: string[] } | undefined>({
|
||||||
method: 'get',
|
method: 'get',
|
||||||
url: `push/connect/${expoToken}`
|
url: `push/connect/${expoToken}`
|
||||||
}),
|
}).then(res => res.body),
|
||||||
{
|
{
|
||||||
enabled: false,
|
enabled: false,
|
||||||
retry: (failureCount, error) => {
|
retry: (failureCount, error) => {
|
||||||
@ -48,6 +59,17 @@ const pushUseConnect = () => {
|
|||||||
refetchOnWindowFocus: false,
|
refetchOnWindowFocus: false,
|
||||||
refetchOnReconnect: false,
|
refetchOnReconnect: false,
|
||||||
onSettled: () => Notifications.setBadgeCountAsync(0),
|
onSettled: () => Notifications.setBadgeCountAsync(0),
|
||||||
|
onSuccess: data => {
|
||||||
|
if (!startupChecked.current && data?.accounts) {
|
||||||
|
startupChecked.current = true
|
||||||
|
for (const acct of data.accounts) {
|
||||||
|
const matchedAcct = pushEnabled?.find(p => p?.accountKey === acct)
|
||||||
|
if (matchedAcct && !matchedAcct.push.global) {
|
||||||
|
setAccountStorage([{ key: 'push', value: { ...matchedAcct.push, global: true } }])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
onError: error => {
|
onError: error => {
|
||||||
if (error?.status == 404) {
|
if (error?.status == 404) {
|
||||||
displayMessage({
|
displayMessage({
|
||||||
@ -96,18 +118,21 @@ const pushUseConnect = () => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
Sentry.setTags({ expoToken, pushEnabledCount })
|
updateExpoToken().then(async token => {
|
||||||
|
const badgeCount = await Notifications.getBadgeCountAsync()
|
||||||
|
if (token && (pushEnabled?.length || badgeCount)) {
|
||||||
|
connectQuery.refetch()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}, [])
|
||||||
|
|
||||||
if (expoToken && pushEnabledCount) {
|
useEffect(() => {
|
||||||
connectQuery.refetch()
|
Sentry.setTags({ expoToken, pushEnabledCount: pushEnabled?.length })
|
||||||
}
|
|
||||||
|
|
||||||
const appStateListener = AppState.addEventListener('change', state => {
|
const appStateListener = AppState.addEventListener('change', state => {
|
||||||
if (expoToken && pushEnabledCount && state === 'active') {
|
if (expoToken && pushEnabled?.length && state === 'active') {
|
||||||
Notifications.getBadgeCountAsync().then(count => {
|
Notifications.getBadgeCountAsync().then(count => {
|
||||||
if (count > 0) {
|
if (count > 0) connectQuery.refetch()
|
||||||
connectQuery.refetch()
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -115,7 +140,7 @@ const pushUseConnect = () => {
|
|||||||
return () => {
|
return () => {
|
||||||
appStateListener.remove()
|
appStateListener.remove()
|
||||||
}
|
}
|
||||||
}, [expoToken, pushEnabledCount])
|
}, [expoToken, pushEnabled?.length])
|
||||||
}
|
}
|
||||||
|
|
||||||
export default pushUseConnect
|
export default pushUseConnect
|
||||||
|
Loading…
x
Reference in New Issue
Block a user