resolve short links
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
import {logger} from '#/logger'
|
||||||
|
|
||||||
|
export async function resolveShortLink(shortLink: string) {
|
||||||
|
const controller = new AbortController()
|
||||||
|
const to = setTimeout(() => controller.abort(), 2e3)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(shortLink, {
|
||||||
|
method: 'GET',
|
||||||
|
signal: controller.signal,
|
||||||
|
})
|
||||||
|
if (res.status !== 301) {
|
||||||
|
return res.url
|
||||||
|
}
|
||||||
|
return res.headers.get('Location')
|
||||||
|
} catch (e: unknown) {
|
||||||
|
logger.error('Failed to resolve short link', {safeMessage: e})
|
||||||
|
return null
|
||||||
|
} finally {
|
||||||
|
clearTimeout(to)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ import {AtUri} from '@atproto/api'
|
|||||||
import psl from 'psl'
|
import psl from 'psl'
|
||||||
import TLDs from 'tlds'
|
import TLDs from 'tlds'
|
||||||
|
|
||||||
|
import {logger} from '#/logger'
|
||||||
import {BSKY_SERVICE} from 'lib/constants'
|
import {BSKY_SERVICE} from 'lib/constants'
|
||||||
import {isInvalidHandle} from 'lib/strings/handles'
|
import {isInvalidHandle} from 'lib/strings/handles'
|
||||||
|
|
||||||
@@ -285,3 +286,13 @@ export function createBskyAppAbsoluteUrl(path: string): string {
|
|||||||
const sanitizedPath = path.replace(BSKY_APP_HOST, '').replace(/^\/+/, '')
|
const sanitizedPath = path.replace(BSKY_APP_HOST, '').replace(/^\/+/, '')
|
||||||
return `${BSKY_APP_HOST.replace(/\/$/, '')}/${sanitizedPath}`
|
return `${BSKY_APP_HOST.replace(/\/$/, '')}/${sanitizedPath}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isShortLink(url: string): boolean {
|
||||||
|
try {
|
||||||
|
const urlp = new URL(url)
|
||||||
|
return urlp.host === 'go.bsky.app'
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('Failed to parse possible short link', {safeMessage: e})
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
import {logger} from '#/logger'
|
|
||||||
|
|
||||||
export function useResolveShortLink() {
|
|
||||||
return async (shortLink: string) => {
|
|
||||||
const controller = new AbortController()
|
|
||||||
const to = setTimeout(() => controller.abort(), 2e3)
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(
|
|
||||||
shortLink,
|
|
||||||
{method: 'HEAD', redirect: 'manual'},
|
|
||||||
{signal: controller.signal},
|
|
||||||
)
|
|
||||||
if (res.status !== 301) {
|
|
||||||
return shortLink
|
|
||||||
}
|
|
||||||
return res.headers.get('Location')
|
|
||||||
} catch (e: unknown) {
|
|
||||||
logger.error('Failed to resolve short link', {safeMessage: e})
|
|
||||||
} finally {
|
|
||||||
clearTimeout(to)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,11 +12,13 @@ import {
|
|||||||
getPostAsQuote,
|
getPostAsQuote,
|
||||||
} from 'lib/link-meta/bsky'
|
} from 'lib/link-meta/bsky'
|
||||||
import {getLinkMeta} from 'lib/link-meta/link-meta'
|
import {getLinkMeta} from 'lib/link-meta/link-meta'
|
||||||
|
import {resolveShortLink} from 'lib/link-meta/resolve-short-link'
|
||||||
import {downloadAndResize} from 'lib/media/manip'
|
import {downloadAndResize} from 'lib/media/manip'
|
||||||
import {
|
import {
|
||||||
isBskyCustomFeedUrl,
|
isBskyCustomFeedUrl,
|
||||||
isBskyListUrl,
|
isBskyListUrl,
|
||||||
isBskyPostUrl,
|
isBskyPostUrl,
|
||||||
|
isShortLink,
|
||||||
} from 'lib/strings/url-helpers'
|
} from 'lib/strings/url-helpers'
|
||||||
import {ImageModel} from 'state/models/media/image'
|
import {ImageModel} from 'state/models/media/image'
|
||||||
import {ComposerOpts} from 'state/shell/composer'
|
import {ComposerOpts} from 'state/shell/composer'
|
||||||
@@ -94,6 +96,17 @@ export function useExternalLinkFetch({
|
|||||||
setExtLink(undefined)
|
setExtLink(undefined)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
} else if (isShortLink(extLink.uri)) {
|
||||||
|
if (isShortLink(extLink.uri)) {
|
||||||
|
resolveShortLink(extLink.uri).then(res => {
|
||||||
|
if (res && res !== extLink.uri) {
|
||||||
|
setExtLink({
|
||||||
|
uri: res,
|
||||||
|
isLoading: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
getLinkMeta(agent, extLink.uri).then(meta => {
|
getLinkMeta(agent, extLink.uri).then(meta => {
|
||||||
if (aborted) {
|
if (aborted) {
|
||||||
|
|||||||
Reference in New Issue
Block a user