Set attachment limit from instance

This commit is contained in:
Alex Gleason 2022-01-31 17:52:12 -06:00
parent 04979486e8
commit e04e75f831
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
5 changed files with 16 additions and 19 deletions

View File

@ -296,8 +296,7 @@ export function submitComposeFail(error) {
export function uploadCompose(files) { export function uploadCompose(files) {
return function(dispatch, getState) { return function(dispatch, getState) {
if (!isLoggedIn(getState)) return; if (!isLoggedIn(getState)) return;
const instance = getState().get('instance'); const attachmentLimit = getState().getIn(['instance', 'configuration', 'statuses', 'max_media_attachments']);
const { attachmentLimit } = getFeatures(instance);
const media = getState().getIn(['compose', 'media_attachments']); const media = getState().getIn(['compose', 'media_attachments']);
const progress = new Array(files.length).fill(0); const progress = new Array(files.length).fill(0);

View File

@ -11,6 +11,7 @@ describe('instance reducer', () => {
configuration: ImmutableMap({ configuration: ImmutableMap({
statuses: ImmutableMap({ statuses: ImmutableMap({
max_characters: 500, max_characters: 500,
max_media_attachments: 4,
}), }),
polls: ImmutableMap({ polls: ImmutableMap({
max_options: 4, max_options: 4,
@ -36,6 +37,7 @@ describe('instance reducer', () => {
configuration: { configuration: {
statuses: { statuses: {
max_characters: 5000, max_characters: 5000,
max_media_attachments: Infinity,
}, },
polls: { polls: {
max_options: 20, max_options: 20,
@ -95,6 +97,7 @@ describe('instance reducer', () => {
configuration: { configuration: {
statuses: { statuses: {
max_characters: 500, max_characters: 500,
max_media_attachments: 4,
}, },
polls: { polls: {
max_options: 4, max_options: 4,

View File

@ -4,6 +4,8 @@ import { ADMIN_CONFIG_UPDATE_REQUEST, ADMIN_CONFIG_UPDATE_SUCCESS } from 'soapbo
import { PLEROMA_PRELOAD_IMPORT } from 'soapbox/actions/preload'; import { PLEROMA_PRELOAD_IMPORT } from 'soapbox/actions/preload';
import KVStore from 'soapbox/storage/kv_store'; import KVStore from 'soapbox/storage/kv_store';
import { ConfigDB } from 'soapbox/utils/config_db'; import { ConfigDB } from 'soapbox/utils/config_db';
import { parseVersion, PLEROMA } from 'soapbox/utils/features';
import { isNumber } from 'soapbox/utils/numbers';
import { import {
INSTANCE_REMEMBER_SUCCESS, INSTANCE_REMEMBER_SUCCESS,
@ -34,6 +36,7 @@ const initialState = ImmutableMap({
configuration: ImmutableMap({ configuration: ImmutableMap({
statuses: ImmutableMap({ statuses: ImmutableMap({
max_characters: 500, max_characters: 500,
max_media_attachments: 4,
}), }),
polls: ImmutableMap({ polls: ImmutableMap({
max_options: 4, max_options: 4,
@ -63,8 +66,12 @@ const pleromaToMastodonConfig = instance => {
// Use new value only if old value is undefined // Use new value only if old value is undefined
const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal; const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal;
// Get the software's default attachment limit
const getAttachmentLimit = software => software === PLEROMA ? Infinity : 4;
// Normalize instance (Pleroma, Mastodon, etc.) to Mastodon's format // Normalize instance (Pleroma, Mastodon, etc.) to Mastodon's format
const normalizeInstance = instance => { const normalizeInstance = instance => {
const { software } = parseVersion(instance.get('version'));
const mastodonConfig = pleromaToMastodonConfig(instance); const mastodonConfig = pleromaToMastodonConfig(instance);
return instance.withMutations(instance => { return instance.withMutations(instance => {
@ -73,6 +80,11 @@ const normalizeInstance = instance => {
configuration.mergeDeepWith(mergeDefined, mastodonConfig) configuration.mergeDeepWith(mergeDefined, mastodonConfig)
)); ));
// If max attachments isn't set, check the backend software
instance.updateIn(['configuration', 'statuses', 'max_media_attachments'], value => {
return isNumber(value) ? value : getAttachmentLimit(software);
});
// Merge defaults & cleanup // Merge defaults & cleanup
instance.mergeDeepWith(mergeDefined, initialState); instance.mergeDeepWith(mergeDefined, initialState);
instance.deleteAll(['max_toot_chars', 'poll_limits']); instance.deleteAll(['max_toot_chars', 'poll_limits']);

View File

@ -94,22 +94,6 @@ describe('getFeatures', () => {
}); });
}); });
describe('attachmentLimit', () => {
it('is 4 by default', () => {
const instance = ImmutableMap({ version: '3.1.4' });
const features = getFeatures(instance);
expect(features.attachmentLimit).toEqual(4);
});
it('is Infinity for Pleroma', () => {
const instance = ImmutableMap({
version: '2.7.2 (compatible; Pleroma 1.1.50-42-g3d9ac6ae-develop)',
});
const features = getFeatures(instance);
expect(features.attachmentLimit).toEqual(Infinity);
});
});
describe('focalPoint', () => { describe('focalPoint', () => {
it('is true for Mastodon 2.3.0+', () => { it('is true for Mastodon 2.3.0+', () => {
const instance = ImmutableMap({ version: '2.3.0' }); const instance = ImmutableMap({ version: '2.3.0' });

View File

@ -49,7 +49,6 @@ export const getFeatures = createSelector([
]), ]),
emojiReacts: v.software === PLEROMA && gte(v.version, '2.0.0'), emojiReacts: v.software === PLEROMA && gte(v.version, '2.0.0'),
emojiReactsRGI: v.software === PLEROMA && gte(v.version, '2.2.49'), emojiReactsRGI: v.software === PLEROMA && gte(v.version, '2.2.49'),
attachmentLimit: v.software === PLEROMA ? Infinity : 4,
focalPoint: v.software === MASTODON && gte(v.compatVersion, '2.3.0'), focalPoint: v.software === MASTODON && gte(v.compatVersion, '2.3.0'),
importAPI: v.software === PLEROMA, importAPI: v.software === PLEROMA,
importMutes: v.software === PLEROMA && gte(v.version, '2.2.0'), importMutes: v.software === PLEROMA && gte(v.version, '2.2.0'),