From aadd35e1bdd86c6cd1450e6d197ad04c4473d5f4 Mon Sep 17 00:00:00 2001 From: Daniel Holmgren Date: Tue, 19 Dec 2023 11:43:16 -0600 Subject: [PATCH] Better handle 3rd party blocks in feeds (#1677) * better handle 3rd party blocks inf eeds * show anchor post in threads * test --- .../bsky/src/api/app/bsky/feed/getPostThread.ts | 7 ++++--- packages/bsky/src/services/feed/views.ts | 9 ++++++--- packages/bsky/tests/views/blocks.test.ts | 15 ++++++++++++++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/packages/bsky/src/api/app/bsky/feed/getPostThread.ts b/packages/bsky/src/api/app/bsky/feed/getPostThread.ts index 873dd311ba0..edf339513bc 100644 --- a/packages/bsky/src/api/app/bsky/feed/getPostThread.ts +++ b/packages/bsky/src/api/app/bsky/feed/getPostThread.ts @@ -128,9 +128,10 @@ const composeThread = ( // @TODO re-enable invalidReplyRoot check // const badReply = !!info?.invalidReplyRoot || !!info?.violatesThreadGate const badReply = !!info?.violatesThreadGate - const omitBadReply = !isAnchorPost && badReply + const violatesBlock = (post && blocks[post.uri]?.reply) ?? false + const omitBadReply = !isAnchorPost && (badReply || violatesBlock) - if (!post || blocks[post.uri]?.reply || omitBadReply) { + if (!post || omitBadReply) { return { $type: 'app.bsky.feed.defs#notFoundPost', uri: threadData.post.postUri, @@ -156,7 +157,7 @@ const composeThread = ( } let parent - if (threadData.parent && !badReply) { + if (threadData.parent && !badReply && !violatesBlock) { if (threadData.parent instanceof ParentNotFoundError) { parent = { $type: 'app.bsky.feed.defs#notFoundPost', diff --git a/packages/bsky/src/services/feed/views.ts b/packages/bsky/src/services/feed/views.ts index f013570e2d7..7f9fd12e082 100644 --- a/packages/bsky/src/services/feed/views.ts +++ b/packages/bsky/src/services/feed/views.ts @@ -132,8 +132,8 @@ export class FeedViews { lists, viewer, ) - // skip over not found & blocked posts - if (!post || blocks[post.uri]?.reply) { + // skip over not found post + if (!post) { continue } const feedPost = { post } @@ -159,6 +159,7 @@ export class FeedViews { ) { const replyParent = this.formatMaybePostView( item.replyParent, + item.uri, actors, posts, threadgates, @@ -171,6 +172,7 @@ export class FeedViews { ) const replyRoot = this.formatMaybePostView( item.replyRoot, + item.uri, actors, posts, threadgates, @@ -291,6 +293,7 @@ export class FeedViews { formatMaybePostView( uri: string, + replyUri: string | null, actors: ActorInfoMap, posts: PostInfoMap, threadgates: ThreadgateInfoMap, @@ -320,7 +323,7 @@ export class FeedViews { if ( post.author.viewer?.blockedBy || post.author.viewer?.blocking || - blocks[uri]?.reply + (replyUri !== null && blocks[replyUri]?.reply) ) { if (!opts?.usePostViewUnion) return return this.blockedPost(post) diff --git a/packages/bsky/tests/views/blocks.test.ts b/packages/bsky/tests/views/blocks.test.ts index 5d344a823d9..74079c7f7c6 100644 --- a/packages/bsky/tests/views/blocks.test.ts +++ b/packages/bsky/tests/views/blocks.test.ts @@ -112,6 +112,18 @@ describe('pds views with blocking', () => { expect(forSnapshot(thread)).toMatchSnapshot() }) + it('loads blocked reply as anchor with no parent', async () => { + const { data: thread } = await agent.api.app.bsky.feed.getPostThread( + { depth: 1, uri: carolReplyToDan.ref.uriStr }, + { headers: await network.serviceHeaders(alice) }, + ) + if (!isThreadViewPost(thread.thread)) { + throw new Error('Expected thread view post') + } + expect(thread.thread.post.uri).toEqual(carolReplyToDan.ref.uriStr) + expect(thread.thread.parent).toBeUndefined() + }) + it('blocks thread parent', async () => { // Parent is a post by dan const { data: thread } = await agent.api.app.bsky.feed.getPostThread( @@ -498,7 +510,8 @@ describe('pds views with blocking', () => { const replyBlockedPost = timeline.feed.find( (item) => item.post.uri === replyBlockedUri, ) - expect(replyBlockedPost).toBeUndefined() + assert(replyBlockedPost) + expect(replyBlockedPost.reply?.parent).toBeUndefined() const embedBlockedPost = timeline.feed.find( (item) => item.post.uri === embedBlockedUri, )