[READ ONLY MIRROR] Spark Social AppView Server
github.com/sprksocial/server
atproto
deno
hono
lexicon
1import { parseList } from "structured-headers";
2
3export type ParsedLabelers = {
4 dids: string[];
5 redact: Set<string>;
6};
7
8export const parseLabelerHeader = (
9 header: string | undefined,
10): ParsedLabelers | null => {
11 // An empty header is valid, so we shouldn't return null
12 // https://datatracker.ietf.org/doc/html/rfc7230#section-3.2
13 if (header === undefined) return null;
14 const labelerDids = new Set<string>();
15 const redactDids = new Set<string>();
16 const parsed = parseList(header);
17 for (const item of parsed) {
18 const did = item[0].toString();
19 if (!did) {
20 return null;
21 }
22 labelerDids.add(did);
23 const redact = item[1].get("redact")?.valueOf();
24 if (redact === true) {
25 redactDids.add(did);
26 }
27 }
28 return {
29 dids: [...labelerDids],
30 redact: redactDids,
31 };
32};
33
34export const defaultLabelerHeader = (dids: string[]): ParsedLabelers => {
35 return {
36 dids,
37 redact: new Set(dids),
38 };
39};
40
41export const formatLabelerHeader = (parsed: ParsedLabelers): string => {
42 const parts = parsed.dids.map((did) =>
43 parsed.redact.has(did) ? `${did};redact` : did
44 );
45 return parts.join(",");
46};