This commit is contained in:
xmflsct 2023-08-16 23:47:00 +02:00
parent 34043fc1b3
commit c5c616e3dc
No known key found for this signature in database
GPG Key ID: 078A93AB607D85E0
4 changed files with 104 additions and 24 deletions

View File

@ -23,6 +23,20 @@ import { useTranslation } from 'react-i18next'
import { AppState, Linking, Platform, ScrollView, View } from 'react-native'
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 { colors } = useTheme()
const { t } = useTranslation('screenTabs')
@ -116,7 +130,7 @@ const TabMePush: React.FC = () => {
))
: null
const pushPath = `${expoToken}/${domain}/${accountId}`
const pushPath = getPushPath({ expoToken, domain, accountId })
const accountFull = `@${accountAcct}@${accountDomain}`
return appsQuery.isFetched ? (
@ -176,17 +190,13 @@ const TabMePush: React.FC = () => {
if (push.key?.length <= 10) {
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: {
subscription: any
data: { alerts: Mastodon.PushSubscription['alerts'] }
} = {
subscription: {
endpoint,
endpoint: getPushendpoint(pushPath),
keys: {
p256dh:
'BMn2PLpZrMefG981elzG6SB1EY9gU7QZwmtZ/a/J2vUeWG+zXgeskMPwHh4T/bxsD4l7/8QT94F57CbZqYRRfJo=',

View File

@ -6,7 +6,7 @@ import { useNavigation } from '@react-navigation/native'
import { androidActionSheetStyles } from '@utils/helpers/androidActionSheetStyles'
import { urlMatcher } from '@utils/helpers/urlMatcher'
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 { useTheme } from '@utils/styles/ThemeManager'
import React from 'react'
@ -59,6 +59,17 @@ const SettingsDev: React.FC = () => {
}}
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
type='text'
content={'Purge MMKV'}
@ -87,7 +98,7 @@ const SettingsDev: React.FC = () => {
content={'Crash test'}
style={{
marginHorizontal: StyleConstants.Spacing.Global.PagePadding * 2,
marginBottom: StyleConstants.Spacing.Global.PagePadding * 2
marginBottom: StyleConstants.Spacing.Global.PagePadding
}}
destructive
onPress={() => {

View File

@ -4,6 +4,9 @@ import { getGlobalStorage, setGlobalStorage } from '@utils/storage/actions'
import * as Notifications from 'expo-notifications'
import { Platform } from 'react-native'
export const toRawExpoToken = (token: string): string =>
token.replace('ExponentPushToken[', '').replace(']', '')
export const updateExpoToken = async (): Promise<string> => {
const expoToken = getGlobalStorage.string('app.expo_token')
@ -11,24 +14,26 @@ export const updateExpoToken = async (): Promise<string> => {
await setChannels()
}
const getAndSetToken = () =>
Notifications.getExpoPushTokenAsync({
projectId: '3288313f-3ff0-496a-a5a9-d8985e7cad5f',
applicationId: 'com.xmflsct.app.tooot'
}).then(({ data }) => {
setGlobalStorage('app.expo_token', data)
return data
})
const getAndSetToken = () => {
if (isDevelopment) {
const devToken = toRawExpoToken('ExponentPushToken[DEVELOPMENT_1]')
setGlobalStorage('app.expo_token', devToken)
return devToken
} else {
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) {
getAndSetToken()
return Promise.resolve(expoToken)
} else {
if (isDevelopment) {
setGlobalStorage('app.expo_token', 'ExponentPushToken[DEVELOPMENT_1]')
return Promise.resolve('ExponentPushToken[DEVELOPMENT_1]')
}
return await getAndSetToken()
}
}

View File

@ -1,7 +1,9 @@
import { displayMessage } from '@components/Message'
import { getPushPath, getPushendpoint } from '@screens/Tabs/Me/Push'
import * as Sentry from '@sentry/react-native'
import { useQuery } from '@tanstack/react-query'
import apiGeneral from '@utils/api/general'
import apiInstance from '@utils/api/instance'
import apiTooot from '@utils/api/tooot'
import navigationRef from '@utils/navigation/navigationRef'
import {
@ -9,6 +11,7 @@ import {
getAccountDetails,
getGlobalStorage,
setAccountStorage,
setGlobalStorage,
useGlobalStorage
} from '@utils/storage/actions'
import { AxiosError } from 'axios'
@ -16,7 +19,7 @@ import * as Notifications from 'expo-notifications'
import { useEffect, useRef } from 'react'
import { useTranslation } from 'react-i18next'
import { AppState } from 'react-native'
import { updateExpoToken } from './updateExpoToken'
import { toRawExpoToken, updateExpoToken } from './updateExpoToken'
const pushUseConnect = () => {
const { t } = useTranslation('screens')
@ -34,7 +37,9 @@ const pushUseConnect = () => {
domain: details['auth.domain'],
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(() => {
updateExpoToken().then(async token => {
const badgeCount = await Notifications.getBadgeCountAsync()
if (token && (pushEnabled?.length || badgeCount)) {
if (token && !token.startsWith('ExponentPushToken') && (pushEnabled?.length || badgeCount)) {
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}` })
}
})
}, [])