recursively fetch quotes
1import { Client, ok, simpleFetchHandler } from "@atcute/client"
2import type { AppBskyFeedDefs } from "@atcute/bluesky"
3
4const handler = simpleFetchHandler({ service: "https://public.api.bsky.app" })
5const rpc = new Client({ handler })
6
7declare const input: HTMLInputElement
8declare const form: HTMLFormElement
9
10function link(quote: AppBskyFeedDefs.PostView) {
11 const a = document.createElement("a")
12 a.href = quote.uri
13 .replace("at://", "https://bsky.app/profile/")
14 .replace("app.bsky.feed.post", "post")
15 a.textContent = `${quote.author.displayName} (@${quote.author.handle}), ${quote.likeCount} likes`
16 return a
17}
18
19let total = 0
20async function walk(li, uri, count = 0) {
21 const ul = document.createElement("ul")
22 li.append(ul)
23 for (let i = 0; i < count; i++) {
24 const placeholderLi = document.createElement("li")
25 placeholderLi.textContent = "Loading..."
26 ul.append(placeholderLi)
27 }
28 const quotes = await ok(
29 rpc.get("app.bsky.feed.getQuotes", {
30 params: { uri, limit: 100 },
31 }),
32 )
33 ul.innerHTML = ""
34 console.log((total += quotes.posts.length))
35 if (quotes.posts.length > 100)
36 console.log("OH NO IT'S", quotes.posts.length)
37 for (const quote of quotes.posts) {
38 const childLi = document.createElement("li")
39 ul.append(childLi)
40 childLi.append(link(quote))
41 if (quote.quoteCount) walk(childLi, quote.uri, quote.quoteCount)
42 }
43}
44
45form.addEventListener("submit", (e) => {
46 e.preventDefault()
47 const url = input.value
48 walk(
49 document.body,
50 url,
51 )
52})