Create Records for Account and Status
This commit is contained in:
parent
1c36d1b91c
commit
7a18f8b9c8
7 changed files with 160 additions and 75 deletions
|
@ -11,13 +11,6 @@ const makeEmojiMap = record => record.emojis.reduce((obj, emoji) => {
|
||||||
export function normalizeAccount(account) {
|
export function normalizeAccount(account) {
|
||||||
account = { ...account };
|
account = { ...account };
|
||||||
|
|
||||||
// Some backends can return null, or omit these required fields
|
|
||||||
if (!account.emojis) account.emojis = [];
|
|
||||||
if (!account.display_name) account.display_name = '';
|
|
||||||
if (!account.note) account.note = '';
|
|
||||||
if (!account.avatar) account.avatar = account.avatar_static || require('images/avatar-missing.png');
|
|
||||||
if (!account.avatar_static) account.avatar_static = account.avatar;
|
|
||||||
|
|
||||||
const emojiMap = makeEmojiMap(account);
|
const emojiMap = makeEmojiMap(account);
|
||||||
const displayName = account.display_name.trim().length === 0 ? account.username : account.display_name;
|
const displayName = account.display_name.trim().length === 0 ? account.username : account.display_name;
|
||||||
|
|
||||||
|
|
|
@ -4,25 +4,48 @@ import { normalizeInstance } from '../instance';
|
||||||
|
|
||||||
describe('normalizeInstance()', () => {
|
describe('normalizeInstance()', () => {
|
||||||
it('normalizes an empty Map', () => {
|
it('normalizes an empty Map', () => {
|
||||||
const expected = ImmutableMap({
|
const expected = {
|
||||||
description_limit: 1500,
|
approval_required: false,
|
||||||
configuration: ImmutableMap({
|
contact_account: {},
|
||||||
statuses: ImmutableMap({
|
configuration: {
|
||||||
max_characters: 500,
|
media_attachments: {
|
||||||
max_media_attachments: 4,
|
image_size_limit: 10485760,
|
||||||
}),
|
image_matrix_limit: 16777216,
|
||||||
polls: ImmutableMap({
|
video_size_limit: 41943040,
|
||||||
|
video_frame_rate_limit: 60,
|
||||||
|
video_matrix_limit: 2304000,
|
||||||
|
},
|
||||||
|
polls: {
|
||||||
max_options: 4,
|
max_options: 4,
|
||||||
max_characters_per_option: 25,
|
max_characters_per_option: 25,
|
||||||
min_expiration: 300,
|
min_expiration: 300,
|
||||||
max_expiration: 2629746,
|
max_expiration: 2629746,
|
||||||
}),
|
},
|
||||||
}),
|
statuses: {
|
||||||
|
max_characters: 500,
|
||||||
|
max_media_attachments: 4,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
description: '',
|
||||||
|
description_limit: 1500,
|
||||||
|
email: '',
|
||||||
|
fedibird_capabilities: [],
|
||||||
|
invites_enabled: false,
|
||||||
|
languages: [],
|
||||||
|
pleroma: {},
|
||||||
|
registrations: false,
|
||||||
|
rules: [],
|
||||||
|
short_description: '',
|
||||||
|
stats: {},
|
||||||
|
title: '',
|
||||||
|
thumbnail: '',
|
||||||
|
uri: '',
|
||||||
|
urls: {},
|
||||||
version: '0.0.0',
|
version: '0.0.0',
|
||||||
});
|
};
|
||||||
|
|
||||||
const result = normalizeInstance(ImmutableMap());
|
const result = normalizeInstance(ImmutableMap());
|
||||||
expect(result).toEqual(expected);
|
expect(result.toJS()).toEqual(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('normalizes Pleroma instance with Mastodon configuration format', () => {
|
it('normalizes Pleroma instance with Mastodon configuration format', () => {
|
||||||
|
@ -104,10 +127,10 @@ describe('normalizeInstance()', () => {
|
||||||
const result = normalizeInstance(instance);
|
const result = normalizeInstance(instance);
|
||||||
|
|
||||||
// Sets description_limit
|
// Sets description_limit
|
||||||
expect(result.get('description_limit')).toEqual(1500);
|
expect(result.description_limit).toEqual(1500);
|
||||||
|
|
||||||
// But otherwise, it's the same
|
// Preserves fedibird_capabilities
|
||||||
expect(result.delete('description_limit')).toEqual(instance);
|
expect(result.fedibird_capabilities).toEqual(instance.get('fedibird_capabilities'));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('normalizes Mitra instance', () => {
|
it('normalizes Mitra instance', () => {
|
||||||
|
|
|
@ -1,7 +1,43 @@
|
||||||
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
import { Map as ImmutableMap, List as ImmutableList, Record } from 'immutable';
|
||||||
|
|
||||||
import { mergeDefined } from 'soapbox/utils/normalizers';
|
import { mergeDefined } from 'soapbox/utils/normalizers';
|
||||||
|
|
||||||
|
const AccountRecord = Record({
|
||||||
|
acct: '',
|
||||||
|
avatar: '',
|
||||||
|
avatar_static: '',
|
||||||
|
birthday: undefined,
|
||||||
|
bot: false,
|
||||||
|
created_at: new Date(),
|
||||||
|
display_name: '',
|
||||||
|
emojis: ImmutableList(),
|
||||||
|
fields: ImmutableList(),
|
||||||
|
followers_count: 0,
|
||||||
|
following_count: 0,
|
||||||
|
fqn: '',
|
||||||
|
header: '',
|
||||||
|
header_static: '',
|
||||||
|
id: '',
|
||||||
|
last_status_at: new Date(),
|
||||||
|
location: '',
|
||||||
|
locked: false,
|
||||||
|
moved: null,
|
||||||
|
note: '',
|
||||||
|
pleroma: ImmutableMap(),
|
||||||
|
source: ImmutableMap(),
|
||||||
|
statuses_count: 0,
|
||||||
|
uri: '',
|
||||||
|
url: '',
|
||||||
|
username: '',
|
||||||
|
verified: false,
|
||||||
|
|
||||||
|
// Internal fields
|
||||||
|
display_name_html: '',
|
||||||
|
note_emojified: '',
|
||||||
|
note_plain: '',
|
||||||
|
should_refetch: false,
|
||||||
|
});
|
||||||
|
|
||||||
// https://gitlab.com/soapbox-pub/soapbox-fe/-/issues/549
|
// https://gitlab.com/soapbox-pub/soapbox-fe/-/issues/549
|
||||||
const normalizePleromaLegacyFields = account => {
|
const normalizePleromaLegacyFields = account => {
|
||||||
return account.update('pleroma', ImmutableMap(), pleroma => {
|
return account.update('pleroma', ImmutableMap(), pleroma => {
|
||||||
|
@ -49,10 +85,12 @@ const normalizeLocation = account => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const normalizeAccount = account => {
|
export const normalizeAccount = account => {
|
||||||
return account.withMutations(account => {
|
return AccountRecord(
|
||||||
normalizePleromaLegacyFields(account);
|
account.withMutations(account => {
|
||||||
normalizeVerified(account);
|
normalizePleromaLegacyFields(account);
|
||||||
normalizeBirthday(account);
|
normalizeVerified(account);
|
||||||
normalizeLocation(account);
|
normalizeBirthday(account);
|
||||||
});
|
normalizeLocation(account);
|
||||||
|
}),
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,16 +1,20 @@
|
||||||
import { Map as ImmutableMap } from 'immutable';
|
import { Map as ImmutableMap, List as ImmutableList, Record } from 'immutable';
|
||||||
|
|
||||||
import { parseVersion, PLEROMA } from 'soapbox/utils/features';
|
import { parseVersion, PLEROMA } from 'soapbox/utils/features';
|
||||||
import { mergeDefined } from 'soapbox/utils/normalizers';
|
import { mergeDefined } from 'soapbox/utils/normalizers';
|
||||||
import { isNumber } from 'soapbox/utils/numbers';
|
import { isNumber } from 'soapbox/utils/numbers';
|
||||||
|
|
||||||
// Use Mastodon defaults
|
// Use Mastodon defaults
|
||||||
const baseInstance = ImmutableMap({
|
const InstanceRecord = Record({
|
||||||
description_limit: 1500,
|
approval_required: false,
|
||||||
|
contact_account: ImmutableMap(),
|
||||||
configuration: ImmutableMap({
|
configuration: ImmutableMap({
|
||||||
statuses: ImmutableMap({
|
media_attachments: ImmutableMap({
|
||||||
max_characters: 500,
|
image_size_limit: 10485760,
|
||||||
max_media_attachments: 4,
|
image_matrix_limit: 16777216,
|
||||||
|
video_size_limit: 41943040,
|
||||||
|
video_frame_rate_limit: 60,
|
||||||
|
video_matrix_limit: 2304000,
|
||||||
}),
|
}),
|
||||||
polls: ImmutableMap({
|
polls: ImmutableMap({
|
||||||
max_options: 4,
|
max_options: 4,
|
||||||
|
@ -18,7 +22,38 @@ const baseInstance = ImmutableMap({
|
||||||
min_expiration: 300,
|
min_expiration: 300,
|
||||||
max_expiration: 2629746,
|
max_expiration: 2629746,
|
||||||
}),
|
}),
|
||||||
|
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(),
|
||||||
|
}),
|
||||||
|
registrations: false,
|
||||||
|
rules: ImmutableList(),
|
||||||
|
short_description: '',
|
||||||
|
stats: ImmutableMap(),
|
||||||
|
title: '',
|
||||||
|
thumbnail: '',
|
||||||
|
uri: '',
|
||||||
|
urls: ImmutableMap(),
|
||||||
version: '0.0.0',
|
version: '0.0.0',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -45,19 +80,21 @@ export const normalizeInstance = instance => {
|
||||||
const { software } = parseVersion(instance.get('version'));
|
const { software } = parseVersion(instance.get('version'));
|
||||||
const mastodonConfig = pleromaToMastodonConfig(instance);
|
const mastodonConfig = pleromaToMastodonConfig(instance);
|
||||||
|
|
||||||
return instance.withMutations(instance => {
|
return InstanceRecord(
|
||||||
// Merge configuration
|
instance.withMutations(instance => {
|
||||||
instance.update('configuration', ImmutableMap(), configuration => (
|
// Merge configuration
|
||||||
configuration.mergeDeepWith(mergeDefined, mastodonConfig)
|
instance.update('configuration', ImmutableMap(), configuration => (
|
||||||
));
|
configuration.mergeDeepWith(mergeDefined, mastodonConfig)
|
||||||
|
));
|
||||||
|
|
||||||
// If max attachments isn't set, check the backend software
|
// If max attachments isn't set, check the backend software
|
||||||
instance.updateIn(['configuration', 'statuses', 'max_media_attachments'], value => {
|
instance.updateIn(['configuration', 'statuses', 'max_media_attachments'], value => {
|
||||||
return isNumber(value) ? value : getAttachmentLimit(software);
|
return isNumber(value) ? value : getAttachmentLimit(software);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Merge defaults & cleanup
|
// Merge defaults & cleanup
|
||||||
instance.mergeDeepWith(mergeDefined, baseInstance);
|
instance.mergeDeepWith(mergeDefined, InstanceRecord());
|
||||||
instance.deleteAll(['max_toot_chars', 'poll_limits']);
|
instance.deleteAll(['max_toot_chars', 'poll_limits']);
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -33,6 +33,12 @@ const StatusRecord = Record({
|
||||||
uri: '',
|
uri: '',
|
||||||
url: '',
|
url: '',
|
||||||
visibility: 'public',
|
visibility: 'public',
|
||||||
|
|
||||||
|
// Internal fields
|
||||||
|
contentHtml: '',
|
||||||
|
hidden: false,
|
||||||
|
search_index: '',
|
||||||
|
spoilerHtml: '',
|
||||||
});
|
});
|
||||||
|
|
||||||
const basePollOption = ImmutableMap({ title: '', votes_count: 0 });
|
const basePollOption = ImmutableMap({ title: '', votes_count: 0 });
|
||||||
|
|
|
@ -1,27 +1,29 @@
|
||||||
import { Map as ImmutableMap } from 'immutable';
|
|
||||||
|
|
||||||
import { INSTANCE_REMEMBER_SUCCESS } from 'soapbox/actions/instance';
|
import { INSTANCE_REMEMBER_SUCCESS } from 'soapbox/actions/instance';
|
||||||
|
|
||||||
import reducer from '../instance';
|
import reducer from '../instance';
|
||||||
|
|
||||||
describe('instance reducer', () => {
|
describe('instance reducer', () => {
|
||||||
it('should return the initial state', () => {
|
it('should return the initial state', () => {
|
||||||
expect(reducer(undefined, {})).toEqual(ImmutableMap({
|
const result = reducer(undefined, {});
|
||||||
|
|
||||||
|
const expected = {
|
||||||
description_limit: 1500,
|
description_limit: 1500,
|
||||||
configuration: ImmutableMap({
|
configuration: {
|
||||||
statuses: ImmutableMap({
|
statuses: {
|
||||||
max_characters: 500,
|
max_characters: 500,
|
||||||
max_media_attachments: 4,
|
max_media_attachments: 4,
|
||||||
}),
|
},
|
||||||
polls: ImmutableMap({
|
polls: {
|
||||||
max_options: 4,
|
max_options: 4,
|
||||||
max_characters_per_option: 25,
|
max_characters_per_option: 25,
|
||||||
min_expiration: 300,
|
min_expiration: 300,
|
||||||
max_expiration: 2629746,
|
max_expiration: 2629746,
|
||||||
}),
|
},
|
||||||
}),
|
},
|
||||||
version: '0.0.0',
|
version: '0.0.0',
|
||||||
}));
|
};
|
||||||
|
|
||||||
|
expect(result.toJS()).toMatchObject(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('INSTANCE_REMEMBER_SUCCESS', () => {
|
describe('INSTANCE_REMEMBER_SUCCESS', () => {
|
||||||
|
|
|
@ -40,17 +40,8 @@ import {
|
||||||
|
|
||||||
const initialState = ImmutableMap();
|
const initialState = ImmutableMap();
|
||||||
|
|
||||||
const minifyAccount = account => {
|
|
||||||
return account.deleteAll([
|
|
||||||
'followers_count',
|
|
||||||
'following_count',
|
|
||||||
'statuses_count',
|
|
||||||
'source',
|
|
||||||
]);
|
|
||||||
};
|
|
||||||
|
|
||||||
const fixAccount = (state, account) => {
|
const fixAccount = (state, account) => {
|
||||||
const normalized = minifyAccount(normalizeAccount(fromJS(account)));
|
const normalized = normalizeAccount(fromJS(account));
|
||||||
return state.set(account.id, normalized);
|
return state.set(account.id, normalized);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -125,20 +116,15 @@ const removePermission = (state, accountIds, permissionGroup) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const buildAccount = adminUser => fromJS({
|
const buildAccount = adminUser => normalizeAccount(fromJS({
|
||||||
id: adminUser.get('id'),
|
id: adminUser.get('id'),
|
||||||
username: adminUser.get('nickname').split('@')[0],
|
username: adminUser.get('nickname').split('@')[0],
|
||||||
acct: adminUser.get('nickname'),
|
acct: adminUser.get('nickname'),
|
||||||
display_name: adminUser.get('display_name'),
|
display_name: adminUser.get('display_name'),
|
||||||
display_name_html: adminUser.get('display_name'),
|
display_name_html: adminUser.get('display_name'),
|
||||||
note: '',
|
|
||||||
url: adminUser.get('url'),
|
url: adminUser.get('url'),
|
||||||
avatar: adminUser.get('avatar'),
|
avatar: adminUser.get('avatar'),
|
||||||
avatar_static: adminUser.get('avatar'),
|
avatar_static: adminUser.get('avatar'),
|
||||||
header: '',
|
|
||||||
header_static: '',
|
|
||||||
emojis: [],
|
|
||||||
fields: [],
|
|
||||||
created_at: adminUser.get('created_at'),
|
created_at: adminUser.get('created_at'),
|
||||||
pleroma: {
|
pleroma: {
|
||||||
is_active: adminUser.get('is_active'),
|
is_active: adminUser.get('is_active'),
|
||||||
|
@ -153,7 +139,7 @@ const buildAccount = adminUser => fromJS({
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
should_refetch: true,
|
should_refetch: true,
|
||||||
});
|
}));
|
||||||
|
|
||||||
const mergeAdminUser = (account, adminUser) => {
|
const mergeAdminUser = (account, adminUser) => {
|
||||||
return account.withMutations(account => {
|
return account.withMutations(account => {
|
||||||
|
|
Loading…
Reference in a new issue