Skip to content

Commit

Permalink
support feed search on getPopularFeedGenerators
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Jan 31, 2024
1 parent 8f613b5 commit c03fcec
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,26 @@ export default function (server: Server, ctx: AppContext) {
}
}

const suggestedRes = await ctx.dataplane.getSuggestedFeeds({
actorDid: viewer ?? undefined,
limit: params.limit,
cursor: params.cursor,
})
const uris = suggestedRes.uris
let uris: string[]
let cursor: string | undefined

const query = params.query?.trim() ?? ''
if (query) {
const res = await ctx.dataplane.searchFeedGenerators({
query,
limit: params.limit,
})
uris = res.uris
} else {
const res = await ctx.dataplane.getSuggestedFeeds({
actorDid: viewer ?? undefined,
limit: params.limit,
cursor: params.cursor,
})
uris = res.uris
cursor = parseString(res.cursor)
}

const hydration = await ctx.hydrator.hydrateFeedGens(uris, viewer)
const feedViews = mapDefined(uris, (uri) =>
ctx.views.feedGenerator(uri, hydration),
Expand All @@ -35,7 +49,7 @@ export default function (server: Server, ctx: AppContext) {
encoding: 'application/json',
body: {
feeds: feedViews,
cursor: parseString(suggestedRes.cursor),
cursor,
},
}
},
Expand Down
25 changes: 24 additions & 1 deletion packages/bsky/src/data-plane/server/routes/feed-gens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,37 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
}
},

async getSuggestedFeeds() {
async getSuggestedFeeds(req) {
const feeds = await db.db
.selectFrom('suggested_feed')
.orderBy('suggested_feed.order', 'asc')
.if(!!req.cursor, (q) => q.where('order', '>', parseInt(req.cursor, 10)))
.limit(req.limit || 50)
.selectAll()
.execute()
return {
uris: feeds.map((f) => f.uri),
cursor: feeds.at(-1)?.order.toString(),
}
},

async searchFeedGenerators(req) {
const { ref } = db.db.dynamic
const limit = req.limit
const query = req.query.trim()
let builder = db.db
.selectFrom('feed_generator')
.if(!!query, (q) => q.where('displayName', 'ilike', `%${query}%`))
.selectAll()
const keyset = new TimeCidKeyset(
ref('feed_generator.createdAt'),
ref('feed_generator.cid'),
)
builder = paginate(builder, { limit, keyset })
const feeds = await builder.execute()
return {
uris: feeds.map((f) => f.uri),
cursor: keyset.packFromResult(feeds),
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,7 @@ Object {

exports[`feed generation getSuggestedFeeds returns list of suggested feed generators 1`] = `
Object {
"cursor": "4",
"feeds": Array [
Object {
"cid": "cids(0)",
Expand Down
29 changes: 18 additions & 11 deletions packages/bsky/tests/feed-generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,26 @@ describe('feed generation', () => {
})
})

// @TODO support from dataplane
describe.skip('getPopularFeedGenerators', () => {
describe('getPopularFeedGenerators', () => {
it('gets popular feed generators', async () => {
const resEven =
await agent.api.app.bsky.unspecced.getPopularFeedGenerators(
{},
{ headers: await network.serviceHeaders(sc.dids.bob) },
)
expect(resEven.data.feeds.map((f) => f.likeCount)).toEqual([
2, 0, 0, 0, 0,
const res = await agent.api.app.bsky.unspecced.getPopularFeedGenerators(
{},
{ headers: await network.serviceHeaders(sc.dids.bob) },
)
expect(res.data.feeds.map((f) => f.uri)).not.toContain(feedUriPrime) // taken-down
expect(res.data.feeds.map((f) => f.uri)).toEqual([
feedUriAll,
feedUriEven,
feedUriBadPagination,
])
expect(resEven.data.feeds.map((f) => f.uri)).not.toContain(feedUriPrime) // taken-down
})

it('searches feed generators', async () => {
const res = await agent.api.app.bsky.unspecced.getPopularFeedGenerators(
{ query: 'all' },
{ headers: await network.serviceHeaders(sc.dids.bob) },
)
expect(res.data.feeds.map((f) => f.uri)).toEqual([feedUriAll])
})

it('paginates', async () => {
Expand All @@ -368,7 +376,6 @@ describe('feed generation', () => {
{},
{ headers: await network.serviceHeaders(sc.dids.bob) },
)

const resOne =
await agent.api.app.bsky.unspecced.getPopularFeedGenerators(
{ limit: 2 },
Expand Down

0 comments on commit c03fcec

Please sign in to comment.