mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Allow push count badge
This commit is contained in:
71
src/utils/slices/appSlice.ts
Normal file
71
src/utils/slices/appSlice.ts
Normal file
@ -0,0 +1,71 @@
|
||||
import apiGeneral from '@api/general'
|
||||
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
|
||||
import { RootState } from '@root/store'
|
||||
import { isDevelopment } from '@utils/checkEnvironment'
|
||||
import Constants from 'expo-constants'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
|
||||
export const retriveExpoToken = createAsyncThunk(
|
||||
'app/expoToken',
|
||||
async (): Promise<string> => {
|
||||
if (isDevelopment) {
|
||||
return 'DEVELOPMENT_TOKEN_1'
|
||||
}
|
||||
|
||||
const res = await Notifications.getExpoPushTokenAsync({
|
||||
experienceId: '@xmflsct/tooot',
|
||||
applicationId: 'com.xmflsct.app.tooot'
|
||||
})
|
||||
return res.data
|
||||
}
|
||||
)
|
||||
|
||||
export const retriveVersionLatest = createAsyncThunk(
|
||||
'app/versionUpdate',
|
||||
async (): Promise<string> => {
|
||||
const res = await apiGeneral<{ latest: string }>({
|
||||
method: 'get',
|
||||
domain: 'tooot.app',
|
||||
url: 'version.json'
|
||||
})
|
||||
return res.body.latest
|
||||
}
|
||||
)
|
||||
|
||||
export type AppState = {
|
||||
expoToken?: string
|
||||
versionUpdate: boolean
|
||||
}
|
||||
|
||||
export const appInitialState: AppState = {
|
||||
expoToken: undefined,
|
||||
versionUpdate: false
|
||||
}
|
||||
|
||||
const appSlice = createSlice({
|
||||
name: 'app',
|
||||
initialState: appInitialState,
|
||||
reducers: {},
|
||||
extraReducers: builder => {
|
||||
builder
|
||||
.addCase(retriveExpoToken.fulfilled, (state, action) => {
|
||||
if (action.payload) {
|
||||
state.expoToken = action.payload
|
||||
}
|
||||
})
|
||||
.addCase(retriveVersionLatest.fulfilled, (state, action) => {
|
||||
if (action.payload && Constants.manifest?.version) {
|
||||
if (
|
||||
parseFloat(action.payload) > parseFloat(Constants.manifest.version)
|
||||
) {
|
||||
state.versionUpdate = true
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
export const getExpoToken = (state: RootState) => state.app.expoToken
|
||||
export const getVersionUpdate = (state: RootState) => state.app.versionUpdate
|
||||
|
||||
export default appSlice.reducer
|
@ -48,8 +48,9 @@ const pushRegister = async (
|
||||
|
||||
const accountId = instanceAccount.id
|
||||
const accountFull = `@${instanceAccount.acct}@${instanceUri}`
|
||||
const randomPath = (Math.random() + 1).toString(36).substring(2)
|
||||
|
||||
const endpoint = `https://${TOOOT_API_DOMAIN}/push/send/${expoToken}/${instanceUrl}/${accountId}`
|
||||
const endpoint = `https://${TOOOT_API_DOMAIN}/push/send/${expoToken}/${instanceUrl}/${accountId}/${randomPath}`
|
||||
const auth = base64.encodeFromByteArray(Random.getRandomBytes(16))
|
||||
|
||||
const alerts = instancePush.alerts
|
||||
|
@ -1,8 +1,6 @@
|
||||
import { createAsyncThunk } from '@reduxjs/toolkit'
|
||||
import { RootState } from '@root/store'
|
||||
import { isDevelopment } from '@utils/checkEnvironment'
|
||||
import { InstanceLatest } from '@utils/migrations/instances/migration'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import pushRegister from './push/register'
|
||||
import pushUnregister from './push/unregister'
|
||||
|
||||
@ -13,14 +11,10 @@ export const updateInstancePush = createAsyncThunk(
|
||||
{ getState }
|
||||
): Promise<InstanceLatest['push']['keys']['auth'] | undefined> => {
|
||||
const state = getState() as RootState
|
||||
const expoToken = isDevelopment
|
||||
? 'DEVELOPMENT_TOKEN_1'
|
||||
: (
|
||||
await Notifications.getExpoPushTokenAsync({
|
||||
experienceId: '@xmflsct/tooot',
|
||||
applicationId: 'com.xmflsct.app.tooot'
|
||||
})
|
||||
).data
|
||||
const expoToken = state.app.expoToken
|
||||
if (!expoToken) {
|
||||
return Promise.reject()
|
||||
}
|
||||
|
||||
if (disable) {
|
||||
return await pushRegister(state, expoToken)
|
||||
|
@ -2,7 +2,6 @@ import apiTooot from '@api/tooot'
|
||||
import { createAsyncThunk } from '@reduxjs/toolkit'
|
||||
import i18n from '@root/i18n/i18n'
|
||||
import { RootState } from '@root/store'
|
||||
import { isDevelopment } from '@utils/checkEnvironment'
|
||||
import { InstanceLatest } from '@utils/migrations/instances/migration'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import { Platform } from 'react-native'
|
||||
@ -21,14 +20,10 @@ export const updateInstancePushDecode = createAsyncThunk(
|
||||
return Promise.reject()
|
||||
}
|
||||
|
||||
const expoToken = isDevelopment
|
||||
? 'DEVELOPMENT_TOKEN_1'
|
||||
: (
|
||||
await Notifications.getExpoPushTokenAsync({
|
||||
experienceId: '@xmflsct/tooot',
|
||||
applicationId: 'com.xmflsct.app.tooot'
|
||||
})
|
||||
).data
|
||||
const expoToken = state.app.expoToken
|
||||
if (!expoToken) {
|
||||
return Promise.reject()
|
||||
}
|
||||
|
||||
await apiTooot({
|
||||
method: 'put',
|
||||
|
@ -1,43 +0,0 @@
|
||||
import apiGeneral from '@api/general'
|
||||
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
|
||||
import { RootState } from '@root/store'
|
||||
import Constants from 'expo-constants'
|
||||
|
||||
export const retriveVersionLatest = createAsyncThunk(
|
||||
'version/latest',
|
||||
async () => {
|
||||
const res = await apiGeneral<{ latest: string }>({
|
||||
method: 'get',
|
||||
domain: 'tooot.app',
|
||||
url: 'version.json'
|
||||
})
|
||||
return res.body.latest
|
||||
}
|
||||
)
|
||||
|
||||
export type VersionState = {
|
||||
update: boolean
|
||||
}
|
||||
|
||||
export const versionInitialState = {
|
||||
update: false
|
||||
}
|
||||
|
||||
const versionSlice = createSlice({
|
||||
name: 'version',
|
||||
initialState: versionInitialState,
|
||||
reducers: {},
|
||||
extraReducers: builder => {
|
||||
builder.addCase(retriveVersionLatest.fulfilled, (state, action) => {
|
||||
if (action.payload && Constants.manifest?.version) {
|
||||
if (parseFloat(action.payload) > parseFloat(Constants.manifest.version)) {
|
||||
state.update = true
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
export const getVersionUpdate = (state: RootState) => state.version.update
|
||||
|
||||
export default versionSlice.reducer
|
Reference in New Issue
Block a user