Skip to content

Commit

Permalink
add search mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Dec 13, 2023
1 parent 7d249bd commit 9f92374
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 74 deletions.
52 changes: 50 additions & 2 deletions packages/bsky/src/data-plane/server/routes/search.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,60 @@
import { ServiceImpl } from '@connectrpc/connect'
import { Service } from '../../gen/bsky_connect'
import { Database } from '../../../db'
import {
IndexedAtDidKeyset,
TimeCidKeyset,
paginate,
} from '../../../db/pagination'

export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
async searchActors(req) {
throw new Error('unimplemented')
const { term, limit, cursor } = req
const { ref } = db.db.dynamic
let builder = db.db
.selectFrom('actor')
.where('actor.handle', 'like', `%${term}%`)
.selectAll()

const keyset = new IndexedAtDidKeyset(
ref('actor.indexedAt'),
ref('actor.did'),
)
builder = paginate(builder, {
limit,
cursor,
keyset,
tryIndex: true,
})

const res = await builder.execute()

return {
dids: res.map((row) => row.did),
cursor: keyset.packFromResult(res),
}
},

async searchPosts(req) {
throw new Error('unimplemented')
const { term, limit, cursor } = req
const { ref } = db.db.dynamic
let builder = db.db
.selectFrom('post')
.where('post.text', 'like', `%${term}%`)
.selectAll()

const keyset = new TimeCidKeyset(ref('post.sortAt'), ref('post.cid'))
builder = paginate(builder, {
limit,
cursor,
keyset,
tryIndex: true,
})

const res = await builder.execute()
return {
uris: res.map((row) => row.uri),
cursor: keyset.packFromResult(res),
}
},
})
9 changes: 9 additions & 0 deletions packages/bsky/src/db/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ export class CreatedAtDidKeyset extends TimeCidKeyset<{
}
}

export class IndexedAtDidKeyset extends TimeCidKeyset<{
indexedAt: string
did: string // dids are treated identically to cids in TimeCidKeyset
}> {
labelResult(result: { indexedAt: string; did: string }) {
return { primary: result.indexedAt, secondary: result.did }
}
}

export const paginate = <
QB extends AnyQb,
K extends GenericKeyset<unknown, any>,
Expand Down
71 changes: 35 additions & 36 deletions packages/bsky/tests/views/block-lists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,42 +273,41 @@ describe('pds views with blocking from block lists', () => {
).toBeFalsy()
})

// @TODO uncomment after implemeting search in dataplane mock
// it('does not return blocked accounts in actor search', async () => {
// const resCarol = await agent.api.app.bsky.actor.searchActors(
// {
// term: 'dan.test',
// },
// { headers: await network.serviceHeaders(carol) },
// )
// expect(resCarol.data.actors.some((actor) => actor.did === dan)).toBeFalsy()

// const resDan = await agent.api.app.bsky.actor.searchActors(
// {
// term: 'carol.test',
// },
// { headers: await network.serviceHeaders(dan) },
// )
// expect(resDan.data.actors.some((actor) => actor.did === carol)).toBeFalsy()
// })

// it('does not return blocked accounts in actor search typeahead', async () => {
// const resCarol = await agent.api.app.bsky.actor.searchActorsTypeahead(
// {
// term: 'dan.test',
// },
// { headers: await network.serviceHeaders(carol) },
// )
// expect(resCarol.data.actors.some((actor) => actor.did === dan)).toBeFalsy()

// const resDan = await agent.api.app.bsky.actor.searchActorsTypeahead(
// {
// term: 'carol.test',
// },
// { headers: await network.serviceHeaders(dan) },
// )
// expect(resDan.data.actors.some((actor) => actor.did === carol)).toBeFalsy()
// })
it('does not return blocked accounts in actor search', async () => {
const resCarol = await agent.api.app.bsky.actor.searchActors(
{
term: 'dan.test',
},
{ headers: await network.serviceHeaders(carol) },
)
expect(resCarol.data.actors.some((actor) => actor.did === dan)).toBeFalsy()

const resDan = await agent.api.app.bsky.actor.searchActors(
{
term: 'carol.test',
},
{ headers: await network.serviceHeaders(dan) },
)
expect(resDan.data.actors.some((actor) => actor.did === carol)).toBeFalsy()
})

it('does not return blocked accounts in actor search typeahead', async () => {
const resCarol = await agent.api.app.bsky.actor.searchActorsTypeahead(
{
term: 'dan.test',
},
{ headers: await network.serviceHeaders(carol) },
)
expect(resCarol.data.actors.some((actor) => actor.did === dan)).toBeFalsy()

const resDan = await agent.api.app.bsky.actor.searchActorsTypeahead(
{
term: 'carol.test',
},
{ headers: await network.serviceHeaders(dan) },
)
expect(resDan.data.actors.some((actor) => actor.did === carol)).toBeFalsy()
})

it('does not return blocked accounts in get suggestions', async () => {
// unfollow so they _would_ show up in suggestions if not for block
Expand Down
71 changes: 35 additions & 36 deletions packages/bsky/tests/views/blocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,42 +315,41 @@ describe('pds views with blocking', () => {
).toBeFalsy()
})

// @TODO uncomment after adding search to dataplane mock
// it('does not return blocked accounts in actor search', async () => {
// const resCarol = await agent.api.app.bsky.actor.searchActors(
// {
// term: 'dan.test',
// },
// { headers: await network.serviceHeaders(carol) },
// )
// expect(resCarol.data.actors.some((actor) => actor.did === dan)).toBeFalsy()

// const resDan = await agent.api.app.bsky.actor.searchActors(
// {
// term: 'carol.test',
// },
// { headers: await network.serviceHeaders(dan) },
// )
// expect(resDan.data.actors.some((actor) => actor.did === carol)).toBeFalsy()
// })

// it('does not return blocked accounts in actor search typeahead', async () => {
// const resCarol = await agent.api.app.bsky.actor.searchActorsTypeahead(
// {
// term: 'dan.test',
// },
// { headers: await network.serviceHeaders(carol) },
// )
// expect(resCarol.data.actors.some((actor) => actor.did === dan)).toBeFalsy()

// const resDan = await agent.api.app.bsky.actor.searchActorsTypeahead(
// {
// term: 'carol.test',
// },
// { headers: await network.serviceHeaders(dan) },
// )
// expect(resDan.data.actors.some((actor) => actor.did === carol)).toBeFalsy()
// })
it('does not return blocked accounts in actor search', async () => {
const resCarol = await agent.api.app.bsky.actor.searchActors(
{
term: 'dan.test',
},
{ headers: await network.serviceHeaders(carol) },
)
expect(resCarol.data.actors.some((actor) => actor.did === dan)).toBeFalsy()

const resDan = await agent.api.app.bsky.actor.searchActors(
{
term: 'carol.test',
},
{ headers: await network.serviceHeaders(dan) },
)
expect(resDan.data.actors.some((actor) => actor.did === carol)).toBeFalsy()
})

it('does not return blocked accounts in actor search typeahead', async () => {
const resCarol = await agent.api.app.bsky.actor.searchActorsTypeahead(
{
term: 'dan.test',
},
{ headers: await network.serviceHeaders(carol) },
)
expect(resCarol.data.actors.some((actor) => actor.did === dan)).toBeFalsy()

const resDan = await agent.api.app.bsky.actor.searchActorsTypeahead(
{
term: 'carol.test',
},
{ headers: await network.serviceHeaders(dan) },
)
expect(resDan.data.actors.some((actor) => actor.did === carol)).toBeFalsy()
})

it('does not return blocked accounts in get suggestions', async () => {
// unfollow so they _would_ show up in suggestions if not for block
Expand Down

0 comments on commit 9f92374

Please sign in to comment.