Fix: persist labels to local storage to reduce coverage gaps

This commit is contained in:
Paul Frazee
2024-03-13 21:19:06 -07:00
parent 429981708a
commit 02c270c102
3 changed files with 36 additions and 4 deletions
+8
View File
@@ -22,6 +22,7 @@ import {
import {STALE} from '#/state/queries'
import {useLabelDefinitions} from '#/state/queries/preferences/moderation'
import {useHiddenPosts} from '#/state/preferences'
import {saveLabelers} from '#/state/session/agent-config'
export * from '#/state/queries/preferences/types'
export * from '#/state/queries/preferences/moderation'
@@ -42,6 +43,13 @@ export function usePreferencesQuery() {
return DEFAULT_LOGGED_OUT_PREFERENCES
} else {
const res = await agent.getPreferences()
// save to local storage to ensure there are labels on initial requests
saveLabelers(
agent.session.did,
res.moderationPrefs.labelers.map(l => l.did),
)
const preferences: UsePreferencesQueryResponse = {
...res,
feeds: {
+12
View File
@@ -0,0 +1,12 @@
import AsyncStorage from '@react-native-async-storage/async-storage'
const PREFIX = 'agent-labelers'
export async function saveLabelers(did: string, value: string[]) {
await AsyncStorage.setItem(`${PREFIX}:${did}`, JSON.stringify(value))
}
export async function readLabelers(did: string): Promise<string[] | undefined> {
const rawData = await AsyncStorage.getItem(`${PREFIX}:${did}`)
return rawData ? JSON.parse(rawData) : undefined
}
+16 -4
View File
@@ -19,6 +19,7 @@ import {useLoggedOutViewControls} from '#/state/shell/logged-out'
import {useCloseAllActiveElements} from '#/state/util'
import {track} from '#/lib/analytics/analytics'
import {hasProp} from '#/lib/type-guards'
import {readLabelers} from './agent-config'
let __globalAgent: BskyAgent = PUBLIC_BSKY_AGENT
@@ -661,12 +662,23 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
async function configureModeration(agent: BskyAgent, account: SessionAccount) {
if (IS_TEST_USER(account.handle)) {
console.warn('USING TEST ENV MODERATION')
const did = (await agent.resolveHandle({handle: 'mod-authority.test'})).data
.did
BskyAgent.configure({appLabelers: [did]})
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]})
}
} else {
BskyAgent.configure({appLabelers: [BSKY_LABELER_DID]})
const labelerDids = await readLabelers(account.did).catch(_ => {})
if (labelerDids) {
agent.configureLabelersHeader(
labelerDids.filter(did => did !== BSKY_LABELER_DID),
)
}
}
}