Prefer higher resolution link preview images
See [#5000][0]. [0]: https://github.com/signalapp/Signal-Desktop/pull/5000
This commit is contained in:
parent
ea7a5449f1
commit
e8fb835857
2 changed files with 64 additions and 1 deletions
|
@ -354,9 +354,10 @@ const parseMetadata = (
|
||||||
const rawImageHref =
|
const rawImageHref =
|
||||||
getOpenGraphContent(document, ['og:image', 'og:image:url']) ||
|
getOpenGraphContent(document, ['og:image', 'og:image:url']) ||
|
||||||
getLinkHrefAttribute(document, [
|
getLinkHrefAttribute(document, [
|
||||||
|
'apple-touch-icon',
|
||||||
|
'apple-touch-icon-precomposed',
|
||||||
'shortcut icon',
|
'shortcut icon',
|
||||||
'icon',
|
'icon',
|
||||||
'apple-touch-icon',
|
|
||||||
]);
|
]);
|
||||||
const imageUrl = rawImageHref ? maybeParseUrl(rawImageHref, href) : null;
|
const imageUrl = rawImageHref ? maybeParseUrl(rawImageHref, href) : null;
|
||||||
const imageHref = imageUrl ? imageUrl.href : null;
|
const imageHref = imageUrl ? imageUrl.href : null;
|
||||||
|
|
|
@ -122,6 +122,68 @@ describe('link preview fetching', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('handles image href sources in the correct order', async () => {
|
||||||
|
const orderedImageHrefSources = [
|
||||||
|
{
|
||||||
|
tag:
|
||||||
|
'<meta property="og:image" content="https://example.com/og-image.jpg">',
|
||||||
|
expectedHref: 'https://example.com/og-image.jpg',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tag:
|
||||||
|
'<meta property="og:image:url" content="https://example.com/og-image-url.jpg">',
|
||||||
|
expectedHref: 'https://example.com/og-image-url.jpg',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tag:
|
||||||
|
'<link rel="apple-touch-icon" href="https://example.com/apple-touch-icon.jpg">',
|
||||||
|
expectedHref: 'https://example.com/apple-touch-icon.jpg',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tag:
|
||||||
|
'<link rel="apple-touch-icon-precomposed" href="https://example.com/apple-touch-icon-precomposed.jpg">',
|
||||||
|
expectedHref: 'https://example.com/apple-touch-icon-precomposed.jpg',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tag:
|
||||||
|
'<link rel="shortcut icon" href="https://example.com/shortcut-icon.jpg">',
|
||||||
|
expectedHref: 'https://example.com/shortcut-icon.jpg',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tag: '<link rel="icon" href="https://example.com/icon.jpg">',
|
||||||
|
expectedHref: 'https://example.com/icon.jpg',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
for (let i = orderedImageHrefSources.length - 1; i >= 0; i -= 1) {
|
||||||
|
const imageTags = orderedImageHrefSources
|
||||||
|
.slice(i)
|
||||||
|
.map(({ tag }) => tag)
|
||||||
|
// Reverse the array to make sure that we're prioritizing properly,
|
||||||
|
// instead of just using whichever comes first.
|
||||||
|
.reverse();
|
||||||
|
const fakeFetch = stub().resolves(
|
||||||
|
makeResponse({
|
||||||
|
body: makeHtml([
|
||||||
|
'<meta property="og:title" content="test title">',
|
||||||
|
...imageTags,
|
||||||
|
]),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-await-in-loop
|
||||||
|
const val = await fetchLinkPreviewMetadata(
|
||||||
|
fakeFetch,
|
||||||
|
'https://example.com',
|
||||||
|
new AbortController().signal
|
||||||
|
);
|
||||||
|
assert.propertyVal(
|
||||||
|
val,
|
||||||
|
'imageHref',
|
||||||
|
orderedImageHrefSources[i].expectedHref
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it('logs no warnings if everything goes smoothly', async () => {
|
it('logs no warnings if everything goes smoothly', async () => {
|
||||||
const fakeFetch = stub().resolves(
|
const fakeFetch = stub().resolves(
|
||||||
makeResponse({
|
makeResponse({
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue