Break Attachment normalizer into its own module

This commit is contained in:
Alex Gleason 2022-03-12 15:33:53 -06:00
parent 6812e7bfd4
commit 5c8e8d9f99
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 46 additions and 34 deletions

Binary file not shown.

View file

@ -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));
};

View file

@ -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);