Skip to content

Commit

Permalink
Merge branch 'atproto-accept-labelers' into takedown-labels
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Mar 12, 2024
2 parents 30a4c02 + aa1ce7c commit 512929b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/bsky/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"pino": "^8.15.0",
"pino-http": "^8.2.1",
"sharp": "^0.32.6",
"structured-headers": "^1.0.1",
"typed-emitter": "^2.1.0",
"uint8arrays": "3.0.0"
},
Expand Down
12 changes: 10 additions & 2 deletions packages/bsky/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
defaultLabelerHeader,
parseLabelerHeader,
} from './util'
import { httpLogger as log } from './logger'

export class AppContext {
constructor(
Expand Down Expand Up @@ -88,8 +89,15 @@ export class AppContext {

reqLabelers(req: express.Request): ParsedLabelers {
const val = req.header('atproto-accept-labelers')
if (!val) return defaultLabelerHeader(this.cfg.labelsFromIssuerDids)
return parseLabelerHeader(val)
let parsed: ParsedLabelers | null
try {
parsed = parseLabelerHeader(val)
} catch (err) {
parsed = null
log.info({ err, val }, 'failed to parse labeler header')
}
if (!parsed) return defaultLabelerHeader(this.cfg.labelsFromIssuerDids)
return parsed
}
}

Expand Down
22 changes: 13 additions & 9 deletions packages/bsky/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { parseList } from 'structured-headers'

export type ParsedLabelers = {
dids: string[]
redact: Set<string>
}

export const parseLabelerHeader = (header: string): ParsedLabelers => {
const labelers = header.split(',').map((part) => part.trim())
export const parseLabelerHeader = (
header: string | undefined,
): ParsedLabelers | null => {
if (!header) return null
const labelerDids = new Set<string>()
const redactDids = new Set<string>()
for (const labeler of labelers) {
if (labeler.length === 0) {
continue
const parsed = parseList(header)
for (const item of parsed) {
const did = item[0].toString()
if (!did) {
return null
}
const parts = labeler.split(';')
const did = parts[0].trim()
labelerDids.add(did)
const rest = parts.slice(1).map((part) => part.trim())
if (rest.includes('redact')) {
const redact = item[1].get('redact')?.valueOf()
if (redact === true) {
redactDids.add(did)
}
}
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 512929b

Please sign in to comment.