mirror of https://github.com/tooot-app/app
Fix #754
This commit is contained in:
parent
34043fc1b3
commit
c5c616e3dc
|
@ -23,6 +23,20 @@ import { useTranslation } from 'react-i18next'
|
||||||
import { AppState, Linking, Platform, ScrollView, View } from 'react-native'
|
import { AppState, Linking, Platform, ScrollView, View } from 'react-native'
|
||||||
import { fromByteArray } from 'react-native-quick-base64'
|
import { fromByteArray } from 'react-native-quick-base64'
|
||||||
|
|
||||||
|
export const getPushPath = ({
|
||||||
|
expoToken,
|
||||||
|
domain,
|
||||||
|
accountId
|
||||||
|
}: {
|
||||||
|
expoToken?: string
|
||||||
|
domain: string
|
||||||
|
accountId: string | number
|
||||||
|
}) => `${expoToken}/${domain}/${accountId}`
|
||||||
|
export const getPushendpoint = (pushPath: string) =>
|
||||||
|
`https://${TOOOT_API_DOMAIN}/push/send/${pushPath}/${(Math.random() + 1)
|
||||||
|
.toString(36)
|
||||||
|
.substring(2)}`
|
||||||
|
|
||||||
const TabMePush: React.FC = () => {
|
const TabMePush: React.FC = () => {
|
||||||
const { colors } = useTheme()
|
const { colors } = useTheme()
|
||||||
const { t } = useTranslation('screenTabs')
|
const { t } = useTranslation('screenTabs')
|
||||||
|
@ -116,7 +130,7 @@ const TabMePush: React.FC = () => {
|
||||||
))
|
))
|
||||||
: null
|
: null
|
||||||
|
|
||||||
const pushPath = `${expoToken}/${domain}/${accountId}`
|
const pushPath = getPushPath({ expoToken, domain, accountId })
|
||||||
const accountFull = `@${accountAcct}@${accountDomain}`
|
const accountFull = `@${accountAcct}@${accountDomain}`
|
||||||
|
|
||||||
return appsQuery.isFetched ? (
|
return appsQuery.isFetched ? (
|
||||||
|
@ -176,17 +190,13 @@ const TabMePush: React.FC = () => {
|
||||||
if (push.key?.length <= 10) {
|
if (push.key?.length <= 10) {
|
||||||
authKey = fromByteArray(Crypto.getRandomBytes(16))
|
authKey = fromByteArray(Crypto.getRandomBytes(16))
|
||||||
}
|
}
|
||||||
// Turning on
|
|
||||||
const randomPath = (Math.random() + 1).toString(36).substring(2)
|
|
||||||
|
|
||||||
const endpoint = `https://${TOOOT_API_DOMAIN}/push/send/${pushPath}/${randomPath}`
|
|
||||||
|
|
||||||
const body: {
|
const body: {
|
||||||
subscription: any
|
subscription: any
|
||||||
data: { alerts: Mastodon.PushSubscription['alerts'] }
|
data: { alerts: Mastodon.PushSubscription['alerts'] }
|
||||||
} = {
|
} = {
|
||||||
subscription: {
|
subscription: {
|
||||||
endpoint,
|
endpoint: getPushendpoint(pushPath),
|
||||||
keys: {
|
keys: {
|
||||||
p256dh:
|
p256dh:
|
||||||
'BMn2PLpZrMefG981elzG6SB1EY9gU7QZwmtZ/a/J2vUeWG+zXgeskMPwHh4T/bxsD4l7/8QT94F57CbZqYRRfJo=',
|
'BMn2PLpZrMefG981elzG6SB1EY9gU7QZwmtZ/a/J2vUeWG+zXgeskMPwHh4T/bxsD4l7/8QT94F57CbZqYRRfJo=',
|
||||||
|
|
|
@ -6,7 +6,7 @@ import { useNavigation } from '@react-navigation/native'
|
||||||
import { androidActionSheetStyles } from '@utils/helpers/androidActionSheetStyles'
|
import { androidActionSheetStyles } from '@utils/helpers/androidActionSheetStyles'
|
||||||
import { urlMatcher } from '@utils/helpers/urlMatcher'
|
import { urlMatcher } from '@utils/helpers/urlMatcher'
|
||||||
import { storage } from '@utils/storage'
|
import { storage } from '@utils/storage'
|
||||||
import { getGlobalStorage, useGlobalStorage } from '@utils/storage/actions'
|
import { getGlobalStorage, setGlobalStorage, useGlobalStorage } from '@utils/storage/actions'
|
||||||
import { StyleConstants } from '@utils/styles/constants'
|
import { StyleConstants } from '@utils/styles/constants'
|
||||||
import { useTheme } from '@utils/styles/ThemeManager'
|
import { useTheme } from '@utils/styles/ThemeManager'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
@ -59,6 +59,17 @@ const SettingsDev: React.FC = () => {
|
||||||
}}
|
}}
|
||||||
onPress={() => displayMessage({ message: 'This is a testing message' })}
|
onPress={() => displayMessage({ message: 'This is a testing message' })}
|
||||||
/>
|
/>
|
||||||
|
<Button
|
||||||
|
type='text'
|
||||||
|
content={'Set ExpoToken wrapped'}
|
||||||
|
style={{
|
||||||
|
marginHorizontal: StyleConstants.Spacing.Global.PagePadding * 2,
|
||||||
|
marginBottom: StyleConstants.Spacing.Global.PagePadding * 2
|
||||||
|
}}
|
||||||
|
onPress={() => {
|
||||||
|
setGlobalStorage('app.expo_token', 'ExponentPushToken[DEVELOPMENT_1]')
|
||||||
|
}}
|
||||||
|
/>
|
||||||
<Button
|
<Button
|
||||||
type='text'
|
type='text'
|
||||||
content={'Purge MMKV'}
|
content={'Purge MMKV'}
|
||||||
|
@ -87,7 +98,7 @@ const SettingsDev: React.FC = () => {
|
||||||
content={'Crash test'}
|
content={'Crash test'}
|
||||||
style={{
|
style={{
|
||||||
marginHorizontal: StyleConstants.Spacing.Global.PagePadding * 2,
|
marginHorizontal: StyleConstants.Spacing.Global.PagePadding * 2,
|
||||||
marginBottom: StyleConstants.Spacing.Global.PagePadding * 2
|
marginBottom: StyleConstants.Spacing.Global.PagePadding
|
||||||
}}
|
}}
|
||||||
destructive
|
destructive
|
||||||
onPress={() => {
|
onPress={() => {
|
||||||
|
|
|
@ -4,6 +4,9 @@ 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 toRawExpoToken = (token: string): string =>
|
||||||
|
token.replace('ExponentPushToken[', '').replace(']', '')
|
||||||
|
|
||||||
export const updateExpoToken = async (): Promise<string> => {
|
export const updateExpoToken = async (): Promise<string> => {
|
||||||
const expoToken = getGlobalStorage.string('app.expo_token')
|
const expoToken = getGlobalStorage.string('app.expo_token')
|
||||||
|
|
||||||
|
@ -11,24 +14,26 @@ export const updateExpoToken = async (): Promise<string> => {
|
||||||
await setChannels()
|
await setChannels()
|
||||||
}
|
}
|
||||||
|
|
||||||
const getAndSetToken = () =>
|
const getAndSetToken = () => {
|
||||||
Notifications.getExpoPushTokenAsync({
|
if (isDevelopment) {
|
||||||
projectId: '3288313f-3ff0-496a-a5a9-d8985e7cad5f',
|
const devToken = toRawExpoToken('ExponentPushToken[DEVELOPMENT_1]')
|
||||||
applicationId: 'com.xmflsct.app.tooot'
|
setGlobalStorage('app.expo_token', devToken)
|
||||||
}).then(({ data }) => {
|
return devToken
|
||||||
setGlobalStorage('app.expo_token', data)
|
} else {
|
||||||
return data
|
return Notifications.getExpoPushTokenAsync({
|
||||||
})
|
projectId: '3288313f-3ff0-496a-a5a9-d8985e7cad5f',
|
||||||
|
applicationId: 'com.xmflsct.app.tooot'
|
||||||
|
}).then(({ data }) => {
|
||||||
|
setGlobalStorage('app.expo_token', toRawExpoToken(data))
|
||||||
|
return data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (expoToken?.length) {
|
if (expoToken?.length) {
|
||||||
getAndSetToken()
|
getAndSetToken()
|
||||||
return Promise.resolve(expoToken)
|
return Promise.resolve(expoToken)
|
||||||
} else {
|
} else {
|
||||||
if (isDevelopment) {
|
|
||||||
setGlobalStorage('app.expo_token', 'ExponentPushToken[DEVELOPMENT_1]')
|
|
||||||
return Promise.resolve('ExponentPushToken[DEVELOPMENT_1]')
|
|
||||||
}
|
|
||||||
|
|
||||||
return await getAndSetToken()
|
return await getAndSetToken()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import { displayMessage } from '@components/Message'
|
import { displayMessage } from '@components/Message'
|
||||||
|
import { getPushPath, getPushendpoint } from '@screens/Tabs/Me/Push'
|
||||||
import * as Sentry from '@sentry/react-native'
|
import * as Sentry from '@sentry/react-native'
|
||||||
import { useQuery } from '@tanstack/react-query'
|
import { useQuery } from '@tanstack/react-query'
|
||||||
import apiGeneral from '@utils/api/general'
|
import apiGeneral from '@utils/api/general'
|
||||||
|
import apiInstance from '@utils/api/instance'
|
||||||
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 {
|
||||||
|
@ -9,6 +11,7 @@ import {
|
||||||
getAccountDetails,
|
getAccountDetails,
|
||||||
getGlobalStorage,
|
getGlobalStorage,
|
||||||
setAccountStorage,
|
setAccountStorage,
|
||||||
|
setGlobalStorage,
|
||||||
useGlobalStorage
|
useGlobalStorage
|
||||||
} from '@utils/storage/actions'
|
} from '@utils/storage/actions'
|
||||||
import { AxiosError } from 'axios'
|
import { AxiosError } from 'axios'
|
||||||
|
@ -16,7 +19,7 @@ import * as Notifications from 'expo-notifications'
|
||||||
import { useEffect, useRef } 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 { toRawExpoToken, updateExpoToken } from './updateExpoToken'
|
||||||
|
|
||||||
const pushUseConnect = () => {
|
const pushUseConnect = () => {
|
||||||
const { t } = useTranslation('screens')
|
const { t } = useTranslation('screens')
|
||||||
|
@ -34,7 +37,9 @@ const pushUseConnect = () => {
|
||||||
domain: details['auth.domain'],
|
domain: details['auth.domain'],
|
||||||
id: details['auth.account.id']
|
id: details['auth.account.id']
|
||||||
}),
|
}),
|
||||||
push: details.push
|
push: details.push,
|
||||||
|
domain: details['auth.domain'],
|
||||||
|
accountId: details['auth.account.id']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -120,9 +125,58 @@ const pushUseConnect = () => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
updateExpoToken().then(async token => {
|
updateExpoToken().then(async token => {
|
||||||
const badgeCount = await Notifications.getBadgeCountAsync()
|
const badgeCount = await Notifications.getBadgeCountAsync()
|
||||||
if (token && (pushEnabled?.length || badgeCount)) {
|
if (token && !token.startsWith('ExponentPushToken') && (pushEnabled?.length || badgeCount)) {
|
||||||
connectQuery.refetch()
|
connectQuery.refetch()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (expoToken?.startsWith('ExponentPushToken')) {
|
||||||
|
const newToken = toRawExpoToken(expoToken)
|
||||||
|
|
||||||
|
if (pushEnabled) {
|
||||||
|
for (const account of pushEnabled) {
|
||||||
|
if (account) {
|
||||||
|
const oldSub = await apiInstance<Mastodon.PushSubscription>({
|
||||||
|
account: account.accountKey,
|
||||||
|
method: 'get',
|
||||||
|
url: 'push/subscription'
|
||||||
|
})
|
||||||
|
if (oldSub.body.id) {
|
||||||
|
const body: {
|
||||||
|
subscription: any
|
||||||
|
data: { alerts: Mastodon.PushSubscription['alerts'] }
|
||||||
|
} = {
|
||||||
|
subscription: {
|
||||||
|
endpoint: getPushendpoint(
|
||||||
|
getPushPath({
|
||||||
|
expoToken: newToken,
|
||||||
|
domain: account.domain,
|
||||||
|
accountId: account.accountId
|
||||||
|
})
|
||||||
|
),
|
||||||
|
keys: {
|
||||||
|
p256dh:
|
||||||
|
'BMn2PLpZrMefG981elzG6SB1EY9gU7QZwmtZ/a/J2vUeWG+zXgeskMPwHh4T/bxsD4l7/8QT94F57CbZqYRRfJo=',
|
||||||
|
auth: account.push.key
|
||||||
|
}
|
||||||
|
},
|
||||||
|
data: { alerts: account.push.alerts }
|
||||||
|
}
|
||||||
|
console.log(body)
|
||||||
|
await apiInstance({
|
||||||
|
account: account?.accountKey,
|
||||||
|
method: 'post',
|
||||||
|
url: 'push/subscription',
|
||||||
|
body
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
setGlobalStorage('app.expo_token', newToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
await apiTooot({ method: 'post', url: `push/migrate/${expoToken}` })
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue