2021-09-05 11:16:19 -07:00
|
|
|
import api, { staticClient } from '../api';
|
2020-08-23 13:56:18 -07:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
2021-01-18 11:31:16 -08:00
|
|
|
import { getFeatures } from 'soapbox/utils/features';
|
2021-07-06 11:06:21 -07:00
|
|
|
import { createSelector } from 'reselect';
|
2021-11-15 14:56:33 -08:00
|
|
|
import { getHost } from 'soapbox/actions/instance';
|
|
|
|
import KVStore from 'soapbox/storage/kv_store';
|
2020-04-01 12:38:08 -07:00
|
|
|
|
2020-06-03 12:24:53 -07:00
|
|
|
export const SOAPBOX_CONFIG_REQUEST_SUCCESS = 'SOAPBOX_CONFIG_REQUEST_SUCCESS';
|
|
|
|
export const SOAPBOX_CONFIG_REQUEST_FAIL = 'SOAPBOX_CONFIG_REQUEST_FAIL';
|
2020-04-01 12:38:08 -07:00
|
|
|
|
2021-11-15 14:56:33 -08:00
|
|
|
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';
|
|
|
|
|
2021-01-18 11:31:16 -08:00
|
|
|
const allowedEmoji = ImmutableList([
|
|
|
|
'👍',
|
|
|
|
'❤',
|
|
|
|
'😆',
|
|
|
|
'😮',
|
|
|
|
'😢',
|
|
|
|
'😩',
|
|
|
|
]);
|
|
|
|
|
|
|
|
// https://git.pleroma.social/pleroma/pleroma/-/issues/2355
|
|
|
|
const allowedEmojiRGI = ImmutableList([
|
|
|
|
'👍',
|
|
|
|
'❤️',
|
|
|
|
'😆',
|
|
|
|
'😮',
|
|
|
|
'😢',
|
|
|
|
'😩',
|
|
|
|
]);
|
|
|
|
|
2021-04-10 12:13:07 -07:00
|
|
|
const year = new Date().getFullYear();
|
|
|
|
|
2021-08-23 12:14:47 -07:00
|
|
|
export const makeDefaultConfig = features => {
|
|
|
|
return ImmutableMap({
|
|
|
|
logo: '',
|
|
|
|
banner: '',
|
|
|
|
brandColor: '', // Empty
|
|
|
|
customCss: ImmutableList(),
|
|
|
|
promoPanel: ImmutableMap({
|
|
|
|
items: ImmutableList(),
|
|
|
|
}),
|
|
|
|
extensions: ImmutableMap(),
|
|
|
|
defaultSettings: ImmutableMap(),
|
|
|
|
copyright: `♥${year}. Copying is an act of love. Please copy and share.`,
|
|
|
|
navlinks: ImmutableMap({
|
|
|
|
homeFooter: ImmutableList(),
|
|
|
|
}),
|
|
|
|
allowedEmoji: features.emojiReactsRGI ? allowedEmojiRGI : allowedEmoji,
|
|
|
|
verifiedCanEditName: false,
|
|
|
|
displayFqn: Boolean(features.federating),
|
|
|
|
cryptoAddresses: ImmutableList(),
|
|
|
|
cryptoDonatePanel: ImmutableMap({
|
|
|
|
limit: 1,
|
|
|
|
}),
|
|
|
|
aboutPages: ImmutableMap(),
|
2021-09-10 09:44:18 -07:00
|
|
|
authenticatedProfile: true,
|
2021-08-23 12:14:47 -07:00
|
|
|
});
|
|
|
|
};
|
2020-08-23 13:56:18 -07:00
|
|
|
|
2021-07-06 11:06:21 -07:00
|
|
|
export const getSoapboxConfig = createSelector([
|
|
|
|
state => state.get('soapbox'),
|
2021-08-23 12:14:47 -07:00
|
|
|
state => getFeatures(state.get('instance')),
|
|
|
|
], (soapbox, features) => {
|
|
|
|
return makeDefaultConfig(features).merge(soapbox);
|
2021-07-06 11:06:21 -07:00
|
|
|
});
|
2020-08-23 13:56:18 -07:00
|
|
|
|
2021-11-15 14:56:33 -08: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 fetchSoapboxConfig(host) {
|
2020-04-01 12:38:08 -07:00
|
|
|
return (dispatch, getState) => {
|
2020-07-25 16:27:39 -07:00
|
|
|
api(getState).get('/api/pleroma/frontend_configurations').then(response => {
|
|
|
|
if (response.data.soapbox_fe) {
|
2021-11-15 14:56:33 -08:00
|
|
|
dispatch(importSoapboxConfig(response.data.soapbox_fe, host));
|
2020-07-25 16:27:39 -07:00
|
|
|
} else {
|
2021-11-15 14:56:33 -08:00
|
|
|
dispatch(fetchSoapboxJson(host));
|
2020-07-25 16:27:39 -07:00
|
|
|
}
|
2020-04-01 12:38:08 -07:00
|
|
|
}).catch(error => {
|
2021-11-15 14:56:33 -08:00
|
|
|
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
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-15 14:56:33 -08:00
|
|
|
export function fetchSoapboxJson(host) {
|
2020-08-23 13:32:44 -07:00
|
|
|
return (dispatch, getState) => {
|
2021-09-05 11:16:19 -07:00
|
|
|
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));
|
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
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-15 14:56:33 -08:00
|
|
|
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
|
|
|
}
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-15 14:56:33 -08:00
|
|
|
export function soapboxConfigFail(error, host) {
|
2020-04-01 12:38:08 -07:00
|
|
|
return {
|
2020-06-03 12:24:53 -07:00
|
|
|
type: SOAPBOX_CONFIG_REQUEST_FAIL,
|
2020-04-01 12:38:08 -07:00
|
|
|
error,
|
|
|
|
skipAlert: true,
|
2021-11-15 14:56:33 -08:00
|
|
|
host,
|
2020-04-01 12:38:08 -07:00
|
|
|
};
|
2020-06-09 16:53:35 -07:00
|
|
|
}
|
2020-08-31 22:02:01 -07:00
|
|
|
|
|
|
|
// https://stackoverflow.com/a/46663081
|
|
|
|
function isObject(o) {
|
|
|
|
return o instanceof Object && o.constructor === Object;
|
|
|
|
}
|