From a19b7fbe9efe56b8d9f45bba371c7383b751987f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 7 Sep 2024 13:07:47 -0500 Subject: [PATCH] Fetch link previews with facebookexternalhit instead of WhatsApp/2 by default, make it configurable --- src/config.ts | 4 ++++ src/utils/unfurl.ts | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/config.ts b/src/config.ts index 0cb71509..7954e744 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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; diff --git a/src/utils/unfurl.ts b/src/utils/unfurl.ts index a0ab1d7b..8123c423 100644 --- a/src/utils/unfurl.ts +++ b/src/utils/unfurl.ts @@ -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 fetchWorker(url, { - headers: { 'User-Agent': 'WhatsApp/2' }, + headers: { + 'Accept': 'text/html, application/xhtml+xml', + 'User-Agent': Conf.fetchUserAgent, + }, signal, }), });