2020-05-31 10:01:21 -07:00
|
|
|
import { get } from 'lodash';
|
|
|
|
import { parseVersion } from 'soapbox/utils/features';
|
2021-10-20 11:18:55 -07:00
|
|
|
import { getAuthUserUrl } from 'soapbox/utils/auth';
|
2021-10-20 13:17:02 -07:00
|
|
|
import KVStore from 'soapbox/storage/kv_store';
|
2022-01-10 14:01:24 -08:00
|
|
|
import api from '../api';
|
2021-10-20 11:18:55 -07:00
|
|
|
|
2021-10-20 13:17:02 -07:00
|
|
|
export const INSTANCE_FETCH_REQUEST = 'INSTANCE_FETCH_REQUEST';
|
|
|
|
export const INSTANCE_FETCH_SUCCESS = 'INSTANCE_FETCH_SUCCESS';
|
|
|
|
export const INSTANCE_FETCH_FAIL = 'INSTANCE_FETCH_FAIL';
|
|
|
|
|
|
|
|
export const INSTANCE_REMEMBER_REQUEST = 'INSTANCE_REMEMBER_REQUEST';
|
2021-10-20 11:18:55 -07:00
|
|
|
export const INSTANCE_REMEMBER_SUCCESS = 'INSTANCE_REMEMBER_SUCCESS';
|
2021-10-20 13:17:02 -07:00
|
|
|
export const INSTANCE_REMEMBER_FAIL = 'INSTANCE_REMEMBER_FAIL';
|
2020-04-01 13:05:52 -07:00
|
|
|
|
2021-10-20 13:17:02 -07:00
|
|
|
export const NODEINFO_FETCH_REQUEST = 'NODEINFO_FETCH_REQUEST';
|
2020-06-03 12:24:53 -07:00
|
|
|
export const NODEINFO_FETCH_SUCCESS = 'NODEINFO_FETCH_SUCCESS';
|
|
|
|
export const NODEINFO_FETCH_FAIL = 'NODEINFO_FETCH_FAIL';
|
2020-04-01 13:05:52 -07:00
|
|
|
|
2021-10-20 11:18:55 -07:00
|
|
|
const getMeUrl = state => {
|
|
|
|
const me = state.get('me');
|
|
|
|
return state.getIn(['accounts', me, 'url']);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Figure out the appropriate instance to fetch depending on the state
|
2021-11-15 14:56:33 -08:00
|
|
|
export const getHost = state => {
|
2021-10-20 11:18:55 -07:00
|
|
|
const accountUrl = getMeUrl(state) || getAuthUserUrl(state);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return new URL(accountUrl).host;
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export function rememberInstance(host) {
|
|
|
|
return (dispatch, getState) => {
|
2021-10-20 13:17:02 -07:00
|
|
|
dispatch({ type: INSTANCE_REMEMBER_REQUEST, host });
|
|
|
|
return KVStore.getItemOrError(`instance:${host}`).then(instance => {
|
|
|
|
dispatch({ type: INSTANCE_REMEMBER_SUCCESS, host, instance });
|
2021-10-20 11:18:55 -07:00
|
|
|
return instance;
|
2021-10-20 13:17:02 -07:00
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: INSTANCE_REMEMBER_FAIL, host, error, skipAlert: true });
|
2021-10-20 11:18:55 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-20 13:17:02 -07:00
|
|
|
// We may need to fetch nodeinfo on Pleroma < 2.1
|
|
|
|
const needsNodeinfo = instance => {
|
|
|
|
const v = parseVersion(get(instance, 'version'));
|
|
|
|
return v.software === 'Pleroma' && !get(instance, ['pleroma', 'metadata']);
|
|
|
|
};
|
|
|
|
|
2020-04-01 13:05:52 -07:00
|
|
|
export function fetchInstance() {
|
|
|
|
return (dispatch, getState) => {
|
2021-10-20 13:17:02 -07:00
|
|
|
dispatch({ type: INSTANCE_FETCH_REQUEST });
|
|
|
|
return api(getState).get('/api/v1/instance').then(({ data: instance }) => {
|
|
|
|
dispatch({ type: INSTANCE_FETCH_SUCCESS, instance });
|
|
|
|
if (needsNodeinfo(instance)) {
|
2020-05-31 10:01:21 -07:00
|
|
|
dispatch(fetchNodeinfo()); // Pleroma < 2.1 backwards compatibility
|
|
|
|
}
|
2020-04-01 13:05:52 -07:00
|
|
|
}).catch(error => {
|
2021-10-20 13:17:02 -07:00
|
|
|
dispatch({ type: INSTANCE_FETCH_FAIL, error, skipAlert: true });
|
2020-04-01 13:05:52 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-20 11:18:55 -07:00
|
|
|
// Tries to remember the instance from browser storage before fetching it
|
|
|
|
export function loadInstance() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const host = getHost(getState());
|
|
|
|
|
2021-10-20 14:27:36 -07:00
|
|
|
return dispatch(rememberInstance(host)).finally(() => {
|
2021-10-20 11:18:55 -07:00
|
|
|
return dispatch(fetchInstance());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-31 10:01:21 -07:00
|
|
|
export function fetchNodeinfo() {
|
|
|
|
return (dispatch, getState) => {
|
2021-10-20 13:17:02 -07:00
|
|
|
dispatch({ type: NODEINFO_FETCH_REQUEST });
|
|
|
|
api(getState).get('/nodeinfo/2.1.json').then(({ data: nodeinfo }) => {
|
|
|
|
dispatch({ type: NODEINFO_FETCH_SUCCESS, nodeinfo });
|
2020-05-31 10:01:21 -07:00
|
|
|
}).catch(error => {
|
2021-10-20 13:17:02 -07:00
|
|
|
dispatch({ type: NODEINFO_FETCH_FAIL, error, skipAlert: true });
|
2020-05-31 10:01:21 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|