2021-07-06 11:06:21 -07:00
|
|
|
import { createSelector } from 'reselect';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2021-11-15 14:56:33 -08:00
|
|
|
import { getHost } from 'soapbox/actions/instance';
|
2022-03-28 12:58:50 -07:00
|
|
|
import { normalizeSoapboxConfig } from 'soapbox/normalizers';
|
2022-11-15 12:42:22 -08:00
|
|
|
import KVStore from 'soapbox/storage/kv-store';
|
2022-04-10 13:44:51 -07:00
|
|
|
import { removeVS16s } from 'soapbox/utils/emoji';
|
2022-01-10 14:17:52 -08:00
|
|
|
import { getFeatures } from 'soapbox/utils/features';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-03-21 13:13:33 -07:00
|
|
|
import api, { staticClient } from '../api';
|
2020-04-01 12:38:08 -07:00
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
import type { AxiosError } from 'axios';
|
|
|
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
|
|
|
import type { APIEntity } from 'soapbox/types/entities';
|
2020-04-01 12:38:08 -07:00
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
const SOAPBOX_CONFIG_REQUEST_SUCCESS = 'SOAPBOX_CONFIG_REQUEST_SUCCESS';
|
|
|
|
const SOAPBOX_CONFIG_REQUEST_FAIL = 'SOAPBOX_CONFIG_REQUEST_FAIL';
|
2021-11-15 14:56:33 -08:00
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
const SOAPBOX_CONFIG_REMEMBER_REQUEST = 'SOAPBOX_CONFIG_REMEMBER_REQUEST';
|
|
|
|
const SOAPBOX_CONFIG_REMEMBER_SUCCESS = 'SOAPBOX_CONFIG_REMEMBER_SUCCESS';
|
|
|
|
const SOAPBOX_CONFIG_REMEMBER_FAIL = 'SOAPBOX_CONFIG_REMEMBER_FAIL';
|
|
|
|
|
|
|
|
const getSoapboxConfig = createSelector([
|
|
|
|
(state: RootState) => state.soapbox,
|
|
|
|
(state: RootState) => getFeatures(state.instance),
|
2022-04-10 13:44:51 -07:00
|
|
|
], (soapbox, features) => {
|
|
|
|
// Do some additional normalization with the state
|
|
|
|
return normalizeSoapboxConfig(soapbox).withMutations(soapboxConfig => {
|
2021-01-18 11:31:16 -08:00
|
|
|
|
2022-04-10 13:44:51 -07:00
|
|
|
// If displayFqn isn't set, infer it from federation
|
|
|
|
if (soapbox.get('displayFqn') === undefined) {
|
|
|
|
soapboxConfig.set('displayFqn', features.federating);
|
|
|
|
}
|
2021-01-18 11:31:16 -08:00
|
|
|
|
2022-04-10 13:44:51 -07:00
|
|
|
// If RGI reacts aren't supported, strip VS16s
|
|
|
|
// // https://git.pleroma.social/pleroma/pleroma/-/issues/2355
|
|
|
|
if (!features.emojiReactsRGI) {
|
2022-04-10 14:07:23 -07:00
|
|
|
soapboxConfig.set('allowedEmoji', soapboxConfig.allowedEmoji.map(removeVS16s));
|
2022-04-10 13:44:51 -07:00
|
|
|
}
|
2021-08-23 12:14:47 -07:00
|
|
|
});
|
2021-07-06 11:06:21 -07:00
|
|
|
});
|
2020-08-23 13:56:18 -07:00
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
const rememberSoapboxConfig = (host: string | null) =>
|
|
|
|
(dispatch: AppDispatch) => {
|
2021-11-15 14:56:33 -08:00
|
|
|
dispatch({ type: SOAPBOX_CONFIG_REMEMBER_REQUEST, host });
|
|
|
|
return KVStore.getItemOrError(`soapbox_config:${host}`).then(soapboxConfig => {
|
|
|
|
dispatch({ type: SOAPBOX_CONFIG_REMEMBER_SUCCESS, host, soapboxConfig });
|
|
|
|
return soapboxConfig;
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: SOAPBOX_CONFIG_REMEMBER_FAIL, host, error, skipAlert: true });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
const fetchFrontendConfigurations = () =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) =>
|
|
|
|
api(getState)
|
2022-04-19 16:33:13 -07:00
|
|
|
.get('/api/pleroma/frontend_configurations')
|
|
|
|
.then(({ data }) => data);
|
|
|
|
|
|
|
|
/** Conditionally fetches Soapbox config depending on backend features */
|
2022-06-19 11:38:51 -07:00
|
|
|
const fetchSoapboxConfig = (host: string | null) =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
2022-04-19 16:33:13 -07:00
|
|
|
const features = getFeatures(getState().instance);
|
|
|
|
|
|
|
|
if (features.frontendConfigurations) {
|
|
|
|
return dispatch(fetchFrontendConfigurations()).then(data => {
|
|
|
|
if (data.soapbox_fe) {
|
|
|
|
dispatch(importSoapboxConfig(data.soapbox_fe, host));
|
2022-05-16 13:12:26 -07:00
|
|
|
return data.soapbox_fe;
|
2022-04-19 16:33:13 -07:00
|
|
|
} else {
|
2022-05-16 13:12:26 -07:00
|
|
|
return dispatch(fetchSoapboxJson(host));
|
2022-04-19 16:33:13 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return dispatch(fetchSoapboxJson(host));
|
|
|
|
}
|
2022-03-21 13:13:33 -07:00
|
|
|
};
|
2021-11-15 14:56:33 -08:00
|
|
|
|
2022-04-19 16:33:13 -07:00
|
|
|
/** Tries to remember the config from browser storage before fetching it */
|
2022-06-19 11:38:51 -07:00
|
|
|
const loadSoapboxConfig = () =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
2021-11-15 14:56:33 -08:00
|
|
|
const host = getHost(getState());
|
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
return dispatch(rememberSoapboxConfig(host)).then(() =>
|
|
|
|
dispatch(fetchSoapboxConfig(host)),
|
|
|
|
);
|
2020-08-23 13:32:44 -07:00
|
|
|
};
|
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
const fetchSoapboxJson = (host: string | null) =>
|
|
|
|
(dispatch: AppDispatch) =>
|
|
|
|
staticClient.get('/instance/soapbox.json').then(({ data }) => {
|
2020-08-31 22:02:01 -07:00
|
|
|
if (!isObject(data)) throw 'soapbox.json failed';
|
2021-11-15 14:56:33 -08:00
|
|
|
dispatch(importSoapboxConfig(data, host));
|
2022-05-16 13:12:26 -07:00
|
|
|
return data;
|
2020-08-23 13:32:44 -07:00
|
|
|
}).catch(error => {
|
2021-11-15 14:56:33 -08:00
|
|
|
dispatch(soapboxConfigFail(error, host));
|
2020-04-01 12:38:08 -07:00
|
|
|
});
|
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
const importSoapboxConfig = (soapboxConfig: APIEntity, host: string | null) => {
|
2020-09-02 19:47:46 -07:00
|
|
|
if (!soapboxConfig.brandColor) {
|
|
|
|
soapboxConfig.brandColor = '#0482d8';
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|
2020-04-01 12:38:08 -07:00
|
|
|
return {
|
2020-06-03 12:24:53 -07:00
|
|
|
type: SOAPBOX_CONFIG_REQUEST_SUCCESS,
|
2020-04-14 11:44:40 -07:00
|
|
|
soapboxConfig,
|
2021-11-15 14:56:33 -08:00
|
|
|
host,
|
2020-04-01 12:38:08 -07:00
|
|
|
};
|
2022-06-19 11:38:51 -07:00
|
|
|
};
|
2020-04-01 12:38:08 -07:00
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
const soapboxConfigFail = (error: AxiosError, host: string | null) => ({
|
|
|
|
type: SOAPBOX_CONFIG_REQUEST_FAIL,
|
|
|
|
error,
|
|
|
|
skipAlert: true,
|
|
|
|
host,
|
|
|
|
});
|
2020-08-31 22:02:01 -07:00
|
|
|
|
|
|
|
// https://stackoverflow.com/a/46663081
|
2022-06-19 11:38:51 -07:00
|
|
|
const isObject = (o: any) => o instanceof Object && o.constructor === Object;
|
|
|
|
|
|
|
|
export {
|
|
|
|
SOAPBOX_CONFIG_REQUEST_SUCCESS,
|
|
|
|
SOAPBOX_CONFIG_REQUEST_FAIL,
|
|
|
|
SOAPBOX_CONFIG_REMEMBER_REQUEST,
|
|
|
|
SOAPBOX_CONFIG_REMEMBER_SUCCESS,
|
|
|
|
SOAPBOX_CONFIG_REMEMBER_FAIL,
|
|
|
|
getSoapboxConfig,
|
|
|
|
rememberSoapboxConfig,
|
|
|
|
fetchFrontendConfigurations,
|
|
|
|
fetchSoapboxConfig,
|
|
|
|
loadSoapboxConfig,
|
|
|
|
fetchSoapboxJson,
|
|
|
|
importSoapboxConfig,
|
|
|
|
soapboxConfigFail,
|
|
|
|
};
|