Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix read after write on threads when posting a media embed #1749

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/pds/src/services/local/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,14 @@ export class LocalService {
const { uri, title, description, thumb } = embed.external
return {
$type: 'app.bsky.embed.external#view',
uri,
title,
description,
thumb: thumb
? this.getImageUrl('feed_thumbnail', did, thumb.ref.toString())
: undefined,
external: {
uri,
title,
description,
thumb: thumb
? this.getImageUrl('feed_thumbnail', did, thumb.ref.toString())
: undefined,
},
}
}
}
Expand Down
86 changes: 86 additions & 0 deletions packages/pds/tests/proxied/read-after-write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { TestNetwork, SeedClient, RecordRef } from '@atproto/dev-env'
import basicSeed from '../seeds/basic'
import { ThreadViewPost } from '../../src/lexicon/types/app/bsky/feed/defs'
import { View as RecordEmbedView } from '../../src/lexicon/types/app/bsky/embed/record'
import { View as ExternalEmbedView } from '../../src/lexicon/types/app/bsky/embed/external'
import { View as ImagesEmbedView } from '../../src/lexicon/types/app/bsky/embed/images'

describe('proxy read after write', () => {
let network: TestNetwork
Expand Down Expand Up @@ -120,6 +122,90 @@ describe('proxy read after write', () => {
)
})

it('handles read after write on threads with record embeds', async () => {
const img = await sc.uploadFile(
alice,
'tests/sample-img/key-landscape-small.jpg',
'image/jpeg',
)
const replyRes1 = await agent.api.app.bsky.feed.post.create(
{ repo: alice },
{
text: 'images test',
reply: {
root: sc.posts[alice][2].ref.raw,
parent: sc.posts[alice][2].ref.raw,
},
embed: {
$type: 'app.bsky.embed.images',
images: [
{
image: img.image,
alt: 'alt text',
},
],
},
createdAt: new Date().toISOString(),
},
sc.getHeaders(alice),
)
const replyRes2 = await agent.api.app.bsky.feed.post.create(
{ repo: alice },
{
text: 'external test',
reply: {
root: sc.posts[alice][2].ref.raw,
parent: {
uri: replyRes1.uri,
cid: replyRes1.cid,
},
},
embed: {
$type: 'app.bsky.embed.external',
external: {
uri: 'https://example.com',
title: 'TestImage',
description: 'testLink',
thumb: img.image,
},
},
createdAt: new Date().toISOString(),
},
sc.getHeaders(alice),
)

const res = await agent.api.app.bsky.feed.getPostThread(
{ uri: sc.posts[alice][2].ref.uriStr },
{ headers: { ...sc.getHeaders(alice) } },
)
const replies = res.data.thread.replies as ThreadViewPost[]
expect(replies.length).toBe(1)
expect(replies[0].post.uri).toEqual(replyRes1.uri)
const imgs = replies[0].post.embed as ImagesEmbedView
expect(imgs.images[0].fullsize).toEqual(
util.format(
network.pds.ctx.cfg.bskyAppView.cdnUrlPattern,
'feed_fullsize',
alice,
img.image.ref.toString(),
),
)
expect(replies[0].replies?.length).toBe(1)
// @ts-ignore
expect(replies[0].replies[0].post.uri).toEqual(replyRes2.uri)
// @ts-ignore
const external = replies[0].replies[0].post.embed as ExternalEmbedView
expect(external.external.title).toEqual('TestImage')
expect(external.external.thumb).toEqual(
util.format(
network.pds.ctx.cfg.bskyAppView.cdnUrlPattern,
'feed_thumbnail',
alice,
img.image.ref.toString(),
),
)
})

it('handles read after write on threads with record embeds', async () => {
const replyRes = await agent.api.app.bsky.feed.post.create(
{ repo: alice },
Expand Down