From 9afd43a42d2c4205eea2284e30e95503ffd428c2 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 10 Mar 2022 17:15:09 -0600 Subject: [PATCH] Normalize Attachment entities as Immutable.Record --- .../normalizers/__tests__/status-test.js | 22 +++++++------ app/soapbox/normalizers/status.ts | 16 ++++++++-- .../reducers/__tests__/statuses-test.js | 31 ++++++++++++------- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/app/soapbox/normalizers/__tests__/status-test.js b/app/soapbox/normalizers/__tests__/status-test.js index f3c836f23..578e71dc4 100644 --- a/app/soapbox/normalizers/__tests__/status-test.js +++ b/app/soapbox/normalizers/__tests__/status-test.js @@ -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', () => { diff --git a/app/soapbox/normalizers/status.ts b/app/soapbox/normalizers/status.ts index 727206265..ad447f8d2 100644 --- a/app/soapbox/normalizers/status.ts +++ b/app/soapbox/normalizers/status.ts @@ -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) => { @@ -78,10 +91,9 @@ const normalizeAttachment = (attachment: ImmutableMap) => { 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) => { diff --git a/app/soapbox/reducers/__tests__/statuses-test.js b/app/soapbox/reducers/__tests__/statuses-test.js index 3ea21ab64..130aa4ec6 100644 --- a/app/soapbox/reducers/__tests__/statuses-test.js +++ b/app/soapbox/reducers/__tests__/statuses-test.js @@ -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 { @@ -19,7 +23,7 @@ describe('statuses reducer', () => { const action = { type: STATUS_IMPORT, status }; 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', () => { @@ -52,42 +56,45 @@ describe('statuses reducer', () => { const state = reducer(undefined, { type: STATUS_IMPORT, status }); - 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, + }]; - 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 action = { type: STATUS_IMPORT, status }; 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', () => {