Skip to content

Commit

Permalink
Add mod authorities header and move bsky labeler into it
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrazee committed Mar 7, 2024
1 parent 6b5091f commit 577ba7b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 220 deletions.
27 changes: 24 additions & 3 deletions packages/api/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
} from './types'
import { BSKY_LABELER_DID } from './const'

const MAX_MOD_AUTHORITIES = 3
const MAX_LABELERS = 10
const REFRESH_SESSION = 'com.atproto.server.refreshSession'

/**
Expand All @@ -30,7 +32,7 @@ export class AtpAgent {
service: URL
api: AtpServiceClient
session?: AtpSessionData
labelersHeader: string[] = [BSKY_LABELER_DID]
labelersHeader: string[] = []

/**
* The PDS URL, driven by the did doc. May be undefined.
Expand All @@ -50,11 +52,21 @@ export class AtpAgent {
*/
static fetch: AtpAgentFetchHandler | undefined = defaultFetchHandler

/**
* The moderation authorities to be used across all requests
*/
static modAuthoritiesHeader: string[] = [BSKY_LABELER_DID]

/**
* Configures the API globally.
*/
static configure(opts: AtpAgentGlobalOpts) {
AtpAgent.fetch = opts.fetch
if (opts.fetch) {
AtpAgent.fetch = opts.fetch
}
if (opts.modAuthorities) {
AtpAgent.modAuthoritiesHeader = opts.modAuthorities
}
}

constructor(opts: AtpAgentOpts) {
Expand Down Expand Up @@ -212,12 +224,21 @@ 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, 10)
.slice(0, MAX_LABELERS)
.join(','),
}
}
Expand Down
15 changes: 0 additions & 15 deletions packages/api/src/bsky-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
BskyInterestsPreference,
} from './types'
import { LabelPreference } from './moderation/types'
import { BSKY_LABELER_DID } from './const'
import { DEFAULT_LABEL_SETTINGS } from './moderation/const/labels'
import { sanitizeMutedWordValue } from './util'

Expand Down Expand Up @@ -419,17 +418,6 @@ export class BskyAgent extends AtpAgent {
}
}

// ensure the bluesky moderation is configured
const bskyModeration = prefs.moderationPrefs.mods.find(
(modPref) => modPref.did === BSKY_LABELER_DID,
)
if (!bskyModeration) {
prefs.moderationPrefs.mods.unshift({
did: BSKY_LABELER_DID,
labels: {},
})
}

// apply the label prefs
for (const pref of labelPrefs) {
if (pref.labelerDid) {
Expand Down Expand Up @@ -893,9 +881,6 @@ function prefsArrayToLabelerDids(
if (modsPref) {
dids = (modsPref as AppBskyActorDefs.ModsPref).mods.map((mod) => mod.did)
}
if (!dids.includes(BSKY_LABELER_DID)) {
dids.unshift(BSKY_LABELER_DID)
}
return dids
}

Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export type AtpAgentFetchHandler = (
* AtpAgent global config opts
*/
export interface AtpAgentGlobalOpts {
fetch: AtpAgentFetchHandler
fetch?: AtpAgentFetchHandler
modAuthorities?: string[]
}

/**
Expand Down
24 changes: 24 additions & 0 deletions packages/api/tests/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AtpAgentFetchHandlerResponse,
AtpSessionEvent,
AtpSessionData,
BSKY_LABELER_DID,
} from '..'
import { TestNetworkNoAppView } from '@atproto/dev-env'
import { getPdsEndpoint, isValidDidDoc } from '@atproto/common-web'
Expand Down Expand Up @@ -481,6 +482,29 @@ describe('agent', () => {
})
})

describe('Mod authorities header', () => {
it('adds the authorities 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)

AtpAgent.configure({ modAuthorities: ['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',
)
const res3 = await agent2.com.atproto.server.describeServer()
expect(res3.data['atproto-mod-authorities']).toEqual(
'did:plc:test1,did:plc:test2',
)

await new Promise((r) => server.close(r))
})
})

describe('configureLabelersHeader', () => {
it('adds the labelers header as expected', async () => {
const server = await createHeaderEchoServer(15991)
Expand Down
Loading

0 comments on commit 577ba7b

Please sign in to comment.