[SDK] Migrate the gate records to raw repo calls on the pds client (#11378)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
import {
|
||||
type AppBskyFeedDefs,
|
||||
AppBskyFeedThreadgate,
|
||||
type AtpAgent,
|
||||
AtUri,
|
||||
} from '@atproto/api'
|
||||
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||
import {type Client} from '@atproto/lex'
|
||||
import {AtUri, type HandleString} from '@atproto/syntax'
|
||||
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
|
||||
|
||||
import {networkRetry, retry} from '#/lib/async/retry'
|
||||
import {isRecordNotFoundError} from '#/lib/xrpc-error'
|
||||
import {STALE} from '#/state/queries'
|
||||
import {useGetPost} from '#/state/queries/post'
|
||||
import {type ThreadgateAllowUISetting} from '#/state/queries/threadgate/types'
|
||||
@@ -17,8 +15,9 @@ import {
|
||||
threadgateViewToAllowUISetting,
|
||||
} from '#/state/queries/threadgate/util'
|
||||
import {useUpdatePostThreadThreadgateQueryCache} from '#/state/queries/usePostThread'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {usePdsClient} from '#/state/session'
|
||||
import {useThreadgateHiddenReplyUrisAPI} from '#/state/threadgate-hidden-replies'
|
||||
import {app, com} from '#/lexicons'
|
||||
import * as bsky from '#/types/bsky'
|
||||
|
||||
export * from '#/state/queries/threadgate/types'
|
||||
@@ -40,9 +39,9 @@ export function useThreadgateRecordQuery({
|
||||
initialData,
|
||||
}: {
|
||||
postUri?: string
|
||||
initialData?: AppBskyFeedThreadgate.Record
|
||||
initialData?: app.bsky.feed.threadgate.Main
|
||||
} = {}) {
|
||||
const agent = useAgent()
|
||||
const pdsClient = usePdsClient()
|
||||
|
||||
return useQuery({
|
||||
enabled: !!postUri,
|
||||
@@ -51,7 +50,7 @@ export function useThreadgateRecordQuery({
|
||||
staleTime: STALE.MINUTES.ONE,
|
||||
async queryFn() {
|
||||
return getThreadgateRecord({
|
||||
agent,
|
||||
pdsClient,
|
||||
postUri: postUri!,
|
||||
})
|
||||
},
|
||||
@@ -85,24 +84,23 @@ export function useThreadgateViewQuery({
|
||||
}
|
||||
|
||||
export async function getThreadgateRecord({
|
||||
agent,
|
||||
pdsClient,
|
||||
postUri,
|
||||
}: {
|
||||
agent: AtpAgent
|
||||
pdsClient: Client
|
||||
postUri: string
|
||||
}): Promise<AppBskyFeedThreadgate.Record | null> {
|
||||
}): Promise<app.bsky.feed.threadgate.Main | null> {
|
||||
const urip = new AtUri(postUri)
|
||||
|
||||
if (!urip.host.startsWith('did:')) {
|
||||
const res = await agent.resolveHandle({
|
||||
handle: urip.host,
|
||||
const {did} = await pdsClient.call(com.atproto.identity.resolveHandle, {
|
||||
handle: urip.host as HandleString,
|
||||
})
|
||||
// @ts-expect-error TODO new-sdk-migration
|
||||
urip.host = res.data.did
|
||||
urip.host = did
|
||||
}
|
||||
|
||||
try {
|
||||
const {data} = await retry(
|
||||
const data = await retry(
|
||||
2,
|
||||
e => {
|
||||
/*
|
||||
@@ -110,34 +108,31 @@ export async function getThreadgateRecord({
|
||||
* throwing an error. NB: This will also catch reference errors, such as
|
||||
* a typo in the URI.
|
||||
*/
|
||||
if (e.message.includes(`Could not locate record:`)) {
|
||||
if (isRecordNotFoundError(e)) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
},
|
||||
() =>
|
||||
agent.api.com.atproto.repo.getRecord({
|
||||
pdsClient.call(com.atproto.repo.getRecord, {
|
||||
repo: urip.host,
|
||||
collection: 'app.bsky.feed.threadgate',
|
||||
rkey: urip.rkey,
|
||||
rkey: urip.rkeySafe,
|
||||
}),
|
||||
)
|
||||
|
||||
if (
|
||||
data.value &&
|
||||
bsky.validate(data.value, AppBskyFeedThreadgate.validateRecord)
|
||||
) {
|
||||
if (data.value && bsky.matches(app.bsky.feed.threadgate, data.value)) {
|
||||
return data.value
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
} catch (e: any) {
|
||||
} catch (e) {
|
||||
/*
|
||||
* If the record doesn't exist, we want to return null instead of
|
||||
* throwing an error. NB: This will also catch reference errors, such as
|
||||
* a typo in the URI.
|
||||
*/
|
||||
if (e.message.includes(`Could not locate record:`)) {
|
||||
if (isRecordNotFoundError(e)) {
|
||||
return null
|
||||
} else {
|
||||
throw e
|
||||
@@ -146,13 +141,13 @@ export async function getThreadgateRecord({
|
||||
}
|
||||
|
||||
export async function writeThreadgateRecord({
|
||||
agent,
|
||||
pdsClient,
|
||||
postUri,
|
||||
threadgate,
|
||||
}: {
|
||||
agent: AtpAgent
|
||||
pdsClient: Client
|
||||
postUri: string
|
||||
threadgate: AppBskyFeedThreadgate.Record
|
||||
threadgate: app.bsky.feed.threadgate.Main
|
||||
}) {
|
||||
const postUrip = new AtUri(postUri)
|
||||
const record = createThreadgateRecord({
|
||||
@@ -162,10 +157,10 @@ export async function writeThreadgateRecord({
|
||||
})
|
||||
|
||||
await networkRetry(2, () =>
|
||||
agent.api.com.atproto.repo.putRecord({
|
||||
repo: agent.session!.did,
|
||||
pdsClient.call(com.atproto.repo.putRecord, {
|
||||
repo: pdsClient.assertDid,
|
||||
collection: 'app.bsky.feed.threadgate',
|
||||
rkey: postUrip.rkey,
|
||||
rkey: postUrip.rkeySafe,
|
||||
record,
|
||||
}),
|
||||
)
|
||||
@@ -173,25 +168,25 @@ export async function writeThreadgateRecord({
|
||||
|
||||
export async function upsertThreadgate(
|
||||
{
|
||||
agent,
|
||||
pdsClient,
|
||||
postUri,
|
||||
}: {
|
||||
agent: AtpAgent
|
||||
pdsClient: Client
|
||||
postUri: string
|
||||
},
|
||||
callback: (
|
||||
threadgate: AppBskyFeedThreadgate.Record | null,
|
||||
) => Promise<AppBskyFeedThreadgate.Record | undefined>,
|
||||
threadgate: app.bsky.feed.threadgate.Main | null,
|
||||
) => Promise<app.bsky.feed.threadgate.Main | undefined>,
|
||||
) {
|
||||
const prev = await getThreadgateRecord({
|
||||
agent,
|
||||
pdsClient,
|
||||
postUri,
|
||||
})
|
||||
const next = await callback(prev)
|
||||
if (!next) return
|
||||
validateThreadgateRecordOrThrow(next)
|
||||
await writeThreadgateRecord({
|
||||
agent,
|
||||
pdsClient,
|
||||
postUri,
|
||||
threadgate: next,
|
||||
})
|
||||
@@ -201,15 +196,15 @@ export async function upsertThreadgate(
|
||||
* Update the allow list for a threadgate record.
|
||||
*/
|
||||
export async function updateThreadgateAllow({
|
||||
agent,
|
||||
pdsClient,
|
||||
postUri,
|
||||
allow,
|
||||
}: {
|
||||
agent: AtpAgent
|
||||
pdsClient: Client
|
||||
postUri: string
|
||||
allow: ThreadgateAllowUISetting[]
|
||||
}) {
|
||||
return upsertThreadgate({agent, postUri}, async prev => {
|
||||
return upsertThreadgate({pdsClient, postUri}, async prev => {
|
||||
if (prev) {
|
||||
return {
|
||||
...prev,
|
||||
@@ -225,7 +220,7 @@ export async function updateThreadgateAllow({
|
||||
}
|
||||
|
||||
export function useSetThreadgateAllowMutation() {
|
||||
const agent = useAgent()
|
||||
const pdsClient = usePdsClient()
|
||||
const queryClient = useQueryClient()
|
||||
const getPost = useGetPost()
|
||||
const updatePostThreadThreadgate = useUpdatePostThreadThreadgateQueryCache()
|
||||
@@ -238,7 +233,7 @@ export function useSetThreadgateAllowMutation() {
|
||||
postUri: string
|
||||
allow: ThreadgateAllowUISetting[]
|
||||
}) => {
|
||||
return upsertThreadgate({agent, postUri}, async prev => {
|
||||
return upsertThreadgate({pdsClient, postUri}, async prev => {
|
||||
if (prev) {
|
||||
return {
|
||||
...prev,
|
||||
@@ -290,7 +285,7 @@ export function useSetThreadgateAllowMutation() {
|
||||
}
|
||||
|
||||
export function useToggleReplyVisibilityMutation() {
|
||||
const agent = useAgent()
|
||||
const pdsClient = usePdsClient()
|
||||
const queryClient = useQueryClient()
|
||||
const hiddenReplies = useThreadgateHiddenReplyUrisAPI()
|
||||
|
||||
@@ -310,7 +305,7 @@ export function useToggleReplyVisibilityMutation() {
|
||||
hiddenReplies.removeHiddenReplyUri(replyUri)
|
||||
}
|
||||
|
||||
await upsertThreadgate({agent, postUri}, async prev => {
|
||||
await upsertThreadgate({pdsClient, postUri}, async prev => {
|
||||
if (prev) {
|
||||
if (action === 'hide') {
|
||||
return mergeThreadgateRecords(prev, {
|
||||
@@ -363,9 +358,9 @@ export class InvalidInteractionSettingsError extends Error {
|
||||
}
|
||||
|
||||
export function validateThreadgateRecordOrThrow(
|
||||
record: AppBskyFeedThreadgate.Record,
|
||||
record: app.bsky.feed.threadgate.Main,
|
||||
) {
|
||||
const result = AppBskyFeedThreadgate.validateRecord(record)
|
||||
const result = bsky.safeParse(app.bsky.feed.threadgate, record)
|
||||
|
||||
if (result.success) {
|
||||
if ((result.value.hiddenReplies?.length ?? 0) > MAX_HIDDEN_REPLIES) {
|
||||
|
||||
@@ -1,26 +1,34 @@
|
||||
import {type AppBskyFeedDefs, AppBskyFeedThreadgate} from '@atproto/api'
|
||||
import {type AppBskyFeedDefs} from '@atproto/api'
|
||||
import {type AtUriString, toDatetimeString} from '@atproto/syntax'
|
||||
|
||||
import {type ThreadgateAllowUISetting} from '#/state/queries/threadgate/types'
|
||||
import {app} from '#/lexicons'
|
||||
import * as bsky from '#/types/bsky'
|
||||
|
||||
/*
|
||||
* Threadgate VIEWS stay on the legacy client types: they are read-only inputs
|
||||
* from the appview, and branding them here would ripple through every post
|
||||
* component. Only the threadgate RECORD is migrated, because it is written
|
||||
* through `com.atproto.repo.putRecord`, whose body is typed as a lex `LexMap`.
|
||||
*/
|
||||
export function threadgateViewToAllowUISetting(
|
||||
threadgateView: AppBskyFeedDefs.ThreadgateView | undefined,
|
||||
): ThreadgateAllowUISetting[] {
|
||||
// Validate the record for clarity, since backwards compat code is a little confusing
|
||||
const threadgate =
|
||||
threadgateView &&
|
||||
bsky.validate(threadgateView.record, AppBskyFeedThreadgate.validateRecord)
|
||||
bsky.matches(app.bsky.feed.threadgate, threadgateView.record)
|
||||
? threadgateView.record
|
||||
: undefined
|
||||
return threadgateRecordToAllowUISetting(threadgate)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a full {@link AppBskyFeedThreadgate.Record} to a list of
|
||||
* Converts a full {@link app.bsky.feed.threadgate.Main} to a list of
|
||||
* {@link ThreadgateAllowUISetting}, for use by app UI.
|
||||
*/
|
||||
export function threadgateRecordToAllowUISetting(
|
||||
threadgate: AppBskyFeedThreadgate.Record | undefined,
|
||||
threadgate: app.bsky.feed.threadgate.Main | undefined,
|
||||
): ThreadgateAllowUISetting[] {
|
||||
/*
|
||||
* If `threadgate` doesn't exist (default), or if `threadgate.allow === undefined`, it means
|
||||
@@ -40,13 +48,13 @@ export function threadgateRecordToAllowUISetting(
|
||||
const settings: ThreadgateAllowUISetting[] = threadgate.allow
|
||||
.map(allow => {
|
||||
let setting: ThreadgateAllowUISetting | undefined
|
||||
if (AppBskyFeedThreadgate.isMentionRule(allow)) {
|
||||
if (bsky.isType(app.bsky.feed.threadgate.mentionRule, allow)) {
|
||||
setting = {type: 'mention'}
|
||||
} else if (AppBskyFeedThreadgate.isFollowingRule(allow)) {
|
||||
} else if (bsky.isType(app.bsky.feed.threadgate.followingRule, allow)) {
|
||||
setting = {type: 'following'}
|
||||
} else if (AppBskyFeedThreadgate.isListRule(allow)) {
|
||||
} else if (bsky.isType(app.bsky.feed.threadgate.listRule, allow)) {
|
||||
setting = {type: 'list', list: allow.list}
|
||||
} else if (AppBskyFeedThreadgate.isFollowerRule(allow)) {
|
||||
} else if (bsky.isType(app.bsky.feed.threadgate.followerRule, allow)) {
|
||||
setting = {type: 'followers'}
|
||||
}
|
||||
return setting
|
||||
@@ -57,7 +65,7 @@ export function threadgateRecordToAllowUISetting(
|
||||
|
||||
/**
|
||||
* Converts an array of {@link ThreadgateAllowUISetting} to the `allow` prop on
|
||||
* {@link AppBskyFeedThreadgate.Record}.
|
||||
* {@link app.bsky.feed.threadgate.Main}.
|
||||
*
|
||||
* If the `allow` property on the record is undefined, we infer that to mean
|
||||
* that everyone can reply. If it's an empty array, we infer that to mean that
|
||||
@@ -65,12 +73,12 @@ export function threadgateRecordToAllowUISetting(
|
||||
*/
|
||||
export function threadgateAllowUISettingToAllowRecordValue(
|
||||
threadgate: ThreadgateAllowUISetting[],
|
||||
): AppBskyFeedThreadgate.Record['allow'] {
|
||||
): app.bsky.feed.threadgate.Main['allow'] {
|
||||
if (threadgate.find(v => v.type === 'everybody')) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
let allow: Exclude<AppBskyFeedThreadgate.Record['allow'], undefined> = []
|
||||
let allow: Exclude<app.bsky.feed.threadgate.Main['allow'], undefined> = []
|
||||
|
||||
if (!threadgate.find(v => v.type === 'nobody')) {
|
||||
for (const rule of threadgate) {
|
||||
@@ -83,7 +91,7 @@ export function threadgateAllowUISettingToAllowRecordValue(
|
||||
} else if (rule.type === 'list') {
|
||||
allow.push({
|
||||
$type: 'app.bsky.feed.threadgate#listRule',
|
||||
list: rule.list,
|
||||
list: rule.list as AtUriString,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -93,18 +101,20 @@ export function threadgateAllowUISettingToAllowRecordValue(
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges two {@link AppBskyFeedThreadgate.Record} objects, combining their
|
||||
* Merges two {@link app.bsky.feed.threadgate.Main} objects, combining their
|
||||
* `allow` and `hiddenReplies` arrays and de-deduplicating them.
|
||||
*
|
||||
* Note: `allow` can be undefined here, be sure you don't accidentally set it
|
||||
* to an empty array. See other comments in this file.
|
||||
*/
|
||||
export function mergeThreadgateRecords(
|
||||
prev: AppBskyFeedThreadgate.Record,
|
||||
next: Partial<AppBskyFeedThreadgate.Record>,
|
||||
): AppBskyFeedThreadgate.Record {
|
||||
prev: app.bsky.feed.threadgate.Main,
|
||||
next: Omit<Partial<app.bsky.feed.threadgate.Main>, 'hiddenReplies'> & {
|
||||
hiddenReplies?: string[]
|
||||
},
|
||||
): app.bsky.feed.threadgate.Main {
|
||||
// can be undefined if everyone can reply!
|
||||
const allow: AppBskyFeedThreadgate.Record['allow'] | undefined =
|
||||
const allow: app.bsky.feed.threadgate.Main['allow'] | undefined =
|
||||
prev.allow || next.allow
|
||||
? [...(prev.allow || []), ...(next.allow || [])].filter(
|
||||
(v, i, a) => a.findIndex(t => t.$type === v.$type) === i,
|
||||
@@ -112,7 +122,7 @@ export function mergeThreadgateRecords(
|
||||
: undefined
|
||||
const hiddenReplies = Array.from(
|
||||
new Set([...(prev.hiddenReplies || []), ...(next.hiddenReplies || [])]),
|
||||
)
|
||||
) as AtUriString[]
|
||||
|
||||
return createThreadgateRecord({
|
||||
post: prev.post,
|
||||
@@ -122,21 +132,28 @@ export function mergeThreadgateRecords(
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link AppBskyFeedThreadgate.Record} object with the given
|
||||
* properties.
|
||||
* Create a new {@link app.bsky.feed.threadgate.Main} object with the given
|
||||
* properties. `post` is accepted as a plain string (callers hold raw AT-URIs)
|
||||
* and asserted to the branded `AtUriString` here.
|
||||
*/
|
||||
export function createThreadgateRecord(
|
||||
threadgate: Partial<AppBskyFeedThreadgate.Record>,
|
||||
): AppBskyFeedThreadgate.Record {
|
||||
threadgate: Omit<
|
||||
Partial<app.bsky.feed.threadgate.Main>,
|
||||
'post' | 'hiddenReplies'
|
||||
> & {
|
||||
post?: string
|
||||
hiddenReplies?: string[]
|
||||
},
|
||||
): app.bsky.feed.threadgate.Main {
|
||||
if (!threadgate.post) {
|
||||
throw new Error('Cannot create a threadgate record without a post URI')
|
||||
}
|
||||
|
||||
return {
|
||||
$type: 'app.bsky.feed.threadgate',
|
||||
post: threadgate.post,
|
||||
createdAt: new Date().toISOString(),
|
||||
post: threadgate.post as AtUriString,
|
||||
createdAt: toDatetimeString(new Date()),
|
||||
allow: threadgate.allow, // can be undefined!
|
||||
hiddenReplies: threadgate.hiddenReplies || [],
|
||||
hiddenReplies: (threadgate.hiddenReplies || []) as AtUriString[],
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user