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

111 lines
2.8 KiB
JavaScript
Raw Normal View History

import api, { staticClient } from '../api';
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
2021-01-18 11:31:16 -08:00
import { getFeatures } from 'soapbox/utils/features';
import { createSelector } from 'reselect';
export const SOAPBOX_CONFIG_REQUEST_SUCCESS = 'SOAPBOX_CONFIG_REQUEST_SUCCESS';
export const SOAPBOX_CONFIG_REQUEST_FAIL = 'SOAPBOX_CONFIG_REQUEST_FAIL';
2021-01-18 11:31:16 -08:00
const allowedEmoji = ImmutableList([
'👍',
'❤',
'😆',
'😮',
'😢',
'😩',
]);
// https://git.pleroma.social/pleroma/pleroma/-/issues/2355
const allowedEmojiRGI = ImmutableList([
'👍',
'❤️',
'😆',
'😮',
'😢',
'😩',
]);
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(),
authenticatedProfile: true,
2021-08-23 12:14:47 -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);
});
export function fetchSoapboxConfig() {
return (dispatch, getState) => {
api(getState).get('/api/pleroma/frontend_configurations').then(response => {
if (response.data.soapbox_fe) {
dispatch(importSoapboxConfig(response.data.soapbox_fe));
} else {
2020-08-23 13:32:44 -07:00
dispatch(fetchSoapboxJson());
}
}).catch(error => {
2020-08-23 13:32:44 -07:00
dispatch(fetchSoapboxJson());
});
};
}
export function fetchSoapboxJson() {
return (dispatch, getState) => {
staticClient.get('/instance/soapbox.json').then(({ data }) => {
if (!isObject(data)) throw 'soapbox.json failed';
dispatch(importSoapboxConfig(data));
2020-08-23 13:32:44 -07:00
}).catch(error => {
dispatch(soapboxConfigFail(error));
});
};
}
export function importSoapboxConfig(soapboxConfig) {
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,
};
}
export function soapboxConfigFail(error) {
return {
type: SOAPBOX_CONFIG_REQUEST_FAIL,
error,
skipAlert: true,
};
}
// https://stackoverflow.com/a/46663081
function isObject(o) {
return o instanceof Object && o.constructor === Object;
}