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
import { states } from "https://esm.town/v/jordan/states";
export default async function(req: Request): Promise<Response> {
const url = new URL(req.url);
const path = url.pathname;
const params = url.searchParams;
const endpoint = "https://jordan-feeds.web.val.run";
const did = "did:plc:27rjcwbur2bizjjx3zakeme5";
let feed_lis = "";
let uri_lis = "";
states.forEach(state => {
feed_lis += `<li><a href="https://bsky.app/profile/${did}/feed/${state}wx">${state}wx</a></li>`;
uri_lis +=
`<li><a href="/xrpc/app.bsky.feed.getFeedSkeleton?feed=at://${did}/app.bsky.feed.generator/${state}wx"><code>at://${did}/app.bsky.feed.generator/${state}wx</code></a></li>`;
});
if (path === "/") {
return new Response(
`<!DOCTYPE html><html><head><style>:root {color-scheme: light dark;}#feeds li {text-transform: uppercase;}</style><meta name="viewport" content="width=device-width, initial-scale=1" /></head>
<body><h1>feeds</h1><p>A list of all feeds containing the hashtag #StateWX.</p><ul id="feeds">${feed_lis}</ul>
<details><summary>Server info</summary><h2>Paths</h2><p>Static endpoints:</p><ul><li><a href="/.well-known/did.json"><code>/.well-known/did.json</code></a></li><li><a href="/xrpc/app.bsky.feed.describeFeedGenerator"><code>/xrpc/app.bsky.feed.describeFeedGe
{ headers: { "Content-Type": "text/html;charset=utf-8" } },
);
}
if (path === "/.well-known/did.json") {
return new Response(
`{"id":"did:web:${
new URL(endpoint).host
}","service":[{"id":"#bsky_fg","type":"BskyFeedGenerator","serviceEndpoint":"${endpoint}"}]}`,
{ headers: { "Content-Type": "application/json" } },
);
}
if (path === "/xrpc/app.bsky.feed.describeFeedGenerator") {
let feeds = [];
for (const state of states) {
feeds.push({ "uri": `at://${did}/app.bsky.feed.generator/${state}wx` });
}
return new Response(JSON.stringify({ "did": `did:web:${new URL(endpoint).host}`, "feeds": feeds }));
}
if (path === "/xrpc/app.bsky.feed.getFeedSkeleton") {
const feed = params.get("feed");
const state = feed.split("/")[feed.split("/").length - 1];
// gets all posts under #StateWX and returns them
const res = await fetch(
`https://public.api.bsky.app/xrpc/app.bsky.feed.searchPosts?q=%23${state}&limit=50&sort=latest`,
).then((res) => res.json());
let posts = [];
for (const post of res.posts) {
posts.push({ "post": post.uri });
}
return new Response(JSON.stringify({ "feed": posts }));
}
return Response.redirect(`${url.origin}`, 303);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 8, 2024