diff --git a/app/soapbox/normalizers/card.ts b/app/soapbox/normalizers/card.ts index f222dc8f9..536d12c38 100644 --- a/app/soapbox/normalizers/card.ts +++ b/app/soapbox/normalizers/card.ts @@ -7,6 +7,8 @@ import punycode from 'punycode'; import { Record as ImmutableRecord, Map as ImmutableMap, fromJS } from 'immutable'; +import { mergeDefined } from 'soapbox/utils/normalizers'; + // https://docs.joinmastodon.org/entities/card/ export const CardRecord = ImmutableRecord({ author_name: '', @@ -41,27 +43,15 @@ const getHostname = (url: string): string => { }; /** Fall back to Pleroma's OG data */ -const normalizeWidth = (card: ImmutableMap) => { - const width = card.get('width') || card.getIn(['pleroma', 'opengraph', 'width']) || 0; - return card.set('width', width); -}; +const normalizePleromaOpengraph = (card: ImmutableMap) => { + const opengraph = ImmutableMap({ + width: card.getIn(['pleroma', 'opengraph', 'width']), + height: card.getIn(['pleroma', 'opengraph', 'height']), + html: card.getIn(['pleroma', 'opengraph', 'html']), + image: card.getIn(['pleroma', 'opengraph', 'thumbnail_url']), + }); -/** Fall back to Pleroma's OG data */ -const normalizeHeight = (card: ImmutableMap) => { - const height = card.get('height') || card.getIn(['pleroma', 'opengraph', 'height']) || 0; - return card.set('height', height); -}; - -/** Fall back to Pleroma's OG data */ -const normalizeHtml = (card: ImmutableMap) => { - const html = card.get('html') || card.getIn(['pleroma', 'opengraph', 'html']) || ''; - return card.set('html', html); -}; - -/** Fall back to Pleroma's OG data */ -const normalizeImage = (card: ImmutableMap) => { - const image = card.get('image') || card.getIn(['pleroma', 'opengraph', 'thumbnail_url']) || null; - return card.set('image', image); + return card.mergeWith(mergeDefined, opengraph); }; /** Set provider from URL if not found */ @@ -73,10 +63,7 @@ const normalizeProviderName = (card: ImmutableMap) => { export const normalizeCard = (card: Record) => { return CardRecord( ImmutableMap(fromJS(card)).withMutations(card => { - normalizeWidth(card); - normalizeHeight(card); - normalizeHtml(card); - normalizeImage(card); + normalizePleromaOpengraph(card); normalizeProviderName(card); }), ); diff --git a/app/soapbox/utils/normalizers.ts b/app/soapbox/utils/normalizers.ts index a74ca582d..6604f1caa 100644 --- a/app/soapbox/utils/normalizers.ts +++ b/app/soapbox/utils/normalizers.ts @@ -1,4 +1,4 @@ -// Use new value only if old value is undefined +/** Use new value only if old value is undefined */ export const mergeDefined = (oldVal: any, newVal: any) => oldVal === undefined ? newVal : oldVal; export const makeEmojiMap = (emojis: any) => emojis.reduce((obj: any, emoji: any) => {