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

105 lines
3.1 KiB
JavaScript
Raw Normal View History

2022-03-09 14:00:43 -08:00
import {
Map as ImmutableMap,
List as ImmutableList,
Record as ImmutableRecord,
} from 'immutable';
import { parseVersion, PLEROMA } from 'soapbox/utils/features';
import { mergeDefined } from 'soapbox/utils/normalizers';
import { isNumber } from 'soapbox/utils/numbers';
// Use Mastodon defaults
2022-03-09 14:00:43 -08:00
const InstanceRecord = ImmutableRecord({
2022-03-08 21:25:30 -08:00
approval_required: false,
contact_account: ImmutableMap(),
configuration: ImmutableMap({
2022-03-08 21:25:30 -08:00
media_attachments: ImmutableMap({
image_size_limit: 10485760,
image_matrix_limit: 16777216,
video_size_limit: 41943040,
video_frame_rate_limit: 60,
video_matrix_limit: 2304000,
}),
polls: ImmutableMap({
max_options: 4,
max_characters_per_option: 25,
min_expiration: 300,
max_expiration: 2629746,
}),
2022-03-08 21:25:30 -08:00
statuses: ImmutableMap({
max_characters: 500,
max_media_attachments: 4,
}),
}),
description: '',
description_limit: 1500,
email: '',
fedibird_capabilities: ImmutableList(),
invites_enabled: false,
languages: ImmutableList(),
pleroma: ImmutableMap({
metadata: ImmutableMap({
account_activation_required: false,
birthday_min_age: 0,
birthday_required: false,
features: ImmutableList(),
federation: ImmutableMap({
enabled: true,
exclusions: false,
}),
}),
stats: ImmutableMap(),
}),
2022-03-08 21:25:30 -08:00
registrations: false,
rules: ImmutableList(),
short_description: '',
stats: ImmutableMap(),
title: '',
thumbnail: '',
uri: '',
urls: ImmutableMap(),
version: '0.0.0',
});
// Build Mastodon configuration from Pleroma instance
const pleromaToMastodonConfig = instance => {
return ImmutableMap({
statuses: ImmutableMap({
max_characters: instance.get('max_toot_chars'),
}),
polls: ImmutableMap({
max_options: instance.getIn(['poll_limits', 'max_options']),
max_characters_per_option: instance.getIn(['poll_limits', 'max_option_chars']),
min_expiration: instance.getIn(['poll_limits', 'min_expiration']),
max_expiration: instance.getIn(['poll_limits', 'max_expiration']),
}),
});
};
// Get the software's default attachment limit
const getAttachmentLimit = software => software === PLEROMA ? Infinity : 4;
// Normalize instance (Pleroma, Mastodon, etc.) to Mastodon's format
export const normalizeInstance = instance => {
const { software } = parseVersion(instance.get('version'));
const mastodonConfig = pleromaToMastodonConfig(instance);
2022-03-08 21:25:30 -08:00
return InstanceRecord(
instance.withMutations(instance => {
// Merge configuration
instance.update('configuration', ImmutableMap(), configuration => (
configuration.mergeDeepWith(mergeDefined, mastodonConfig)
));
2022-03-08 21:25:30 -08:00
// If max attachments isn't set, check the backend software
instance.updateIn(['configuration', 'statuses', 'max_media_attachments'], value => {
return isNumber(value) ? value : getAttachmentLimit(software);
});
2022-03-08 21:25:30 -08:00
// Merge defaults & cleanup
instance.mergeDeepWith(mergeDefined, InstanceRecord());
instance.deleteAll(['max_toot_chars', 'poll_limits']);
}),
);
};