bigbuffet-rw/app/soapbox/actions/soapbox.js

123 lines
3.8 KiB
JavaScript
Raw Normal View History

import { createSelector } from 'reselect';
import { getHost } from 'soapbox/actions/instance';
2022-03-28 12:58:50 -07:00
import { normalizeSoapboxConfig } from 'soapbox/normalizers';
import KVStore from 'soapbox/storage/kv_store';
import { removeVS16s } from 'soapbox/utils/emoji';
import { getFeatures } from 'soapbox/utils/features';
import api, { staticClient } from '../api';
export const SOAPBOX_CONFIG_REQUEST_SUCCESS = 'SOAPBOX_CONFIG_REQUEST_SUCCESS';
export const SOAPBOX_CONFIG_REQUEST_FAIL = 'SOAPBOX_CONFIG_REQUEST_FAIL';
export const SOAPBOX_CONFIG_REMEMBER_REQUEST = 'SOAPBOX_CONFIG_REMEMBER_REQUEST';
export const SOAPBOX_CONFIG_REMEMBER_SUCCESS = 'SOAPBOX_CONFIG_REMEMBER_SUCCESS';
export const SOAPBOX_CONFIG_REMEMBER_FAIL = 'SOAPBOX_CONFIG_REMEMBER_FAIL';
export const getSoapboxConfig = createSelector([
state => state.soapbox,
state => getFeatures(state.instance),
], (soapbox, features) => {
// Do some additional normalization with the state
return normalizeSoapboxConfig(soapbox).withMutations(soapboxConfig => {
2021-01-18 11:31:16 -08: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
// 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));
}
2021-08-23 12:14:47 -07:00
});
});
export function rememberSoapboxConfig(host) {
return (dispatch, getState) => {
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 });
});
};
}
export function fetchFrontendConfigurations() {
return (dispatch, getState) => {
return api(getState)
.get('/api/pleroma/frontend_configurations')
.then(({ data }) => data);
};
}
/** Conditionally fetches Soapbox config depending on backend features */
export function fetchSoapboxConfig(host) {
return (dispatch, getState) => {
const features = getFeatures(getState().instance);
if (features.frontendConfigurations) {
return dispatch(fetchFrontendConfigurations()).then(data => {
if (data.soapbox_fe) {
dispatch(importSoapboxConfig(data.soapbox_fe, host));
} else {
dispatch(fetchSoapboxJson(host));
}
});
} else {
return dispatch(fetchSoapboxJson(host));
}
};
}
/** Tries to remember the config from browser storage before fetching it */
export function loadSoapboxConfig() {
return (dispatch, getState) => {
const host = getHost(getState());
return dispatch(rememberSoapboxConfig(host)).finally(() => {
return dispatch(fetchSoapboxConfig(host));
2020-08-23 13:32:44 -07:00
});
};
}
export function fetchSoapboxJson(host) {
2020-08-23 13:32:44 -07:00
return (dispatch, getState) => {
staticClient.get('/instance/soapbox.json').then(({ data }) => {
if (!isObject(data)) throw 'soapbox.json failed';
dispatch(importSoapboxConfig(data, host));
2020-08-23 13:32:44 -07:00
}).catch(error => {
dispatch(soapboxConfigFail(error, host));
});
};
}
export function importSoapboxConfig(soapboxConfig, host) {
2020-09-02 19:47:46 -07:00
if (!soapboxConfig.brandColor) {
soapboxConfig.brandColor = '#0482d8';
2021-08-03 12:22:51 -07:00
}
return {
type: SOAPBOX_CONFIG_REQUEST_SUCCESS,
2020-04-14 11:44:40 -07:00
soapboxConfig,
host,
};
}
export function soapboxConfigFail(error, host) {
return {
type: SOAPBOX_CONFIG_REQUEST_FAIL,
error,
skipAlert: true,
host,
};
}
// https://stackoverflow.com/a/46663081
function isObject(o) {
return o instanceof Object && o.constructor === Object;
}