about summary refs log tree commit diff
path: root/src/lib/strings/starter-pack.ts
blob: 01b5a6587055c3401c0965e9e38282fea715e1a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {AppBskyGraphDefs, AtUri} from '@atproto/api'

export function createStarterPackLinkFromAndroidReferrer(
  referrerQueryString: string,
): string | null {
  try {
    // The referrer string is just some URL parameters, so lets add them to a fake URL
    const url = new URL('http://throwaway.com/?' + referrerQueryString)
    const utmContent = url.searchParams.get('utm_content')
    const utmSource = url.searchParams.get('utm_source')

    if (!utmContent) return null
    if (utmSource !== 'bluesky') return null

    // This should be a string like `starterpack_haileyok.com_rkey`
    const contentParts = utmContent.split('_')

    if (contentParts[0] !== 'starterpack') return null
    if (contentParts.length !== 3) return null

    return `at://${contentParts[1]}/app.bsky.graph.starterpack/${contentParts[2]}`
  } catch (e) {
    return null
  }
}

export function parseStarterPackUri(uri?: string): {
  name: string
  rkey: string
} | null {
  if (!uri) return null

  try {
    if (uri.startsWith('at://')) {
      const atUri = new AtUri(uri)
      if (atUri.collection !== 'app.bsky.graph.starterpack') return null
      if (atUri.rkey) {
        return {
          name: atUri.hostname,
          rkey: atUri.rkey,
        }
      }
      return null
    } else {
      const url = new URL(uri)
      const parts = url.pathname.split('/')
      const [_, path, name, rkey] = parts

      if (parts.length !== 4) return null
      if (path !== 'starter-pack' && path !== 'start') return null
      if (!name || !rkey) return null
      return {
        name,
        rkey,
      }
    }
  } catch (e) {
    return null
  }
}

export function createStarterPackGooglePlayUri(
  name: string,
  rkey: string,
): string | null {
  if (!name || !rkey) return null
  return `https://play.google.com/store/apps/details?id=xyz.blueskyweb.app&referrer=utm_source%3Dbluesky%26utm_medium%3Dstarterpack%26utm_content%3Dstarterpack_${name}_${rkey}`
}

export function httpStarterPackUriToAtUri(httpUri?: string): string | null {
  if (!httpUri) return null

  const parsed = parseStarterPackUri(httpUri)
  if (!parsed) return null

  if (httpUri.startsWith('at://')) return httpUri

  return `at://${parsed.name}/app.bsky.graph.starterpack/${parsed.rkey}`
}

export function getStarterPackOgCard(
  didOrStarterPack: AppBskyGraphDefs.StarterPackView | string,
  rkey?: string,
) {
  if (typeof didOrStarterPack === 'string') {
    return `https://ogcard.cdn.bsky.app/start/${didOrStarterPack}/${rkey}`
  } else {
    const rkey = new AtUri(didOrStarterPack.uri).rkey
    return `https://ogcard.cdn.bsky.app/start/${didOrStarterPack.creator.did}/${rkey}`
  }
}

export function createStarterPackUri({
  did,
  rkey,
}: {
  did: string
  rkey: string
}): string | null {
  return new AtUri(`at://${did}/app.bsky.graph.starterpack/${rkey}`).toString()
}

export function startUriToStarterPackUri(uri: string) {
  return uri.replace('/start/', '/starter-pack/')
}