Skip to content

Commit a3ae197

Browse files
authored
feat: adds tangled.org repos fork count (#243)
1 parent 804d738 commit a3ae197

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

app/composables/useRepoMeta.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ type RadicleProjectResponse = {
8686
issues?: { open: number; closed: number }
8787
}
8888

89+
/** microcosm's constellation API response for /links/all to get tangled.org stats */
90+
type ConstellationAllLinksResponse = {
91+
links: Record<
92+
string,
93+
Record<
94+
string,
95+
{
96+
records: number
97+
distinct_dids: number
98+
}
99+
>
100+
>
101+
}
102+
89103
type ProviderAdapter = {
90104
id: ProviderId
91105
parse(url: URL): RepoRef | null
@@ -568,15 +582,35 @@ const tangledAdapter: ProviderAdapter = {
568582
{ headers: { 'User-Agent': 'npmx', 'Accept': 'text/html' } },
569583
REPO_META_TTL,
570584
)
585+
// Extracts the at-uri used in atproto
586+
const atUriMatch = html.match(/data-star-subject-at="([^"]+)"/)
571587
// Extract star count from: hx-post="/star?subject=...&countHint=23"
572588
const starMatch = html.match(/countHint=(\d+)/)
573-
const stars = starMatch?.[1] ? parseInt(starMatch[1], 10) : 0
589+
//We'll set the stars from tangled's repo page and may override it with constellation if successful
590+
let stars = starMatch?.[1] ? parseInt(starMatch[1], 10) : 0
591+
let forks = 0
592+
const atUri = atUriMatch?.[1]
593+
594+
if (atUriMatch) {
595+
try {
596+
//Get counts of records that reference this repo in the atmosphere using constellation
597+
const allLinks = await cachedFetch<ConstellationAllLinksResponse>(
598+
`https://constellation.microcosm.blue/links/all?target=${atUri}`,
599+
{ headers: { 'User-Agent': 'npmx' } },
600+
REPO_META_TTL,
601+
)
602+
stars = allLinks.links['sh.tangled.feed.star']?.['.subject']?.distinct_dids ?? stars
603+
forks = allLinks.links['sh.tangled.repo']?.['.source']?.distinct_dids ?? stars
604+
} catch {
605+
//failing silently since this is just an enhancement to the information already showing
606+
}
607+
}
574608

575609
return {
576610
provider: 'tangled',
577611
url: links.repo,
578612
stars,
579-
forks: 0, // Tangled doesn't expose fork count
613+
forks,
580614
links,
581615
}
582616
} catch {

0 commit comments

Comments
 (0)