From 36fba20a01a438b35b938e49e0c8cb9e7465a8c9 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Mon, 21 Jul 2025 17:18:33 +0300 Subject: Follow redirects for soundcloud shortlinks (#8614) * follow redirects for soundcloud shortlinks * clear timeout in `finally` --- src/lib/link-meta/link-meta.ts | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/lib/link-meta/link-meta.ts b/src/lib/link-meta/link-meta.ts index ea9190b6e..b46609533 100644 --- a/src/lib/link-meta/link-meta.ts +++ b/src/lib/link-meta/link-meta.ts @@ -1,4 +1,4 @@ -import {BskyAgent} from '@atproto/api' +import {type BskyAgent} from '@atproto/api' import {LINK_META_PROXY} from '#/lib/constants' import {getGiphyMetaUri} from '#/lib/strings/embed-player' @@ -37,6 +37,7 @@ export async function getLinkMeta( } let urlp + let shouldFollowRedirect = false try { urlp = new URL(url) @@ -46,6 +47,9 @@ export async function getLinkMeta( url = giphyMetaUri urlp = new URL(url) } + // follow redirects for soundcloud shortlinks + // QUESTION - do we want to follow redirects in other cases? -sfn + shouldFollowRedirect = urlp.hostname === 'on.soundcloud.com' } catch (e) { return { error: 'Invalid URL', @@ -62,33 +66,35 @@ export async function getLinkMeta( return meta } - try { - const controller = new AbortController() - const to = setTimeout(() => controller.abort(), timeout || 5e3) + const controller = new AbortController() + const to = setTimeout(() => controller.abort(), timeout || 5e3) + try { const response = await fetch( - `${LINK_META_PROXY(agent.service.toString() || '')}${encodeURIComponent( + `${LINK_META_PROXY(agent.serviceUrl.toString() || '')}${encodeURIComponent( url, )}`, {signal: controller.signal}, ) const body = await response.json() - clearTimeout(to) - const {description, error, image, title} = body - - if (error !== '') { - throw new Error(error) + if (body.error !== '') { + throw new Error(body.error) } - meta.description = description - meta.image = image - meta.title = title + meta.description = body.description + meta.image = body.image + meta.title = body.title + if (shouldFollowRedirect) { + meta.url = body.url + } } catch (e) { // failed console.error(e) meta.error = e instanceof Error ? e.toString() : 'Failed to fetch link' + } finally { + clearTimeout(to) } return meta -- cgit 1.4.1