Merge branch 'fetch-ua' into 'main'

Fetch link previews with facebookexternalhit instead of WhatsApp/2 by default, make it configurable

See merge request soapbox-pub/ditto!482
This commit is contained in:
Alex Gleason 2024-09-07 18:12:33 +00:00
commit 044708d49f
2 changed files with 9 additions and 1 deletions

View file

@ -258,6 +258,10 @@ class Conf {
'i',
);
}
/** User-Agent to use when fetching link previews. Pretend to be Facebook by default. */
static get fetchUserAgent(): string {
return Deno.env.get('DITTO_FETCH_USER_AGENT') ?? 'facebookexternalhit';
}
/** Path to the custom policy module. Must be an absolute path, https:, npm:, or jsr: URI. */
static get policy(): string {
return Deno.env.get('DITTO_POLICY') || new URL('../data/policy.ts', import.meta.url).pathname;

View file

@ -3,6 +3,7 @@ import Debug from '@soapbox/stickynotes/debug';
import DOMPurify from 'isomorphic-dompurify';
import { unfurl } from 'unfurl.js';
import { Conf } from '@/config.ts';
import { PreviewCard } from '@/entities/PreviewCard.ts';
import { Time } from '@/utils/time.ts';
import { fetchWorker } from '@/workers/fetch.ts';
@ -15,7 +16,10 @@ async function unfurlCard(url: string, signal: AbortSignal): Promise<PreviewCard
const result = await unfurl(url, {
fetch: (url) =>
fetchWorker(url, {
headers: { 'User-Agent': 'WhatsApp/2' },
headers: {
'Accept': 'text/html, application/xhtml+xml',
'User-Agent': Conf.fetchUserAgent,
},
signal,
}),
});