From 5e90a0d8f3d4f7afbb02e0a57ed855ed6fad8f75 Mon Sep 17 00:00:00 2001 From: xmflsct Date: Wed, 21 Dec 2022 15:24:23 +0100 Subject: [PATCH] Attempt to fix #596 --- src/screens/Tabs/Me/Push.tsx | 15 ++------ src/screens/Tabs/Me/SettingsLanguage.tsx | 2 +- src/utils/slices/appSlice.ts | 37 ++++++++++++++----- src/utils/slices/instances/push/register.ts | 4 +- src/utils/slices/instances/push/unregister.ts | 2 +- src/utils/slices/instances/push/utils.ts | 5 ++- .../slices/instances/updatePushDecode.ts | 4 +- 7 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/screens/Tabs/Me/Push.tsx b/src/screens/Tabs/Me/Push.tsx index 2cdc4622..242b6156 100644 --- a/src/screens/Tabs/Me/Push.tsx +++ b/src/screens/Tabs/Me/Push.tsx @@ -8,12 +8,7 @@ import { isDevelopment } from '@utils/checkEnvironment' import { useAppsQuery } from '@utils/queryHooks/apps' import { useProfileQuery } from '@utils/queryHooks/profile' import { getExpoToken, retrieveExpoToken } from '@utils/slices/appSlice' -import { - PUSH_ADMIN, - PUSH_DEFAULT, - setChannels, - usePushFeatures -} from '@utils/slices/instances/push/utils' +import { PUSH_ADMIN, PUSH_DEFAULT, usePushFeatures } from '@utils/slices/instances/push/utils' import { updateInstancePush } from '@utils/slices/instances/updatePush' import { updateInstancePushAlert } from '@utils/slices/instances/updatePushAlert' import { updateInstancePushDecode } from '@utils/slices/instances/updatePushDecode' @@ -25,7 +20,7 @@ import * as Notifications from 'expo-notifications' import * as WebBrowser from 'expo-web-browser' import React, { useState, useEffect } from 'react' import { useTranslation } from 'react-i18next' -import { AppState, Linking, Platform, ScrollView, View } from 'react-native' +import { AppState, Linking, ScrollView, View } from 'react-native' import { useSelector } from 'react-redux' const TabMePush: React.FC = () => { @@ -50,11 +45,7 @@ const TabMePush: React.FC = () => { setPushEnabled(permissions.granted) setPushCanAskAgain(permissions.canAskAgain) layoutAnimation() - - if (Platform.OS === 'android') { - await setChannels(instance) - dispatch(retrieveExpoToken()) - } + dispatch(retrieveExpoToken()) } if (appsQuery.data?.vapid_key) { diff --git a/src/screens/Tabs/Me/SettingsLanguage.tsx b/src/screens/Tabs/Me/SettingsLanguage.tsx index ee87aa16..41cfa98f 100644 --- a/src/screens/Tabs/Me/SettingsLanguage.tsx +++ b/src/screens/Tabs/Me/SettingsLanguage.tsx @@ -27,7 +27,7 @@ const TabMeSettingsLanguage: React.FC setChannels(instance, true)) } navigation.pop(1) diff --git a/src/utils/slices/appSlice.ts b/src/utils/slices/appSlice.ts index a6cc10a3..9ce39037 100644 --- a/src/utils/slices/appSlice.ts +++ b/src/utils/slices/appSlice.ts @@ -2,18 +2,35 @@ import { createAsyncThunk, createSlice } from '@reduxjs/toolkit' import { RootState } from '@root/store' import { isDevelopment } from '@utils/checkEnvironment' import * as Notifications from 'expo-notifications' +import { Platform } from 'react-native' +import { setChannels } from './instances/push/utils' +import { getInstance } from './instancesSlice' -export const retrieveExpoToken = createAsyncThunk('app/expoToken', async (): Promise => { - if (isDevelopment) { - return 'ExponentPushToken[DEVELOPMENT_1]' +export const retrieveExpoToken = createAsyncThunk( + 'app/expoToken', + async (_, { getState }): Promise => { + const instance = getInstance(getState() as RootState) + const expoToken = getExpoToken(getState() as RootState) + + if (Platform.OS === 'android') { + await setChannels(instance) + } + + if (expoToken?.length) { + return expoToken + } else { + if (isDevelopment) { + return 'ExponentPushToken[DEVELOPMENT_1]' + } + + const res = await Notifications.getExpoPushTokenAsync({ + experienceId: '@xmflsct/tooot', + applicationId: 'com.xmflsct.app.tooot' + }) + return res.data + } } - - const res = await Notifications.getExpoPushTokenAsync({ - experienceId: '@xmflsct/tooot', - applicationId: 'com.xmflsct.app.tooot' - }) - return res.data -}) +) export type AppState = { expoToken?: string diff --git a/src/utils/slices/instances/push/register.ts b/src/utils/slices/instances/push/register.ts index 93a339a8..7ef97bcf 100644 --- a/src/utils/slices/instances/push/register.ts +++ b/src/utils/slices/instances/push/register.ts @@ -28,7 +28,7 @@ const subscribe = async ({ }) => { return apiTooot({ method: 'post', - url: `/push/subscribe/${expoToken}/${instanceUrl}/${accountId}`, + url: `push/subscribe/${expoToken}/${instanceUrl}/${accountId}`, body: { accountFull, serverKey, auth } }) } @@ -97,7 +97,7 @@ const pushRegister = async ( }) if (Platform.OS === 'android') { - setChannels(instance) + setChannels(instance, true) } return Promise.resolve(auth) diff --git a/src/utils/slices/instances/push/unregister.ts b/src/utils/slices/instances/push/unregister.ts index 30dc5ac1..5301721e 100644 --- a/src/utils/slices/instances/push/unregister.ts +++ b/src/utils/slices/instances/push/unregister.ts @@ -21,7 +21,7 @@ const pushUnregister = async (state: RootState, expoToken: string) => { await apiTooot<{ endpoint: string; publicKey: string; auth: string }>({ method: 'delete', - url: `/push/unsubscribe/${expoToken}/${instance.url}/${instance.account.id}` + url: `push/unsubscribe/${expoToken}/${instance.url}/${instance.account.id}` }) if (Platform.OS === 'android') { diff --git a/src/utils/slices/instances/push/utils.ts b/src/utils/slices/instances/push/utils.ts index 443fc553..67b4fe28 100644 --- a/src/utils/slices/instances/push/utils.ts +++ b/src/utils/slices/instances/push/utils.ts @@ -62,7 +62,7 @@ export const PUSH_ADMIN = ( } }) as { type: 'admin.sign_up' | 'admin.report'; permission: number }[] -export const setChannels = async (instance: InstanceLatest) => { +export const setChannels = async (instance: InstanceLatest, reset: boolean | undefined = false) => { const account = `@${instance.account.acct}@${instance.uri}` const deleteChannel = async (type: string) => @@ -82,6 +82,9 @@ export const setChannels = async (instance: InstanceLatest) => { const profileQuery = await queryClient.fetchQuery(queryKey, queryFunctionProfile) const channelGroup = await Notifications.getNotificationChannelGroupAsync(account) + if (channelGroup && !reset) { + return + } if (!channelGroup) { await Notifications.setNotificationChannelGroupAsync(account, { name: account }) } diff --git a/src/utils/slices/instances/updatePushDecode.ts b/src/utils/slices/instances/updatePushDecode.ts index 2e8e21a7..7f68f25a 100644 --- a/src/utils/slices/instances/updatePushDecode.ts +++ b/src/utils/slices/instances/updatePushDecode.ts @@ -25,14 +25,14 @@ export const updateInstancePushDecode = createAsyncThunk( await apiTooot({ method: 'put', - url: `/push/update-decode/${expoToken}/${instance.url}/${instance.account.id}`, + url: `push/update-decode/${expoToken}/${instance.url}/${instance.account.id}`, body: { auth: !disable ? null : instance.push.keys.auth } }) if (Platform.OS === 'android') { - setChannels(instance) + setChannels(instance, true) } return Promise.resolve({ disable })