2020-11-21 13:19:05 +01:00
|
|
|
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
|
|
|
|
|
2020-12-13 14:04:25 +01:00
|
|
|
import { RootState } from '@root/store'
|
|
|
|
import client from '@api/client'
|
2020-11-21 00:40:55 +01:00
|
|
|
|
2020-11-21 13:19:05 +01:00
|
|
|
export type InstancesState = {
|
2020-11-21 00:40:55 +01:00
|
|
|
local: {
|
|
|
|
url: string | undefined
|
|
|
|
token: string | undefined
|
|
|
|
account: {
|
|
|
|
id: Mastodon.Account['id'] | undefined
|
|
|
|
preferences: Mastodon.Preferences
|
|
|
|
}
|
|
|
|
}
|
|
|
|
remote: {
|
|
|
|
url: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 00:18:47 +01:00
|
|
|
const initialStateLocal = {
|
|
|
|
url: undefined,
|
|
|
|
token: undefined,
|
|
|
|
account: {
|
|
|
|
id: undefined,
|
|
|
|
preferences: {
|
|
|
|
'posting:default:visibility': undefined,
|
|
|
|
'posting:default:sensitive': undefined,
|
|
|
|
'posting:default:language': undefined,
|
|
|
|
'reading:expand:media': undefined,
|
|
|
|
'reading:expand:spoilers': undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 00:40:55 +01:00
|
|
|
export const updateLocal = createAsyncThunk(
|
|
|
|
'instances/updateLocal',
|
|
|
|
async ({
|
|
|
|
url,
|
|
|
|
token
|
|
|
|
}: {
|
2020-11-24 00:18:47 +01:00
|
|
|
url?: InstancesState['local']['url']
|
|
|
|
token?: InstancesState['local']['token']
|
2020-11-21 00:40:55 +01:00
|
|
|
}) => {
|
2020-11-24 00:18:47 +01:00
|
|
|
if (!url || !token) {
|
|
|
|
return initialStateLocal
|
|
|
|
}
|
|
|
|
|
2020-11-21 00:40:55 +01:00
|
|
|
const {
|
|
|
|
body: { id }
|
|
|
|
} = await client({
|
|
|
|
method: 'get',
|
|
|
|
instance: 'remote',
|
2020-12-05 01:55:53 +01:00
|
|
|
instanceDomain: url,
|
|
|
|
url: `accounts/verify_credentials`,
|
2020-11-21 00:40:55 +01:00
|
|
|
headers: { Authorization: `Bearer ${token}` }
|
|
|
|
})
|
|
|
|
|
|
|
|
const { body: preferences } = await client({
|
|
|
|
method: 'get',
|
|
|
|
instance: 'remote',
|
2020-12-05 01:55:53 +01:00
|
|
|
instanceDomain: url,
|
|
|
|
url: `preferences`,
|
2020-11-21 00:40:55 +01:00
|
|
|
headers: { Authorization: `Bearer ${token}` }
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
url,
|
|
|
|
token,
|
|
|
|
account: {
|
|
|
|
id,
|
|
|
|
preferences
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const instancesSlice = createSlice({
|
|
|
|
name: 'instances',
|
|
|
|
initialState: {
|
2020-11-24 00:18:47 +01:00
|
|
|
local: initialStateLocal,
|
2020-11-21 00:40:55 +01:00
|
|
|
remote: {
|
2020-11-24 00:18:47 +01:00
|
|
|
url: 'm.cmx.im'
|
2020-11-21 00:40:55 +01:00
|
|
|
}
|
|
|
|
} as InstancesState,
|
|
|
|
reducers: {},
|
|
|
|
extraReducers: builder => {
|
|
|
|
builder.addCase(updateLocal.fulfilled, (state, action) => {
|
|
|
|
state.local = action.payload
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
export const getLocalUrl = (state: RootState) => state.instances.local.url
|
2020-12-05 01:55:53 +01:00
|
|
|
export const getLocalToken = (state: RootState) => state.instances.local.token
|
2020-11-24 00:18:47 +01:00
|
|
|
export const getRemoteUrl = (state: RootState) => state.instances.remote.url
|
2020-11-21 00:40:55 +01:00
|
|
|
export const getLocalAccountId = (state: RootState) =>
|
|
|
|
state.instances.local.account.id
|
|
|
|
export const getLocalAccountPreferences = (state: RootState) =>
|
|
|
|
state.instances.local.account.preferences
|
|
|
|
|
|
|
|
export default instancesSlice.reducer
|