Refactor supportsInstanceV2 to use features directly
This commit is contained in:
parent
998db8477b
commit
4ffa67c9c1
|
@ -1,10 +1,9 @@
|
||||||
import { createAsyncThunk } from '@reduxjs/toolkit';
|
import { createAsyncThunk } from '@reduxjs/toolkit';
|
||||||
import get from 'lodash/get';
|
|
||||||
import { gte } from 'semver';
|
|
||||||
|
|
||||||
|
import { instanceSchema } from 'soapbox/schemas';
|
||||||
import { RootState } from 'soapbox/store';
|
import { RootState } from 'soapbox/store';
|
||||||
import { getAuthUserUrl, getMeUrl } from 'soapbox/utils/auth';
|
import { getAuthUserUrl, getMeUrl } from 'soapbox/utils/auth';
|
||||||
import { MASTODON, parseVersion, PLEROMA, REBASED } from 'soapbox/utils/features';
|
import { getFeatures } from 'soapbox/utils/features';
|
||||||
|
|
||||||
import api from '../api';
|
import api from '../api';
|
||||||
|
|
||||||
|
@ -19,18 +18,6 @@ export const getHost = (state: RootState) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const supportsInstanceV2 = (instance: Record<string, any>): boolean => {
|
|
||||||
const v = parseVersion(get(instance, 'version'));
|
|
||||||
return (v.software === MASTODON && gte(v.compatVersion, '4.0.0')) ||
|
|
||||||
(v.software === PLEROMA && v.build === REBASED && gte(v.version, '2.5.54'));
|
|
||||||
};
|
|
||||||
|
|
||||||
/** We may need to fetch nodeinfo on Pleroma < 2.1 */
|
|
||||||
const needsNodeinfo = (instance: Record<string, any>): boolean => {
|
|
||||||
const v = parseVersion(get(instance, 'version'));
|
|
||||||
return v.software === PLEROMA && !get(instance, ['pleroma', 'metadata']);
|
|
||||||
};
|
|
||||||
|
|
||||||
interface InstanceData {
|
interface InstanceData {
|
||||||
instance: Record<string, any>;
|
instance: Record<string, any>;
|
||||||
host: string | null | undefined;
|
host: string | null | undefined;
|
||||||
|
@ -40,15 +27,14 @@ export const fetchInstance = createAsyncThunk<InstanceData, InstanceData['host']
|
||||||
'instance/fetch',
|
'instance/fetch',
|
||||||
async(host, { dispatch, getState, rejectWithValue }) => {
|
async(host, { dispatch, getState, rejectWithValue }) => {
|
||||||
try {
|
try {
|
||||||
const { data: instance } = await api(getState).get('/api/v1/instance');
|
const { data } = await api(getState).get('/api/v1/instance');
|
||||||
|
const instance = instanceSchema.parse(data);
|
||||||
|
const features = getFeatures(instance);
|
||||||
|
|
||||||
if (supportsInstanceV2(instance)) {
|
if (features.instanceV2) {
|
||||||
dispatch(fetchInstanceV2(host));
|
dispatch(fetchInstanceV2(host));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (needsNodeinfo(instance)) {
|
|
||||||
dispatch(fetchNodeinfo());
|
|
||||||
}
|
|
||||||
return { instance, host };
|
return { instance, host };
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return rejectWithValue(e);
|
return rejectWithValue(e);
|
||||||
|
@ -67,8 +53,3 @@ export const fetchInstanceV2 = createAsyncThunk<InstanceData, InstanceData['host
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
export const fetchNodeinfo = createAsyncThunk<void, void, { state: RootState }>(
|
|
||||||
'nodeinfo/fetch',
|
|
||||||
async(_arg, { getState }) => await api(getState).get('/nodeinfo/2.1.json'),
|
|
||||||
);
|
|
||||||
|
|
|
@ -711,6 +711,7 @@ const getInstanceFeatures = (instance: Instance) => {
|
||||||
instanceV2: any([
|
instanceV2: any([
|
||||||
v.software === MASTODON && gte(v.compatVersion, '4.0.0'),
|
v.software === MASTODON && gte(v.compatVersion, '4.0.0'),
|
||||||
v.software === PLEROMA && v.build === REBASED && gte(v.version, '2.5.54'),
|
v.software === PLEROMA && v.build === REBASED && gte(v.version, '2.5.54'),
|
||||||
|
v.software === DITTO,
|
||||||
]),
|
]),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue