Skip to content

Commit

Permalink
Compute 3p blocks for ReplyRef.root (bluesky-social#2721)
Browse files Browse the repository at this point in the history
* Add grandparent to reply ref

* Hydrate grandparent on reply ref

* Changeset

* Check for 3p blocks on ReplyRef.root

* Revert "Add grandparent to reply ref"

This reverts commit 73012b6.

* Revert "Changeset"

This reverts commit 4264b08.

* Remove redundant hydration

* More tests

* Check for 3p blocks between grandparent and root

* Just kidding, ensure we only use first slice to compute from

* Clarify block relationship

* Add anti-test

* Rename test file to be more precise

* Make sure we get child->root block relationships

* Rename postBlocks.reply to parent

* Ensure first slice relationships are still correct if replyRef data is outside first page of results

* Remove unnecessary check

* Format

* Use latest APIs

* Prevent [creator, creator] checks
  • Loading branch information
estrattonbailey authored Aug 20, 2024
1 parent 5e2f261 commit 922b2e8
Show file tree
Hide file tree
Showing 3 changed files with 535 additions and 18 deletions.
26 changes: 19 additions & 7 deletions packages/bsky/src/hydration/hydrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,13 @@ export type HydrationState = {
bidirectionalBlocks?: BidirectionalBlocks
}

export type PostBlock = { embed: boolean; reply: boolean }
export type PostBlock = { embed: boolean; parent: boolean; root: boolean }
export type PostBlocks = HydrationMap<PostBlock>
type PostBlockPairs = { embed?: RelationshipPair; reply?: RelationshipPair }
type PostBlockPairs = {
embed?: RelationshipPair
parent?: RelationshipPair
root?: RelationshipPair
}

export type FollowBlock = boolean
export type FollowBlocks = HydrationMap<FollowBlock>
Expand Down Expand Up @@ -445,10 +449,17 @@ export class Hydrator {
// 3p block for replies
const parentUri = post.reply?.parent.uri
const parentDid = parentUri && didFromUri(parentUri)
if (parentDid) {
if (parentDid && parentDid !== creator) {
const pair: RelationshipPair = [creator, parentDid]
relationships.push(pair)
postBlockPairs.reply = pair
postBlockPairs.parent = pair
}
const rootUri = post.reply?.root.uri
const rootDid = rootUri && didFromUri(rootUri)
if (rootDid && rootDid !== creator) {
const pair: RelationshipPair = [creator, rootDid]
relationships.push(pair)
postBlockPairs.root = pair
}
// 3p block for record embeds
for (const embedUri of nestedRecordUris(post)) {
Expand All @@ -457,12 +468,13 @@ export class Hydrator {
postBlockPairs.embed = pair
}
}
// replace embed/reply pairs with block state
// replace embed/parent/root pairs with block state
const blocks = await this.graph.getBidirectionalBlocks(relationships)
for (const [uri, { embed, reply }] of postBlocksPairs) {
for (const [uri, { embed, parent, root }] of postBlocksPairs) {
postBlocks.set(uri, {
embed: !!embed && blocks.isBlocked(...embed),
reply: !!reply && blocks.isBlocked(...reply),
parent: !!parent && blocks.isBlocked(...parent),
root: !!root && blocks.isBlocked(...root),
})
}
return postBlocks
Expand Down
26 changes: 15 additions & 11 deletions packages/bsky/src/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,15 +622,16 @@ export class Views {
if (!postRecord?.reply) return
let root = this.maybePost(postRecord.reply.root.uri, state)
let parent = this.maybePost(postRecord.reply.parent.uri, state)
if (
!state.ctx?.include3pBlocks &&
state.postBlocks?.get(uri)?.reply &&
isPostView(parent)
) {
parent = this.blockedPost(parent.uri, parent.author.did, state)
// in a reply to the root of a thread, parent and root are the same post.
if (root.uri === parent.uri) {
root = parent
if (!state.ctx?.include3pBlocks) {
const childBlocks = state.postBlocks?.get(uri)
const parentBlocks = state.postBlocks?.get(parent.uri)
// if child blocks parent, block parent
if (isPostView(parent) && childBlocks?.parent) {
parent = this.blockedPost(parent.uri, parent.author.did, state)
}
// if child or parent blocks root, block root
if (isPostView(root) && (childBlocks?.root || parentBlocks?.root)) {
root = this.blockedPost(root.uri, root.author.did, state)
}
}
let grandparentAuthor: ProfileViewBasic | undefined
Expand Down Expand Up @@ -755,7 +756,10 @@ export class Views {
if (height < 1) return undefined
const parentUri = state.posts?.get(childUri)?.record.reply?.parent.uri
if (!parentUri) return undefined
if (!state.ctx?.include3pBlocks && state.postBlocks?.get(childUri)?.reply) {
if (
!state.ctx?.include3pBlocks &&
state.postBlocks?.get(childUri)?.parent
) {
return this.blockedPost(parentUri, creatorFromUri(parentUri), state)
}
const post = this.post(parentUri, state)
Expand Down Expand Up @@ -786,7 +790,7 @@ export class Views {
if (postInfo?.violatesThreadGate) {
return undefined
}
if (!state.ctx?.include3pBlocks && state.postBlocks?.get(uri)?.reply) {
if (!state.ctx?.include3pBlocks && state.postBlocks?.get(uri)?.parent) {
return undefined
}
const post = this.post(uri, state)
Expand Down
Loading

0 comments on commit 922b2e8

Please sign in to comment.