Handle pressing all go.bsky.app links in-app w/ resolution (#4680)

This commit is contained in:
Hailey 2024-06-27 19:35:20 -07:00 committed by GitHub
parent 030c8e268e
commit 91c4aa7c2d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 186 additions and 17 deletions

View file

@ -1,5 +1,4 @@
import {logger} from '#/logger'
import {startUriToStarterPackUri} from 'lib/strings/starter-pack'
export async function resolveShortLink(shortLink: string) {
const controller = new AbortController()
@ -8,15 +7,20 @@ export async function resolveShortLink(shortLink: string) {
try {
const res = await fetch(shortLink, {
method: 'GET',
headers: {
Accept: 'application/json',
},
signal: controller.signal,
})
if (res.status !== 200) {
logger.error('Failed to resolve short link', {status: res.status})
return shortLink
}
return startUriToStarterPackUri(res.url)
const json = (await res.json()) as {url: string}
return json.url
} catch (e: unknown) {
logger.error('Failed to resolve short link', {safeMessage: e})
return null
return shortLink
} finally {
clearTimeout(to)
}

View file

@ -44,6 +44,7 @@ export type CommonNavigatorParams = {
Feeds: undefined
Start: {name: string; rkey: string}
StarterPack: {name: string; rkey: string; new?: boolean}
StarterPackShort: {code: string}
StarterPackWizard: undefined
StarterPackEdit: {
rkey?: string
@ -101,6 +102,7 @@ export type AllNavigatorParams = CommonNavigatorParams & {
Messages: {animation?: 'push' | 'pop'}
Start: {name: string; rkey: string}
StarterPack: {name: string; rkey: string; new?: boolean}
StarterPackShort: {code: string}
StarterPackWizard: undefined
StarterPackEdit: {
rkey?: string

View file

@ -167,6 +167,9 @@ export function convertBskyAppUrlIfNeeded(url: string): string {
} catch (e) {
console.error('Unexpected error in convertBskyAppUrlIfNeeded()', e)
}
} else if (isShortLink(url)) {
// We only want to do this on native, web handles the 301 for us
return shortLinkToHref(url)
}
return url
}
@ -288,11 +291,21 @@ export function createBskyAppAbsoluteUrl(path: string): string {
}
export function isShortLink(url: string): boolean {
return url.startsWith('https://go.bsky.app/')
}
export function shortLinkToHref(url: string): string {
try {
const urlp = new URL(url)
return urlp.host === 'go.bsky.app'
// For now we only support starter packs, but in the future we should add additional paths to this check
const parts = urlp.pathname.split('/').filter(Boolean)
if (parts.length === 1) {
return `/starter-pack-short/${parts[0]}`
}
return url
} catch (e) {
logger.error('Failed to parse possible short link', {safeMessage: e})
return false
return url
}
}