46 lines
1.8 KiB
JavaScript
46 lines
1.8 KiB
JavaScript
import {DidResolver} from "npm:@atproto/identity";
|
|
var didres = new DidResolver({});
|
|
var getpds = async did => (await didres.resolve(did)).service.find(s => s.id == "#atproto_pds")?.serviceEndpoint;
|
|
|
|
Deno.serve({port: 11735}, async (req, info) => {
|
|
console.log(info.remoteAddr.hostname, req.headers.get('x-forwarded-for'), req.url);
|
|
var url = new URL(req.url);
|
|
|
|
if (url.pathname == "/.well-known/did.json") {
|
|
return new Response(`{"@context":["https://www.w3.org/ns/did/v1"],"id":"did:web:${url.hostname}","service":[{"id":"#bsky_fg","type":"BskyFeedGenerator","serviceEndpoint":"https://${url.hostname}"}]}`, {
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Access-Control-Allow-Origin": "*"
|
|
}
|
|
});
|
|
}
|
|
|
|
if (!url.pathname.startsWith("/xrpc/app.bsky.feed.getFeedSkeleton")) {
|
|
return new Response(null, {status: 404});
|
|
}
|
|
|
|
var feed = url.searchParams.get("feed");
|
|
var limit = url.searchParams.get("limit") || "50";
|
|
var cursor = url.searchParams.get("cursor");
|
|
|
|
if (!feed) return new Response("missing feed url", {status: 400});
|
|
feed = decodeURIComponent(feed);
|
|
|
|
var did = feed.match(/^at:\/\/(did:(?:web|plc):[a-z0-9-_.]+)\/app\.bsky\.feed\.generator/)?.[1];
|
|
if (!did) return new Response("could not find did in feed uri", {status: 400});
|
|
did = decodeURIComponent(did);
|
|
|
|
var pds = await getpds(did);
|
|
var url2 = `${pds}/xrpc/com.atproto.repo.listRecords?collection=app.bsky.feed.like&repo=${did}&limit=${limit}`;
|
|
if (cursor) url2 += `&cursor=${cursor}`;
|
|
|
|
var res2 = await fetch(url2);
|
|
if (!res2.ok) return new Response(`upstream failure: ${res2.status} ${await res2.text()}`, {status: 500});
|
|
|
|
var json2 = await res2.json();
|
|
var json1 = {cursor: json2.cursor};
|
|
|
|
json1.feed = json2.records.map(r => ({post: r.value.subject.uri}));
|
|
|
|
return Response.json(json1);
|
|
}); |