Prefer higher resolution link preview images

See [#5000][0].

[0]: https://github.com/signalapp/Signal-Desktop/pull/5000
This commit is contained in:
Sasha Chedygov 2021-04-02 13:32:45 -07:00 committed by GitHub
parent ea7a5449f1
commit e8fb835857
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 1 deletions

View file

@ -354,9 +354,10 @@ const parseMetadata = (
const rawImageHref =
getOpenGraphContent(document, ['og:image', 'og:image:url']) ||
getLinkHrefAttribute(document, [
'apple-touch-icon',
'apple-touch-icon-precomposed',
'shortcut icon',
'icon',
'apple-touch-icon',
]);
const imageUrl = rawImageHref ? maybeParseUrl(rawImageHref, href) : null;
const imageHref = imageUrl ? imageUrl.href : null;

View file

@ -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 () => {
const fakeFetch = stub().resolves(
makeResponse({