Break Mention normalizer into its own module (with tests)
This commit is contained in:
parent
b100068b95
commit
08f219ab64
|
@ -1,11 +1,21 @@
|
||||||
import { fromJS } from 'immutable';
|
import { Record as ImmutableRecord, fromJS } from 'immutable';
|
||||||
|
|
||||||
import { normalizeAccount } from '../account';
|
import { normalizeAccount } from '../account';
|
||||||
|
|
||||||
describe('normalizeAccount()', () => {
|
const AVATAR_MISSING = require('images/avatar-missing.png');
|
||||||
it('normalizes a mention', () => {
|
|
||||||
const avatarMissing = require('images/avatar-missing.png');
|
|
||||||
|
|
||||||
|
describe('normalizeAccount()', () => {
|
||||||
|
it('adds base fields', () => {
|
||||||
|
const account = fromJS({});
|
||||||
|
const result = normalizeAccount(account);
|
||||||
|
|
||||||
|
expect(ImmutableRecord.isRecord(result)).toBe(true);
|
||||||
|
expect(result.acct).toEqual('');
|
||||||
|
expect(result.note).toEqual('');
|
||||||
|
expect(result.avatar).toEqual(AVATAR_MISSING);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('normalizes a mention', () => {
|
||||||
const mention = fromJS({
|
const mention = fromJS({
|
||||||
acct: 'NEETzsche@iddqd.social',
|
acct: 'NEETzsche@iddqd.social',
|
||||||
id: '9v5bw7hEGBPc9nrpzc',
|
id: '9v5bw7hEGBPc9nrpzc',
|
||||||
|
@ -16,8 +26,8 @@ describe('normalizeAccount()', () => {
|
||||||
const result = normalizeAccount(mention);
|
const result = normalizeAccount(mention);
|
||||||
expect(result.emojis).toEqual(fromJS([]));
|
expect(result.emojis).toEqual(fromJS([]));
|
||||||
expect(result.display_name).toEqual('NEETzsche');
|
expect(result.display_name).toEqual('NEETzsche');
|
||||||
expect(result.avatar).toEqual(avatarMissing);
|
expect(result.avatar).toEqual(AVATAR_MISSING);
|
||||||
expect(result.avatar_static).toEqual(avatarMissing);
|
expect(result.avatar_static).toEqual(AVATAR_MISSING);
|
||||||
expect(result.verified).toBe(false);
|
expect(result.verified).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { Record as ImmutableRecord, fromJS } from 'immutable';
|
||||||
|
|
||||||
|
import { normalizeMention } from '../mention';
|
||||||
|
|
||||||
|
describe('normalizeMention()', () => {
|
||||||
|
it('adds base fields', () => {
|
||||||
|
const account = fromJS({});
|
||||||
|
const result = normalizeMention(account);
|
||||||
|
|
||||||
|
expect(ImmutableRecord.isRecord(result)).toBe(true);
|
||||||
|
expect(result.id).toEqual('');
|
||||||
|
expect(result.acct).toEqual('');
|
||||||
|
expect(result.username).toEqual('');
|
||||||
|
expect(result.url).toEqual('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('infers username from acct', () => {
|
||||||
|
const account = fromJS({ acct: 'alex@gleasonator.com' });
|
||||||
|
const result = normalizeMention(account);
|
||||||
|
|
||||||
|
expect(result.username).toEqual('alex');
|
||||||
|
});
|
||||||
|
});
|
|
@ -2,7 +2,7 @@ import { Record as ImmutableRecord, fromJS } from 'immutable';
|
||||||
|
|
||||||
import { normalizeStatus } from '../status';
|
import { normalizeStatus } from '../status';
|
||||||
|
|
||||||
describe('normalizeStatus', () => {
|
describe('normalizeStatus()', () => {
|
||||||
it('adds base fields', () => {
|
it('adds base fields', () => {
|
||||||
const status = fromJS({});
|
const status = fromJS({});
|
||||||
const result = normalizeStatus(status);
|
const result = normalizeStatus(status);
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
/**
|
||||||
|
* Mention normalizer:
|
||||||
|
* 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 { normalizeAccount } from 'soapbox/normalizers/account';
|
||||||
|
|
||||||
|
// https://docs.joinmastodon.org/entities/mention/
|
||||||
|
const MentionRecord = ImmutableRecord({
|
||||||
|
id: '',
|
||||||
|
acct: '',
|
||||||
|
username: '',
|
||||||
|
url: '',
|
||||||
|
});
|
||||||
|
|
||||||
|
export const normalizeMention = (mention: ImmutableMap<string, any>) => {
|
||||||
|
// Simply normalize it as an account then cast it as a mention ¯\_(ツ)_/¯
|
||||||
|
return MentionRecord(normalizeAccount(mention));
|
||||||
|
};
|
|
@ -11,8 +11,8 @@ import {
|
||||||
} from 'immutable';
|
} from 'immutable';
|
||||||
|
|
||||||
import emojify from 'soapbox/features/emoji/emoji';
|
import emojify from 'soapbox/features/emoji/emoji';
|
||||||
import { normalizeAccount } from 'soapbox/normalizers/account';
|
|
||||||
import { normalizeEmoji } from 'soapbox/normalizers/emoji';
|
import { normalizeEmoji } from 'soapbox/normalizers/emoji';
|
||||||
|
import { normalizeMention } from 'soapbox/normalizers/mention';
|
||||||
import { IStatus } from 'soapbox/types';
|
import { IStatus } from 'soapbox/types';
|
||||||
import { mergeDefined, makeEmojiMap } from 'soapbox/utils/normalizers';
|
import { mergeDefined, makeEmojiMap } from 'soapbox/utils/normalizers';
|
||||||
|
|
||||||
|
@ -73,14 +73,6 @@ const AttachmentRecord = ImmutableRecord({
|
||||||
status: null,
|
status: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
// https://docs.joinmastodon.org/entities/mention/
|
|
||||||
const MentionRecord = ImmutableRecord({
|
|
||||||
id: '',
|
|
||||||
acct: '',
|
|
||||||
username: '',
|
|
||||||
url: '',
|
|
||||||
});
|
|
||||||
|
|
||||||
// https://docs.joinmastodon.org/entities/poll/
|
// https://docs.joinmastodon.org/entities/poll/
|
||||||
const PollRecord = ImmutableRecord({
|
const PollRecord = ImmutableRecord({
|
||||||
emojis: ImmutableList(),
|
emojis: ImmutableList(),
|
||||||
|
@ -126,11 +118,6 @@ const normalizeAttachments = (status: ImmutableMap<string, any>) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Normalize mentions
|
|
||||||
const normalizeMention = (mention: ImmutableMap<string, any>) => {
|
|
||||||
return MentionRecord(normalizeAccount(mention));
|
|
||||||
};
|
|
||||||
|
|
||||||
const normalizeMentions = (status: ImmutableMap<string, any>) => {
|
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);
|
||||||
|
|
Loading…
Reference in New Issue