Skip to content

Commit 8386ad5

Browse files
committed
fix: getting tangled stats from our backend
1 parent 51168b1 commit 8386ad5

2 files changed

Lines changed: 69 additions & 30 deletions

File tree

app/composables/useRepoMeta.ts

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -566,43 +566,18 @@ const tangledAdapter: ProviderAdapter = {
566566
},
567567

568568
async fetchMeta(cachedFetch, ref, links, options = {}) {
569-
// Tangled doesn't have a public JSON API, but we can scrape the star count
570-
// from the HTML page (it's in the hx-post URL as countHint=N)
571569
try {
572-
const { data: html } = await cachedFetch<string>(
573-
`https://tangled.org/${ref.owner}/${ref.repo}`,
574-
{
575-
headers: { 'User-Agent': 'npmx', 'Accept': 'text/html', ...options.headers },
576-
...options,
577-
},
570+
const { data } = await cachedFetch<{ stars: number; forks: number }>(
571+
`/api/atproto/tangled-stats/${ref.owner}/${ref.repo}`,
572+
options,
578573
REPO_META_TTL,
579574
)
580-
// Extracts the at-uri used in atproto
581-
const atUriMatch = html.match(/data-star-subject-at="([^"]+)"/)
582-
// Extract star count from: hx-post="/star?subject=...&countHint=23"
583-
const starMatch = html.match(/countHint=(\d+)/)
584-
//We'll set the stars from tangled's repo page and may override it with constellation if successful
585-
let stars = starMatch?.[1] ? parseInt(starMatch[1], 10) : 0
586-
let forks = 0
587-
const atUri = atUriMatch?.[1]
588-
589-
if (atUri) {
590-
try {
591-
const constellation = new Constellation(cachedFetch)
592-
//Get counts of records that reference this repo in the atmosphere using constellation
593-
const { data: allLinks } = await constellation.getAllLinks(atUri)
594-
stars = allLinks.links['sh.tangled.feed.star']?.['.subject']?.distinct_dids ?? stars
595-
forks = allLinks.links['sh.tangled.repo']?.['.source']?.distinct_dids ?? stars
596-
} catch {
597-
//failing silently since this is just an enhancement to the information already showing
598-
}
599-
}
600575

601576
return {
602577
provider: 'tangled',
603578
url: links.repo,
604-
stars,
605-
forks,
579+
stars: data.stars,
580+
forks: data.forks,
606581
links,
607582
}
608583
} catch {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { CachedFetchFunction } from '#shared/utils/fetch-cache-config'
2+
3+
export default defineEventHandler(async event => {
4+
let owner = getRouterParam(event, 'owner')
5+
let repo = getRouterParam(event, 'repo')
6+
7+
let cachedFetch: CachedFetchFunction
8+
if (event.context.cachedFetch) {
9+
cachedFetch = event.context.cachedFetch
10+
} else {
11+
// Fallback: return a function that uses regular $fetch
12+
// (shouldn't happen in normal operation)
13+
cachedFetch = async <T = unknown>(
14+
url: string,
15+
options: Parameters<typeof $fetch>[1] = {},
16+
_ttl?: number,
17+
): Promise<CachedFetchResult<T>> => {
18+
const data = (await $fetch<T>(url, options)) as T
19+
return { data, isStale: false, cachedAt: null }
20+
}
21+
}
22+
23+
try {
24+
// Tangled doesn't have a public JSON API, but we can scrape the star count
25+
// from the HTML page (it's in the hx-post URL as countHint=N)
26+
const { data: html } = await cachedFetch<string>(
27+
`https://tangled.org/${owner}/${repo}`,
28+
{
29+
headers: { 'User-Agent': 'npmx', 'Accept': 'text/html' },
30+
},
31+
60 * 10,
32+
)
33+
// Extracts the at-uri used in atproto
34+
const atUriMatch = html.match(/data-star-subject-at="([^"]+)"/)
35+
// Extract star count from: hx-post="/star?subject=...&countHint=23"
36+
const starMatch = html.match(/countHint=(\d+)/)
37+
//We'll set the stars from tangled's repo page and may override it with constellation if successful
38+
let stars = starMatch?.[1] ? parseInt(starMatch[1], 10) : 0
39+
let forks = 0
40+
const atUri = atUriMatch?.[1]
41+
42+
if (atUri) {
43+
try {
44+
const constellation = new Constellation(cachedFetch)
45+
//Get counts of records that reference this repo in the atmosphere using constellation
46+
const { data: allLinks } = await constellation.getAllLinks(atUri)
47+
stars = allLinks.links['sh.tangled.feed.star']?.['.subject']?.distinct_dids ?? stars
48+
forks = allLinks.links['sh.tangled.repo']?.['.source']?.distinct_dids ?? stars
49+
} catch {
50+
//failing silently since this is just an enhancement to the information already showing
51+
}
52+
}
53+
54+
return {
55+
stars,
56+
forks,
57+
}
58+
} catch {
59+
return {
60+
stars: 0,
61+
forks: 0,
62+
}
63+
}
64+
})

0 commit comments

Comments
 (0)