Skip to content

Commit

Permalink
Update to new atproto-accept-labelers header behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrazee committed Mar 12, 2024
1 parent a09fa92 commit b28bb39
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
32 changes: 11 additions & 21 deletions packages/api/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export class AtpAgent {
static fetch: AtpAgentFetchHandler | undefined = defaultFetchHandler

/**
* The moderation authorities to be used across all requests
* The labelers to be used across all requests with the takedown capability
*/
static modAuthoritiesHeader: string[] = [BSKY_LABELER_DID]
static appLabelers: string[] = [BSKY_LABELER_DID]

/**
* Configures the API globally.
Expand All @@ -64,8 +64,8 @@ export class AtpAgent {
if (opts.fetch) {
AtpAgent.fetch = opts.fetch
}
if (opts.modAuthorities) {
AtpAgent.modAuthoritiesHeader = opts.modAuthorities
if (opts.appLabelers) {
AtpAgent.appLabelers = opts.appLabelers
}
}

Expand Down Expand Up @@ -224,23 +224,13 @@ export class AtpAgent {
authorization: `Bearer ${this.session.accessJwt}`,
}
}
if (AtpAgent.modAuthoritiesHeader.length) {
reqHeaders = {
...reqHeaders,
'atproto-mod-authorities': AtpAgent.modAuthoritiesHeader
.filter((str) => str.startsWith('did:'))
.slice(0, MAX_MOD_AUTHORITIES)
.join(','),
}
}
if (this.labelersHeader.length) {
reqHeaders = {
...reqHeaders,
'atproto-labelers': this.labelersHeader
.filter((str) => str.startsWith('did:'))
.slice(0, MAX_LABELERS)
.join(','),
}
reqHeaders = {
...reqHeaders,
'atproto-accept-labelers': AtpAgent.appLabelers
.map((str) => `${str};redact`)
.concat(this.labelersHeader.filter((str) => str.startsWith('did:')))
.slice(0, MAX_LABELERS)
.join(', '),
}
return reqHeaders
}
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export type AtpAgentFetchHandler = (
*/
export interface AtpAgentGlobalOpts {
fetch?: AtpAgentFetchHandler
modAuthorities?: string[]
appLabelers?: string[]
}

/**
Expand Down
27 changes: 16 additions & 11 deletions packages/api/tests/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,24 +482,27 @@ describe('agent', () => {
})
})

describe('Mod authorities header', () => {
it('adds the authorities header as expected', async () => {
describe('App labelers header', () => {
it('adds the labelers header as expected', async () => {
const server = await createHeaderEchoServer(15991)
const agent = new AtpAgent({ service: 'http://localhost:15991' })
const agent2 = new AtpAgent({ service: 'http://localhost:15991' })

const res1 = await agent.com.atproto.server.describeServer()
expect(res1.data['atproto-mod-authorities']).toEqual(BSKY_LABELER_DID)
expect(res1.data['atproto-accept-labelers']).toEqual(
`${BSKY_LABELER_DID};redact`,
)

AtpAgent.configure({ modAuthorities: ['did:plc:test1', 'did:plc:test2'] })
AtpAgent.configure({ appLabelers: ['did:plc:test1', 'did:plc:test2'] })
const res2 = await agent.com.atproto.server.describeServer()
expect(res2.data['atproto-mod-authorities']).toEqual(
'did:plc:test1,did:plc:test2',
expect(res2.data['atproto-accept-labelers']).toEqual(
'did:plc:test1;redact, did:plc:test2;redact',
)
const res3 = await agent2.com.atproto.server.describeServer()
expect(res3.data['atproto-mod-authorities']).toEqual(
'did:plc:test1,did:plc:test2',
expect(res3.data['atproto-accept-labelers']).toEqual(
'did:plc:test1;redact, did:plc:test2;redact',
)
AtpAgent.configure({ appLabelers: [BSKY_LABELER_DID] })

await new Promise((r) => server.close(r))
})
Expand All @@ -512,12 +515,14 @@ describe('agent', () => {

agent.configureLabelersHeader(['did:plc:test1'])
const res1 = await agent.com.atproto.server.describeServer()
expect(res1.data['atproto-labelers']).toEqual('did:plc:test1')
expect(res1.data['atproto-accept-labelers']).toEqual(
`${BSKY_LABELER_DID};redact, did:plc:test1`,
)

agent.configureLabelersHeader(['did:plc:test1', 'did:plc:test2'])
const res2 = await agent.com.atproto.server.describeServer()
expect(res2.data['atproto-labelers']).toEqual(
'did:plc:test1,did:plc:test2',
expect(res2.data['atproto-accept-labelers']).toEqual(
`${BSKY_LABELER_DID};redact, did:plc:test1, did:plc:test2`,
)

await new Promise((r) => server.close(r))
Expand Down

0 comments on commit b28bb39

Please sign in to comment.