My website, rebuilt yet again
1import { Client, ok, simpleFetchHandler } from "@atcute/client";
2import type { } from "@atcute/identity";
3
4import type {} from '@atcute/atproto';
5import { is } from "@atcute/lexicons";
6import { isBlob, isLegacyBlob } from "@atcute/lexicons/interfaces";
7import { AppBskyActorProfile } from "@atcute/bluesky";
8
9export const PDS_URL = "https://at.hayden.moe";
10export const AT_HANDLE = "hayden.moe";
11
12let pdsClient: Client | null = null;
13
14export const getPdsClient = (): Client => {
15 if (!pdsClient) {
16 pdsClient = new Client({
17 handler: simpleFetchHandler({
18 service: 'https://public.api.bsky.app',
19 }),
20 });
21 }
22
23 return pdsClient;
24};
25
26export const getAvatarBlob = async (handle: `${string}.${string}`): Promise<string | null> => {
27 const client = getPdsClient();
28 const didReq = await ok(client.get('com.atproto.identity.resolveHandle', { params: { handle } }));
29
30 const req = await ok(client.get('com.atproto.repo.getRecord', {
31 params: {
32 repo: didReq.did,
33 collection: 'app.bsky.actor.profile',
34 rkey: 'self',
35 },
36 }));
37
38 if (!is(AppBskyActorProfile.mainSchema, req.value)) {
39 throw new Error('Invalid profile record');
40 }
41
42 let avatarBlobRef: string | null = null;
43 if (isBlob(req.value.avatar)) {
44 avatarBlobRef = req.value.avatar.ref.$link;
45 } else if (isLegacyBlob(req.value.avatar)) {
46 avatarBlobRef = req.value.avatar.cid;
47 }
48
49 if (!avatarBlobRef) {
50 return null;
51 }
52
53 return getCdnUrl('avatar_thumbnail', didReq.did, avatarBlobRef);
54};
55
56export const getCdnUrl = (kind: string, did: string, blobRef: string): string => {
57 return `https://cdn.bsky.app/img/${kind}/plain/${did}/${blobRef}`;
58}