Use generated content visibility helpers
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
import {View} from 'react-native'
|
import {View} from 'react-native'
|
||||||
import {msg} from '@lingui/core/macro'
|
import {Trans, useLingui} from '@lingui/react/macro'
|
||||||
import {useLingui} from '@lingui/react'
|
|
||||||
import {Trans} from '@lingui/react/macro'
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
useContentVisibilityMutation,
|
useContentVisibilityMutation,
|
||||||
@@ -13,7 +11,7 @@ import {Text} from '#/components/Typography'
|
|||||||
|
|
||||||
export function AlgoVisibilityOptOut() {
|
export function AlgoVisibilityOptOut() {
|
||||||
const t = useTheme()
|
const t = useTheme()
|
||||||
const {_} = useLingui()
|
const {t: l} = useLingui()
|
||||||
const {data, isPending: isQueryPending} = useContentVisibilityQuery()
|
const {data, isPending: isQueryPending} = useContentVisibilityQuery()
|
||||||
const updateContentVisibility = useContentVisibilityMutation()
|
const updateContentVisibility = useContentVisibilityMutation()
|
||||||
|
|
||||||
@@ -31,9 +29,7 @@ export function AlgoVisibilityOptOut() {
|
|||||||
disabled={!canToggle}
|
disabled={!canToggle}
|
||||||
value={isOptedOut}
|
value={isOptedOut}
|
||||||
onChange={onToggleOptOut}
|
onChange={onToggleOptOut}
|
||||||
label={_(
|
label={l`Ask apps to hide my posts from algorithmic recommendations`}
|
||||||
msg`Ask apps to hide my posts from algorithmic recommendations`,
|
|
||||||
)}
|
|
||||||
style={[a.w_full]}>
|
style={[a.w_full]}>
|
||||||
<Toggle.LabelText style={[a.flex_1]}>
|
<Toggle.LabelText style={[a.flex_1]}>
|
||||||
<Trans>
|
<Trans>
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
import {app} from '#/lexicons'
|
|
||||||
|
|
||||||
export const CONTENT_VISIBILITY_COLLECTION =
|
|
||||||
'app.bsky.actor.contentVisibilityDeclaration' as const
|
|
||||||
export const CONTENT_VISIBILITY_RKEY = 'self'
|
|
||||||
|
|
||||||
export type ContentVisibilityRecord =
|
|
||||||
app.bsky.actor.contentVisibilityDeclaration.Main
|
|
||||||
|
|
||||||
export function createContentVisibilityRecord(
|
|
||||||
hideFromAlgorithmicRecommendations: boolean,
|
|
||||||
): ContentVisibilityRecord {
|
|
||||||
return {
|
|
||||||
$type: CONTENT_VISIBILITY_COLLECTION,
|
|
||||||
hideFromAlgorithmicRecommendations,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseContentVisibilityRecord(
|
|
||||||
value: unknown,
|
|
||||||
): ContentVisibilityRecord {
|
|
||||||
const result =
|
|
||||||
app.bsky.actor.contentVisibilityDeclaration.main.safeParse(value)
|
|
||||||
if (!result.success) {
|
|
||||||
throw new Error('Invalid content visibility record', {
|
|
||||||
cause: result.reason,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return result.value
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
import {
|
|
||||||
CONTENT_VISIBILITY_COLLECTION,
|
|
||||||
createContentVisibilityRecord,
|
|
||||||
parseContentVisibilityRecord,
|
|
||||||
} from './content-visibility-record'
|
|
||||||
|
|
||||||
describe('content visibility records', () => {
|
|
||||||
it('creates a record with explicit hide semantics', () => {
|
|
||||||
expect(createContentVisibilityRecord(true)).toEqual({
|
|
||||||
$type: CONTENT_VISIBILITY_COLLECTION,
|
|
||||||
hideFromAlgorithmicRecommendations: true,
|
|
||||||
})
|
|
||||||
expect(createContentVisibilityRecord(false)).toEqual({
|
|
||||||
$type: CONTENT_VISIBILITY_COLLECTION,
|
|
||||||
hideFromAlgorithmicRecommendations: false,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('parses valid records', () => {
|
|
||||||
const record = createContentVisibilityRecord(true)
|
|
||||||
expect(parseContentVisibilityRecord(record)).toBe(record)
|
|
||||||
})
|
|
||||||
|
|
||||||
it.each([
|
|
||||||
null,
|
|
||||||
{},
|
|
||||||
{$type: CONTENT_VISIBILITY_COLLECTION},
|
|
||||||
{
|
|
||||||
$type: CONTENT_VISIBILITY_COLLECTION,
|
|
||||||
hideFromAlgorithmicRecommendations: 'true',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$type: 'app.bsky.actor.profile',
|
|
||||||
hideFromAlgorithmicRecommendations: true,
|
|
||||||
},
|
|
||||||
])('rejects invalid records: %p', value => {
|
|
||||||
expect(() => parseContentVisibilityRecord(value)).toThrow(
|
|
||||||
'Invalid content visibility record',
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -6,12 +6,6 @@ import {usePdsClient, useSession} from '#/state/session'
|
|||||||
import * as Toast from '#/components/Toast'
|
import * as Toast from '#/components/Toast'
|
||||||
import {useAnalytics} from '#/analytics'
|
import {useAnalytics} from '#/analytics'
|
||||||
import {app} from '#/lexicons'
|
import {app} from '#/lexicons'
|
||||||
import {
|
|
||||||
CONTENT_VISIBILITY_RKEY,
|
|
||||||
type ContentVisibilityRecord,
|
|
||||||
createContentVisibilityRecord,
|
|
||||||
parseContentVisibilityRecord,
|
|
||||||
} from './content-visibility-record'
|
|
||||||
|
|
||||||
export const contentVisibilityQueryKey = (did: string) => [
|
export const contentVisibilityQueryKey = (did: string) => [
|
||||||
'content-visibility',
|
'content-visibility',
|
||||||
@@ -31,13 +25,17 @@ export function useContentVisibilityQuery() {
|
|||||||
app.bsky.actor.contentVisibilityDeclaration,
|
app.bsky.actor.contentVisibilityDeclaration,
|
||||||
{
|
{
|
||||||
repo: did!,
|
repo: did!,
|
||||||
rkey: CONTENT_VISIBILITY_RKEY,
|
rkey: 'self',
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
return parseContentVisibilityRecord(response.value)
|
return app.bsky.actor.contentVisibilityDeclaration.$parse(
|
||||||
|
response.value,
|
||||||
|
)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (isRecordNotFoundError(error)) {
|
if (isRecordNotFoundError(error)) {
|
||||||
return createContentVisibilityRecord(false)
|
return app.bsky.actor.contentVisibilityDeclaration.$build({
|
||||||
|
hideFromAlgorithmicRecommendations: false,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
@@ -58,22 +56,26 @@ export function useContentVisibilityMutation() {
|
|||||||
mutationFn: async (hideFromAlgorithmicRecommendations: boolean) => {
|
mutationFn: async (hideFromAlgorithmicRecommendations: boolean) => {
|
||||||
if (!did) throw new Error('Not signed in')
|
if (!did) throw new Error('Not signed in')
|
||||||
|
|
||||||
const record = createContentVisibilityRecord(
|
const record = app.bsky.actor.contentVisibilityDeclaration.$build({
|
||||||
hideFromAlgorithmicRecommendations,
|
hideFromAlgorithmicRecommendations,
|
||||||
)
|
})
|
||||||
await client.put(app.bsky.actor.contentVisibilityDeclaration, record, {
|
await client.put(app.bsky.actor.contentVisibilityDeclaration, record, {
|
||||||
repo: did,
|
repo: did,
|
||||||
rkey: CONTENT_VISIBILITY_RKEY,
|
rkey: 'self',
|
||||||
})
|
})
|
||||||
return record
|
return record
|
||||||
},
|
},
|
||||||
onMutate: async hideFromAlgorithmicRecommendations => {
|
onMutate: async hideFromAlgorithmicRecommendations => {
|
||||||
await queryClient.cancelQueries({queryKey})
|
await queryClient.cancelQueries({queryKey})
|
||||||
const previous =
|
const previous =
|
||||||
queryClient.getQueryData<ContentVisibilityRecord>(queryKey)
|
queryClient.getQueryData<app.bsky.actor.contentVisibilityDeclaration.Main>(
|
||||||
|
queryKey,
|
||||||
|
)
|
||||||
queryClient.setQueryData(
|
queryClient.setQueryData(
|
||||||
queryKey,
|
queryKey,
|
||||||
createContentVisibilityRecord(hideFromAlgorithmicRecommendations),
|
app.bsky.actor.contentVisibilityDeclaration.$build({
|
||||||
|
hideFromAlgorithmicRecommendations,
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
return {previous}
|
return {previous}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user