migrate the list and listitem records to the pds client
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,13 @@
|
|||||||
import {
|
import {
|
||||||
type AppBskyActorDefs,
|
type AppBskyActorDefs,
|
||||||
type AppBskyGraphGetStarterPacksWithMembership,
|
type AppBskyGraphGetStarterPacksWithMembership,
|
||||||
AtUri,
|
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
|
import {
|
||||||
|
AtUri,
|
||||||
|
type AtUriString,
|
||||||
|
type DidString,
|
||||||
|
toDatetimeString,
|
||||||
|
} from '@atproto/syntax'
|
||||||
import {
|
import {
|
||||||
type InfiniteData,
|
type InfiniteData,
|
||||||
useMutation,
|
useMutation,
|
||||||
@@ -13,7 +18,8 @@ import {
|
|||||||
RQKEY as LIST_MEMBERS_RQKEY,
|
RQKEY as LIST_MEMBERS_RQKEY,
|
||||||
RQKEY_ALL as LIST_MEMBERS_ALL_RQKEY,
|
RQKEY_ALL as LIST_MEMBERS_ALL_RQKEY,
|
||||||
} from '#/state/queries/list-members'
|
} from '#/state/queries/list-members'
|
||||||
import {useAgent, useSession} from '#/state/session'
|
import {usePdsClient, useSession} from '#/state/session'
|
||||||
|
import {app} from '#/lexicons'
|
||||||
import type * as bsky from '#/types/bsky'
|
import type * as bsky from '#/types/bsky'
|
||||||
import {RQKEY_WITH_MEMBERSHIP as STARTER_PACKS_WITH_MEMBERSHIPS_RKEY} from './actor-starter-packs'
|
import {RQKEY_WITH_MEMBERSHIP as STARTER_PACKS_WITH_MEMBERSHIPS_RKEY} from './actor-starter-packs'
|
||||||
|
|
||||||
@@ -30,7 +36,7 @@ export function useListMembershipAddMutation({
|
|||||||
onError?: (error: Error) => void
|
onError?: (error: Error) => void
|
||||||
} = {}) {
|
} = {}) {
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
const agent = useAgent()
|
const pdsClient = usePdsClient()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
return useMutation<
|
return useMutation<
|
||||||
{uri: string; cid: string},
|
{uri: string; cid: string},
|
||||||
@@ -41,14 +47,15 @@ export function useListMembershipAddMutation({
|
|||||||
if (!currentAccount) {
|
if (!currentAccount) {
|
||||||
throw new Error('Not signed in')
|
throw new Error('Not signed in')
|
||||||
}
|
}
|
||||||
const res = await agent.app.bsky.graph.listitem.create(
|
/*
|
||||||
{repo: currentAccount.did},
|
* The mutation's inputs are plain strings held by legacy-typed views, so
|
||||||
{
|
* they are asserted to their branded forms here.
|
||||||
subject: actorDid,
|
*/
|
||||||
list: listUri,
|
const res = await pdsClient.create(app.bsky.graph.listitem, {
|
||||||
createdAt: new Date().toISOString(),
|
subject: actorDid as DidString,
|
||||||
},
|
list: listUri as AtUriString,
|
||||||
)
|
createdAt: toDatetimeString(new Date()),
|
||||||
|
})
|
||||||
return res
|
return res
|
||||||
},
|
},
|
||||||
onSuccess: (data, variables) => {
|
onSuccess: (data, variables) => {
|
||||||
@@ -129,7 +136,7 @@ export function useListMembershipRemoveMutation({
|
|||||||
onError?: (error: Error) => void
|
onError?: (error: Error) => void
|
||||||
} = {}) {
|
} = {}) {
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
const agent = useAgent()
|
const pdsClient = usePdsClient()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
return useMutation<
|
return useMutation<
|
||||||
void,
|
void,
|
||||||
@@ -141,9 +148,9 @@ export function useListMembershipRemoveMutation({
|
|||||||
throw new Error('Not signed in')
|
throw new Error('Not signed in')
|
||||||
}
|
}
|
||||||
const membershipUrip = new AtUri(membershipUri)
|
const membershipUrip = new AtUri(membershipUri)
|
||||||
await agent.app.bsky.graph.listitem.delete({
|
await pdsClient.delete(app.bsky.graph.listitem, {
|
||||||
repo: currentAccount.did,
|
repo: currentAccount.did as DidString,
|
||||||
rkey: membershipUrip.rkey,
|
rkey: membershipUrip.rkeySafe,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
onSuccess: (data, variables) => {
|
onSuccess: (data, variables) => {
|
||||||
|
|||||||
+72
-65
@@ -1,14 +1,11 @@
|
|||||||
|
import {type AppBskyGraphDefs} from '@atproto/api'
|
||||||
|
import {type $Typed, type Client, type l} from '@atproto/lex'
|
||||||
import {
|
import {
|
||||||
type $Typed,
|
type AtIdentifierString,
|
||||||
type AppBskyGraphDefs,
|
|
||||||
type AppBskyGraphGetList,
|
|
||||||
type AppBskyGraphList,
|
|
||||||
type AtpAgent,
|
|
||||||
AtUri,
|
AtUri,
|
||||||
type ComAtprotoRepoApplyWrites,
|
type AtUriString,
|
||||||
type Facet,
|
toDatetimeString,
|
||||||
type Un$Typed,
|
} from '@atproto/syntax'
|
||||||
} from '@atproto/api'
|
|
||||||
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
|
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
|
||||||
import chunk from 'lodash.chunk'
|
import chunk from 'lodash.chunk'
|
||||||
|
|
||||||
@@ -16,7 +13,13 @@ import {uploadBlob} from '#/lib/api'
|
|||||||
import {until} from '#/lib/async/until'
|
import {until} from '#/lib/async/until'
|
||||||
import {type ImageMeta} from '#/state/gallery'
|
import {type ImageMeta} from '#/state/gallery'
|
||||||
import {STALE} from '#/state/queries'
|
import {STALE} from '#/state/queries'
|
||||||
import {useAgent, useSession} from '#/state/session'
|
import {
|
||||||
|
useAgent,
|
||||||
|
useAppviewClient,
|
||||||
|
usePdsClient,
|
||||||
|
useSession,
|
||||||
|
} from '#/state/session'
|
||||||
|
import {app, com} from '#/lexicons'
|
||||||
import {FEED_INFO_RQKEY_ROOT} from './feed'
|
import {FEED_INFO_RQKEY_ROOT} from './feed'
|
||||||
import {invalidate as invalidateMyLists} from './my-lists'
|
import {invalidate as invalidateMyLists} from './my-lists'
|
||||||
import {RQKEY as PROFILE_LISTS_RQKEY} from './profile-lists'
|
import {RQKEY as PROFILE_LISTS_RQKEY} from './profile-lists'
|
||||||
@@ -25,7 +28,7 @@ export const RQKEY_ROOT = 'list'
|
|||||||
export const RQKEY = (uri: string) => [RQKEY_ROOT, uri]
|
export const RQKEY = (uri: string) => [RQKEY_ROOT, uri]
|
||||||
|
|
||||||
export function useListQuery(uri?: string) {
|
export function useListQuery(uri?: string) {
|
||||||
const agent = useAgent()
|
const client = useAppviewClient()
|
||||||
return useQuery<AppBskyGraphDefs.ListView, Error>({
|
return useQuery<AppBskyGraphDefs.ListView, Error>({
|
||||||
staleTime: STALE.MINUTES.ONE,
|
staleTime: STALE.MINUTES.ONE,
|
||||||
queryKey: RQKEY(uri || ''),
|
queryKey: RQKEY(uri || ''),
|
||||||
@@ -33,11 +36,11 @@ export function useListQuery(uri?: string) {
|
|||||||
if (!uri) {
|
if (!uri) {
|
||||||
throw new Error('URI not provided')
|
throw new Error('URI not provided')
|
||||||
}
|
}
|
||||||
const res = await agent.app.bsky.graph.getList({
|
const res = await client.call(app.bsky.graph.getList, {
|
||||||
list: uri,
|
list: uri as AtUriString,
|
||||||
limit: 1,
|
limit: 1,
|
||||||
})
|
})
|
||||||
return res.data.list
|
return res.list
|
||||||
},
|
},
|
||||||
enabled: !!uri,
|
enabled: !!uri,
|
||||||
})
|
})
|
||||||
@@ -47,13 +50,15 @@ export interface ListCreateMutateParams {
|
|||||||
purpose: string
|
purpose: string
|
||||||
name: string
|
name: string
|
||||||
description: string
|
description: string
|
||||||
descriptionFacets: Facet[] | undefined
|
descriptionFacets: app.bsky.richtext.facet.Main[] | undefined
|
||||||
avatar: ImageMeta | null | undefined
|
avatar: ImageMeta | null | undefined
|
||||||
}
|
}
|
||||||
export function useListCreateMutation() {
|
export function useListCreateMutation() {
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
|
const appviewClient = useAppviewClient()
|
||||||
|
const pdsClient = usePdsClient()
|
||||||
return useMutation<{uri: string; cid: string}, Error, ListCreateMutateParams>(
|
return useMutation<{uri: string; cid: string}, Error, ListCreateMutateParams>(
|
||||||
{
|
{
|
||||||
async mutationFn({
|
async mutationFn({
|
||||||
@@ -72,33 +77,28 @@ export function useListCreateMutation() {
|
|||||||
) {
|
) {
|
||||||
throw new Error('Invalid list purpose: must be curatelist or modlist')
|
throw new Error('Invalid list purpose: must be curatelist or modlist')
|
||||||
}
|
}
|
||||||
const record: Un$Typed<AppBskyGraphList.Record> = {
|
const record: Omit<app.bsky.graph.list.Main, '$type'> = {
|
||||||
purpose,
|
purpose,
|
||||||
name,
|
name,
|
||||||
description,
|
description,
|
||||||
descriptionFacets,
|
descriptionFacets,
|
||||||
avatar: undefined,
|
avatar: undefined,
|
||||||
createdAt: new Date().toISOString(),
|
createdAt: toDatetimeString(new Date()),
|
||||||
}
|
}
|
||||||
if (avatar) {
|
if (avatar) {
|
||||||
const blobRes = await uploadBlob(agent, avatar.path, avatar.mime)
|
const blobRes = await uploadBlob(agent, avatar.path, avatar.mime)
|
||||||
record.avatar = blobRes.data.blob
|
/*
|
||||||
|
* `uploadBlob` still returns the legacy `BlobRef` class instance;
|
||||||
|
* it moves to the client with the rest of the blob pipeline.
|
||||||
|
*/
|
||||||
|
record.avatar = blobRes.data.blob as unknown as l.BlobRef
|
||||||
}
|
}
|
||||||
const res = await agent.app.bsky.graph.list.create(
|
const res = await pdsClient.create(app.bsky.graph.list, record)
|
||||||
{
|
|
||||||
repo: currentAccount.did,
|
|
||||||
},
|
|
||||||
record,
|
|
||||||
)
|
|
||||||
|
|
||||||
// wait for the appview to update
|
// wait for the appview to update
|
||||||
await whenAppViewReady(
|
await whenAppViewReady(appviewClient, res.uri, v => {
|
||||||
agent,
|
return typeof v?.list.uri === 'string'
|
||||||
res.uri,
|
})
|
||||||
(v: AppBskyGraphGetList.Response) => {
|
|
||||||
return typeof v?.data?.list.uri === 'string'
|
|
||||||
},
|
|
||||||
)
|
|
||||||
return res
|
return res
|
||||||
},
|
},
|
||||||
onSuccess() {
|
onSuccess() {
|
||||||
@@ -115,12 +115,14 @@ export interface ListMetadataMutateParams {
|
|||||||
uri: string
|
uri: string
|
||||||
name: string
|
name: string
|
||||||
description: string
|
description: string
|
||||||
descriptionFacets: Facet[] | undefined
|
descriptionFacets: app.bsky.richtext.facet.Main[] | undefined
|
||||||
avatar: ImageMeta | null | undefined
|
avatar: ImageMeta | null | undefined
|
||||||
}
|
}
|
||||||
export function useListMetadataMutation() {
|
export function useListMetadataMutation() {
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
|
const appviewClient = useAppviewClient()
|
||||||
|
const pdsClient = usePdsClient()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
return useMutation<
|
return useMutation<
|
||||||
{uri: string; cid: string},
|
{uri: string; cid: string},
|
||||||
@@ -137,7 +139,7 @@ export function useListMetadataMutation() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get the current record
|
// get the current record
|
||||||
const {value: record} = await agent.app.bsky.graph.list.get({
|
const {value: record} = await pdsClient.get(app.bsky.graph.list, {
|
||||||
repo: currentAccount.did,
|
repo: currentAccount.did,
|
||||||
rkey,
|
rkey,
|
||||||
})
|
})
|
||||||
@@ -148,30 +150,24 @@ export function useListMetadataMutation() {
|
|||||||
record.descriptionFacets = descriptionFacets
|
record.descriptionFacets = descriptionFacets
|
||||||
if (avatar) {
|
if (avatar) {
|
||||||
const blobRes = await uploadBlob(agent, avatar.path, avatar.mime)
|
const blobRes = await uploadBlob(agent, avatar.path, avatar.mime)
|
||||||
record.avatar = blobRes.data.blob
|
record.avatar = blobRes.data.blob as unknown as l.BlobRef
|
||||||
} else if (avatar === null) {
|
} else if (avatar === null) {
|
||||||
record.avatar = undefined
|
record.avatar = undefined
|
||||||
}
|
}
|
||||||
const res = (
|
const res = await pdsClient.call(com.atproto.repo.putRecord, {
|
||||||
await agent.com.atproto.repo.putRecord({
|
|
||||||
repo: currentAccount.did,
|
repo: currentAccount.did,
|
||||||
collection: 'app.bsky.graph.list',
|
collection: 'app.bsky.graph.list',
|
||||||
rkey,
|
rkey,
|
||||||
record,
|
record,
|
||||||
})
|
})
|
||||||
).data
|
|
||||||
|
|
||||||
// wait for the appview to update
|
// wait for the appview to update
|
||||||
await whenAppViewReady(
|
await whenAppViewReady(appviewClient, res.uri, v => {
|
||||||
agent,
|
const list = v.list
|
||||||
res.uri,
|
|
||||||
(v: AppBskyGraphGetList.Response) => {
|
|
||||||
const list = v.data.list
|
|
||||||
return (
|
return (
|
||||||
list.name === record.name && list.description === record.description
|
list.name === record.name && list.description === record.description
|
||||||
)
|
)
|
||||||
},
|
})
|
||||||
)
|
|
||||||
return res
|
return res
|
||||||
},
|
},
|
||||||
onSuccess(data, variables) {
|
onSuccess(data, variables) {
|
||||||
@@ -191,7 +187,8 @@ export function useListMetadataMutation() {
|
|||||||
|
|
||||||
export function useListDeleteMutation() {
|
export function useListDeleteMutation() {
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
const agent = useAgent()
|
const appviewClient = useAppviewClient()
|
||||||
|
const pdsClient = usePdsClient()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
return useMutation<void, Error, {uri: string}>({
|
return useMutation<void, Error, {uri: string}>({
|
||||||
mutationFn: async ({uri}) => {
|
mutationFn: async ({uri}) => {
|
||||||
@@ -199,11 +196,12 @@ export function useListDeleteMutation() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// fetch all the listitem records that belong to this list
|
// fetch all the listitem records that belong to this list
|
||||||
let cursor
|
let cursor: string | undefined
|
||||||
let listitemRecordUris: string[] = []
|
let listitemRecordUris: string[] = []
|
||||||
for (let i = 0; i < 100; i++) {
|
for (let i = 0; i < 100; i++) {
|
||||||
const res = await agent.app.bsky.graph.listitem.list({
|
const res = await pdsClient.list(app.bsky.graph.listitem, {
|
||||||
repo: currentAccount.did,
|
// the session account is still legacy-typed, so its did is unbranded
|
||||||
|
repo: currentAccount.did as AtIdentifierString,
|
||||||
cursor,
|
cursor,
|
||||||
limit: 100,
|
limit: 100,
|
||||||
})
|
})
|
||||||
@@ -221,12 +219,12 @@ export function useListDeleteMutation() {
|
|||||||
// batch delete the list and listitem records
|
// batch delete the list and listitem records
|
||||||
const createDel = (
|
const createDel = (
|
||||||
uri: string,
|
uri: string,
|
||||||
): $Typed<ComAtprotoRepoApplyWrites.Delete> => {
|
): $Typed<com.atproto.repo.applyWrites.Delete> => {
|
||||||
const urip = new AtUri(uri)
|
const urip = new AtUri(uri)
|
||||||
return {
|
return {
|
||||||
$type: 'com.atproto.repo.applyWrites#delete',
|
$type: 'com.atproto.repo.applyWrites#delete',
|
||||||
collection: urip.collection,
|
collection: urip.collectionSafe,
|
||||||
rkey: urip.rkey,
|
rkey: urip.rkeySafe,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const writes = listitemRecordUris
|
const writes = listitemRecordUris
|
||||||
@@ -235,15 +233,20 @@ export function useListDeleteMutation() {
|
|||||||
|
|
||||||
// apply in chunks
|
// apply in chunks
|
||||||
for (const writesChunk of chunk(writes, 10)) {
|
for (const writesChunk of chunk(writes, 10)) {
|
||||||
await agent.com.atproto.repo.applyWrites({
|
await pdsClient.call(com.atproto.repo.applyWrites, {
|
||||||
repo: currentAccount.did,
|
repo: currentAccount.did as AtIdentifierString,
|
||||||
writes: writesChunk,
|
writes: writesChunk,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait for the appview to update
|
/*
|
||||||
await whenAppViewReady(agent, uri, (v: AppBskyGraphGetList.Response) => {
|
* Wait for the appview to update. Once the list is deleted `getList`
|
||||||
return !v?.success
|
* throws, `until` catches it and passes `undefined` here, so an absent
|
||||||
|
* body signals a completed delete - the old check read `!v.success` on
|
||||||
|
* the legacy response envelope, which lex does not expose.
|
||||||
|
*/
|
||||||
|
await whenAppViewReady(appviewClient, uri, v => {
|
||||||
|
return !v
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
onSuccess() {
|
onSuccess() {
|
||||||
@@ -259,16 +262,18 @@ export function useListDeleteMutation() {
|
|||||||
export function useListMuteMutation() {
|
export function useListMuteMutation() {
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
|
const appviewClient = useAppviewClient()
|
||||||
return useMutation<void, Error, {uri: string; mute: boolean}>({
|
return useMutation<void, Error, {uri: string; mute: boolean}>({
|
||||||
mutationFn: async ({uri, mute}) => {
|
mutationFn: async ({uri, mute}) => {
|
||||||
|
// `muteModList`/`unmuteModList` are preference writes, migrated in wave B
|
||||||
if (mute) {
|
if (mute) {
|
||||||
await agent.muteModList(uri)
|
await agent.muteModList(uri)
|
||||||
} else {
|
} else {
|
||||||
await agent.unmuteModList(uri)
|
await agent.unmuteModList(uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
await whenAppViewReady(agent, uri, (v: AppBskyGraphGetList.Response) => {
|
await whenAppViewReady(appviewClient, uri, v => {
|
||||||
return Boolean(v?.data.list.viewer?.muted) === mute
|
return Boolean(v?.list.viewer?.muted) === mute
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
onSuccess(data, variables) {
|
onSuccess(data, variables) {
|
||||||
@@ -282,18 +287,20 @@ export function useListMuteMutation() {
|
|||||||
export function useListBlockMutation() {
|
export function useListBlockMutation() {
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
|
const appviewClient = useAppviewClient()
|
||||||
return useMutation<void, Error, {uri: string; block: boolean}>({
|
return useMutation<void, Error, {uri: string; block: boolean}>({
|
||||||
mutationFn: async ({uri, block}) => {
|
mutationFn: async ({uri, block}) => {
|
||||||
|
// `blockModList`/`unblockModList` write a block record, migrated in wave B
|
||||||
if (block) {
|
if (block) {
|
||||||
await agent.blockModList(uri)
|
await agent.blockModList(uri)
|
||||||
} else {
|
} else {
|
||||||
await agent.unblockModList(uri)
|
await agent.unblockModList(uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
await whenAppViewReady(agent, uri, (v: AppBskyGraphGetList.Response) => {
|
await whenAppViewReady(appviewClient, uri, v => {
|
||||||
return block
|
return block
|
||||||
? typeof v?.data.list.viewer?.blocked === 'string'
|
? typeof v?.list.viewer?.blocked === 'string'
|
||||||
: !v?.data.list.viewer?.blocked
|
: !v?.list.viewer?.blocked
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
onSuccess(data, variables) {
|
onSuccess(data, variables) {
|
||||||
@@ -305,17 +312,17 @@ export function useListBlockMutation() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function whenAppViewReady(
|
async function whenAppViewReady(
|
||||||
agent: AtpAgent,
|
client: Client,
|
||||||
uri: string,
|
uri: string,
|
||||||
fn: (res: AppBskyGraphGetList.Response) => boolean,
|
fn: (res: app.bsky.graph.getList.$OutputBody) => boolean,
|
||||||
) {
|
) {
|
||||||
await until(
|
await until(
|
||||||
5, // 5 tries
|
5, // 5 tries
|
||||||
1e3, // 1s delay between tries
|
1e3, // 1s delay between tries
|
||||||
fn,
|
fn,
|
||||||
() =>
|
() =>
|
||||||
agent.app.bsky.graph.getList({
|
client.call(app.bsky.graph.getList, {
|
||||||
list: uri,
|
list: uri as AtUriString,
|
||||||
limit: 1,
|
limit: 1,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user