Skip to content

Commit

Permalink
Simplify moderation fanout to configured url (bluesky-social#1804)
Browse files Browse the repository at this point in the history
* simplify moderation fanout to configured url

* fix bsky mod fanout tests
  • Loading branch information
devinivy authored Nov 5, 2023
1 parent a3d81dd commit bebc4ba
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ export default function (server: Server, ctx: AppContext) {
return { result, restored }
})

if (restored) {
const { did, subjects } = restored
const agent = await ctx.pdsAdminAgent(did)
if (restored && ctx.moderationPushAgent) {
const agent = ctx.moderationPushAgent
const { subjects } = restored
const results = await Promise.allSettled(
subjects.map((subject) =>
retryHttp(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ export default function (server: Server, ctx: AppContext) {
return { result, takenDown }
})

if (takenDown) {
if (takenDown && ctx.moderationPushAgent) {
const agent = ctx.moderationPushAgent
const { did, subjects } = takenDown
if (did && subjects.length > 0) {
const agent = await ctx.pdsAdminAgent(did)
const results = await Promise.allSettled(
subjects.map((subject) =>
retryHttp(() =>
Expand Down
3 changes: 2 additions & 1 deletion packages/bsky/src/api/com/atproto/admin/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export const getPdsAccountInfo = async (
ctx: AppContext,
did: string,
): Promise<AccountView | null> => {
const agent = ctx.moderationPushAgent
if (!agent) return null
try {
const agent = await ctx.pdsAdminAgent(did)
const res = await agent.api.com.atproto.admin.getAccountInfo({ did })
return res.data
} catch (err) {
Expand Down
12 changes: 6 additions & 6 deletions packages/bsky/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface ServerConfigValues {
adminPassword: string
moderatorPassword?: string
triagePassword?: string
moderationActionReverseUrl?: string
moderationPushUrl?: string
}

export class ServerConfig {
Expand Down Expand Up @@ -78,8 +78,8 @@ export class ServerConfig {
const moderatorPassword = process.env.MODERATOR_PASSWORD || undefined
const triagePassword = process.env.TRIAGE_PASSWORD || undefined
const labelerDid = process.env.LABELER_DID || 'did:example:labeler'
const moderationActionReverseUrl =
overrides?.moderationActionReverseUrl ||
const moderationPushUrl =
overrides?.moderationPushUrl ||
process.env.MODERATION_PUSH_URL ||
undefined
return new ServerConfig({
Expand All @@ -104,7 +104,7 @@ export class ServerConfig {
adminPassword,
moderatorPassword,
triagePassword,
moderationActionReverseUrl,
moderationPushUrl,
...stripUndefineds(overrides ?? {}),
})
}
Expand Down Expand Up @@ -206,8 +206,8 @@ export class ServerConfig {
return this.cfg.triagePassword
}

get moderationActionReverseUrl() {
return this.cfg.moderationActionReverseUrl
get moderationPushUrl() {
return this.cfg.moderationPushUrl
}
}

Expand Down
20 changes: 11 additions & 9 deletions packages/bsky/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { LabelCache } from './label-cache'
import { NotificationServer } from './notifications'

export class AppContext {
public moderationPushAgent: AtpAgent | undefined
constructor(
private opts: {
db: DatabaseCoordinator
Expand All @@ -30,7 +31,16 @@ export class AppContext {
algos: MountedAlgos
notifServer: NotificationServer
},
) {}
) {
if (opts.cfg.moderationPushUrl) {
const url = new URL(opts.cfg.moderationPushUrl)
this.moderationPushAgent = new AtpAgent({ service: url.origin })
this.moderationPushAgent.api.setHeader(
'authorization',
auth.buildBasicAuth(url.username, url.password),
)
}
}

get db(): DatabaseCoordinator {
return this.opts.db
Expand Down Expand Up @@ -107,14 +117,6 @@ export class AppContext {
})
}

async pdsAdminAgent(did: string): Promise<AtpAgent> {
const data = await this.idResolver.did.resolveAtprotoData(did)
const agent = new AtpAgent({ service: data.pds })
const jwt = await this.serviceAuthJwt(did)
agent.api.setHeader('authorization', `Bearer ${jwt}`)
return agent
}

get backgroundQueue(): BackgroundQueue {
return this.opts.backgroundQueue
}
Expand Down
10 changes: 1 addition & 9 deletions packages/bsky/src/db/periodic-moderation-action-reversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Leader } from './leader'
import { dbLogger } from '../logger'
import AppContext from '../context'
import AtpAgent from '@atproto/api'
import { buildBasicAuth } from '../auth'
import { LabelService } from '../services/label'
import { ModerationActionRow } from '../services/moderation'

Expand All @@ -18,14 +17,7 @@ export class PeriodicModerationActionReversal {
pushAgent?: AtpAgent

constructor(private appContext: AppContext) {
if (appContext.cfg.moderationActionReverseUrl) {
const url = new URL(appContext.cfg.moderationActionReverseUrl)
this.pushAgent = new AtpAgent({ service: url.origin })
this.pushAgent.api.setHeader(
'authorization',
buildBasicAuth(url.username, url.password),
)
}
this.pushAgent = appContext.moderationPushAgent
}

// invert label creation & negations
Expand Down
1 change: 1 addition & 0 deletions packages/dev-env/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class TestNetwork extends TestNetworkNoAppView {
dbPostgresSchema: `appview_${dbPostgresSchema}`,
dbPrimaryPostgresUrl: dbPostgresUrl,
redisHost,
moderationPushUrl: `http://admin:${ADMIN_PASSWORD}@localhost:${pdsPort}`,
...params.bsky,
})
const pds = await TestPds.create({
Expand Down

0 comments on commit bebc4ba

Please sign in to comment.