sforkowany z mirror/soapbox
Break Attachment normalizer into its own module
rodzic
6812e7bfd4
commit
5c8e8d9f99
|
@ -0,0 +1,21 @@
|
|||
import { Record as ImmutableRecord, fromJS } from 'immutable';
|
||||
|
||||
import { normalizeAttachment } from '../attachment';
|
||||
|
||||
describe('normalizeAttachment()', () => {
|
||||
it('adds base fields', () => {
|
||||
const attachment = fromJS({});
|
||||
const result = normalizeAttachment(attachment);
|
||||
|
||||
expect(ImmutableRecord.isRecord(result)).toBe(true);
|
||||
expect(result.type).toEqual('unknown');
|
||||
expect(result.url).toEqual('');
|
||||
});
|
||||
|
||||
it('infers preview_url from url', () => {
|
||||
const attachment = fromJS({ url: 'https://site.fedi/123.png' });
|
||||
const result = normalizeAttachment(attachment);
|
||||
|
||||
expect(result.preview_url).toEqual('https://site.fedi/123.png');
|
||||
});
|
||||
});
|
|
@ -0,0 +1,45 @@
|
|||
/**
|
||||
* Attachment normalizer:
|
||||
* Converts API attachments into our internal format.
|
||||
* @see {@link https://docs.joinmastodon.org/entities/attachment/}
|
||||
*/
|
||||
import {
|
||||
Map as ImmutableMap,
|
||||
Record as ImmutableRecord,
|
||||
} from 'immutable';
|
||||
|
||||
import { mergeDefined } from 'soapbox/utils/normalizers';
|
||||
|
||||
// https://docs.joinmastodon.org/entities/attachment/
|
||||
const AttachmentRecord = ImmutableRecord({
|
||||
blurhash: undefined,
|
||||
description: '',
|
||||
id: '',
|
||||
meta: ImmutableMap(),
|
||||
pleroma: ImmutableMap(),
|
||||
preview_url: '',
|
||||
remote_url: null,
|
||||
type: 'unknown',
|
||||
url: '',
|
||||
|
||||
// Internal fields
|
||||
// TODO: Remove these? They're set in selectors/index.js
|
||||
account: null,
|
||||
status: null,
|
||||
});
|
||||
|
||||
// Ensure attachments have required fields
|
||||
export const normalizeAttachment = (attachment: ImmutableMap<string, any>) => {
|
||||
const url = [
|
||||
attachment.get('url'),
|
||||
attachment.get('preview_url'),
|
||||
attachment.get('remote_url'),
|
||||
].find(url => url) || '';
|
||||
|
||||
const base = ImmutableMap({
|
||||
url,
|
||||
preview_url: url,
|
||||
});
|
||||
|
||||
return AttachmentRecord(attachment.mergeWith(mergeDefined, base));
|
||||
};
|
|
@ -9,11 +9,11 @@ import {
|
|||
Record as ImmutableRecord,
|
||||
} from 'immutable';
|
||||
|
||||
import { normalizeAttachment } from 'soapbox/normalizers/attachment';
|
||||
import { normalizeEmoji } from 'soapbox/normalizers/emoji';
|
||||
import { normalizeMention } from 'soapbox/normalizers/mention';
|
||||
import { normalizePoll } from 'soapbox/normalizers/poll';
|
||||
import { IStatus } from 'soapbox/types';
|
||||
import { mergeDefined } from 'soapbox/utils/normalizers';
|
||||
|
||||
// https://docs.joinmastodon.org/entities/status/
|
||||
const StatusRecord = ImmutableRecord({
|
||||
|
@ -55,39 +55,6 @@ const StatusRecord = ImmutableRecord({
|
|||
spoilerHtml: '',
|
||||
});
|
||||
|
||||
// https://docs.joinmastodon.org/entities/attachment/
|
||||
const AttachmentRecord = ImmutableRecord({
|
||||
blurhash: undefined,
|
||||
description: '',
|
||||
id: '',
|
||||
meta: ImmutableMap(),
|
||||
pleroma: ImmutableMap(),
|
||||
preview_url: '',
|
||||
remote_url: null,
|
||||
type: 'unknown',
|
||||
url: '',
|
||||
|
||||
// Internal fields
|
||||
account: null,
|
||||
status: null,
|
||||
});
|
||||
|
||||
// Ensure attachments have required fields
|
||||
const normalizeAttachment = (attachment: ImmutableMap<string, any>) => {
|
||||
const url = [
|
||||
attachment.get('url'),
|
||||
attachment.get('preview_url'),
|
||||
attachment.get('remote_url'),
|
||||
].find(url => url) || '';
|
||||
|
||||
const base = ImmutableMap({
|
||||
url,
|
||||
preview_url: url,
|
||||
});
|
||||
|
||||
return AttachmentRecord(attachment.mergeWith(mergeDefined, base));
|
||||
};
|
||||
|
||||
const normalizeAttachments = (status: ImmutableMap<string, any>) => {
|
||||
return status.update('media_attachments', ImmutableList(), attachments => {
|
||||
return attachments.map(normalizeAttachment);
|
||||
|
|
Ładowanie…
Reference in New Issue