2020-04-01 13:05:52 -07:00
|
|
|
import api from '../api';
|
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';
|
|
|
|
import localforage from 'localforage';
|
|
|
|
|
|
|
|
export const INSTANCE_FETCH_SUCCESS = 'INSTANCE_FETCH_SUCCESS';
|
|
|
|
export const INSTANCE_REMEMBER_SUCCESS = 'INSTANCE_REMEMBER_SUCCESS';
|
|
|
|
export const INSTANCE_FETCH_FAIL = 'INSTANCE_FETCH_FAIL';
|
2020-04-01 13:05:52 -07:00
|
|
|
|
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
|
|
|
|
const getHost = state => {
|
|
|
|
const accountUrl = getMeUrl(state) || getAuthUserUrl(state);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return new URL(accountUrl).host;
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export function rememberInstance(host) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
return localforage.getItem(`instance:${host}`).then(instance => {
|
|
|
|
dispatch({ type: INSTANCE_REMEMBER_SUCCESS, instance });
|
|
|
|
return instance;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-01 13:05:52 -07:00
|
|
|
export function fetchInstance() {
|
|
|
|
return (dispatch, getState) => {
|
2021-07-27 12:40:41 -07:00
|
|
|
return api(getState).get('/api/v1/instance').then(response => {
|
2020-04-01 13:05:52 -07:00
|
|
|
dispatch(importInstance(response.data));
|
2020-05-31 10:01:21 -07:00
|
|
|
const v = parseVersion(get(response.data, 'version'));
|
|
|
|
if (v.software === 'Pleroma' && !get(response.data, ['pleroma', 'metadata'])) {
|
|
|
|
dispatch(fetchNodeinfo()); // Pleroma < 2.1 backwards compatibility
|
|
|
|
}
|
2020-04-01 13:05:52 -07:00
|
|
|
}).catch(error => {
|
|
|
|
dispatch(instanceFail(error));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
return dispatch(rememberInstance(host)).then(instance => {
|
|
|
|
return dispatch(fetchInstance());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-31 10:01:21 -07:00
|
|
|
export function fetchNodeinfo() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
api(getState).get('/nodeinfo/2.1.json').then(response => {
|
|
|
|
dispatch(importNodeinfo(response.data));
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch(nodeinfoFail(error));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-01 13:05:52 -07:00
|
|
|
export function importInstance(instance) {
|
|
|
|
return {
|
2020-06-03 12:24:53 -07:00
|
|
|
type: INSTANCE_FETCH_SUCCESS,
|
2020-04-14 11:44:40 -07:00
|
|
|
instance,
|
2020-04-01 13:05:52 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function instanceFail(error) {
|
|
|
|
return {
|
2020-06-03 12:24:53 -07:00
|
|
|
type: INSTANCE_FETCH_FAIL,
|
2020-04-01 13:05:52 -07:00
|
|
|
error,
|
|
|
|
skipAlert: true,
|
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|
2020-05-31 10:01:21 -07:00
|
|
|
|
|
|
|
export function importNodeinfo(nodeinfo) {
|
|
|
|
return {
|
2020-06-03 12:24:53 -07:00
|
|
|
type: NODEINFO_FETCH_SUCCESS,
|
2020-05-31 10:01:21 -07:00
|
|
|
nodeinfo,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function nodeinfoFail(error) {
|
|
|
|
return {
|
2020-06-03 12:24:53 -07:00
|
|
|
type: NODEINFO_FETCH_FAIL,
|
2020-05-31 10:01:21 -07:00
|
|
|
error,
|
|
|
|
skipAlert: true,
|
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|