Skip to content

Commit

Permalink
fix up mock dataplane to match new protos
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Dec 7, 2023
1 parent a49f483 commit a99f28f
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 130 deletions.
2 changes: 1 addition & 1 deletion packages/bsky/src/data-plane/server/routes/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
.limit(1)
.executeTakeFirst()
return {
subscribed: !!res,
listblockUri: res?.uri,
}
},

Expand Down
20 changes: 0 additions & 20 deletions packages/bsky/src/data-plane/server/routes/feed-gens.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
import { ServiceImpl } from '@connectrpc/connect'
import { Service } from '../../gen/bsky_connect'
import * as ui8 from 'uint8arrays'
import { Database } from '../../../db'
import { keyBy } from '@atproto/common'
import { TimeCidKeyset, paginate } from '../../../db/pagination'

export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
async getFeedGenerators(req) {
if (req.uris.length === 0) {
return { records: [] }
}
const res = await db.db
.selectFrom('record')
.selectAll()
.where('uri', 'in', req.uris)
.execute()
const byUri = keyBy(res, 'uri')
const records = req.uris.map((uri) => {
const row = byUri[uri]
const json = row ? row.json : JSON.stringify(null)
return ui8.fromString(json, 'utf8')
})
return { records }
},

async getActorFeeds(req) {
const { actorDid, limit, cursor } = req

Expand Down
9 changes: 6 additions & 3 deletions packages/bsky/src/data-plane/server/routes/feeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { TimeCidKeyset, paginate } from '../../../db/pagination'

export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
async getAuthorFeed(req) {
const { actorDid, limit, cursor, repliesOnly, mediaOnly } = req
const { actorDid, limit, cursor, noReplies, mediaOnly } = req
const { ref } = db.db.dynamic

// defaults to posts, reposts, and replies
let builder = db.db
.selectFrom('feed_item')
.innerJoin('post', 'post.uri', 'feed_item.postUri')
.selectAll('feed_item')
.where('originatorDid', '=', actorDid)

Expand All @@ -25,8 +26,10 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
.select('post_embed_image.postUri')
.whereRef('post_embed_image.postUri', '=', 'feed_item.postUri'),
)
} else if (repliesOnly) {
// @TODO
} else if (noReplies) {
builder = builder.where((qb) =>
qb.where('post.replyParent', 'is', null).orWhere('type', '=', 'repost'),
)
}

const keyset = new TimeCidKeyset(
Expand Down
34 changes: 20 additions & 14 deletions packages/bsky/src/data-plane/server/routes/follows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,30 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
cursor: keyset.packFromResult(follows),
}
},
async getFollowersCount(req) {
async getFollowerCounts(req) {
if (req.dids.length === 0) {
return { counts: [] }
}
const res = await db.db
.selectFrom('profile_agg')
.select('followersCount')
.where('did', '=', req.actorDid)
.executeTakeFirst()
return {
count: res?.followersCount,
}
.selectAll()
.where('did', 'in', req.dids)
.execute()
const byDid = keyBy(res, 'did')
const counts = req.dids.map((did) => byDid[did]?.followersCount ?? 0)
return { counts }
},
async getFollowsCount(req) {
async getFollowCounts(req) {
if (req.dids.length === 0) {
return { counts: [] }
}
const res = await db.db
.selectFrom('profile_agg')
.select('followsCount')
.where('did', '=', req.actorDid)
.executeTakeFirst()
return {
count: res?.followsCount,
}
.selectAll()
.where('did', 'in', req.dids)
.execute()
const byDid = keyBy(res, 'did')
const counts = req.dids.map((did) => byDid[did]?.followsCount ?? 0)
return { counts }
},
})
6 changes: 3 additions & 3 deletions packages/bsky/src/data-plane/server/routes/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
if (subjects.length === 0 || issuers.length === 0) {
return { records: [] }
}
const labels = await db.db
const res = await db.db
.selectFrom('label')
.where('src', 'in', issuers)
.where('uri', 'in', subjects)
.selectAll()
.execute()

const records = labels.map((l) => {
const labels = res.map((l) => {
const formatted = {
...l,
cid: l.cid === '' ? undefined : l.cid,
}
return ui8.fromString(JSON.stringify(formatted), 'utf8')
})
return { records }
return { labels }
},
})
35 changes: 22 additions & 13 deletions packages/bsky/src/data-plane/server/routes/likes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ServiceImpl } from '@connectrpc/connect'
import { Service } from '../../gen/bsky_connect'
import { Database } from '../../../db'
import { TimeCidKeyset, paginate } from '../../../db/pagination'
import { keyBy } from '@atproto/common'

export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
async getLikesBySubject(req) {
Expand All @@ -28,15 +29,20 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
}
},

async getLikeByActorAndSubject(req) {
const { actorDid, subjectUri } = req
async getLikesByActorAndSubjects(req) {
const { actorDid, subjectUris } = req
if (subjectUris.length === 0) {
return { uris: [] }
}
const res = await db.db
.selectFrom('like')
.where('creator', '=', actorDid)
.where('subject', '=', subjectUri)
.select('uri')
.executeTakeFirst()
return { uri: res?.uri }
.where('subject', 'in', subjectUris)
.selectAll()
.execute()
const bySubject = keyBy(res, 'subject')
const uris = req.subjectUris.map((uri) => bySubject[uri]?.uri)
return { uris }
},

async getActorLikes(req) {
Expand Down Expand Up @@ -64,14 +70,17 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
}
},

async getLikesCount(req) {
async getLikeCounts(req) {
if (req.uris.length === 0) {
return { counts: [] }
}
const res = await db.db
.selectFrom('post_agg')
.where('uri', '=', req.subjectUri)
.select('likeCount')
.executeTakeFirst()
return {
count: res?.likeCount,
}
.where('uri', 'in', req.uris)
.selectAll()
.execute()
const byUri = keyBy(res, 'uri')
const counts = req.uris.map((uri) => byUri[uri]?.likeCount ?? 0)
return { counts }
},
})
13 changes: 0 additions & 13 deletions packages/bsky/src/data-plane/server/routes/lists.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ServiceImpl } from '@connectrpc/connect'
import { Service } from '../../gen/bsky_connect'
import * as ui8 from 'uint8arrays'
import { Database } from '../../../db'
import { countAll } from '../../../db/util'
import { keyBy } from '@atproto/common'
Expand Down Expand Up @@ -51,18 +50,6 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
}
},

async getList(req) {
const res = await db.db
.selectFrom('record')
.where('uri', '=', req.listUri)
.select('json')
.executeTakeFirst()
const record = res ? ui8.fromString(res.json, 'utf8') : undefined
return {
record,
}
},

async getListCount(req) {
const res = await db.db
.selectFrom('list_item')
Expand Down
31 changes: 13 additions & 18 deletions packages/bsky/src/data-plane/server/routes/posts.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
import { ServiceImpl } from '@connectrpc/connect'
import { Service } from '../../gen/bsky_connect'
import { keyBy } from '@atproto/common'
import * as ui8 from 'uint8arrays'
import { Database } from '../../../db'

export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
async getPosts(req) {
async getPostReplyCounts(req) {
if (req.uris.length === 0) {
return { records: [] }
return { counts: [] }
}
const res = await db.db
.selectFrom('record')
.selectAll()
.selectFrom('post_agg')
.select(['uri', 'replyCount'])
.where('uri', 'in', req.uris)
.execute()
const byUri = keyBy(res, 'uri')
const records = req.uris.map((uri) => {
const row = byUri[uri]
const json = row ? row.json : JSON.stringify(null)
return ui8.fromString(json, 'utf8')
})
return { records }
const counts = req.uris.map((uri) => byUri[uri]?.replyCount ?? 0)
return { counts }
},
async getPostReplyCount(req) {
if (req.uris.length === 0) {
async getPostCounts(req) {
if (req.dids.length === 0) {
return { counts: [] }
}
const res = await db.db
.selectFrom('post_agg')
.select(['uri', 'replyCount'])
.where('uri', 'in', req.uris)
.selectFrom('profile_agg')
.selectAll()
.where('did', 'in', req.dids)
.execute()
const byUri = keyBy(res, 'uri')
const counts = req.uris.map((uri) => byUri[uri]?.replyCount ?? 0)
const byDid = keyBy(res, 'did')
const counts = req.dids.map((did) => byDid[did]?.postsCount ?? 0)
return { counts }
},
})
47 changes: 17 additions & 30 deletions packages/bsky/src/data-plane/server/routes/profile.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,30 @@
import { ServiceImpl } from '@connectrpc/connect'
import { Service } from '../../gen/bsky_connect'
import { keyBy } from '@atproto/common'
import * as ui8 from 'uint8arrays'
import { Database } from '../../../db'
import { getRecords } from './records'

export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
async getProfiles(req) {
async getActors(req) {
const { dids } = req
if (dids.length === 0) {
return { records: [] }
return { actors: [] }
}
const uris = dids.map((did) => `at://${did}/app.bsky.actor.profile/self`)
const res = await db.db
.selectFrom('record')
.selectAll()
.where('uri', 'in', uris)
.execute()
const byUri = keyBy(res, 'uri')
const records = uris.map((uri) => {
const row = byUri[uri]
const json = row ? row.json : JSON.stringify(null)
return ui8.fromString(json, 'utf8')
const profileUris = dids.map(
(did) => `at://${did}/app.bsky.actor.profile/self`,
)
const [handlesRes, profiles] = await Promise.all([
db.db.selectFrom('actor').where('did', 'in', dids).selectAll().execute(),
getRecords(db)({ uris: profileUris }),
])
const byDid = keyBy(handlesRes, 'did')
const actors = dids.map((did, i) => {
return {
handle: byDid[did]?.handle ?? undefined,
profile: profiles[i],
}
})
return { records }
},

async getHandles(req) {
const { dids } = req
if (dids.length === 0) {
return { handles: [] }
}
const res = await db.db
.selectFrom('actor')
.where('did', 'in', dids)
.selectAll()
.execute()
const byDid = keyBy(res, 'did')
const handles = dids.map((did) => byDid[did]?.handle ?? '')
return { handles }
return { actors }
},

async getDidsByHandles(req) {
Expand Down
2 changes: 1 addition & 1 deletion packages/bsky/src/data-plane/server/routes/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
getThreadGateRecords: getRecords(db),
})

const getRecords =
export const getRecords =
(db: Database) =>
async (req: { uris: string[] }): Promise<{ records: Record[] }> => {
if (req.uris.length === 0) {
Expand Down
Loading

0 comments on commit a99f28f

Please sign in to comment.