Skip to content

Commit 9a07faf

Browse files
ux fixes (#25)
1 parent 53d8f25 commit 9a07faf

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/controllers/moderation.controller.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,55 @@ const isAccountSubject = (subject: unknown): subject is { did: string } =>
538538
const isRecordSubject = (subject: unknown): subject is { uri: string; cid: string } =>
539539
typeof subject === 'object' && subject !== null && 'uri' in subject;
540540

541+
// PLC Directory response types
542+
interface PlcDirectoryResponse {
543+
id: string;
544+
alsoKnownAs?: string[];
545+
verificationMethod?: Array<{
546+
id: string;
547+
type: string;
548+
controller: string;
549+
publicKeyMultibase: string;
550+
}>;
551+
service?: Array<{
552+
id: string;
553+
type: string;
554+
serviceEndpoint: string;
555+
}>;
556+
}
557+
558+
/**
559+
* Fetches PDS endpoint from PLC directory for a given DID
560+
* Returns the PDS service endpoint URL or null if not found
561+
*/
562+
async function fetchPdsFromPlcDirectory(did: string): Promise<{ pdsEndpoint: string | null; error?: string }> {
563+
try {
564+
const response = await fetch(`https://plc.directory/${did}`);
565+
566+
if (response.status === 404) {
567+
return { pdsEndpoint: null, error: "DID not registered in PLC directory" };
568+
}
569+
570+
if (response.status === 410) {
571+
return { pdsEndpoint: null, error: "DID has been tombstoned (deleted)" };
572+
}
573+
574+
if (!response.ok) {
575+
return { pdsEndpoint: null, error: `PLC directory returned status ${response.status}` };
576+
}
577+
578+
const data = await response.json() as PlcDirectoryResponse;
579+
580+
// Find the atproto PDS service endpoint
581+
const pdsService = data.service?.find(s => s.id === "#atproto_pds" || s.type === "AtprotoPersonalDataServer");
582+
583+
return { pdsEndpoint: pdsService?.serviceEndpoint || null };
584+
} catch (error) {
585+
console.error(`Error fetching PLC directory for ${did}:`, error);
586+
return { pdsEndpoint: null, error: error instanceof Error ? error.message : "Unknown error" };
587+
}
588+
}
589+
541590
export const getEscalatedUsers = async (
542591
req: Request,
543592
res: Response,
@@ -751,11 +800,19 @@ export const getProfileModerationData = async (
751800
console.warn(`Failed to fetch profile for ${did}:`, profileError);
752801
}
753802

803+
// Fetch PDS endpoint from PLC directory
804+
let pdsInfo: { pdsEndpoint: string | null; error?: string } = { pdsEndpoint: null };
805+
if (did.startsWith("did:plc:")) {
806+
pdsInfo = await fetchPdsFromPlcDirectory(did);
807+
}
808+
754809
res.status(200).json({
755810
did,
756811
subjectStatus,
757812
recentEvents,
758813
profile,
814+
pdsEndpoint: pdsInfo.pdsEndpoint,
815+
...(pdsInfo.error && { pdsError: pdsInfo.error }),
759816
});
760817
} catch (error) {
761818
console.error("Error fetching profile moderation data:", error);

0 commit comments

Comments
 (0)