diff options
author | dan <dan.abramov@gmail.com> | 2024-05-08 03:30:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-08 03:30:55 +0100 |
commit | 0910525e2efe124c63c1c8147a98450e43110681 (patch) | |
tree | 58162303b6c224e9ad90c552f66e29c7415bece4 /src/state/session/moderation.ts | |
parent | 4fe5a869c32c696862308cb8ff4537f34f43f06a (diff) | |
download | voidsky-0910525e2efe124c63c1c8147a98450e43110681.tar.zst |
[Session] Code cleanup (#3854)
* Split utils into files * Move reducer to another file * Write types explicitly * Remove unnnecessary check * Move things around a bit * Move more stuff into agent factories * Move more stuff into agent * Fix gates await * Clarify comments * Enforce more via types * Nit * initSession -> resumeSession * Protect against races * Make agent opaque to reducer * Check using plain condition
Diffstat (limited to 'src/state/session/moderation.ts')
-rw-r--r-- | src/state/session/moderation.ts | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/state/session/moderation.ts b/src/state/session/moderation.ts new file mode 100644 index 000000000..d8ded90f6 --- /dev/null +++ b/src/state/session/moderation.ts @@ -0,0 +1,50 @@ +import {BSKY_LABELER_DID, BskyAgent} from '@atproto/api' + +import {IS_TEST_USER} from '#/lib/constants' +import {readLabelers} from './agent-config' +import {SessionAccount} from './types' + +export function configureModerationForGuest() { + // This global mutation is *only* OK because this code is only relevant for testing. + // Don't add any other global behavior here! + switchToBskyAppLabeler() +} + +export async function configureModerationForAccount( + agent: BskyAgent, + account: SessionAccount, +) { + // This global mutation is *only* OK because this code is only relevant for testing. + // Don't add any other global behavior here! + switchToBskyAppLabeler() + if (IS_TEST_USER(account.handle)) { + await trySwitchToTestAppLabeler(agent) + } + + // The code below is actually relevant to production (and isn't global). + const labelerDids = await readLabelers(account.did).catch(_ => {}) + if (labelerDids) { + agent.configureLabelersHeader( + labelerDids.filter(did => did !== BSKY_LABELER_DID), + ) + } else { + // If there are no headers in the storage, we'll not send them on the initial requests. + // If we wanted to fix this, we could block on the preferences query here. + } +} + +function switchToBskyAppLabeler() { + BskyAgent.configure({appLabelers: [BSKY_LABELER_DID]}) +} + +async function trySwitchToTestAppLabeler(agent: BskyAgent) { + const did = ( + await agent + .resolveHandle({handle: 'mod-authority.test'}) + .catch(_ => undefined) + )?.data.did + if (did) { + console.warn('USING TEST ENV MODERATION') + BskyAgent.configure({appLabelers: [did]}) + } +} |