Card: improve normalization of Pleroma OG fields
This commit is contained in:
parent
53a54bcc96
commit
9d4b084764
2 changed files with 12 additions and 25 deletions
|
@ -7,6 +7,8 @@ import punycode from 'punycode';
|
||||||
|
|
||||||
import { Record as ImmutableRecord, Map as ImmutableMap, fromJS } from 'immutable';
|
import { Record as ImmutableRecord, Map as ImmutableMap, fromJS } from 'immutable';
|
||||||
|
|
||||||
|
import { mergeDefined } from 'soapbox/utils/normalizers';
|
||||||
|
|
||||||
// https://docs.joinmastodon.org/entities/card/
|
// https://docs.joinmastodon.org/entities/card/
|
||||||
export const CardRecord = ImmutableRecord({
|
export const CardRecord = ImmutableRecord({
|
||||||
author_name: '',
|
author_name: '',
|
||||||
|
@ -41,27 +43,15 @@ const getHostname = (url: string): string => {
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Fall back to Pleroma's OG data */
|
/** Fall back to Pleroma's OG data */
|
||||||
const normalizeWidth = (card: ImmutableMap<string, any>) => {
|
const normalizePleromaOpengraph = (card: ImmutableMap<string, any>) => {
|
||||||
const width = card.get('width') || card.getIn(['pleroma', 'opengraph', 'width']) || 0;
|
const opengraph = ImmutableMap({
|
||||||
return card.set('width', width);
|
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 */
|
return card.mergeWith(mergeDefined, opengraph);
|
||||||
const normalizeHeight = (card: ImmutableMap<string, any>) => {
|
|
||||||
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<string, any>) => {
|
|
||||||
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<string, any>) => {
|
|
||||||
const image = card.get('image') || card.getIn(['pleroma', 'opengraph', 'thumbnail_url']) || null;
|
|
||||||
return card.set('image', image);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Set provider from URL if not found */
|
/** Set provider from URL if not found */
|
||||||
|
@ -73,10 +63,7 @@ const normalizeProviderName = (card: ImmutableMap<string, any>) => {
|
||||||
export const normalizeCard = (card: Record<string, any>) => {
|
export const normalizeCard = (card: Record<string, any>) => {
|
||||||
return CardRecord(
|
return CardRecord(
|
||||||
ImmutableMap(fromJS(card)).withMutations(card => {
|
ImmutableMap(fromJS(card)).withMutations(card => {
|
||||||
normalizeWidth(card);
|
normalizePleromaOpengraph(card);
|
||||||
normalizeHeight(card);
|
|
||||||
normalizeHtml(card);
|
|
||||||
normalizeImage(card);
|
|
||||||
normalizeProviderName(card);
|
normalizeProviderName(card);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
|
@ -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 mergeDefined = (oldVal: any, newVal: any) => oldVal === undefined ? newVal : oldVal;
|
||||||
|
|
||||||
export const makeEmojiMap = (emojis: any) => emojis.reduce((obj: any, emoji: any) => {
|
export const makeEmojiMap = (emojis: any) => emojis.reduce((obj: any, emoji: any) => {
|
||||||
|
|
Loading…
Reference in a new issue