Merge branch 'record-enhancements' into 'develop'

Record enhancements

See merge request soapbox-pub/soapbox-fe!1084
merge-requests/1089/head
Alex Gleason 3 years ago
commit 5ddd0b9686

@ -40,9 +40,7 @@ module.exports = {
react: { react: {
version: 'detect', version: 'detect',
}, },
'import/extensions': [ 'import/extensions': ['.js', '.jsx', '.ts', '.tsx'],
'.js',
],
'import/ignore': [ 'import/ignore': [
'node_modules', 'node_modules',
'\\.(css|scss|json)$', '\\.(css|scss|json)$',
@ -257,9 +255,17 @@ module.exports = {
overrides: [ overrides: [
{ {
files: ['**/*.tsx'], files: ['**/*.tsx'],
'rules': { rules: {
'react/prop-types': 'off', 'react/prop-types': 'off',
}, },
}, },
// Disable no-undef in TypeScript
// https://stackoverflow.com/a/69155899
{
files: ['*.ts', '*.tsx'],
rules: {
'no-undef': 'off',
},
},
], ],
}; };

@ -8,13 +8,7 @@ describe('normalizeInstance()', () => {
approval_required: false, approval_required: false,
contact_account: {}, contact_account: {},
configuration: { configuration: {
media_attachments: { media_attachments: {},
image_size_limit: 10485760,
image_matrix_limit: 16777216,
video_size_limit: 41943040,
video_frame_rate_limit: 60,
video_matrix_limit: 2304000,
},
polls: { polls: {
max_options: 4, max_options: 4,
max_characters_per_option: 25, max_characters_per_option: 25,
@ -48,7 +42,11 @@ describe('normalizeInstance()', () => {
registrations: false, registrations: false,
rules: [], rules: [],
short_description: '', short_description: '',
stats: {}, stats: {
domain_count: 0,
status_count: 0,
user_count: 0,
},
title: '', title: '',
thumbnail: '', thumbnail: '',
uri: '', uri: '',

@ -1,8 +1,13 @@
import { Map as ImmutableMap, List as ImmutableList, Record } from 'immutable'; import {
Map as ImmutableMap,
List as ImmutableList,
Record as ImmutableRecord,
} from 'immutable';
import { IAccount } from 'soapbox/types';
import { mergeDefined } from 'soapbox/utils/normalizers'; import { mergeDefined } from 'soapbox/utils/normalizers';
const AccountRecord = Record({ const AccountRecord = ImmutableRecord({
acct: '', acct: '',
avatar: '', avatar: '',
avatar_static: '', avatar_static: '',
@ -41,8 +46,8 @@ const AccountRecord = Record({
}); });
// https://gitlab.com/soapbox-pub/soapbox-fe/-/issues/549 // https://gitlab.com/soapbox-pub/soapbox-fe/-/issues/549
const normalizePleromaLegacyFields = account => { const normalizePleromaLegacyFields = (account: ImmutableMap<string, any>) => {
return account.update('pleroma', ImmutableMap(), pleroma => { return account.update('pleroma', ImmutableMap(), (pleroma: ImmutableMap<string, any>) => {
return pleroma.withMutations(pleroma => { return pleroma.withMutations(pleroma => {
const legacy = ImmutableMap({ const legacy = ImmutableMap({
is_active: !pleroma.get('deactivated'), is_active: !pleroma.get('deactivated'),
@ -57,7 +62,7 @@ const normalizePleromaLegacyFields = account => {
}; };
// Normalize Pleroma/Fedibird birthday // Normalize Pleroma/Fedibird birthday
const normalizeBirthday = account => { const normalizeBirthday = (account: ImmutableMap<string, any>) => {
const birthday = [ const birthday = [
account.getIn(['pleroma', 'birthday']), account.getIn(['pleroma', 'birthday']),
account.getIn(['other_settings', 'birthday']), account.getIn(['other_settings', 'birthday']),
@ -66,18 +71,24 @@ const normalizeBirthday = account => {
return account.set('birthday', birthday); return account.set('birthday', birthday);
}; };
// Get Pleroma tags
const getTags = (account: ImmutableMap<string, any>): ImmutableList<any> => {
const tags = account.getIn(['pleroma', 'tags']);
return ImmutableList(ImmutableList.isList(tags) ? tags : []);
};
// Normalize Truth Social/Pleroma verified // Normalize Truth Social/Pleroma verified
const normalizeVerified = account => { const normalizeVerified = (account: ImmutableMap<string, any>) => {
return account.update('verified', verified => { return account.update('verified', verified => {
return [ return [
verified === true, verified === true,
account.getIn(['pleroma', 'tags'], ImmutableList()).includes('verified'), getTags(account).includes('verified'),
].some(Boolean); ].some(Boolean);
}); });
}; };
// Normalize Fedibird/Truth Social location // Normalize Fedibird/Truth Social location
const normalizeLocation = account => { const normalizeLocation = (account: ImmutableMap<string, any>) => {
return account.update('location', location => { return account.update('location', location => {
return [ return [
location, location,
@ -86,7 +97,7 @@ const normalizeLocation = account => {
}); });
}; };
export const normalizeAccount = account => { export const normalizeAccount = (account: ImmutableMap<string, any>): IAccount => {
return AccountRecord( return AccountRecord(
account.withMutations(account => { account.withMutations(account => {
normalizePleromaLegacyFields(account); normalizePleromaLegacyFields(account);

@ -1,21 +1,19 @@
import { Map as ImmutableMap, List as ImmutableList, Record } from 'immutable'; import {
Map as ImmutableMap,
List as ImmutableList,
Record as ImmutableRecord,
} 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 InstanceRecord = Record({ const InstanceRecord = ImmutableRecord({
approval_required: false, approval_required: false,
contact_account: ImmutableMap(), contact_account: ImmutableMap(),
configuration: ImmutableMap({ configuration: ImmutableMap({
media_attachments: ImmutableMap({ 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({ polls: ImmutableMap({
max_options: 4, max_options: 4,
max_characters_per_option: 25, max_characters_per_option: 25,
@ -49,7 +47,11 @@ const InstanceRecord = Record({
registrations: false, registrations: false,
rules: ImmutableList(), rules: ImmutableList(),
short_description: '', short_description: '',
stats: ImmutableMap(), stats: ImmutableMap({
domain_count: 0,
status_count: 0,
user_count: 0,
}),
title: '', title: '',
thumbnail: '', thumbnail: '',
uri: '', uri: '',

@ -1,9 +1,14 @@
import { Map as ImmutableMap, List as ImmutableList, Record } from 'immutable'; import {
Map as ImmutableMap,
List as ImmutableList,
Record as ImmutableRecord,
} from 'immutable';
import { IStatus } from 'soapbox/types';
import { accountToMention } from 'soapbox/utils/accounts'; import { accountToMention } from 'soapbox/utils/accounts';
import { mergeDefined } from 'soapbox/utils/normalizers'; import { mergeDefined } from 'soapbox/utils/normalizers';
const StatusRecord = Record({ const StatusRecord = ImmutableRecord({
account: ImmutableMap(), account: ImmutableMap(),
application: null, application: null,
bookmarked: false, bookmarked: false,
@ -56,7 +61,7 @@ const basePoll = ImmutableMap({
// Ensure attachments have required fields // Ensure attachments have required fields
// https://docs.joinmastodon.org/entities/attachment/ // https://docs.joinmastodon.org/entities/attachment/
const normalizeAttachment = attachment => { const normalizeAttachment = (attachment: ImmutableMap<string, any>) => {
const url = [ const url = [
attachment.get('url'), attachment.get('url'),
attachment.get('preview_url'), attachment.get('preview_url'),
@ -72,14 +77,14 @@ const normalizeAttachment = attachment => {
return attachment.mergeWith(mergeDefined, base); return attachment.mergeWith(mergeDefined, base);
}; };
const normalizeAttachments = status => { const normalizeAttachments = (status: ImmutableMap<string, any>) => {
return status.update('media_attachments', ImmutableList(), attachments => { return status.update('media_attachments', ImmutableList(), attachments => {
return attachments.map(normalizeAttachment); return attachments.map(normalizeAttachment);
}); });
}; };
// Normalize mentions // Normalize mentions
const normalizeMention = mention => { const normalizeMention = (mention: ImmutableMap<string, any>) => {
const base = ImmutableMap({ const base = ImmutableMap({
acct: '', acct: '',
username: (mention.get('acct') || '').split('@')[0], username: (mention.get('acct') || '').split('@')[0],
@ -89,22 +94,22 @@ const normalizeMention = mention => {
return mention.mergeWith(mergeDefined, base); return mention.mergeWith(mergeDefined, base);
}; };
const normalizeMentions = status => { const normalizeMentions = (status: ImmutableMap<string, any>) => {
return status.update('mentions', ImmutableList(), mentions => { return status.update('mentions', ImmutableList(), mentions => {
return mentions.map(normalizeMention); return mentions.map(normalizeMention);
}); });
}; };
// Normalize poll option // Normalize poll option
const normalizePollOption = option => { const normalizePollOption = (option: ImmutableMap<string, any>) => {
return option.mergeWith(mergeDefined, basePollOption); return option.mergeWith(mergeDefined, basePollOption);
}; };
// Normalize poll // Normalize poll
const normalizePoll = status => { const normalizePoll = (status: ImmutableMap<string, any>) => {
if (status.hasIn(['poll', 'options'])) { if (status.hasIn(['poll', 'options'])) {
return status.update('poll', ImmutableMap(), poll => { return status.update('poll', ImmutableMap(), poll => {
return poll.mergeWith(mergeDefined, basePoll).update('options', options => { return poll.mergeWith(mergeDefined, basePoll).update('options', (options: ImmutableList<ImmutableMap<string, any>>) => {
return options.map(normalizePollOption); return options.map(normalizePollOption);
}); });
}); });
@ -113,12 +118,12 @@ const normalizePoll = status => {
} }
}; };
// Fix order of mentions // Fix order of mentions
const fixMentionsOrder = status => { const fixMentionsOrder = (status: ImmutableMap<string, any>) => {
const mentions = status.get('mentions', ImmutableList()); const mentions = status.get('mentions', ImmutableList());
const inReplyToAccountId = status.get('in_reply_to_account_id'); const inReplyToAccountId = status.get('in_reply_to_account_id');
// Sort the replied-to mention to the top // Sort the replied-to mention to the top
const sorted = mentions.sort((a, b) => { const sorted = mentions.sort((a: ImmutableMap<string, any>, _b: ImmutableMap<string, any>) => {
if (a.get('id') === inReplyToAccountId) { if (a.get('id') === inReplyToAccountId) {
return -1; return -1;
} else { } else {
@ -130,7 +135,7 @@ const fixMentionsOrder = status => {
}; };
// Add self to mentions if it's a reply to self // Add self to mentions if it's a reply to self
const addSelfMention = status => { const addSelfMention = (status: ImmutableMap<string, any>) => {
const accountId = status.getIn(['account', 'id']); const accountId = status.getIn(['account', 'id']);
const isSelfReply = accountId === status.get('in_reply_to_account_id'); const isSelfReply = accountId === status.get('in_reply_to_account_id');
@ -147,14 +152,14 @@ const addSelfMention = status => {
}; };
// Move the quote to the top-level // Move the quote to the top-level
const fixQuote = status => { const fixQuote = (status: ImmutableMap<string, any>) => {
return status.withMutations(status => { return status.withMutations(status => {
status.update('quote', quote => quote || status.getIn(['pleroma', 'quote']) || null); status.update('quote', quote => quote || status.getIn(['pleroma', 'quote']) || null);
status.deleteIn(['pleroma', 'quote']); status.deleteIn(['pleroma', 'quote']);
}); });
}; };
export const normalizeStatus = status => { export const normalizeStatus = (status: ImmutableMap<any, string>): IStatus => {
return StatusRecord( return StatusRecord(
status.withMutations(status => { status.withMutations(status => {
normalizeAttachments(status); normalizeAttachments(status);

@ -7,6 +7,6 @@ describe('root reducer', () => {
const result = reducer(undefined, {}); const result = reducer(undefined, {});
expect(ImmutableRecord.isRecord(result)).toBe(true); expect(ImmutableRecord.isRecord(result)).toBe(true);
expect(result.accounts.get('')).toBe(undefined); expect(result.accounts.get('')).toBe(undefined);
expect(result.instance.get('version')).toEqual('0.0.0'); expect(result.instance.version).toEqual('0.0.0');
}); });
}); });

@ -0,0 +1,44 @@
/**
* Account entity.
* https://docs.joinmastodon.org/entities/account/
**/
interface IAccount {
acct: string;
avatar: string;
avatar_static: string;
birthday: Date | undefined;
bot: boolean;
created_at: Date;
display_name: string;
emojis: Iterable<any>;
fields: Iterable<any>;
followers_count: number;
following_count: number;
fqn: string;
header: string;
header_static: string;
id: string;
last_status_at: Date;
location: string;
locked: boolean;
moved: null;
note: string;
pleroma: Record<any, any>;
source: Record<any, any>;
statuses_count: number;
uri: string;
url: string;
username: string;
verified: boolean;
// Internal fields
display_name_html: string;
note_emojified: string;
note_plain: string;
patron: Record<any, any>;
relationship: Iterable<any>;
should_refetch: boolean;
}
export { IAccount };

@ -0,0 +1,4 @@
import { IAccount } from './account';
import { IStatus } from './status';
export { IAccount, IStatus };

@ -0,0 +1,45 @@
/**
* Status entity.
* https://docs.joinmastodon.org/entities/status/
**/
interface IStatus {
account: Record<any, any>;
application: Record<string, any> | null;
bookmarked: boolean;
card: Record<string, any> | null;
content: string;
created_at: Date;
emojis: Iterable<any>;
favourited: boolean;
favourites_count: number;
in_reply_to_account_id: string | null;
in_reply_to_id: string | null;
id: string;
language: null;
media_attachments: Iterable<any>;
mentions: Iterable<any>;
muted: boolean;
pinned: boolean;
pleroma: Record<string, any>;
poll: null;
quote: null;
reblog: null;
reblogged: boolean;
reblogs_count: number;
replies_count: number;
sensitive: boolean;
spoiler_text: string;
tags: Iterable<any>;
uri: string;
url: string;
visibility: string;
// Internal fields
contentHtml: string;
hidden: boolean;
search_index: string;
spoilerHtml: string;
}
export { IStatus };
Loading…
Cancel
Save