Skip to content

Commit

Permalink
Improve getPopular (bluesky-social#769)
Browse files Browse the repository at this point in the history
Speed up getPopular
  • Loading branch information
dholms authored Apr 6, 2023
1 parent e8e5a88 commit 63abe1f
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/pds/src/app-view/api/app/bsky/unspecced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import AppContext from '../../../../context'
import { FeedRow, FeedItemType } from '../../../services/feed'
import { sql } from 'kysely'
import { FeedViewPost } from '../../../../lexicon/types/app/bsky/feed/defs'
import { countAll } from '../../../../db/util'

// THIS IS A TEMPORARY UNSPECCED ROUTE
export default function (server: Server, ctx: AppContext) {
Expand All @@ -19,21 +20,22 @@ export default function (server: Server, ctx: AppContext) {
const feedService = ctx.services.appView.feed(ctx.db)

const postsQb = ctx.db.db
.with('like_counts', (qb) =>
qb
.selectFrom('like')
.groupBy('like.subject')
.select([sql`count(*)`.as('count'), 'like.subject']),
)
.selectFrom('post')
.innerJoin('like_counts', 'like_counts.subject', 'post.uri')
.leftJoin('repost', (join) =>
// this works well for one curating user. reassess if adding more
join
.on('repost.creator', '=', 'did:plc:ea2eqamjmtuo6f4rvhl3g6ne')
.onRef('repost.subject', '=', 'post.uri'),
)
.where('like_counts.count', '>=', 5)
.where(
(qb) =>
qb
.selectFrom('like')
.whereRef('like.subject', '=', 'post.uri')
.select(countAll.as('count')),
'>=',
5,
)
.orWhere('repost.creator', 'is not', null)
.select([
sql<FeedItemType>`${'post'}`.as('type'),
Expand Down

0 comments on commit 63abe1f

Please sign in to comment.