Skip to content

Commit

Permalink
filter blocks in curate list (#2720)
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyok authored Aug 21, 2024
1 parent 1572058 commit 47263e9
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 26 deletions.
33 changes: 30 additions & 3 deletions packages/bsky/src/api/app/bsky/feed/getListFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import {
HydrateCtx,
HydrationState,
Hydrator,
mergeStates,
} from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { DataPlaneClient } from '../../../../data-plane'
import { mapDefined } from '@atproto/common'
import { parseString } from '../../../../hydration/util'
import { FeedItem } from '../../../../hydration/feed'
import { uriToDid } from '../../../../util/uris'

export default function (server: Server, ctx: AppContext) {
const getListFeed = createPipeline(
Expand Down Expand Up @@ -71,23 +73,34 @@ const hydration = async (inputs: {
skeleton: Skeleton
}): Promise<HydrationState> => {
const { ctx, params, skeleton } = inputs
return ctx.hydrator.hydrateFeedItems(skeleton.items, params.hydrateCtx)
const [feedItemsState, bidirectionalBlocks] = await Promise.all([
ctx.hydrator.hydrateFeedItems(skeleton.items, params.hydrateCtx),
getBlocks({ ctx, params, skeleton }),
])
return mergeStates(feedItemsState, {
bidirectionalBlocks,
})
}

const noBlocksOrMutes = (inputs: {
ctx: Context
params: Params
skeleton: Skeleton
hydration: HydrationState
}): Skeleton => {
const { ctx, skeleton, hydration } = inputs
const { ctx, params, skeleton, hydration } = inputs
skeleton.items = skeleton.items.filter((item) => {
const bam = ctx.views.feedItemBlocksAndMutes(item, hydration)
const creatorBlocks = hydration.bidirectionalBlocks?.get(
uriToDid(params.list),
)
return (
!bam.authorBlocked &&
!bam.authorMuted &&
!bam.originatorBlocked &&
!bam.originatorMuted &&
!bam.ancestorAuthorBlocked
!bam.ancestorAuthorBlocked &&
!creatorBlocks?.get(uriToDid(item.post.uri))
)
})
return skeleton
Expand All @@ -105,6 +118,20 @@ const presentation = (inputs: {
return { feed, cursor: skeleton.cursor }
}

const getBlocks = async (input: {
ctx: Context
skeleton: Skeleton
params: Params
}) => {
const { ctx, skeleton, params } = input
const pairs: Map<string, string[]> = new Map()
pairs.set(
uriToDid(params.list),
skeleton.items.map((item) => uriToDid(item.post.uri)),
)
return await ctx.hydrator.hydrateBidirectionalBlocks(pairs)
}

type Context = {
hydrator: Hydrator
views: Views
Expand Down
8 changes: 4 additions & 4 deletions packages/bsky/src/api/app/bsky/graph/getList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const hydration = async (
params.hydrateCtx,
),
])
const bidirectionalBlocks = await maybeGetBlocksForReferenceList({
const bidirectionalBlocks = await maybeGetBlocksForReferenceAndCurateList({
ctx,
params,
skeleton,
Expand Down Expand Up @@ -106,7 +106,7 @@ const presentation = (
return { list, items, cursor }
}

const maybeGetBlocksForReferenceList = async (input: {
const maybeGetBlocksForReferenceAndCurateList = async (input: {
ctx: Context
listState: HydrationState
skeleton: SkeletonState
Expand All @@ -118,8 +118,8 @@ const maybeGetBlocksForReferenceList = async (input: {
const listRecord = listState.lists?.get(list)
const creator = didFromUri(list)
if (
listRecord?.record.purpose !== 'app.bsky.graph.defs#referencelist' ||
params.hydrateCtx.viewer === creator
params.hydrateCtx.viewer === creator ||
listRecord?.record.purpose === 'app.bsky.graph.defs#modlist'
) {
return
}
Expand Down
186 changes: 185 additions & 1 deletion packages/bsky/tests/views/__snapshots__/lists.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,52 @@ Array [
]
`;

exports[`bsky actor likes feed views does include users with creator block relationship in reference lists for creator 2`] = `
Array [
Object {
"subject": Object {
"did": "user(0)",
"handle": "frankie.test",
"labels": Array [],
"viewer": Object {
"blockedBy": true,
"muted": false,
},
},
"uri": "record(0)",
},
Object {
"subject": Object {
"avatar": "https://bsky.public.url/img/avatar/plain/user(2)/cids(0)@jpeg",
"createdAt": "1970-01-01T00:00:00.000Z",
"description": "hi im bob label_me",
"did": "user(1)",
"displayName": "bobby",
"handle": "bob.test",
"indexedAt": "1970-01-01T00:00:00.000Z",
"labels": Array [],
"viewer": Object {
"blockedBy": false,
"muted": false,
},
},
"uri": "record(1)",
},
Object {
"subject": Object {
"did": "user(3)",
"handle": "eve.test",
"labels": Array [],
"viewer": Object {
"blockedBy": false,
"muted": false,
},
},
"uri": "record(2)",
},
]
`;

exports[`bsky actor likes feed views does not include reference lists in getActorLists 1`] = `
Array [
Object {
Expand All @@ -62,6 +108,72 @@ Array [
"purpose": "app.bsky.graph.defs#curatelist",
"uri": "record(0)",
},
Object {
"cid": "cids(1)",
"creator": Object {
"did": "user(0)",
"handle": "eve.test",
"labels": Array [],
},
"indexedAt": "1970-01-01T00:00:00.000Z",
"labels": Array [],
"listItemCount": 3,
"name": "blah curate list!",
"purpose": "app.bsky.graph.defs#curatelist",
"uri": "record(1)",
},
]
`;

exports[`bsky actor likes feed views does not include users with creator block relationship in reference and curate lists for signed-out viewers 1`] = `
Array [
Object {
"subject": Object {
"avatar": "https://bsky.public.url/img/avatar/plain/user(1)/cids(0)@jpeg",
"createdAt": "1970-01-01T00:00:00.000Z",
"description": "hi im bob label_me",
"did": "user(0)",
"displayName": "bobby",
"handle": "bob.test",
"indexedAt": "1970-01-01T00:00:00.000Z",
"labels": Array [],
},
"uri": "record(0)",
},
Object {
"subject": Object {
"did": "user(2)",
"handle": "eve.test",
"labels": Array [],
},
"uri": "record(1)",
},
]
`;

exports[`bsky actor likes feed views does not include users with creator block relationship in reference and curate lists for signed-out viewers 2`] = `
Array [
Object {
"subject": Object {
"avatar": "https://bsky.public.url/img/avatar/plain/user(1)/cids(0)@jpeg",
"createdAt": "1970-01-01T00:00:00.000Z",
"description": "hi im bob label_me",
"did": "user(0)",
"displayName": "bobby",
"handle": "bob.test",
"indexedAt": "1970-01-01T00:00:00.000Z",
"labels": Array [],
},
"uri": "record(0)",
},
Object {
"subject": Object {
"did": "user(2)",
"handle": "eve.test",
"labels": Array [],
},
"uri": "record(1)",
},
]
`;

Expand Down Expand Up @@ -100,6 +212,41 @@ Array [
]
`;

exports[`bsky actor likes feed views does not include users with creator block relationship in reference lists for non-creator, in-list viewers 2`] = `
Array [
Object {
"subject": Object {
"avatar": "https://bsky.public.url/img/avatar/plain/user(1)/cids(0)@jpeg",
"createdAt": "1970-01-01T00:00:00.000Z",
"description": "hi im bob label_me",
"did": "user(0)",
"displayName": "bobby",
"handle": "bob.test",
"indexedAt": "1970-01-01T00:00:00.000Z",
"labels": Array [],
"viewer": Object {
"blockedBy": false,
"muted": false,
},
},
"uri": "record(0)",
},
Object {
"subject": Object {
"did": "user(2)",
"handle": "eve.test",
"labels": Array [],
"viewer": Object {
"blockedBy": false,
"blocking": "record(2)",
"muted": false,
},
},
"uri": "record(1)",
},
]
`;

exports[`bsky actor likes feed views does not include users with creator block relationship in reference lists for non-creator, not-in-list viewers 1`] = `
Array [
Object {
Expand Down Expand Up @@ -134,7 +281,7 @@ Array [
]
`;

exports[`bsky actor likes feed views does not include users with creator block relationship in reference lists for signed-out viewers 1`] = `
exports[`bsky actor likes feed views does not include users with creator block relationship in reference lists for non-creator, not-in-list viewers 2`] = `
Array [
Object {
"subject": Object {
Expand All @@ -146,6 +293,10 @@ Array [
"handle": "bob.test",
"indexedAt": "1970-01-01T00:00:00.000Z",
"labels": Array [],
"viewer": Object {
"blockedBy": false,
"muted": false,
},
},
"uri": "record(0)",
},
Expand All @@ -154,6 +305,39 @@ Array [
"did": "user(2)",
"handle": "eve.test",
"labels": Array [],
"viewer": Object {
"blockedBy": false,
"muted": false,
},
},
"uri": "record(1)",
},
]
`;

exports[`bsky actor likes feed views does return all users regardless of creator block relationship in moderation lists 1`] = `
Array [
Object {
"subject": Object {
"did": "user(0)",
"handle": "greta.test",
"labels": Array [],
"viewer": Object {
"blockedBy": false,
"muted": false,
},
},
"uri": "record(0)",
},
Object {
"subject": Object {
"did": "user(1)",
"handle": "frankie.test",
"labels": Array [],
"viewer": Object {
"blockedBy": false,
"muted": false,
},
},
"uri": "record(1)",
},
Expand Down
12 changes: 12 additions & 0 deletions packages/bsky/tests/views/list-feed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,16 @@ describe('list feed views', () => {
recordUri: postRef.uriStr,
})
})

it('does not return posts with creator blocks', async () => {
await sc.block(bob, alice)
await network.processAll()

const res = await agent.api.app.bsky.feed.getListFeed({
list: listRef.uriStr,
})

const hasBob = res.data.feed.some((item) => item.post.author.did === bob)
expect(hasBob).toBe(false)
})
})
Loading

0 comments on commit 47263e9

Please sign in to comment.