|
|
|
@ -8,7 +8,6 @@ import KVStore from 'soapbox/storage/kv-store';
|
|
|
|
|
import { ConfigDB } from 'soapbox/utils/config-db';
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
rememberInstance,
|
|
|
|
|
fetchInstance,
|
|
|
|
|
fetchInstanceV2,
|
|
|
|
|
} from '../actions/instance';
|
|
|
|
@ -18,11 +17,16 @@ import type { APIEntity } from 'soapbox/types/entities';
|
|
|
|
|
|
|
|
|
|
const initialState: Instance = instanceSchema.parse({});
|
|
|
|
|
|
|
|
|
|
const importInstance = (_state: typeof initialState, instance: APIEntity) => {
|
|
|
|
|
const importInstance = (_state: Instance, instance: APIEntity): Instance => {
|
|
|
|
|
return instanceSchema.parse(instance);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const preloadImport = (state: typeof initialState, action: Record<string, any>, path: string) => {
|
|
|
|
|
const importInstanceV2 = (state: Instance, data: APIEntity): Instance => {
|
|
|
|
|
const instance = instanceSchema.parse(data);
|
|
|
|
|
return { ...instance, stats: state.stats };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const preloadImport = (state: Instance, action: Record<string, any>, path: string) => {
|
|
|
|
|
const instance = action.data[path];
|
|
|
|
|
return instance ? importInstance(state, instance) : state;
|
|
|
|
|
};
|
|
|
|
@ -34,7 +38,7 @@ const getConfigValue = (instanceConfig: ImmutableMap<string, any>, key: string)
|
|
|
|
|
return v ? v.getIn(['tuple', 1]) : undefined;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const importConfigs = (state: typeof initialState, configs: ImmutableList<any>) => {
|
|
|
|
|
const importConfigs = (state: Instance, configs: ImmutableList<any>) => {
|
|
|
|
|
// FIXME: This is pretty hacked together. Need to make a cleaner map.
|
|
|
|
|
const config = ConfigDB.find(configs, ':pleroma', ':instance');
|
|
|
|
|
const simplePolicy = ConfigDB.toSimplePolicy(configs);
|
|
|
|
@ -59,7 +63,7 @@ const importConfigs = (state: typeof initialState, configs: ImmutableList<any>)
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleAuthFetch = (state: typeof initialState) => {
|
|
|
|
|
const handleAuthFetch = (state: Instance) => {
|
|
|
|
|
// Authenticated fetch is enabled, so make the instance appear censored
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
@ -86,7 +90,13 @@ const persistInstance = (instance: { uri: string }, host: string | null = getHos
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleInstanceFetchFail = (state: typeof initialState, error: Record<string, any>) => {
|
|
|
|
|
const persistInstanceV2 = (instance: { uri: string }, host: string | null = getHost(instance)) => {
|
|
|
|
|
if (host) {
|
|
|
|
|
KVStore.setItem(`instanceV2:${host}`, instance).catch(console.error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleInstanceFetchFail = (state: Instance, error: Record<string, any>) => {
|
|
|
|
|
if (error.response?.status === 401) {
|
|
|
|
|
return handleAuthFetch(state);
|
|
|
|
|
} else {
|
|
|
|
@ -98,14 +108,13 @@ export default function instance(state = initialState, action: AnyAction) {
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case PLEROMA_PRELOAD_IMPORT:
|
|
|
|
|
return preloadImport(state, action, '/api/v1/instance');
|
|
|
|
|
case rememberInstance.fulfilled.type:
|
|
|
|
|
return importInstance(state, action.payload.instance);
|
|
|
|
|
case fetchInstance.fulfilled.type:
|
|
|
|
|
case fetchInstanceV2.fulfilled.type:
|
|
|
|
|
persistInstance(action.payload);
|
|
|
|
|
return importInstance(state, action.payload.instance);
|
|
|
|
|
case fetchInstanceV2.fulfilled.type:
|
|
|
|
|
persistInstanceV2(action.payload);
|
|
|
|
|
return importInstanceV2(state, action.payload.instance);
|
|
|
|
|
case fetchInstance.rejected.type:
|
|
|
|
|
case fetchInstanceV2.rejected.type:
|
|
|
|
|
return handleInstanceFetchFail(state, action.error);
|
|
|
|
|
case ADMIN_CONFIG_UPDATE_REQUEST:
|
|
|
|
|
case ADMIN_CONFIG_UPDATE_SUCCESS:
|
|
|
|
|