Skip to content

Commit

Permalink
Fix optimistic getPostThread for URIs with handle (#2946)
Browse files Browse the repository at this point in the history
* Harden test coverge (add failing test)

* Resolve handle for post thread request

* Changeset
  • Loading branch information
gaearon authored Nov 5, 2024
1 parent 12af21e commit 9e18ab6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-queens-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atproto/pds": patch
---

Fix getPostThread optimistic handling for URIs with handle
19 changes: 15 additions & 4 deletions packages/pds/src/api/app/bsky/feed/getPostThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export default function (server: Server, ctx: AppContext) {
if (!rev) throw err

const uri = new AtUri(params.uri)
if (!uri.hostname.startsWith('did:')) {
const account = await ctx.accountManager.getAccount(uri.hostname)
if (account) {
uri.hostname = account.did
}
}
if (uri.hostname !== requester) throw err

const local = await ctx.actorStore.read(requester, (store) => {
Expand All @@ -51,6 +57,7 @@ export default function (server: Server, ctx: AppContext) {
params,
requester,
rev,
uri,
)
})
if (local === null) {
Expand Down Expand Up @@ -165,17 +172,21 @@ const readAfterWriteNotFound = async (
params: QueryParams,
requester: string,
rev: string,
resolvedUri: AtUri,
): Promise<{ data: OutputSchema; lag?: number } | null> => {
const uri = new AtUri(params.uri)
if (uri.hostname !== requester) {
if (resolvedUri.hostname !== requester) {
return null
}
const local = await localViewer.getRecordsSinceRev(rev)
const found = local.posts.find((p) => p.uri.toString() === uri.toString())
const found = local.posts.find(
(p) => p.uri.toString() === resolvedUri.toString(),
)
if (!found) return null
let thread = await threadPostView(localViewer, found)
if (!thread) return null
const rest = local.posts.filter((p) => p.uri.toString() !== uri.toString())
const rest = local.posts.filter(
(p) => p.uri.toString() !== resolvedUri.toString(),
)
thread = await addPostsToThread(localViewer, thread, rest)
const highestParent = getHighestParent(thread)
if (highestParent) {
Expand Down
21 changes: 20 additions & 1 deletion packages/pds/tests/proxied/read-after-write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,22 @@ describe('proxy read after write', () => {
{ uri: sc.posts[alice][0].ref.uriStr },
{ headers: { ...sc.getHeaders(alice) } },
)
const layerOne = res.data.thread.replies as ThreadViewPost[]
const thread = res.data.thread as ThreadViewPost
const layerOne = thread.replies as ThreadViewPost[]
expect(layerOne.length).toBe(1)
expect(layerOne[0].post.uri).toEqual(reply1.ref.uriStr)
const layerTwo = layerOne[0].replies as ThreadViewPost[]
expect(layerTwo.length).toBe(1)
expect(layerTwo[0].post.uri).toEqual(reply2.ref.uriStr)

const aliceHandle = sc.accounts[alice].handle
const handleUriStr = thread.post.uri.replace(alice, aliceHandle)
expect(handleUriStr).not.toEqual(thread.post.uri)
const handleRes = await agent.api.app.bsky.feed.getPostThread(
{ uri: handleUriStr },
{ headers: { ...sc.getHeaders(alice) } },
)
expect(handleRes.data.thread).toEqual(res.data.thread)
})

it('handles read after write on a thread that is not found on appview', async () => {
Expand All @@ -123,6 +133,15 @@ describe('proxy read after write', () => {
expect((thread.replies?.at(0) as ThreadViewPost).post.uri).toEqual(
replyRef2.uriStr,
)

const aliceHandle = sc.accounts[alice].handle
const handleUriStr = thread.post.uri.replace(alice, aliceHandle)
expect(handleUriStr).not.toEqual(thread.post.uri)
const handleRes = await agent.api.app.bsky.feed.getPostThread(
{ uri: handleUriStr },
{ headers: { ...sc.getHeaders(alice) } },
)
expect(handleRes.data.thread).toEqual(res.data.thread)
})

it('handles read after write on threads with record embeds', async () => {
Expand Down

0 comments on commit 9e18ab6

Please sign in to comment.