diff options
author | Samuel Newman <mozzius@protonmail.com> | 2025-07-21 17:18:33 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-21 09:18:33 -0500 |
commit | 36fba20a01a438b35b938e49e0c8cb9e7465a8c9 (patch) | |
tree | db49f6dc2f5b52e47ffdb55c3095c6b9b106ca42 /src/lib | |
parent | ad16ed3096dcb5d8179a38c1a7f1e953ce0971de (diff) | |
download | voidsky-36fba20a01a438b35b938e49e0c8cb9e7465a8c9.tar.zst |
Follow redirects for soundcloud shortlinks (#8614)
* follow redirects for soundcloud shortlinks * clear timeout in `finally`
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/link-meta/link-meta.ts | 32 |
1 files changed, 19 insertions, 13 deletions
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 |