Break Attachment normalizer into its own module
This commit is contained in:
parent
6812e7bfd4
commit
5c8e8d9f99
3 changed files with 46 additions and 34 deletions
BIN
app/soapbox/normalizers/__tests__/attachment-test.js
Normal file
BIN
app/soapbox/normalizers/__tests__/attachment-test.js
Normal file
Binary file not shown.
45
app/soapbox/normalizers/attachment.ts
Normal file
45
app/soapbox/normalizers/attachment.ts
Normal 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));
|
||||||
|
};
|
|
@ -9,11 +9,11 @@ import {
|
||||||
Record as ImmutableRecord,
|
Record as ImmutableRecord,
|
||||||
} from 'immutable';
|
} from 'immutable';
|
||||||
|
|
||||||
|
import { normalizeAttachment } from 'soapbox/normalizers/attachment';
|
||||||
import { normalizeEmoji } from 'soapbox/normalizers/emoji';
|
import { normalizeEmoji } from 'soapbox/normalizers/emoji';
|
||||||
import { normalizeMention } from 'soapbox/normalizers/mention';
|
import { normalizeMention } from 'soapbox/normalizers/mention';
|
||||||
import { normalizePoll } from 'soapbox/normalizers/poll';
|
import { normalizePoll } from 'soapbox/normalizers/poll';
|
||||||
import { IStatus } from 'soapbox/types';
|
import { IStatus } from 'soapbox/types';
|
||||||
import { mergeDefined } from 'soapbox/utils/normalizers';
|
|
||||||
|
|
||||||
// https://docs.joinmastodon.org/entities/status/
|
// https://docs.joinmastodon.org/entities/status/
|
||||||
const StatusRecord = ImmutableRecord({
|
const StatusRecord = ImmutableRecord({
|
||||||
|
@ -55,39 +55,6 @@ const StatusRecord = ImmutableRecord({
|
||||||
spoilerHtml: '',
|
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>) => {
|
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);
|
||||||
|
|
Loading…
Reference in a new issue