Normalize Attachment entities as Immutable.Record
This commit is contained in:
@@ -61,42 +61,46 @@ describe('normalizeStatus', () => {
|
||||
it('normalizes Mitra attachments', () => {
|
||||
const status = fromJS(require('soapbox/__fixtures__/mitra-status-with-attachments.json'));
|
||||
|
||||
const expected = fromJS([{
|
||||
const expected = [{
|
||||
id: '017eeb0e-e5df-30a4-77a7-a929145cb836',
|
||||
type: 'image',
|
||||
url: 'https://mitra.social/media/8e04e6091bbbac79641b5812508683ce72c38693661c18d16040553f2371e18d.png',
|
||||
preview_url: 'https://mitra.social/media/8e04e6091bbbac79641b5812508683ce72c38693661c18d16040553f2371e18d.png',
|
||||
remote_url: 'https://mitra.social/media/8e04e6091bbbac79641b5812508683ce72c38693661c18d16040553f2371e18d.png',
|
||||
remote_url: null,
|
||||
}, {
|
||||
id: '017eeb0e-e5e4-2a48-2889-afdebf368a54',
|
||||
type: 'unknown',
|
||||
url: 'https://mitra.social/media/8f72dc2e98572eb4ba7c3a902bca5f69c448fc4391837e5f8f0d4556280440ac',
|
||||
preview_url: 'https://mitra.social/media/8f72dc2e98572eb4ba7c3a902bca5f69c448fc4391837e5f8f0d4556280440ac',
|
||||
remote_url: 'https://mitra.social/media/8f72dc2e98572eb4ba7c3a902bca5f69c448fc4391837e5f8f0d4556280440ac',
|
||||
remote_url: null,
|
||||
}, {
|
||||
id: '017eeb0e-e5e5-79fd-6054-8b6869b1db49',
|
||||
type: 'unknown',
|
||||
url: 'https://mitra.social/media/55a81a090247cc4fc127e5716bcf7964f6e0df9b584f85f4696c0b994747a4d0.oga',
|
||||
preview_url: 'https://mitra.social/media/55a81a090247cc4fc127e5716bcf7964f6e0df9b584f85f4696c0b994747a4d0.oga',
|
||||
remote_url: 'https://mitra.social/media/55a81a090247cc4fc127e5716bcf7964f6e0df9b584f85f4696c0b994747a4d0.oga',
|
||||
remote_url: null,
|
||||
}, {
|
||||
id: '017eeb0e-e5e6-c416-a444-21e560c47839',
|
||||
type: 'unknown',
|
||||
url: 'https://mitra.social/media/0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0',
|
||||
preview_url: 'https://mitra.social/media/0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0',
|
||||
remote_url: 'https://mitra.social/media/0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0',
|
||||
}]);
|
||||
remote_url: null,
|
||||
}];
|
||||
|
||||
const result = normalizeStatus(status);
|
||||
|
||||
expect(result.media_attachments).toEqual(expected);
|
||||
expect(result.media_attachments.toJS()).toMatchObject(expected);
|
||||
});
|
||||
|
||||
it('leaves Pleroma attachments alone', () => {
|
||||
const status = fromJS(require('soapbox/__fixtures__/pleroma-status-with-attachments.json'));
|
||||
const result = normalizeStatus(status);
|
||||
const result = normalizeStatus(status).media_attachments;
|
||||
|
||||
expect(status.get('media_attachments')).toEqual(result.media_attachments);
|
||||
expect(result.size).toBe(4);
|
||||
expect(result.get(0).text_url).toBe(undefined);
|
||||
expect(result.get(1).meta).toEqual(fromJS({}));
|
||||
expect(result.getIn([1, 'pleroma', 'mime_type'])).toBe('application/x-nes-rom');
|
||||
expect(ImmutableRecord.isRecord(result.get(3))).toBe(true);
|
||||
});
|
||||
|
||||
it('normalizes Pleroma quote post', () => {
|
||||
|
||||
@@ -66,6 +66,19 @@ const PollRecord = ImmutableRecord({
|
||||
voted: false,
|
||||
});
|
||||
|
||||
// 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: '',
|
||||
});
|
||||
|
||||
// Ensure attachments have required fields
|
||||
// https://docs.joinmastodon.org/entities/attachment/
|
||||
const normalizeAttachment = (attachment: ImmutableMap<string, any>) => {
|
||||
@@ -78,10 +91,9 @@ const normalizeAttachment = (attachment: ImmutableMap<string, any>) => {
|
||||
const base = ImmutableMap({
|
||||
url,
|
||||
preview_url: url,
|
||||
remote_url: url,
|
||||
});
|
||||
|
||||
return attachment.mergeWith(mergeDefined, base);
|
||||
return AttachmentRecord(attachment.mergeWith(mergeDefined, base));
|
||||
};
|
||||
|
||||
const normalizeAttachments = (status: ImmutableMap<string, any>) => {
|
||||
|
||||
Reference in New Issue
Block a user