2022-04-25 11:57:24 -07:00
|
|
|
import { createAsyncThunk } from '@reduxjs/toolkit';
|
2022-06-16 12:32:17 -07:00
|
|
|
import get from 'lodash/get';
|
2023-10-27 12:18:30 -07:00
|
|
|
import { gte } from 'semver';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-04-25 11:57:24 -07:00
|
|
|
import { RootState } from 'soapbox/store';
|
2023-07-20 13:03:23 -07:00
|
|
|
import { getAuthUserUrl, getMeUrl } from 'soapbox/utils/auth';
|
2023-10-27 12:18:30 -07:00
|
|
|
import { MASTODON, parseVersion, PLEROMA, REBASED } from 'soapbox/utils/features';
|
2022-03-18 14:04:08 -07:00
|
|
|
|
2022-01-10 14:01:24 -08:00
|
|
|
import api from '../api';
|
2021-10-20 11:18:55 -07:00
|
|
|
|
2022-04-25 11:57:24 -07:00
|
|
|
/** Figure out the appropriate instance to fetch depending on the state */
|
2022-03-15 06:48:18 -07:00
|
|
|
export const getHost = (state: RootState) => {
|
2022-12-25 15:31:07 -08:00
|
|
|
const accountUrl = getMeUrl(state) || getAuthUserUrl(state) as string;
|
2021-10-20 11:18:55 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
return new URL(accountUrl).host;
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-27 12:18:30 -07:00
|
|
|
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'));
|
|
|
|
};
|
|
|
|
|
2024-01-12 16:10:49 -08:00
|
|
|
interface InstanceData {
|
|
|
|
instance: Record<string, any>;
|
|
|
|
host: string | null | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const fetchInstance = createAsyncThunk<InstanceData, InstanceData['host'], { state: RootState }>(
|
2022-04-25 11:57:24 -07:00
|
|
|
'instance/fetch',
|
2023-10-27 12:18:30 -07:00
|
|
|
async(host, { dispatch, getState, rejectWithValue }) => {
|
2022-05-02 09:46:18 -07:00
|
|
|
try {
|
2024-05-11 14:37:37 -07:00
|
|
|
const response = await api(getState)('/api/v1/instance');
|
|
|
|
const instance = response.json;
|
2023-10-27 12:18:30 -07:00
|
|
|
|
|
|
|
if (supportsInstanceV2(instance)) {
|
2024-01-12 16:10:49 -08:00
|
|
|
dispatch(fetchInstanceV2(host));
|
2023-10-27 12:18:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return { instance, host };
|
|
|
|
} catch (e) {
|
|
|
|
return rejectWithValue(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2024-01-12 16:10:49 -08:00
|
|
|
export const fetchInstanceV2 = createAsyncThunk<InstanceData, InstanceData['host'], { state: RootState }>(
|
|
|
|
'instanceV2/fetch',
|
2023-10-27 12:18:30 -07:00
|
|
|
async(host, { getState, rejectWithValue }) => {
|
|
|
|
try {
|
2024-05-11 14:37:37 -07:00
|
|
|
const response = await api(getState)('/api/v2/instance');
|
|
|
|
const instance = response.json;
|
|
|
|
|
2023-10-27 12:18:30 -07:00
|
|
|
return { instance, host };
|
2022-05-11 10:40:34 -07:00
|
|
|
} catch (e) {
|
2022-05-02 09:46:18 -07:00
|
|
|
return rejectWithValue(e);
|
2022-04-25 11:57:24 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|