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