mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {AppBskyGraphDefs, AtUri} from '@atproto/api'
2
3export function createStarterPackLinkFromAndroidReferrer(
4 referrerQueryString: string,
5): string | null {
6 try {
7 // The referrer string is just some URL parameters, so lets add them to a fake URL
8 const url = new URL('http://throwaway.com/?' + referrerQueryString)
9 const utmContent = url.searchParams.get('utm_content')
10 const utmSource = url.searchParams.get('utm_source')
11
12 if (!utmContent) return null
13 if (utmSource !== 'bluesky') return null
14
15 // This should be a string like `starterpack_haileyok.com_rkey`
16 const contentParts = utmContent.split('_')
17
18 if (contentParts[0] !== 'starterpack') return null
19 if (contentParts.length !== 3) return null
20
21 return `at://${contentParts[1]}/app.bsky.graph.starterpack/${contentParts[2]}`
22 } catch (e) {
23 return null
24 }
25}
26
27export function parseStarterPackUri(uri?: string): {
28 name: string
29 rkey: string
30} | null {
31 if (!uri) return null
32
33 try {
34 if (uri.startsWith('at://')) {
35 const atUri = new AtUri(uri)
36 if (atUri.collection !== 'app.bsky.graph.starterpack') return null
37 if (atUri.rkey) {
38 return {
39 name: atUri.hostname,
40 rkey: atUri.rkey,
41 }
42 }
43 return null
44 } else {
45 const url = new URL(uri)
46 const parts = url.pathname.split('/')
47 const [_, path, name, rkey] = parts
48
49 if (parts.length !== 4) return null
50 if (path !== 'starter-pack' && path !== 'start') return null
51 if (!name || !rkey) return null
52 return {
53 name,
54 rkey,
55 }
56 }
57 } catch (e) {
58 return null
59 }
60}
61
62export function createStarterPackGooglePlayUri(
63 name: string,
64 rkey: string,
65): string | null {
66 if (!name || !rkey) return null
67 return `https://play.google.com/store/apps/details?id=xyz.blueskyweb.app&referrer=utm_source%3Dbluesky%26utm_medium%3Dstarterpack%26utm_content%3Dstarterpack_${name}_${rkey}`
68}
69
70export function httpStarterPackUriToAtUri(httpUri?: string): string | null {
71 if (!httpUri) return null
72
73 const parsed = parseStarterPackUri(httpUri)
74 if (!parsed) return null
75
76 if (httpUri.startsWith('at://')) return httpUri
77
78 return `at://${parsed.name}/app.bsky.graph.starterpack/${parsed.rkey}`
79}
80
81export function getStarterPackOgCard(
82 didOrStarterPack: AppBskyGraphDefs.StarterPackView | string,
83 rkey?: string,
84) {
85 if (typeof didOrStarterPack === 'string') {
86 return `https://ogcard.cdn.bsky.app/start/${didOrStarterPack}/${rkey}`
87 } else {
88 const rkey = new AtUri(didOrStarterPack.uri).rkey
89 return `https://ogcard.cdn.bsky.app/start/${didOrStarterPack.creator.did}/${rkey}`
90 }
91}
92
93export function createStarterPackUri({
94 did,
95 rkey,
96}: {
97 did: string
98 rkey: string
99}): string {
100 return new AtUri(`at://${did}/app.bsky.graph.starterpack/${rkey}`).toString()
101}
102
103export function startUriToStarterPackUri(uri: string) {
104 return uri.replace('/start/', '/starter-pack/')
105}