bigbuffet-rw/app/soapbox/reducers/instance.js

80 lines
2.7 KiB
JavaScript
Raw Normal View History

import {
INSTANCE_FETCH_SUCCESS,
NODEINFO_FETCH_SUCCESS,
} from '../actions/instance';
2020-08-24 13:53:38 -07:00
import { PRELOAD_IMPORT } from 'soapbox/actions/preload';
import { ADMIN_CONFIG_UPDATE_REQUEST, ADMIN_CONFIG_UPDATE_SUCCESS } from 'soapbox/actions/admin';
2020-12-29 21:25:07 -08:00
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
import { ConfigDB } from 'soapbox/utils/config_db';
2020-04-01 13:05:52 -07:00
const nodeinfoToInstance = nodeinfo => {
// Match Pleroma's develop branch
return ImmutableMap({
pleroma: ImmutableMap({
metadata: ImmutableMap({
account_activation_required: nodeinfo.getIn(['metadata', 'accountActivationRequired']),
features: nodeinfo.getIn(['metadata', 'features']),
federation: nodeinfo.getIn(['metadata', 'federation']),
fields_limits: ImmutableMap({
max_fields: nodeinfo.getIn(['metadata', 'fieldsLimits', 'maxFields']),
}),
}),
}),
});
};
2020-04-15 11:20:09 -07:00
// Set Mastodon defaults, overridden by Pleroma servers
const initialState = ImmutableMap({
max_toot_chars: 500,
poll_limits: ImmutableMap({
max_expiration: 2629746,
max_option_chars: 25,
max_options: 4,
min_expiration: 300,
}),
2021-01-18 11:31:16 -08:00
version: '0.0.0',
2020-04-15 11:20:09 -07:00
});
2020-04-01 13:05:52 -07:00
2020-08-24 13:53:38 -07:00
const preloadImport = (state, action, path) => {
const data = action.data[path];
return data ? initialState.mergeDeep(fromJS(data)) : state;
};
2020-12-29 21:25:07 -08:00
const getConfigValue = (instanceConfig, key) => {
const v = instanceConfig
.find(value => value.getIn(['tuple', 0]) === key);
return v ? v.getIn(['tuple', 1]) : undefined;
2020-12-29 21:25:07 -08:00
};
const importConfigs = (state, configs) => {
// FIXME: This is pretty hacked together. Need to make a cleaner map.
const config = ConfigDB.find(configs, ':pleroma', ':instance');
if (!config) return state;
const value = config.get('value', ImmutableList());
return state.withMutations(state => {
const registrationsOpen = getConfigValue(value, ':registrations_open');
const approvalRequired = getConfigValue(value, ':account_approval_required');
state.update('registrations', c => typeof registrationsOpen === 'boolean' ? registrationsOpen : c);
state.update('approval_required', c => typeof approvalRequired === 'boolean' ? approvalRequired : c);
});
};
2020-04-01 13:05:52 -07:00
export default function instance(state = initialState, action) {
switch(action.type) {
2020-08-24 13:53:38 -07:00
case PRELOAD_IMPORT:
return preloadImport(state, action, '/api/v1/instance');
case INSTANCE_FETCH_SUCCESS:
return initialState.mergeDeep(fromJS(action.instance));
case NODEINFO_FETCH_SUCCESS:
return nodeinfoToInstance(fromJS(action.nodeinfo)).mergeDeep(state);
case ADMIN_CONFIG_UPDATE_REQUEST:
2020-12-29 21:25:07 -08:00
case ADMIN_CONFIG_UPDATE_SUCCESS:
return importConfigs(state, fromJS(action.configs));
2020-04-01 13:05:52 -07:00
default:
return state;
}
};