Skip to content
File

Blob: src/worker/dav/discovery/principals.ts

typescript36 lines
1import type { SubjectRow } from "@/worker/db/d1/schema";
2import type { DiscoveryResource } from "@/worker/dav/discovery/resources";
3 
4export const PRINCIPALS_RESOURCE: DiscoveryResource = {
5 kind: "principal-collection",
6 protocol: "discovery",
7 href: "/principals/",
8 displayName: "principals",
9};
10 
11export function principalHref(subject: SubjectRow): string {
12 return `/principals/${subject.hostLabel}/`;
13}
14 
15export function isPrincipalDiscoveryPath(pathname: string): boolean {
16 return pathname === "/principals" || pathname.startsWith("/principals/");
17}
18 
19export function principalResource(pathname: string, subject: SubjectRow): DiscoveryResource | null {
20 if (pathname === "/principals" || pathname === "/principals/") return PRINCIPALS_RESOURCE;
21 if (pathname === "/principals/me" || pathname === "/principals/me/") {
22 return { kind: "principal", protocol: "discovery", href: "/principals/me/", displayName: subject.hostLabel };
23 }
24 if (pathname === `/principals/${subject.hostLabel}` || pathname === `/principals/${subject.hostLabel}/`) {
25 return { kind: "principal", protocol: "discovery", href: principalHref(subject), displayName: subject.hostLabel };
26 }
27 return null;
28}
29 
30export function principalChildResources(subject: SubjectRow): DiscoveryResource[] {
31 return [
32 { kind: "principal", protocol: "discovery", href: "/principals/me/", displayName: subject.hostLabel },
33 { kind: "principal", protocol: "discovery", href: principalHref(subject), displayName: subject.hostLabel },
34 ];
35}