mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Try out new api services
This commit is contained in:
@ -1,53 +1,33 @@
|
||||
import apiInstance from '@api/instance'
|
||||
import apiTooot from '@api/tooot'
|
||||
import apiTooot, { TOOOT_API_DOMAIN } from '@api/tooot'
|
||||
import i18n from '@root/i18n/i18n'
|
||||
import { RootState } from '@root/store'
|
||||
import { getInstance, Instance } from '@utils/slices/instancesSlice'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import * as Random from 'expo-random'
|
||||
import { Platform } from 'react-native'
|
||||
import base64 from 'react-native-base64'
|
||||
import androidDefaults from './androidDefaults'
|
||||
|
||||
const register1 = async ({
|
||||
const subscribe = async ({
|
||||
expoToken,
|
||||
instanceUrl,
|
||||
accountId,
|
||||
accountFull
|
||||
accountFull,
|
||||
serverKey,
|
||||
auth
|
||||
}: {
|
||||
expoToken: string
|
||||
instanceUrl: string
|
||||
accountId: Mastodon.Account['id']
|
||||
accountFull: string
|
||||
}) => {
|
||||
return apiTooot<{
|
||||
endpoint: string
|
||||
keys: { public: string; private: string; auth: string }
|
||||
}>({
|
||||
method: 'post',
|
||||
service: 'push',
|
||||
url: 'register1',
|
||||
body: { expoToken, instanceUrl, accountId, accountFull },
|
||||
sentry: true
|
||||
})
|
||||
}
|
||||
|
||||
const register2 = async ({
|
||||
expoToken,
|
||||
serverKey,
|
||||
instanceUrl,
|
||||
accountId,
|
||||
removeKeys
|
||||
}: {
|
||||
expoToken: string
|
||||
serverKey: Mastodon.PushSubscription['server_key']
|
||||
instanceUrl: string
|
||||
accountId: Mastodon.Account['id']
|
||||
removeKeys: boolean
|
||||
serverKey: string
|
||||
auth: string | null
|
||||
}) => {
|
||||
return apiTooot({
|
||||
method: 'post',
|
||||
service: 'push',
|
||||
url: 'register2',
|
||||
body: { expoToken, instanceUrl, accountId, serverKey, removeKeys },
|
||||
url: `/push/subscribe/${expoToken}/${instanceUrl}/${accountId}`,
|
||||
body: { accountFull, serverKey, auth },
|
||||
sentry: true
|
||||
})
|
||||
}
|
||||
@ -55,7 +35,7 @@ const register2 = async ({
|
||||
const pushRegister = async (
|
||||
state: RootState,
|
||||
expoToken: string
|
||||
): Promise<Instance['push']['keys']> => {
|
||||
): Promise<Instance['push']['keys']['auth']> => {
|
||||
const instance = getInstance(state)
|
||||
const instanceUrl = instance?.url
|
||||
const instanceUri = instance?.uri
|
||||
@ -68,18 +48,18 @@ const pushRegister = async (
|
||||
|
||||
const accountId = instanceAccount.id
|
||||
const accountFull = `@${instanceAccount.acct}@${instanceUri}`
|
||||
const serverRes = await register1({
|
||||
expoToken,
|
||||
instanceUrl,
|
||||
accountId,
|
||||
accountFull
|
||||
})
|
||||
|
||||
const endpoint = `https://${TOOOT_API_DOMAIN}/push/send/${expoToken}/${instanceUrl}/${accountId}`
|
||||
const auth = base64.encodeFromByteArray(Random.getRandomBytes(16))
|
||||
|
||||
const alerts = instancePush.alerts
|
||||
const formData = new FormData()
|
||||
formData.append('subscription[endpoint]', serverRes.body.endpoint)
|
||||
formData.append('subscription[keys][p256dh]', serverRes.body.keys.public)
|
||||
formData.append('subscription[keys][auth]', serverRes.body.keys.auth)
|
||||
formData.append('subscription[endpoint]', endpoint)
|
||||
formData.append(
|
||||
'subscription[keys][p256dh]',
|
||||
'BO3P7Fe/FxPNijeXayVYViCoLicnnACc+a55wzcS0qIjYU++dtAl2XltgEfU5qPuXrFg5rnxBzbwQG4cAmdNLK4='
|
||||
)
|
||||
formData.append('subscription[keys][auth]', auth)
|
||||
Object.keys(alerts).map(key =>
|
||||
// @ts-ignore
|
||||
formData.append(`data[alerts][${key}]`, alerts[key].value.toString())
|
||||
@ -91,12 +71,13 @@ const pushRegister = async (
|
||||
body: formData
|
||||
})
|
||||
|
||||
await register2({
|
||||
await subscribe({
|
||||
expoToken,
|
||||
serverKey: res.body.server_key,
|
||||
instanceUrl,
|
||||
accountId,
|
||||
removeKeys: instancePush.decode.value === false
|
||||
accountFull,
|
||||
serverKey: res.body.server_key,
|
||||
auth
|
||||
})
|
||||
|
||||
if (Platform.OS === 'android') {
|
||||
@ -142,7 +123,7 @@ const pushRegister = async (
|
||||
})
|
||||
}
|
||||
|
||||
return Promise.resolve(serverRes.body.keys)
|
||||
return Promise.resolve(auth)
|
||||
}
|
||||
|
||||
export default pushRegister
|
||||
|
@ -20,14 +20,8 @@ const pushUnregister = async (state: RootState, expoToken: string) => {
|
||||
})
|
||||
|
||||
await apiTooot<{ endpoint: string; publicKey: string; auth: string }>({
|
||||
method: 'post',
|
||||
service: 'push',
|
||||
url: 'unregister',
|
||||
body: {
|
||||
expoToken,
|
||||
instanceUrl: instance.url,
|
||||
accountId: instance.account.id
|
||||
},
|
||||
method: 'delete',
|
||||
url: `/push/unsubscribe/${expoToken}/${instance.url}/${instance.account.id}`,
|
||||
sentry: true
|
||||
})
|
||||
|
||||
|
@ -10,7 +10,7 @@ export const updateInstancePush = createAsyncThunk(
|
||||
async (
|
||||
disable: boolean,
|
||||
{ getState }
|
||||
): Promise<Instance['push']['keys'] | undefined> => {
|
||||
): Promise<Instance['push']['keys']['auth'] | undefined> => {
|
||||
const state = getState() as RootState
|
||||
const expoToken = (
|
||||
await Notifications.getExpoPushTokenAsync({
|
||||
|
@ -26,14 +26,10 @@ export const updateInstancePushDecode = createAsyncThunk(
|
||||
).data
|
||||
|
||||
await apiTooot({
|
||||
method: 'post',
|
||||
service: 'push',
|
||||
url: 'update-decode',
|
||||
method: 'put',
|
||||
url: `/push/update-decode/${expoToken}/${instance.url}/${instance.account.id}`,
|
||||
body: {
|
||||
expoToken,
|
||||
instanceUrl: instance.url,
|
||||
accountId: instance.account.id,
|
||||
...(disable && { keys: instance.push.keys })
|
||||
auth: disable ? null : instance.push.keys.auth
|
||||
},
|
||||
sentry: true
|
||||
})
|
||||
|
@ -39,65 +39,37 @@ export type Instance = {
|
||||
poll: boolean
|
||||
follow_request: boolean
|
||||
}
|
||||
push:
|
||||
| {
|
||||
global: { loading: boolean; value: boolean }
|
||||
decode: { loading: boolean; value: true }
|
||||
alerts: {
|
||||
follow: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['follow']
|
||||
}
|
||||
favourite: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['favourite']
|
||||
}
|
||||
reblog: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['reblog']
|
||||
}
|
||||
mention: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['mention']
|
||||
}
|
||||
poll: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['poll']
|
||||
}
|
||||
}
|
||||
keys: {
|
||||
auth: string
|
||||
public: string
|
||||
private: string
|
||||
}
|
||||
push: {
|
||||
global: { loading: boolean; value: boolean }
|
||||
decode: { loading: boolean; value: boolean }
|
||||
alerts: {
|
||||
follow: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['follow']
|
||||
}
|
||||
| {
|
||||
global: { loading: boolean; value: boolean }
|
||||
decode: { loading: boolean; value: false }
|
||||
alerts: {
|
||||
follow: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['follow']
|
||||
}
|
||||
favourite: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['favourite']
|
||||
}
|
||||
reblog: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['reblog']
|
||||
}
|
||||
mention: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['mention']
|
||||
}
|
||||
poll: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['poll']
|
||||
}
|
||||
}
|
||||
keys: undefined
|
||||
favourite: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['favourite']
|
||||
}
|
||||
reblog: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['reblog']
|
||||
}
|
||||
mention: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['mention']
|
||||
}
|
||||
poll: {
|
||||
loading: boolean
|
||||
value: Mastodon.PushSubscription['alerts']['poll']
|
||||
}
|
||||
}
|
||||
keys: {
|
||||
auth?: string
|
||||
public?: string // legacy
|
||||
private?: string // legacy
|
||||
}
|
||||
}
|
||||
drafts: ComposeStateDraft[]
|
||||
}
|
||||
|
||||
@ -273,7 +245,7 @@ const instancesSlice = createSlice({
|
||||
const activeIndex = findInstanceActive(state.instances)
|
||||
state.instances[activeIndex].push.global.loading = false
|
||||
state.instances[activeIndex].push.global.value = action.meta.arg
|
||||
state.instances[activeIndex].push.keys = action.payload
|
||||
state.instances[activeIndex].push.keys = { auth: action.payload }
|
||||
})
|
||||
.addCase(updateInstancePush.rejected, state => {
|
||||
const activeIndex = findInstanceActive(state.instances)
|
||||
|
Reference in New Issue
Block a user