From 6c6dc0ce6acaa19711cca156264bd505382d7e27 Mon Sep 17 00:00:00 2001 From: Zhiyuan Zheng Date: Mon, 6 Dec 2021 21:25:09 +0100 Subject: [PATCH] Try out new api services --- package.json | 2 + src/api/tooot.ts | 13 +-- src/utils/push/useConnect.ts | 2 +- src/utils/queryHooks/translate.ts | 6 +- src/utils/slices/instances/push/register.ts | 71 ++++++--------- src/utils/slices/instances/push/unregister.ts | 10 +-- src/utils/slices/instances/updatePush.ts | 2 +- .../slices/instances/updatePushDecode.ts | 10 +-- src/utils/slices/instancesSlice.ts | 88 +++++++------------ yarn.lock | 10 +++ 10 files changed, 82 insertions(+), 132 deletions(-) diff --git a/package.json b/package.json index dbb09029..9f35f560 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "react-i18next": "11.14.3", "react-native": "0.66.3", "react-native-animated-spinkit": "1.5.2", + "react-native-base64": "^0.2.1", "react-native-blurhash": "1.1.5", "react-native-fast-image": "8.5.11", "react-native-feather": "1.1.2", @@ -103,6 +104,7 @@ "@types/react": "17.0.37", "@types/react-dom": "17.0.11", "@types/react-native": "0.66.6", + "@types/react-native-base64": "^0.2.0", "@types/react-redux": "7.1.20", "@types/react-timeago": "4.1.3", "@types/valid-url": "1.0.3", diff --git a/src/api/tooot.ts b/src/api/tooot.ts index a8652efb..79afb1a8 100644 --- a/src/api/tooot.ts +++ b/src/api/tooot.ts @@ -6,8 +6,7 @@ import * as Sentry from 'sentry-expo' const ctx = new chalk.Instance({ level: 3 }) export type Params = { - service: 'push' | 'translate' - method: 'get' | 'post' + method: 'get' | 'post' | 'put' | 'delete' url: string params?: { [key: string]: string | number | boolean | string[] | number[] | boolean[] @@ -17,10 +16,9 @@ export type Params = { sentry?: boolean } -const DOMAIN = __DEV__ ? 'testapi.tooot.app' : 'api.tooot.app' +export const TOOOT_API_DOMAIN = __DEV__ ? 'testapi.tooot.app' : 'testapi.tooot.app' const apiTooot = async ({ - service, method, url, params, @@ -28,8 +26,6 @@ const apiTooot = async ({ body, sentry = false }: Params): Promise<{ body: T }> => { - const key = Constants.manifest?.extra?.toootApiKey - console.log( ctx.bgGreen.bold(' API tooot ') + ' ' + @@ -43,11 +39,10 @@ const apiTooot = async ({ return axios({ timeout: method === 'post' ? 1000 * 60 : 1000 * 15, method, - baseURL: `https://${DOMAIN}/`, - url: `${service}/${url}`, + baseURL: `https://${TOOOT_API_DOMAIN}/`, + url: `${url}`, params, headers: { - ...(key && { 'x-tooot-key': key }), 'Content-Type': 'application/json', 'User-Agent': `tooot/${Constants.manifest?.version}`, Accept: '*/*', diff --git a/src/utils/push/useConnect.ts b/src/utils/push/useConnect.ts index a45dd8b1..77f3ad6b 100644 --- a/src/utils/push/useConnect.ts +++ b/src/utils/push/useConnect.ts @@ -76,7 +76,7 @@ const pushUseConnect = ({ mode, t, instances, dispatch }: Params) => { const pushEnabled = instances.filter(instance => instance.push.global.value) if (pushEnabled.length) { - connect() + // connect() } }, [instances]) } diff --git a/src/utils/queryHooks/translate.ts b/src/utils/queryHooks/translate.ts index be4ddab4..7bb8e084 100644 --- a/src/utils/queryHooks/translate.ts +++ b/src/utils/queryHooks/translate.ts @@ -36,9 +36,9 @@ const queryFunction = async ({ queryKey }: { queryKey: QueryKeyTranslate }) => { const res = await apiTooot({ method: 'get', - service: 'translate', - url: `source/${uriEncoded}/target/${target}`, - headers: { original } + url: '/translate', + headers: { original }, + body: { source, target, text } }) haptics('Light') return res.body diff --git a/src/utils/slices/instances/push/register.ts b/src/utils/slices/instances/push/register.ts index 5d631303..38f1cefd 100644 --- a/src/utils/slices/instances/push/register.ts +++ b/src/utils/slices/instances/push/register.ts @@ -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 => { +): Promise => { 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 diff --git a/src/utils/slices/instances/push/unregister.ts b/src/utils/slices/instances/push/unregister.ts index da05239c..18d95e9a 100644 --- a/src/utils/slices/instances/push/unregister.ts +++ b/src/utils/slices/instances/push/unregister.ts @@ -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 }) diff --git a/src/utils/slices/instances/updatePush.ts b/src/utils/slices/instances/updatePush.ts index a3a1794c..8a5354d8 100644 --- a/src/utils/slices/instances/updatePush.ts +++ b/src/utils/slices/instances/updatePush.ts @@ -10,7 +10,7 @@ export const updateInstancePush = createAsyncThunk( async ( disable: boolean, { getState } - ): Promise => { + ): Promise => { const state = getState() as RootState const expoToken = ( await Notifications.getExpoPushTokenAsync({ diff --git a/src/utils/slices/instances/updatePushDecode.ts b/src/utils/slices/instances/updatePushDecode.ts index 7bf16181..172ca3d4 100644 --- a/src/utils/slices/instances/updatePushDecode.ts +++ b/src/utils/slices/instances/updatePushDecode.ts @@ -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 }) diff --git a/src/utils/slices/instancesSlice.ts b/src/utils/slices/instancesSlice.ts index 1272c2e1..e6755978 100644 --- a/src/utils/slices/instancesSlice.ts +++ b/src/utils/slices/instancesSlice.ts @@ -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) diff --git a/yarn.lock b/yarn.lock index 514b4b96..5385dbb2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2190,6 +2190,11 @@ dependencies: "@types/react" "*" +"@types/react-native-base64@^0.2.0": + version "0.2.0" + resolved "https://registry.yarnpkg.com/@types/react-native-base64/-/react-native-base64-0.2.0.tgz#c934e7c3cd549d4613bbfa7929a6845ab07a20af" + integrity sha512-5kUtS7Kf8Xl4zEwbqLITEuQReQTby4UOGfnsEeBFJEVmUfT+ygOv/Qmv0v6El0iV1eDhXS+/0i7CGR9d3/nRSA== + "@types/react-native@0.66.6": version "0.66.6" resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.66.6.tgz#08f5b93cca9a50b7d19f353ff2a1ebe1a7c24a79" @@ -6310,6 +6315,11 @@ react-native-animated-spinkit@1.5.2: resolved "https://registry.yarnpkg.com/react-native-animated-spinkit/-/react-native-animated-spinkit-1.5.2.tgz#b1c00ecbadf48634273e6a843f8dfbcb7c006186" integrity sha512-YCQGR3HzEQvyaAnepiyf/hv88Sta3UIZ2CFZPtFqwu+VbFJfMgjJZniOx4157TuR5AAYajEJP9Fgy+JLIU3jzQ== +react-native-base64@^0.2.1: + version "0.2.1" + resolved "https://registry.yarnpkg.com/react-native-base64/-/react-native-base64-0.2.1.tgz#3d0e73a649c4c0129f7b7695d3912456aebae847" + integrity sha512-eHgt/MA8y5ZF0aHfZ1aTPcIkDWxza9AaEk4GcpIX+ZYfZ04RcaNahO+527KR7J44/mD3efYfM23O2C1N44ByWA== + react-native-blurhash@1.1.5: version "1.1.5" resolved "https://registry.yarnpkg.com/react-native-blurhash/-/react-native-blurhash-1.1.5.tgz#27545afb126347059a3a9324f5c3640a94b1ec53"