///
import {
CompositeHandleResolver,
DohJsonHandleResolver,
WellKnownHandleResolver,
} from "@atcute/identity-resolver";
import { and, eq } from "drizzle-orm";
import { routes } from "../db/schema.ts";
import { type db, isDid, ROOT_DOMAIN } from "../utils.ts";
import ascii from "./ascii.txt" with { type: "text" };
import storeBlob from "../backfill/blob.ts";
const handleResolver = new CompositeHandleResolver({
strategy: "race",
methods: {
dns: new DohJsonHandleResolver({
dohUrl: "https://mozilla.cloudflare-dns.com/dns-query",
}),
http: new WellKnownHandleResolver(),
},
});
export default async function (
db: db,
req: Request,
user:
| { handle: `${string}.${string}` }
| { did: `did:plc:${string}` | `did:web:${string}` }
): Promise {
// if handle: resolve did
let did: `did:${"plc" | "web"}:${string}`;
if ("handle" in user) {
try {
// cast bc i know it will be `string.string`
did = await handleResolver.resolve(user.handle);
} catch {
return new Response(
`${ascii}
This handle could not be resolved.
`,
{
status: 500,
statusText: "Internal Server Error",
}
);
}
} else did = user.did;
// look up in db
const db_res =
(
await db
.select()
.from(routes)
.where(
and(
eq(routes.did, did),
eq(routes.url_route, new URL(req.url).pathname)
)
)
).at(0) ??
(
await db
.select()
.from(routes)
.where(and(eq(routes.did, did), eq(routes.url_route, "404")))
).at(0);
if (!db_res) {
return new Response(`${ascii}
404: The user has no atcities site or is missing a 404 page.
If you're the owner of this account, head to https://atcities.dev/ for more information.
The index of this account is at https://${
"handle" in user
? user.handle
: user.did.split(":").at(-1) + ".did-" + user.did.split(":").at(1)
}.${ROOT_DOMAIN}/
`);
}
try {
const file = await Deno.readFile(
`./blobs/${db_res.did}/${db_res.blob_cid}`
);
return new Response(file, {
headers: {
"Content-Type": db_res.mime,
},
});
} catch {
if (!isDid(db_res.did))
return new Response(`${ascii}
${db_res.did} is not a valid DID. This account could not be resolved
`);
try {
const blob = await storeBlob(db_res.did, db_res.blob_cid);
return new Response(blob, {
headers: {
"Content-Type": db_res.mime,
},
});
} catch (e) {
console.error(e);
return new Response(`${ascii}
This page could not be resolved. Either:
- The user has no PDS
- The blob does not exist
- The user has been manually hidden for uploading illegal content.
`);
}
}
}