1import type { } from "@atcute/atproto"
2import type { } from "@atcute/bluesky"
3import { Client, CredentialManager } from "@atcute/client"
4import { isAtprotoDid } from "@atcute/identity";
5import { CompositeDidDocumentResolver, DocumentNotFoundError, FailedDocumentResolutionError, HandleResolutionError, ImproperDidError, PlcDidDocumentResolver, UnsupportedDidMethodError, WebDidDocumentResolver } from "@atcute/identity-resolver";
6import { tryCatch } from "./utils";
7
8type DID = `did:${string}:${string}`
9
10const pds = location.protocol === "https:" ? location.origin : "https://pds.finxol.io"
11
12export async function getUsers() {
13 const rpc = new Client({
14 handler: new CredentialManager({ service: pds }),
15 })
16
17 const r = await rpc.get("com.atproto.sync.listRepos", {
18 params: { limit: 100, cursor: "" }, // I don't have that many users so I'll implement the cursor later if needed
19 })
20
21 if (!r.ok) {
22 console.error("Failed to fetch users list:", r.data)
23 return []
24 }
25
26 return r.data.repos
27}
28
29export async function resolveAliases(did: DID) {
30 // DID document resolution
31 const docResolver = new CompositeDidDocumentResolver({
32 methods: {
33 plc: new PlcDidDocumentResolver(),
34 web: new WebDidDocumentResolver(),
35 },
36 });
37
38 if (!isAtprotoDid(did)) {
39 return
40 }
41
42 const doc = await tryCatch(docResolver.resolve(did));
43
44 if (!doc.success) {
45 if (doc.error instanceof DocumentNotFoundError) {
46 // did returned no document
47 console.error("Document not found");
48 }
49 if (doc.error instanceof UnsupportedDidMethodError) {
50 // resolver doesn't support did method (composite resolver)
51 console.error("Unsupported DID method");
52 }
53 if (doc.error instanceof ImproperDidError) {
54 // resolver considers did as invalid (atproto did:web)
55 console.error("Improper DID");
56 }
57 if (doc.error instanceof FailedDocumentResolutionError) {
58 // document resolution had thrown something unexpected (fetch error)
59 console.error("Failed document resolution");
60 }
61
62 if (doc.error instanceof HandleResolutionError) {
63 // the errors above extend this class, so you can do a catch-all.
64 console.error("Handle resolution error");
65 }
66 return
67 }
68
69 return doc.value.alsoKnownAs;
70}
71
72export async function getAvatar(did: DID) {
73 const rpc = new Client({
74 handler: new CredentialManager({ service: "https://public.api.bsky.app" }),
75 })
76
77 const res = await rpc.get("app.bsky.actor.getProfile", { params: { actor: did } });
78
79 if (res.ok) {
80 return res.data.avatar;
81 }
82 return undefined;
83}