tooot/src/startup/netInfo.ts

117 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-02-20 19:12:44 +01:00
import apiInstance from '@api/instance'
2021-01-07 19:13:09 +01:00
import NetInfo from '@react-native-community/netinfo'
import { store } from '@root/store'
2022-01-16 23:26:05 +01:00
import { prefetchTimelineQuery } from '@utils/queryHooks/timeline'
import { getPreviousTab } from '@utils/slices/contextsSlice'
2021-02-20 19:12:44 +01:00
import removeInstance from '@utils/slices/instances/remove'
2021-01-23 02:41:50 +01:00
import {
2021-03-01 00:28:14 +01:00
getInstance,
2021-02-20 19:12:44 +01:00
updateInstanceAccount
2021-01-23 02:41:50 +01:00
} from '@utils/slices/instancesSlice'
2022-01-16 23:26:05 +01:00
import { onlineManager } from 'react-query'
2021-01-07 19:13:09 +01:00
import log from './log'
const netInfo = async (): Promise<{
2022-01-16 23:26:05 +01:00
connected?: boolean
2021-01-07 19:13:09 +01:00
corrupted?: string
2022-01-16 23:26:05 +01:00
} | void> => {
2021-01-07 19:13:09 +01:00
log('log', 'netInfo', 'initializing')
2021-03-15 22:30:29 +01:00
2021-01-07 19:13:09 +01:00
const netInfo = await NetInfo.fetch()
2021-03-01 00:28:14 +01:00
const instance = getInstance(store.getState())
2021-01-07 19:13:09 +01:00
2022-01-16 23:26:05 +01:00
onlineManager.setEventListener(setOnline => {
return NetInfo.addEventListener(state => {
setOnline(
typeof state.isConnected === 'boolean' ? state.isConnected : undefined
)
})
})
2021-01-07 19:13:09 +01:00
if (netInfo.isConnected) {
log('log', 'netInfo', 'network connected')
2021-03-01 00:28:14 +01:00
if (instance) {
2021-01-07 19:13:09 +01:00
log('log', 'netInfo', 'checking locally stored credentials')
2022-01-16 23:26:05 +01:00
let resVerify: Mastodon.Account
try {
resVerify = await apiInstance<Mastodon.Account>({
method: 'get',
url: `accounts/verify_credentials`
}).then(res => res.body)
} catch (error: any) {
log('error', 'netInfo', 'local credential check failed')
if (error.status && error.status == 401) {
store.dispatch(removeInstance(instance))
}
return Promise.resolve({ corrupted: error.data.error })
}
log('log', 'netInfo', 'local credential check passed')
if (resVerify.id !== instance.account.id) {
log('error', 'netInfo', 'local id does not match remote id')
store.dispatch(removeInstance(instance))
return Promise.resolve({ connected: true, corruputed: '' })
} else {
store.dispatch(
updateInstanceAccount({
acct: resVerify.acct,
avatarStatic: resVerify.avatar_static
})
)
if (instance.timelinesLookback) {
const previousTab = getPreviousTab(store.getState())
let loadPage: Extract<
App.Pages,
'Following' | 'Local' | 'LocalPublic'
> | void = undefined
if (previousTab === 'Tab-Local') {
loadPage = 'Following'
} else if (previousTab === 'Tab-Public') {
loadPage = 'LocalPublic'
2021-01-07 19:13:09 +01:00
}
2022-01-16 23:26:05 +01:00
if (loadPage) {
if (instance.timelinesLookback?.[loadPage]?.ids.length > 0) {
log(
'log',
'netInfo',
`prefetching landing page ${instance.timelinesLookback[loadPage].queryKey[1].page} ids: ${instance.timelinesLookback[loadPage].ids}`
)
const prefetch = await prefetchTimelineQuery(
instance.timelinesLookback[loadPage]
)
if (prefetch) {
log('log', 'netInfo', `prefetched from status id ${prefetch}`)
} else {
log('error', 'netInfo', 'failed to prefetch')
}
}
2021-01-07 19:13:09 +01:00
}
2022-01-16 23:26:05 +01:00
for (const page of Object.keys(instance.timelinesLookback)) {
if (page !== loadPage) {
log(
'log',
'netInfo',
`prefetching other page ${instance.timelinesLookback[page].queryKey[1].page} ids: ${instance.timelinesLookback[page].ids}`
)
prefetchTimelineQuery(instance.timelinesLookback[page])
}
}
}
return Promise.resolve({ connected: true })
}
2021-01-07 19:13:09 +01:00
} else {
log('log', 'netInfo', 'no local credential found')
2022-01-16 23:26:05 +01:00
return Promise.resolve()
2021-01-07 19:13:09 +01:00
}
} else {
log('warn', 'netInfo', 'network not connected')
2022-01-16 23:26:05 +01:00
return Promise.resolve()
2021-01-07 19:13:09 +01:00
}
}
export default netInfo