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