@atproto/ozone 0.1.15 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/util.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.retryableHttp = exports.retryHttp = exports.getSigningKeyId = void 0;
3
+ exports.formatLabelerHeader = exports.defaultLabelerHeader = exports.parseLabelerHeader = exports.LABELER_HEADER_NAME = exports.retryableHttp = exports.retryHttp = exports.getSigningKeyId = void 0;
4
4
  const axios_1 = require("axios");
5
+ const structured_headers_1 = require("structured-headers");
5
6
  const xrpc_1 = require("@atproto/xrpc");
6
7
  const common_1 = require("@atproto/common");
7
8
  const getSigningKeyId = async (db, signingKey) => {
@@ -42,4 +43,43 @@ exports.retryableHttp = retryableHttp;
42
43
  const retryableHttpStatusCodes = new Set([
43
44
  408, 425, 429, 500, 502, 503, 504, 522, 524,
44
45
  ]);
46
+ exports.LABELER_HEADER_NAME = 'atproto-accept-labelers';
47
+ const parseLabelerHeader = (header, ignoreDid) => {
48
+ if (!header)
49
+ return null;
50
+ const labelerDids = new Set();
51
+ const redactDids = new Set();
52
+ const parsed = (0, structured_headers_1.parseList)(header);
53
+ for (const item of parsed) {
54
+ const did = item[0].toString();
55
+ if (!did) {
56
+ return null;
57
+ }
58
+ if (did === ignoreDid) {
59
+ continue;
60
+ }
61
+ labelerDids.add(did);
62
+ const redact = item[1].get('redact')?.valueOf();
63
+ if (redact === true) {
64
+ redactDids.add(did);
65
+ }
66
+ }
67
+ return {
68
+ dids: [...labelerDids],
69
+ redact: redactDids,
70
+ };
71
+ };
72
+ exports.parseLabelerHeader = parseLabelerHeader;
73
+ const defaultLabelerHeader = (dids) => {
74
+ return {
75
+ dids,
76
+ redact: new Set(dids),
77
+ };
78
+ };
79
+ exports.defaultLabelerHeader = defaultLabelerHeader;
80
+ const formatLabelerHeader = (parsed) => {
81
+ const parts = parsed.dids.map((did) => parsed.redact.has(did) ? `${did};redact` : did);
82
+ return parts.join(',');
83
+ };
84
+ exports.formatLabelerHeader = formatLabelerHeader;
45
85
  //# sourceMappingURL=util.js.map
package/dist/util.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;;AAAA,iCAAkC;AAClC,wCAAuD;AACvD,4CAAqD;AAG9C,MAAM,eAAe,GAAG,KAAK,EAClC,EAAY,EACZ,UAAkB,EACD,EAAE;IACnB,MAAM,SAAS,GAAG,MAAM,EAAE,CAAC,EAAE;SAC1B,UAAU,CAAC,aAAa,CAAC;SACzB,SAAS,EAAE;SACX,KAAK,CAAC,KAAK,EAAE,GAAG,EAAE,UAAU,CAAC;SAC7B,gBAAgB,EAAE,CAAA;IACrB,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,SAAS,CAAC,EAAE,CAAA;IACrB,CAAC;IACD,MAAM,SAAS,GAAG,MAAM,EAAE,CAAC,EAAE;SAC1B,UAAU,CAAC,aAAa,CAAC;SACzB,MAAM,CAAC,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC;SAC3B,YAAY,EAAE;SACd,uBAAuB,EAAE,CAAA;IAC5B,OAAO,SAAS,CAAC,EAAE,CAAA;AACrB,CAAC,CAAA;AAlBY,QAAA,eAAe,mBAkB3B;AAEM,KAAK,UAAU,SAAS,CAC7B,EAAoB,EACpB,OAAqB,EAAE;IAEvB,OAAO,IAAA,cAAK,EAAC,EAAE,EAAE,EAAE,SAAS,EAAE,aAAa,EAAE,GAAG,IAAI,EAAE,CAAC,CAAA;AACzD,CAAC;AALD,8BAKC;AAED,SAAgB,aAAa,CAAC,GAAY;IACxC,IAAI,GAAG,YAAY,gBAAS,EAAE,CAAC;QAC7B,IAAI,GAAG,CAAC,MAAM,KAAK,mBAAY,CAAC,OAAO;YAAE,OAAO,IAAI,CAAA;QACpD,OAAO,wBAAwB,CAAC,GAAG,CAAC,GAAG,CAA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CAAC;YACT,OAAO,IAAI,CAAA;QACb,CAAC;QACD,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACtB,SAAQ;QACV,CAAC;QACD,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QACpB,MAAM,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,CAAA;QAC/C,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;YACpB,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QACrB,CAAC;IACH,CAAC;IACD,OAAO;QACL,IAAI,EAAE,CAAC,GAAG,WAAW,CAAC;QACtB,MAAM,EAAE,UAAU;KACnB,CAAA;AACH,CAAC,CAAA;AA1BY,QAAA,kBAAkB,sBA0B9B;AAEM,MAAM,oBAAoB,GAAG,CAAC,IAAc,EAAkB,EAAE;IACrE,OAAO;QACL,IAAI;QACJ,MAAM,EAAE,IAAI,GAAG,CAAC,IAAI,CAAC;KACtB,CAAA;AACH,CAAC,CAAA;AALY,QAAA,oBAAoB,wBAKhC;AAEM,MAAM,mBAAmB,GAAG,CAAC,MAAsB,EAAU,EAAE;IACpE,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CACpC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,CAC/C,CAAA;IACD,OAAO,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AACxB,CAAC,CAAA;AALY,QAAA,mBAAmB,uBAK/B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/ozone",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "license": "MIT",
5
5
  "description": "Backend service for moderating the Bluesky network.",
6
6
  "keywords": [
@@ -29,9 +29,10 @@
29
29
  "p-queue": "^6.6.2",
30
30
  "pg": "^8.10.0",
31
31
  "pino-http": "^8.2.1",
32
+ "structured-headers": "^1.0.1",
32
33
  "typed-emitter": "^2.1.0",
33
34
  "uint8arrays": "3.0.0",
34
- "@atproto/api": "^0.12.11",
35
+ "@atproto/api": "^0.12.12",
35
36
  "@atproto/common": "^0.4.0",
36
37
  "@atproto/crypto": "^0.4.0",
37
38
  "@atproto/identity": "^0.4.0",
@@ -51,7 +52,7 @@
51
52
  "jest": "^28.1.2",
52
53
  "ts-node": "^10.8.2",
53
54
  "@atproto/lex-cli": "^0.4.0",
54
- "@atproto/pds": "^0.4.23"
55
+ "@atproto/pds": "^0.4.24"
55
56
  },
56
57
  "scripts": {
57
58
  "codegen": "lex gen-server ./src/lexicon ../../lexicons/com/atproto/*/* ../../lexicons/app/bsky/*/* ../../lexicons/chat/bsky/*/* ../../lexicons/tools/ozone/*/*",
@@ -7,11 +7,12 @@ import { AtUri } from '@atproto/syntax'
7
7
  export default function (server: Server, ctx: AppContext) {
8
8
  server.tools.ozone.moderation.getRecord({
9
9
  auth: ctx.authVerifier.modOrAdminToken,
10
- handler: async ({ params, auth }) => {
10
+ handler: async ({ params, auth, req }) => {
11
11
  const db = ctx.db
12
+ const labelers = ctx.reqLabelers(req)
12
13
 
13
14
  const [record, accountInfo] = await Promise.all([
14
- ctx.modService(db).views.recordDetail(params),
15
+ ctx.modService(db).views.recordDetail(params, labelers),
15
16
  getPdsAccountInfo(ctx, new AtUri(params.uri).hostname),
16
17
  ])
17
18
 
@@ -6,11 +6,12 @@ import { addAccountInfoToRepoViewDetail, getPdsAccountInfo } from '../util'
6
6
  export default function (server: Server, ctx: AppContext) {
7
7
  server.tools.ozone.moderation.getRepo({
8
8
  auth: ctx.authVerifier.modOrAdminToken,
9
- handler: async ({ params, auth }) => {
9
+ handler: async ({ params, auth, req }) => {
10
10
  const { did } = params
11
11
  const db = ctx.db
12
+ const labelers = ctx.reqLabelers(req)
12
13
  const [partialRepo, accountInfo] = await Promise.all([
13
- ctx.modService(db).views.repoDetail(did),
14
+ ctx.modService(db).views.repoDetail(did, labelers),
14
15
  getPdsAccountInfo(ctx, did),
15
16
  ])
16
17
  if (!partialRepo) {
@@ -127,4 +127,18 @@ export default function (server: Server, ctx: AppContext) {
127
127
  }
128
128
  },
129
129
  })
130
+
131
+ server.app.bsky.graph.getLists({
132
+ auth: ctx.authVerifier.moderator,
133
+ handler: async (request) => {
134
+ const res = await ctx.appviewAgent.api.app.bsky.graph.getLists(
135
+ request.params,
136
+ await ctx.appviewAuth(),
137
+ )
138
+ return {
139
+ encoding: 'application/json',
140
+ body: res.data,
141
+ }
142
+ },
143
+ })
130
144
  }
package/src/context.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import express from 'express'
1
2
  import * as plc from '@did-plc/lib'
2
3
  import { IdResolver } from '@atproto/identity'
3
4
  import { AtpAgent } from '@atproto/api'
@@ -17,7 +18,13 @@ import {
17
18
  import { BlobDiverter } from './daemon/blob-diverter'
18
19
  import { AuthVerifier } from './auth-verifier'
19
20
  import { ImageInvalidator } from './image-invalidator'
20
- import { getSigningKeyId } from './util'
21
+ import {
22
+ defaultLabelerHeader,
23
+ getSigningKeyId,
24
+ LABELER_HEADER_NAME,
25
+ ParsedLabelers,
26
+ parseLabelerHeader,
27
+ } from './util'
21
28
 
22
29
  export type AppContextOptions = {
23
30
  db: Database
@@ -234,5 +241,17 @@ export class AppContext {
234
241
  ...overrides,
235
242
  }
236
243
  }
244
+
245
+ reqLabelers(req: express.Request): ParsedLabelers {
246
+ const val = req.header(LABELER_HEADER_NAME)
247
+ let parsed: ParsedLabelers | null
248
+ try {
249
+ parsed = parseLabelerHeader(val, this.cfg.service.did)
250
+ } catch (err) {
251
+ parsed = null
252
+ }
253
+ if (!parsed) return defaultLabelerHeader([])
254
+ return parsed
255
+ }
237
256
  }
238
257
  export default AppContext
@@ -48,6 +48,7 @@ import { formatLabel, formatLabelRow, signLabel } from './util'
48
48
  import { ImageInvalidator } from '../image-invalidator'
49
49
  import { httpLogger as log } from '../logger'
50
50
  import { OzoneConfig } from '../config'
51
+ import { LABELER_HEADER_NAME, ParsedLabelers } from '../util'
51
52
 
52
53
  export type ModerationServiceCreator = (db: Database) => ModerationService
53
54
 
@@ -96,7 +97,13 @@ export class ModerationService {
96
97
  this.signingKey,
97
98
  this.signingKeyId,
98
99
  this.appviewAgent,
99
- () => this.createAuthHeaders(this.cfg.appview.did),
100
+ async (labelers?: ParsedLabelers) => {
101
+ const authHeaders = await this.createAuthHeaders(this.cfg.appview.did)
102
+ if (labelers?.dids?.length) {
103
+ authHeaders.headers[LABELER_HEADER_NAME] = labelers.dids.join(', ')
104
+ }
105
+ return authHeaders
106
+ },
100
107
  )
101
108
 
102
109
  async getEvent(id: number): Promise<ModerationEventRow | undefined> {
@@ -28,10 +28,12 @@ import { formatLabel, signLabel } from './util'
28
28
  import { LabelRow } from '../db/schema/label'
29
29
  import { dbLogger } from '../logger'
30
30
  import { httpLogger } from '../logger'
31
+ import { ParsedLabelers } from '../util'
31
32
 
32
33
  export type AuthHeaders = {
33
34
  headers: {
34
35
  authorization: string
36
+ 'atproto-accept-labelers'?: string
35
37
  }
36
38
  }
37
39
 
@@ -210,10 +212,14 @@ export class ModerationViews {
210
212
  }
211
213
  }
212
214
 
213
- async repoDetail(did: string): Promise<RepoViewDetail | undefined> {
214
- const [repos, labels] = await Promise.all([
215
+ async repoDetail(
216
+ did: string,
217
+ labelers?: ParsedLabelers,
218
+ ): Promise<RepoViewDetail | undefined> {
219
+ const [repos, localLabels, externalLabels] = await Promise.all([
215
220
  this.repos([did]),
216
221
  this.labels(did),
222
+ this.getExternalLabels([did], labelers),
217
223
  ])
218
224
  const repo = repos.get(did)
219
225
  if (!repo) return
@@ -223,7 +229,7 @@ export class ModerationViews {
223
229
  moderation: {
224
230
  ...repo.moderation,
225
231
  },
226
- labels,
232
+ labels: [...localLabels, ...externalLabels],
227
233
  }
228
234
  }
229
235
 
@@ -293,6 +299,7 @@ export class ModerationViews {
293
299
 
294
300
  async recordDetail(
295
301
  subject: RecordSubject,
302
+ labelers?: ParsedLabelers,
296
303
  ): Promise<RecordViewDetail | undefined> {
297
304
  const [records, subjectStatusesResult] = await Promise.all([
298
305
  this.records([subject]),
@@ -303,9 +310,10 @@ export class ModerationViews {
303
310
 
304
311
  const status = subjectStatusesResult.get(subject.uri)
305
312
 
306
- const [blobs, labels, subjectStatus] = await Promise.all([
313
+ const [blobs, labels, externalLabels, subjectStatus] = await Promise.all([
307
314
  this.blob(findBlobRefs(record.value)),
308
315
  this.labels(record.uri),
316
+ this.getExternalLabels([record.uri], labelers),
309
317
  status ? this.formatSubjectStatus(status) : Promise.resolve(undefined),
310
318
  ])
311
319
  const selfLabels = getSelfLabels({
@@ -313,6 +321,7 @@ export class ModerationViews {
313
321
  cid: record.cid,
314
322
  record: record.value,
315
323
  })
324
+
316
325
  return {
317
326
  ...record,
318
327
  blobs,
@@ -320,10 +329,26 @@ export class ModerationViews {
320
329
  ...record.moderation,
321
330
  subjectStatus,
322
331
  },
323
- labels: [...labels, ...selfLabels],
332
+ labels: [...labels, ...selfLabels, ...externalLabels],
324
333
  }
325
334
  }
326
335
 
336
+ async getExternalLabels(
337
+ subjects: string[],
338
+ labelers?: ParsedLabelers,
339
+ ): Promise<Label[]> {
340
+ if (!labelers?.dids.length && !labelers?.redact.size) return []
341
+
342
+ const {
343
+ data: { labels },
344
+ } = await this.appviewAgent.api.com.atproto.label.queryLabels({
345
+ uriPatterns: subjects,
346
+ sources: labelers.dids,
347
+ })
348
+
349
+ return labels
350
+ }
351
+
327
352
  formatReport(report: ModerationEventRowWithHandle): ReportOutput {
328
353
  return {
329
354
  id: report.id,
package/src/util.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { AxiosError } from 'axios'
2
+ import { parseList } from 'structured-headers'
2
3
  import { XRPCError, ResponseType } from '@atproto/xrpc'
3
4
  import { RetryOptions, retry } from '@atproto/common'
4
5
  import Database from './db'
@@ -45,3 +46,52 @@ export function retryableHttp(err: unknown) {
45
46
  const retryableHttpStatusCodes = new Set([
46
47
  408, 425, 429, 500, 502, 503, 504, 522, 524,
47
48
  ])
49
+
50
+ export type ParsedLabelers = {
51
+ dids: string[]
52
+ redact: Set<string>
53
+ }
54
+
55
+ export const LABELER_HEADER_NAME = 'atproto-accept-labelers'
56
+
57
+ export const parseLabelerHeader = (
58
+ header: string | undefined,
59
+ ignoreDid?: string,
60
+ ): ParsedLabelers | null => {
61
+ if (!header) return null
62
+ const labelerDids = new Set<string>()
63
+ const redactDids = new Set<string>()
64
+ const parsed = parseList(header)
65
+ for (const item of parsed) {
66
+ const did = item[0].toString()
67
+ if (!did) {
68
+ return null
69
+ }
70
+ if (did === ignoreDid) {
71
+ continue
72
+ }
73
+ labelerDids.add(did)
74
+ const redact = item[1].get('redact')?.valueOf()
75
+ if (redact === true) {
76
+ redactDids.add(did)
77
+ }
78
+ }
79
+ return {
80
+ dids: [...labelerDids],
81
+ redact: redactDids,
82
+ }
83
+ }
84
+
85
+ export const defaultLabelerHeader = (dids: string[]): ParsedLabelers => {
86
+ return {
87
+ dids,
88
+ redact: new Set(dids),
89
+ }
90
+ }
91
+
92
+ export const formatLabelerHeader = (parsed: ParsedLabelers): string => {
93
+ const parts = parsed.dids.map((did) =>
94
+ parsed.redact.has(did) ? `${did};redact` : did,
95
+ )
96
+ return parts.join(',')
97
+ }
@@ -0,0 +1,271 @@
1
+ import {
2
+ SeedClient,
3
+ TestNetwork,
4
+ basicSeed,
5
+ TestOzone,
6
+ ModeratorClient,
7
+ createOzoneDid,
8
+ } from '@atproto/dev-env'
9
+ import AtpAgent from '@atproto/api'
10
+ import { Secp256k1Keypair } from '@atproto/crypto'
11
+ import { LABELER_HEADER_NAME } from '../src/util'
12
+
13
+ describe('labels from 3p labelers', () => {
14
+ let network: TestNetwork
15
+ let ozone: TestOzone
16
+ let thirdPartyLabeler: TestOzone
17
+ let agent: AtpAgent
18
+ let thirdPartyAgent: AtpAgent
19
+ let sc: SeedClient
20
+ let modClient: ModeratorClient
21
+ let thirdPartyModClient: ModeratorClient
22
+
23
+ beforeAll(async () => {
24
+ network = await TestNetwork.create({
25
+ dbPostgresSchema: 'ozone_admin_third_party_labeler',
26
+ })
27
+ ozone = network.ozone
28
+
29
+ const ozoneKey = await Secp256k1Keypair.create({ exportable: true })
30
+ const ozoneDid = await createOzoneDid(network.plc.url, ozoneKey)
31
+ thirdPartyLabeler = await TestOzone.create({
32
+ port: ozone.port + 10,
33
+ plcUrl: network.plc.url,
34
+ signingKey: ozoneKey,
35
+ serverDid: ozoneDid,
36
+ dbPostgresSchema: `ozone_admin_third_party_labeler`,
37
+ dbPostgresUrl: ozone.ctx.cfg.db.postgresUrl,
38
+ appviewUrl: network.bsky.url,
39
+ appviewDid: network.bsky.ctx.cfg.serverDid,
40
+ appviewPushEvents: true,
41
+ pdsUrl: network.pds.url,
42
+ pdsDid: network.pds.ctx.cfg.service.did,
43
+ })
44
+
45
+ thirdPartyAgent = thirdPartyLabeler.getClient()
46
+ agent = ozone.getClient()
47
+ sc = network.getSeedClient()
48
+ modClient = ozone.getModClient()
49
+ thirdPartyModClient = thirdPartyLabeler.getModClient()
50
+ await basicSeed(sc)
51
+ await network.processAll()
52
+ })
53
+
54
+ afterAll(async () => {
55
+ await network.close()
56
+ await thirdPartyLabeler.close()
57
+ })
58
+
59
+ const getPostSubject = () => ({
60
+ $type: 'com.atproto.repo.strongRef',
61
+ uri: sc.posts[sc.dids.alice][0].ref.uriStr,
62
+ cid: sc.posts[sc.dids.alice][0].ref.cidStr,
63
+ })
64
+
65
+ const adjustLabels = async ({
66
+ uri,
67
+ cid,
68
+ src,
69
+ createLabelVals = [],
70
+ negateLabelVals = [],
71
+ }: {
72
+ uri: string
73
+ src: string
74
+ cid?: string
75
+ createLabelVals?: string[]
76
+ negateLabelVals?: string[]
77
+ }) => {
78
+ const labelEntries = createLabelVals.map((val) => ({
79
+ uri,
80
+ cid: cid || '',
81
+ val,
82
+ cts: new Date().toISOString(),
83
+ neg: false,
84
+ src,
85
+ }))
86
+
87
+ negateLabelVals.forEach((val) => {
88
+ labelEntries.push({
89
+ uri,
90
+ cid: cid || '',
91
+ val,
92
+ cts: new Date().toISOString(),
93
+ neg: true,
94
+ src,
95
+ })
96
+ })
97
+ await network.bsky.db.db.insertInto('label').values(labelEntries).execute()
98
+ }
99
+
100
+ const labelAccount = async (
101
+ client: ModeratorClient,
102
+ {
103
+ createLabelVals = [],
104
+ negateLabelVals = [],
105
+ }: { createLabelVals?: string[]; negateLabelVals?: string[] },
106
+ ) => {
107
+ const subject = {
108
+ $type: 'com.atproto.admin.defs#repoRef',
109
+ did: sc.dids.alice,
110
+ }
111
+ await client.emitEvent(
112
+ {
113
+ subject,
114
+ event: {
115
+ $type: 'tools.ozone.moderation.defs#modEventLabel',
116
+ createLabelVals,
117
+ negateLabelVals,
118
+ },
119
+ createdBy: sc.dids.carol,
120
+ },
121
+ 'moderator',
122
+ )
123
+ await adjustLabels({
124
+ createLabelVals,
125
+ negateLabelVals,
126
+ uri: sc.dids.alice,
127
+ src: client.ozone.ctx.cfg.service.did,
128
+ })
129
+ }
130
+
131
+ const labelPost = async (
132
+ client: ModeratorClient,
133
+ {
134
+ createLabelVals = [],
135
+ negateLabelVals = [],
136
+ }: { createLabelVals?: string[]; negateLabelVals?: string[] },
137
+ ) => {
138
+ const subject = getPostSubject()
139
+ await client.emitEvent(
140
+ {
141
+ subject,
142
+ event: {
143
+ $type: 'tools.ozone.moderation.defs#modEventLabel',
144
+ createLabelVals,
145
+ negateLabelVals,
146
+ },
147
+ createdBy: sc.dids.carol,
148
+ },
149
+ 'moderator',
150
+ )
151
+ await adjustLabels({
152
+ createLabelVals,
153
+ negateLabelVals,
154
+ uri: subject.uri,
155
+ cid: subject.cid,
156
+ src: client.ozone.ctx.cfg.service.did,
157
+ })
158
+ }
159
+
160
+ describe('record labels', () => {
161
+ it('includes only labels from current authority by default', async () => {
162
+ await Promise.all([
163
+ labelPost(modClient, { createLabelVals: ['spam'] }),
164
+ labelPost(thirdPartyModClient, { createLabelVals: ['weird'] }),
165
+ ])
166
+
167
+ const [
168
+ { data: recordFromCurrentAuthority },
169
+ { data: recordFromThirdParty },
170
+ ] = await Promise.all([
171
+ agent.api.tools.ozone.moderation.getRecord(
172
+ { uri: sc.posts[sc.dids.alice][0].ref.uriStr },
173
+ { headers: await ozone.modHeaders() },
174
+ ),
175
+ thirdPartyAgent.api.tools.ozone.moderation.getRecord(
176
+ { uri: sc.posts[sc.dids.alice][0].ref.uriStr },
177
+ { headers: await thirdPartyLabeler.modHeaders() },
178
+ ),
179
+ ])
180
+
181
+ const currentAuthorityLabels = recordFromCurrentAuthority.labels?.map(
182
+ (l) => l.val,
183
+ )
184
+ const thirdPartyLabels = recordFromThirdParty.labels?.map((l) => l.val)
185
+ expect(currentAuthorityLabels).toContain('spam')
186
+ expect(currentAuthorityLabels).not.toContain('weird')
187
+ expect(thirdPartyLabels).toContain('weird')
188
+ expect(thirdPartyLabels).not.toContain('spam')
189
+ })
190
+
191
+ it('includes labels from all authorities requested via header', async () => {
192
+ const authHeaders = await ozone.modHeaders()
193
+ const { data: recordIncludingExternalLabels } =
194
+ await agent.api.tools.ozone.moderation.getRecord(
195
+ { uri: sc.posts[sc.dids.alice][0].ref.uriStr },
196
+ {
197
+ headers: {
198
+ ...authHeaders,
199
+ [LABELER_HEADER_NAME]: [
200
+ thirdPartyLabeler.ctx.cfg.service.did,
201
+ ozone.ctx.cfg.service.did,
202
+ ].join(','),
203
+ },
204
+ },
205
+ )
206
+ const labelVals = recordIncludingExternalLabels.labels?.map((l) => l.val)
207
+ const labelSources = recordIncludingExternalLabels.labels?.map(
208
+ (l) => l.src,
209
+ )
210
+ expect(labelVals).toContain('weird')
211
+ expect(labelVals).toContain('spam')
212
+ expect(labelSources).toContain(thirdPartyLabeler.ctx.cfg.service.did)
213
+ expect(labelSources).toContain(ozone.ctx.cfg.service.did)
214
+ })
215
+ })
216
+
217
+ describe('repo labels', () => {
218
+ it('includes only labels from current authority by default', async () => {
219
+ await Promise.all([
220
+ labelAccount(modClient, { createLabelVals: ['spam'] }),
221
+ labelAccount(thirdPartyModClient, { createLabelVals: ['weird'] }),
222
+ ])
223
+
224
+ const [{ data: repoFromCurrentAuthority }, { data: repoFromThirdParty }] =
225
+ await Promise.all([
226
+ agent.api.tools.ozone.moderation.getRepo(
227
+ { did: sc.dids.alice },
228
+ { headers: await ozone.modHeaders() },
229
+ ),
230
+ thirdPartyAgent.api.tools.ozone.moderation.getRepo(
231
+ { did: sc.dids.alice },
232
+ { headers: await thirdPartyLabeler.modHeaders() },
233
+ ),
234
+ ])
235
+
236
+ const currentAuthorityLabels = repoFromCurrentAuthority.labels?.map(
237
+ (l) => l.val,
238
+ )
239
+ const thirdPartyLabels = repoFromThirdParty.labels?.map((l) => l.val)
240
+ expect(currentAuthorityLabels).toContain('spam')
241
+ expect(currentAuthorityLabels).not.toContain('weird')
242
+ expect(thirdPartyLabels).toContain('weird')
243
+ expect(thirdPartyLabels).not.toContain('spam')
244
+ })
245
+
246
+ it('includes labels from all authorities requested via header', async () => {
247
+ const authHeaders = await ozone.modHeaders()
248
+ const { data: recordIncludingExternalLabels } =
249
+ await agent.api.tools.ozone.moderation.getRepo(
250
+ { did: sc.dids.alice },
251
+ {
252
+ headers: {
253
+ ...authHeaders,
254
+ [LABELER_HEADER_NAME]: [
255
+ thirdPartyLabeler.ctx.cfg.service.did,
256
+ ozone.ctx.cfg.service.did,
257
+ ].join(','),
258
+ },
259
+ },
260
+ )
261
+ const labelVals = recordIncludingExternalLabels.labels?.map((l) => l.val)
262
+ const labelSources = recordIncludingExternalLabels.labels?.map(
263
+ (l) => l.src,
264
+ )
265
+ expect(labelVals).toContain('weird')
266
+ expect(labelVals).toContain('spam')
267
+ expect(labelSources).toContain(thirdPartyLabeler.ctx.cfg.service.did)
268
+ expect(labelSources).toContain(ozone.ctx.cfg.service.did)
269
+ })
270
+ })
271
+ })