import type { SubjectRow } from "@/worker/db/d1/schema"; import type { DiscoveryResource } from "@/worker/dav/discovery/resources"; export const PRINCIPALS_RESOURCE: DiscoveryResource = { kind: "principal-collection", protocol: "discovery", href: "/principals/", displayName: "principals", }; export function principalHref(subject: SubjectRow): string { return `/principals/${subject.hostLabel}/`; } export function isPrincipalDiscoveryPath(pathname: string): boolean { return pathname === "/principals" || pathname.startsWith("/principals/"); } export function principalResource(pathname: string, subject: SubjectRow): DiscoveryResource | null { if (pathname === "/principals" || pathname === "/principals/") return PRINCIPALS_RESOURCE; if (pathname === "/principals/me" || pathname === "/principals/me/") { return { kind: "principal", protocol: "discovery", href: "/principals/me/", displayName: subject.hostLabel }; } if (pathname === `/principals/${subject.hostLabel}` || pathname === `/principals/${subject.hostLabel}/`) { return { kind: "principal", protocol: "discovery", href: principalHref(subject), displayName: subject.hostLabel }; } return null; } export function principalChildResources(subject: SubjectRow): DiscoveryResource[] { return [ { kind: "principal", protocol: "discovery", href: "/principals/me/", displayName: subject.hostLabel }, { kind: "principal", protocol: "discovery", href: principalHref(subject), displayName: subject.hostLabel }, ]; }