-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4965900
commit 356ae78
Showing
2 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { listRecords } from "@/lib/atproto"; | ||
import { resolveIdentity } from "@/lib/atproto-server"; | ||
import { getHandle, getPds } from "@atproto/identity"; | ||
import { AtUri } from "@atproto/syntax"; | ||
|
||
export const revalidate = 60; | ||
|
||
const ORIGIN = "https://atproto-browser.vercel.app"; | ||
|
||
export async function GET(request: Request) { | ||
const uri = new AtUri(new URL(request.url).searchParams.get("u")!); | ||
if (!uri.origin || uri.rkey) { | ||
return new Response("Invalid collection URI", { status: 400 }); | ||
} | ||
const identityResult = await resolveIdentity(uri.host); | ||
if (!identityResult.success) { | ||
return new Response(identityResult.error, { status: 400 }); | ||
} | ||
|
||
const didDoc = identityResult.identity; | ||
const pds = getPds(didDoc); | ||
if (!pds) { | ||
return new Response(`No PDS found for ${didDoc.id}`, { status: 400 }); | ||
} | ||
const handle = getHandle(didDoc); | ||
if (!handle) { | ||
return new Response(`No handle found for ${didDoc.id}`, { status: 400 }); | ||
} | ||
|
||
const { records } = await listRecords(pds, didDoc.id, uri.collection); | ||
|
||
const rss = ` | ||
<rss version="2.0"> | ||
<channel> | ||
<title>@${handle}'s ${uri.collection} atproto records</title> | ||
<link>${ORIGIN}/collection-rss?u=${uri.toString()}</link> | ||
<description>Collection ${uri.collection} from ${uri.origin}</description> | ||
<language>en-gb</language> | ||
<ttl>60</ttl> | ||
${records | ||
.map((record) => | ||
` | ||
<item> | ||
<title>${cdata(`New ${uri.collection}: ${new AtUri(record.uri).rkey}`)}</title> | ||
<pubDate>${new Intl.DateTimeFormat("fr-CA", { | ||
year: "numeric", | ||
month: "2-digit", | ||
day: "2-digit", | ||
}).format(new Date())}</pubDate> | ||
<link>${ORIGIN}/collection-rss?u=${uri.toString()}</link> | ||
<guid isPermalink="false">${record.cid}</guid> | ||
</item> | ||
`.trim(), | ||
) | ||
.join("\n")} | ||
</channel> | ||
</rss> | ||
`.trim(); | ||
|
||
return new Response(rss, { | ||
headers: { | ||
"Content-Type": "application/xml", | ||
"Content-Length": String(new TextEncoder().encode(rss).length), | ||
}, | ||
}); | ||
} | ||
|
||
function cdata(s: string) { | ||
return `<![CDATA[${s}]]>`; | ||
} |