Call fromJS in all normalizers

This commit is contained in:
Alex Gleason 2022-03-16 21:33:09 -05:00
parent 6b8e79d898
commit 8167b72762
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
17 changed files with 97 additions and 82 deletions

View file

@ -6,7 +6,7 @@ const AVATAR_MISSING = require('images/avatar-missing.png');
describe('normalizeAccount()', () => {
it('adds base fields', () => {
const account = fromJS({});
const account = {};
const result = normalizeAccount(account);
expect(ImmutableRecord.isRecord(result)).toBe(true);
@ -16,12 +16,12 @@ describe('normalizeAccount()', () => {
});
it('normalizes a mention', () => {
const mention = fromJS({
const mention = {
acct: 'NEETzsche@iddqd.social',
id: '9v5bw7hEGBPc9nrpzc',
url: 'https://iddqd.social/users/NEETzsche',
username: 'NEETzsche',
});
};
const result = normalizeAccount(mention);
expect(result.emojis).toEqual(fromJS([]));
@ -32,21 +32,21 @@ describe('normalizeAccount()', () => {
});
it('normalizes Fedibird birthday', () => {
const account = fromJS(require('soapbox/__fixtures__/fedibird-account.json'));
const account = require('soapbox/__fixtures__/fedibird-account.json');
const result = normalizeAccount(account);
expect(result.birthday).toEqual('1993-07-03');
});
it('normalizes Pleroma birthday', () => {
const account = fromJS(require('soapbox/__fixtures__/pleroma-account.json'));
const account = require('soapbox/__fixtures__/pleroma-account.json');
const result = normalizeAccount(account);
expect(result.birthday).toEqual('1993-07-03');
});
it('normalizes Pleroma legacy fields', () => {
const account = fromJS(require('soapbox/__fixtures__/pleroma-2.2.2-account.json'));
const account = require('soapbox/__fixtures__/pleroma-2.2.2-account.json');
const result = normalizeAccount(account);
expect(result.getIn(['pleroma', 'is_active'])).toBe(true);
@ -57,7 +57,7 @@ describe('normalizeAccount()', () => {
});
it('prefers new Pleroma fields', () => {
const account = fromJS(require('soapbox/__fixtures__/pleroma-account.json'));
const account = require('soapbox/__fixtures__/pleroma-account.json');
const result = normalizeAccount(account);
expect(result.getIn(['pleroma', 'is_active'])).toBe(true);
@ -66,76 +66,76 @@ describe('normalizeAccount()', () => {
});
it('normalizes a verified Pleroma user', () => {
const account = fromJS(require('soapbox/__fixtures__/mk.json'));
const account = require('soapbox/__fixtures__/mk.json');
const result = normalizeAccount(account);
expect(result.verified).toBe(true);
});
it('normalizes an unverified Pleroma user', () => {
const account = fromJS(require('soapbox/__fixtures__/pleroma-account.json'));
const account = require('soapbox/__fixtures__/pleroma-account.json');
const result = normalizeAccount(account);
expect(result.verified).toBe(false);
});
it('normalizes a verified Truth Social user', () => {
const account = fromJS(require('soapbox/__fixtures__/realDonaldTrump.json'));
const account = require('soapbox/__fixtures__/realDonaldTrump.json');
const result = normalizeAccount(account);
expect(result.verified).toBe(true);
});
it('normalizes Fedibird location', () => {
const account = fromJS(require('soapbox/__fixtures__/fedibird-account.json'));
const account = require('soapbox/__fixtures__/fedibird-account.json');
const result = normalizeAccount(account);
expect(result.location).toBe('Texas, USA');
});
it('normalizes Truth Social location', () => {
const account = fromJS(require('soapbox/__fixtures__/truthsocial-account.json'));
const account = require('soapbox/__fixtures__/truthsocial-account.json');
const result = normalizeAccount(account);
expect(result.location).toBe('Texas');
});
it('sets display_name from username', () => {
const account = fromJS({ username: 'alex' });
const account = { username: 'alex' };
const result = normalizeAccount(account);
expect(result.display_name).toBe('alex');
});
it('sets display_name from acct', () => {
const account = fromJS({ acct: 'alex@gleasonator.com' });
const account = { acct: 'alex@gleasonator.com' };
const result = normalizeAccount(account);
expect(result.display_name).toBe('alex');
});
it('overrides a whitespace display_name', () => {
const account = fromJS({ username: 'alex', display_name: ' ' });
const account = { username: 'alex', display_name: ' ' };
const result = normalizeAccount(account);
expect(result.display_name).toBe('alex');
});
it('emojifies display name as `display_name_html`', () => {
const account = fromJS(require('soapbox/__fixtures__/account-with-emojis.json'));
const account = require('soapbox/__fixtures__/account-with-emojis.json');
const result = normalizeAccount(account);
const expected = 'Alex Gleason <img draggable="false" class="emojione" alt="😂" title=":joy:" src="/packs/emoji/1f602.svg" /> <img draggable="false" class="emojione" alt=":soapbox:" title=":soapbox:" src="https://gleasonator.com/emoji/Gleasonator/soapbox.png" /> <img draggable="false" class="emojione" alt=":ablobcatrainbow:" title=":ablobcatrainbow:" src="https://gleasonator.com/emoji/blobcat/ablobcatrainbow.png" />';
expect(result.display_name_html).toBe(expected);
});
it('emojifies note as `note_emojified`', () => {
const account = fromJS(require('soapbox/__fixtures__/account-with-emojis.json'));
const account = require('soapbox/__fixtures__/account-with-emojis.json');
const result = normalizeAccount(account);
const expected = 'I create Fediverse software that empowers people online. <img draggable="false" class="emojione" alt=":soapbox:" title=":soapbox:" src="https://gleasonator.com/emoji/Gleasonator/soapbox.png" /><br/><br/>I&#39;m vegan btw<br/><br/>Note: If you have a question for me, please tag me publicly. This gives the opportunity for others to chime in, and bystanders to learn.';
expect(result.note_emojified).toBe(expected);
});
it('unescapes HTML note as `note_plain`', () => {
const account = fromJS(require('soapbox/__fixtures__/account-with-emojis.json'));
const account = require('soapbox/__fixtures__/account-with-emojis.json');
const result = normalizeAccount(account);
const expected = 'I create Fediverse software that empowers people online. :soapbox:\n\nI\'m vegan btw\n\nNote: If you have a question for me, please tag me publicly. This gives the opportunity for others to chime in, and bystanders to learn.';
expect(result.note_plain).toBe(expected);
});
it('emojifies custom profile field', () => {
const account = fromJS(require('soapbox/__fixtures__/account-with-emojis.json'));
const account = require('soapbox/__fixtures__/account-with-emojis.json');
const result = normalizeAccount(account);
const field = result.fields.get(1);

View file

@ -1,10 +1,10 @@
import { Record as ImmutableRecord, fromJS } from 'immutable';
import { Record as ImmutableRecord } from 'immutable';
import { normalizeAttachment } from '../attachment';
describe('normalizeAttachment()', () => {
it('adds base fields', () => {
const attachment = fromJS({});
const attachment = {};
const result = normalizeAttachment(attachment);
expect(ImmutableRecord.isRecord(result)).toBe(true);
@ -13,7 +13,7 @@ describe('normalizeAttachment()', () => {
});
it('infers preview_url from url', () => {
const attachment = fromJS({ url: 'https://site.fedi/123.png' });
const attachment = { url: 'https://site.fedi/123.png' };
const result = normalizeAttachment(attachment);
expect(result.preview_url).toEqual('https://site.fedi/123.png');

View file

@ -1,10 +1,10 @@
import { Record as ImmutableRecord, fromJS } from 'immutable';
import { Record as ImmutableRecord } from 'immutable';
import { normalizeCard } from '../card';
describe('normalizeCard()', () => {
it('adds base fields', () => {
const card = fromJS({});
const card = {};
const result = normalizeCard(card);
expect(ImmutableRecord.isRecord(result)).toBe(true);

View file

@ -59,7 +59,7 @@ describe('normalizeInstance()', () => {
});
it('normalizes Pleroma instance with Mastodon configuration format', () => {
const instance = fromJS(require('soapbox/__fixtures__/pleroma-instance.json'));
const instance = require('soapbox/__fixtures__/pleroma-instance.json');
const expected = {
configuration: {
@ -81,7 +81,7 @@ describe('normalizeInstance()', () => {
});
it('normalizes Mastodon instance with retained configuration', () => {
const instance = fromJS(require('soapbox/__fixtures__/mastodon-instance.json'));
const instance = require('soapbox/__fixtures__/mastodon-instance.json');
const expected = {
configuration: {
@ -111,7 +111,7 @@ describe('normalizeInstance()', () => {
});
it('normalizes Mastodon 3.0.0 instance with default configuration', () => {
const instance = fromJS(require('soapbox/__fixtures__/mastodon-3.0.0-instance.json'));
const instance = require('soapbox/__fixtures__/mastodon-3.0.0-instance.json');
const expected = {
configuration: {
@ -133,18 +133,18 @@ describe('normalizeInstance()', () => {
});
it('normalizes Fedibird instance', () => {
const instance = fromJS(require('soapbox/__fixtures__/fedibird-instance.json'));
const instance = require('soapbox/__fixtures__/fedibird-instance.json');
const result = normalizeInstance(instance);
// Sets description_limit
expect(result.description_limit).toEqual(1500);
// Preserves fedibird_capabilities
expect(result.fedibird_capabilities).toEqual(instance.get('fedibird_capabilities'));
expect(result.fedibird_capabilities).toEqual(fromJS(instance.fedibird_capabilities));
});
it('normalizes Mitra instance', () => {
const instance = fromJS(require('soapbox/__fixtures__/mitra-instance.json'));
const instance = require('soapbox/__fixtures__/mitra-instance.json');
const result = normalizeInstance(instance);
// Adds configuration and description_limit
@ -153,7 +153,7 @@ describe('normalizeInstance()', () => {
});
it('normalizes GoToSocial instance', () => {
const instance = fromJS(require('soapbox/__fixtures__/gotosocial-instance.json'));
const instance = require('soapbox/__fixtures__/gotosocial-instance.json');
const result = normalizeInstance(instance);
// Normalizes max_toot_chars
@ -166,7 +166,7 @@ describe('normalizeInstance()', () => {
});
it('normalizes Friendica instance', () => {
const instance = fromJS(require('soapbox/__fixtures__/friendica-instance.json'));
const instance = require('soapbox/__fixtures__/friendica-instance.json');
const result = normalizeInstance(instance);
// Normalizes max_toot_chars

View file

@ -1,10 +1,10 @@
import { Record as ImmutableRecord, fromJS } from 'immutable';
import { Record as ImmutableRecord } from 'immutable';
import { normalizeMention } from '../mention';
describe('normalizeMention()', () => {
it('adds base fields', () => {
const account = fromJS({});
const account = {};
const result = normalizeMention(account);
expect(ImmutableRecord.isRecord(result)).toBe(true);
@ -15,7 +15,7 @@ describe('normalizeMention()', () => {
});
it('infers username from acct', () => {
const account = fromJS({ acct: 'alex@gleasonator.com' });
const account = { acct: 'alex@gleasonator.com' };
const result = normalizeMention(account);
expect(result.username).toEqual('alex');

View file

@ -1,10 +1,10 @@
import { Record as ImmutableRecord, fromJS } from 'immutable';
import { Record as ImmutableRecord } from 'immutable';
import { normalizeNotification } from '../notification';
describe('normalizeNotification()', () => {
it('normalizes an empty map', () => {
const notification = fromJS({});
const notification = {};
const result = normalizeNotification(notification);
expect(ImmutableRecord.isRecord(result)).toBe(true);

View file

@ -1,10 +1,10 @@
import { Record as ImmutableRecord, fromJS } from 'immutable';
import { Record as ImmutableRecord } from 'immutable';
import { normalizePoll } from '../poll';
describe('normalizePoll()', () => {
it('adds base fields', () => {
const poll = fromJS({ options: [{ title: 'Apples' }] });
const poll = { options: [{ title: 'Apples' }] };
const result = normalizePoll(poll);
const expected = {
@ -25,7 +25,7 @@ describe('normalizePoll()', () => {
});
it('normalizes a Pleroma logged-out poll', () => {
const poll = fromJS(require('soapbox/__fixtures__/pleroma-status-with-poll.json')).get('poll');
const { poll } = require('soapbox/__fixtures__/pleroma-status-with-poll.json');
const result = normalizePoll(poll);
// Adds logged-in fields
@ -34,7 +34,7 @@ describe('normalizePoll()', () => {
});
it('normalizes poll with emojis', () => {
const poll = fromJS(require('soapbox/__fixtures__/pleroma-status-with-poll-with-emojis.json')).get('poll');
const { poll } = require('soapbox/__fixtures__/pleroma-status-with-poll-with-emojis.json');
const result = normalizePoll(poll);
// Emojifies poll options

View file

@ -4,7 +4,7 @@ import { normalizeStatus } from '../status';
describe('normalizeStatus()', () => {
it('adds base fields', () => {
const status = fromJS({});
const status = {};
const result = normalizeStatus(status);
expect(ImmutableRecord.isRecord(result)).toBe(true);
@ -17,7 +17,7 @@ describe('normalizeStatus()', () => {
});
it('fixes the order of mentions', () => {
const status = fromJS(require('soapbox/__fixtures__/status-unordered-mentions.json'));
const status = require('soapbox/__fixtures__/status-unordered-mentions.json');
const expected = ['NEETzsche', 'alex', 'Lumeinshin', 'sneeden'];
@ -30,7 +30,7 @@ describe('normalizeStatus()', () => {
});
it('adds mention to self in self-reply on Mastodon', () => {
const status = fromJS(require('soapbox/__fixtures__/mastodon-reply-to-self.json'));
const status = require('soapbox/__fixtures__/mastodon-reply-to-self.json');
const expected = {
id: '106801667066418367',
@ -48,7 +48,7 @@ describe('normalizeStatus()', () => {
});
it('normalizes mentions with only acct', () => {
const status = fromJS({ mentions: [{ acct: 'alex@gleasonator.com' }] });
const status = { mentions: [{ acct: 'alex@gleasonator.com' }] };
const expected = [{
id: '',
@ -63,7 +63,7 @@ describe('normalizeStatus()', () => {
});
it('normalizes Mitra attachments', () => {
const status = fromJS(require('soapbox/__fixtures__/mitra-status-with-attachments.json'));
const status = require('soapbox/__fixtures__/mitra-status-with-attachments.json');
const expected = [{
id: '017eeb0e-e5df-30a4-77a7-a929145cb836',
@ -97,7 +97,7 @@ describe('normalizeStatus()', () => {
});
it('leaves Pleroma attachments alone', () => {
const status = fromJS(require('soapbox/__fixtures__/pleroma-status-with-attachments.json'));
const status = require('soapbox/__fixtures__/pleroma-status-with-attachments.json');
const result = normalizeStatus(status).media_attachments;
expect(result.size).toBe(4);
@ -108,15 +108,15 @@ describe('normalizeStatus()', () => {
});
it('normalizes Pleroma quote post', () => {
const status = fromJS(require('soapbox/__fixtures__/pleroma-quote-post.json'));
const status = require('soapbox/__fixtures__/pleroma-quote-post.json');
const result = normalizeStatus(status);
expect(result.quote).toEqual(status.getIn(['pleroma', 'quote']));
expect(result.quote).toEqual(fromJS(status.pleroma.quote));
expect(result.pleroma.get('quote')).toBe(undefined);
});
it('normalizes GoToSocial status', () => {
const status = fromJS(require('soapbox/__fixtures__/gotosocial-status.json'));
const status = require('soapbox/__fixtures__/gotosocial-status.json');
const result = normalizeStatus(status);
// Adds missing fields
@ -132,7 +132,7 @@ describe('normalizeStatus()', () => {
});
it('normalizes Friendica status', () => {
const status = fromJS(require('soapbox/__fixtures__/friendica-status.json'));
const status = require('soapbox/__fixtures__/friendica-status.json');
const result = normalizeStatus(status);
// Adds missing fields
@ -145,7 +145,7 @@ describe('normalizeStatus()', () => {
});
it('normalizes poll and poll options', () => {
const status = fromJS({ poll: { options: [{ title: 'Apples' }] } });
const status = { poll: { options: [{ title: 'Apples' }] } };
const result = normalizeStatus(status);
const expected = {
@ -166,7 +166,7 @@ describe('normalizeStatus()', () => {
});
it('normalizes a Pleroma logged-out poll', () => {
const status = fromJS(require('soapbox/__fixtures__/pleroma-status-with-poll.json'));
const status = require('soapbox/__fixtures__/pleroma-status-with-poll.json');
const result = normalizeStatus(status);
// Adds logged-in fields
@ -175,7 +175,7 @@ describe('normalizeStatus()', () => {
});
it('normalizes poll with emojis', () => {
const status = fromJS(require('soapbox/__fixtures__/pleroma-status-with-poll-with-emojis.json'));
const status = require('soapbox/__fixtures__/pleroma-status-with-poll-with-emojis.json');
const result = normalizeStatus(status);
// Emojifies poll options
@ -188,7 +188,7 @@ describe('normalizeStatus()', () => {
});
it('normalizes a card', () => {
const status = fromJS(require('soapbox/__fixtures__/status-with-card.json'));
const status = require('soapbox/__fixtures__/status-with-card.json');
const result = normalizeStatus(status);
expect(ImmutableRecord.isRecord(result.card)).toBe(true);

View file

@ -8,6 +8,7 @@ import {
Map as ImmutableMap,
List as ImmutableList,
Record as ImmutableRecord,
fromJS,
} from 'immutable';
import emojify from 'soapbox/features/emoji/emoji';
@ -181,9 +182,9 @@ const addInternalFields = (account: ImmutableMap<string, any>) => {
});
};
export const normalizeAccount = (account: ImmutableMap<string, any>): IAccount => {
export const normalizeAccount = (account: Record<string, any>): IAccount => {
return AccountRecord(
account.withMutations(account => {
ImmutableMap(fromJS(account)).withMutations(account => {
normalizePleromaLegacyFields(account);
normalizeEmojis(account);
normalizeAvatar(account);

View file

@ -6,6 +6,7 @@
import {
Map as ImmutableMap,
Record as ImmutableRecord,
fromJS,
} from 'immutable';
import { mergeDefined } from 'soapbox/utils/normalizers';
@ -28,8 +29,7 @@ export const AttachmentRecord = ImmutableRecord({
status: null,
});
// Ensure attachments have required fields
export const normalizeAttachment = (attachment: ImmutableMap<string, any>) => {
const normalizeUrls = (attachment: ImmutableMap<string, any>) => {
const url = [
attachment.get('url'),
attachment.get('preview_url'),
@ -41,5 +41,12 @@ export const normalizeAttachment = (attachment: ImmutableMap<string, any>) => {
preview_url: url,
});
return AttachmentRecord(attachment.mergeWith(mergeDefined, base));
return attachment.mergeWith(mergeDefined, base);
};
// Ensure attachments have required fields
export const normalizeAttachment = (attachment: Record<string, any>) => {
return AttachmentRecord(
normalizeUrls(ImmutableMap(fromJS(attachment))),
);
};

View file

@ -3,7 +3,7 @@
* Converts API cards into our internal format.
* @see {@link https://docs.joinmastodon.org/entities/card/}
*/
import { Record as ImmutableRecord, Map as ImmutableMap } from 'immutable';
import { Record as ImmutableRecord, Map as ImmutableMap, fromJS } from 'immutable';
// https://docs.joinmastodon.org/entities/card/
export const CardRecord = ImmutableRecord({
@ -23,6 +23,8 @@ export const CardRecord = ImmutableRecord({
width: 0,
});
export const normalizeCard = (card: ImmutableMap<string, any>) => {
return CardRecord(card);
export const normalizeCard = (card: Record<string, any>) => {
return CardRecord(
ImmutableMap(fromJS(card)),
);
};

View file

@ -3,7 +3,7 @@
* Converts API emojis into our internal format.
* @see {@link https://docs.joinmastodon.org/entities/emoji/}
*/
import { Record as ImmutableRecord, Map as ImmutableMap } from 'immutable';
import { Record as ImmutableRecord, Map as ImmutableMap, fromJS } from 'immutable';
// https://docs.joinmastodon.org/entities/emoji/
export const EmojiRecord = ImmutableRecord({
@ -14,6 +14,8 @@ export const EmojiRecord = ImmutableRecord({
visible_in_picker: true,
});
export const normalizeEmoji = (emoji: ImmutableMap<string, any>) => {
return EmojiRecord(emoji);
export const normalizeEmoji = (emoji: Record<string, any>) => {
return EmojiRecord(
ImmutableMap(fromJS(emoji)),
);
};

View file

@ -7,6 +7,7 @@ import {
Map as ImmutableMap,
List as ImmutableList,
Record as ImmutableRecord,
fromJS,
} from 'immutable';
import { parseVersion, PLEROMA } from 'soapbox/utils/features';
@ -84,12 +85,12 @@ const pleromaToMastodonConfig = (instance: ImmutableMap<string, any>) => {
const getAttachmentLimit = (software: string) => software === PLEROMA ? Infinity : 4;
// Normalize instance (Pleroma, Mastodon, etc.) to Mastodon's format
export const normalizeInstance = (instance: ImmutableMap<string, any>) => {
const { software } = parseVersion(instance.get('version'));
const mastodonConfig = pleromaToMastodonConfig(instance);
export const normalizeInstance = (instance: Record<string, any>) => {
return InstanceRecord(
instance.withMutations(instance => {
ImmutableMap(fromJS(instance)).withMutations((instance: ImmutableMap<string, any>) => {
const { software } = parseVersion(instance.get('version'));
const mastodonConfig = pleromaToMastodonConfig(instance);
// Merge configuration
instance.update('configuration', ImmutableMap(), configuration => (
configuration.mergeDeepWith(mergeDefined, mastodonConfig)

View file

@ -3,10 +3,7 @@
* Converts API mentions into our internal format.
* @see {@link https://docs.joinmastodon.org/entities/mention/}
*/
import {
Map as ImmutableMap,
Record as ImmutableRecord,
} from 'immutable';
import { Record as ImmutableRecord } from 'immutable';
import { normalizeAccount } from 'soapbox/normalizers/account';
@ -18,7 +15,7 @@ export const MentionRecord = ImmutableRecord({
url: '',
});
export const normalizeMention = (mention: ImmutableMap<string, any>) => {
export const normalizeMention = (mention: Record<string, any>) => {
// Simply normalize it as an account then cast it as a mention ¯\_(ツ)_/¯
return MentionRecord(normalizeAccount(mention));
};

View file

@ -6,6 +6,7 @@
import {
Map as ImmutableMap,
Record as ImmutableRecord,
fromJS,
} from 'immutable';
// https://docs.joinmastodon.org/entities/notification/
@ -20,6 +21,8 @@ export const NotificationRecord = ImmutableRecord({
type: '',
});
export const normalizeNotification = (notification: ImmutableMap<string, any>) => {
return NotificationRecord(notification);
export const normalizeNotification = (notification: Record<string, any>) => {
return NotificationRecord(
ImmutableMap(fromJS(notification)),
);
};

View file

@ -8,6 +8,7 @@ import {
Map as ImmutableMap,
List as ImmutableList,
Record as ImmutableRecord,
fromJS,
} from 'immutable';
import emojify from 'soapbox/features/emoji/emoji';
@ -76,9 +77,9 @@ const normalizePollVoted = (poll: ImmutableMap<string, any>) => {
});
};
export const normalizePoll = (poll: ImmutableMap<string, any>) => {
export const normalizePoll = (poll: Record<string, any>) => {
return PollRecord(
poll.withMutations((poll: ImmutableMap<string, any>) => {
ImmutableMap(fromJS(poll)).withMutations((poll: ImmutableMap<string, any>) => {
normalizeEmojis(poll);
normalizePollOptions(poll);
normalizePollOwnVotes(poll);

View file

@ -7,6 +7,7 @@ import {
Map as ImmutableMap,
List as ImmutableList,
Record as ImmutableRecord,
fromJS,
} from 'immutable';
import { normalizeAttachment } from 'soapbox/normalizers/attachment';
@ -135,9 +136,9 @@ const fixQuote = (status: ImmutableMap<string, any>) => {
});
};
export const normalizeStatus = (status: ImmutableMap<string, any>): IStatus => {
export const normalizeStatus = (status: Record<string, any>): IStatus => {
return StatusRecord(
status.withMutations(status => {
ImmutableMap(fromJS(status)).withMutations(status => {
normalizeAttachments(status);
normalizeMentions(status);
normalizeEmojis(status);