Better starterpack embed (#4659)

This commit is contained in:
Hailey 2024-06-26 17:24:33 -07:00 committed by GitHub
parent da4dfeb9cf
commit 878b0476dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 103 additions and 32 deletions

View file

@ -1,8 +1,10 @@
import {BskyAgent} from '@atproto/api'
import {isBskyAppUrl} from '../strings/url-helpers'
import {extractBskyMeta} from './bsky'
import {LINK_META_PROXY} from 'lib/constants'
import {getGiphyMetaUri} from 'lib/strings/embed-player'
import {parseStarterPackUri} from 'lib/strings/starter-pack'
import {isBskyAppUrl} from '../strings/url-helpers'
import {extractBskyMeta} from './bsky'
export enum LikelyType {
HTML,
@ -28,7 +30,7 @@ export async function getLinkMeta(
url: string,
timeout = 15e3,
): Promise<LinkMeta> {
if (isBskyAppUrl(url)) {
if (isBskyAppUrl(url) && !parseStarterPackUri(url)) {
return extractBskyMeta(agent, url)
}

View file

@ -0,0 +1,23 @@
import {logger} from '#/logger'
import {startUriToStarterPackUri} from 'lib/strings/starter-pack'
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 !== 200) {
return shortLink
}
return startUriToStarterPackUri(res.url)
} catch (e: unknown) {
logger.error('Failed to resolve short link', {safeMessage: e})
return null
} finally {
clearTimeout(to)
}
}

View file

@ -99,3 +99,7 @@ export function createStarterPackUri({
}): string | null {
return new AtUri(`at://${did}/app.bsky.graph.starterpack/${rkey}`).toString()
}
export function startUriToStarterPackUri(uri: string) {
return uri.replace('/start/', '/starter-pack/')
}

View file

@ -2,6 +2,7 @@ import {AtUri} from '@atproto/api'
import psl from 'psl'
import TLDs from 'tlds'
import {logger} from '#/logger'
import {BSKY_SERVICE} from 'lib/constants'
import {isInvalidHandle} from 'lib/strings/handles'
@ -285,3 +286,13 @@ export function createBskyAppAbsoluteUrl(path: string): string {
const sanitizedPath = path.replace(BSKY_APP_HOST, '').replace(/^\/+/, '')
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
}
}