Skip to content

Commit

Permalink
[SH] fixes base (#3445)
Browse files Browse the repository at this point in the history
Co-authored-by: Eric Bailey <[email protected]>
  • Loading branch information
rafaelbsky and estrattonbailey authored Feb 4, 2025
1 parent 8f88623 commit 8a30e0e
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-windows-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atproto/common-web": minor
---

Make keyBy typing stricter
5 changes: 5 additions & 0 deletions .changeset/rich-rocks-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atproto/bsky": patch
---

Replace undefined values with empty strings in mock dataplane
2 changes: 1 addition & 1 deletion packages/bsky/src/data-plane/server/routes/follows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
.selectAll()
.execute()
const bySubject = keyBy(res, 'subjectDid')
const uris = targetDids.map((did) => bySubject[did]?.uri ?? '')
const uris = targetDids.map((did) => bySubject.get(did)?.uri ?? '')
return {
uris,
}
Expand Down
24 changes: 13 additions & 11 deletions packages/bsky/src/data-plane/server/routes/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
.execute()
const byUri = keyBy(res, 'uri')
return {
likes: uris.map((uri) => byUri[uri]?.likeCount ?? 0),
replies: uris.map((uri) => byUri[uri]?.replyCount ?? 0),
reposts: uris.map((uri) => byUri[uri]?.repostCount ?? 0),
quotes: uris.map((uri) => byUri[uri]?.quoteCount ?? 0),
likes: uris.map((uri) => byUri.get(uri)?.likeCount ?? 0),
replies: uris.map((uri) => byUri.get(uri)?.replyCount ?? 0),
reposts: uris.map((uri) => byUri.get(uri)?.repostCount ?? 0),
quotes: uris.map((uri) => byUri.get(uri)?.quoteCount ?? 0),
}
},
async getCountsForUsers(req) {
Expand Down Expand Up @@ -52,12 +52,14 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
.execute()
const byDid = keyBy(res, 'did')
return {
followers: req.dids.map((uri) => byDid[uri]?.followersCount ?? 0),
following: req.dids.map((uri) => byDid[uri]?.followsCount ?? 0),
posts: req.dids.map((uri) => byDid[uri]?.postsCount ?? 0),
lists: req.dids.map((uri) => byDid[uri]?.listsCount ?? 0),
feeds: req.dids.map((uri) => byDid[uri]?.feedGensCount ?? 0),
starterPacks: req.dids.map((uri) => byDid[uri]?.starterPacksCount ?? 0),
followers: req.dids.map((uri) => byDid.get(uri)?.followersCount ?? 0),
following: req.dids.map((uri) => byDid.get(uri)?.followsCount ?? 0),
posts: req.dids.map((uri) => byDid.get(uri)?.postsCount ?? 0),
lists: req.dids.map((uri) => byDid.get(uri)?.listsCount ?? 0),
feeds: req.dids.map((uri) => byDid.get(uri)?.feedGensCount ?? 0),
starterPacks: req.dids.map(
(uri) => byDid.get(uri)?.starterPacksCount ?? 0,
),
}
},
async getStarterPackCounts(req) {
Expand Down Expand Up @@ -105,7 +107,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
.execute()
const countsByUri = keyBy(countsListItems, 'uri')
return {
listItems: uris.map((uri) => countsByUri[uri]?.count ?? 0),
listItems: uris.map((uri) => countsByUri.get(uri)?.count ?? 0),
}
},
})
3 changes: 1 addition & 2 deletions packages/bsky/src/data-plane/server/routes/likes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
.selectAll()
.execute()
const bySubject = keyBy(res, 'subject')
// @TODO handling undefineds properly, or do we need to turn them into empty strings?
const uris = refs.map(({ uri }) => bySubject[uri]?.uri)
const uris = refs.map(({ uri }) => bySubject.get(uri)?.uri ?? '')
return { uris }
},

Expand Down
2 changes: 1 addition & 1 deletion packages/bsky/src/data-plane/server/routes/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
.selectAll()
.execute()
const byListUri = keyBy(res, 'listUri')
const listitemUris = listUris.map((uri) => byListUri[uri]?.uri ?? '')
const listitemUris = listUris.map((uri) => byListUri.get(uri)?.uri ?? '')
return {
listitemUris,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/bsky/src/data-plane/server/routes/mutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
.where('rootUri', 'in', threadRoots)
.execute()
const byRootUri = keyBy(res, 'rootUri')
const muted = threadRoots.map((uri) => !!byRootUri[uri])
const muted = threadRoots.map((uri) => !!byRootUri.get(uri))
return { muted }
},
})
Expand Down
2 changes: 1 addition & 1 deletion packages/bsky/src/data-plane/server/routes/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
.where('uri', 'in', uris)
.execute()
const byUri = keyBy(res, 'uri')
const counts = uris.map((uri) => byUri[uri]?.replyCount ?? 0)
const counts = uris.map((uri) => byUri.get(uri)?.replyCount ?? 0)
return { counts }
},
})
4 changes: 2 additions & 2 deletions packages/bsky/src/data-plane/server/routes/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
])
const byDid = keyBy(handlesRes, 'did')
const actors = dids.map((did, i) => {
const row = byDid[did]
const row = byDid.get(did)
const chatDeclaration = parseRecordBytes(
chatDeclarations.records[i].record,
)
Expand Down Expand Up @@ -74,7 +74,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
.selectAll()
.execute()
const byHandle = keyBy(res, 'handle')
const dids = handles.map((handle) => byHandle[handle]?.did ?? '')
const dids = handles.map((handle) => byHandle.get(handle)?.did ?? '')
return { dids }
},

Expand Down
10 changes: 5 additions & 5 deletions packages/bsky/src/data-plane/server/routes/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const getRecords =
: []
const byUri = keyBy(res, 'uri')
const records: Record[] = req.uris.map((uri) => {
const row = byUri[uri]
const row = byUri.get(uri)
const json = row ? row.json : JSON.stringify(null)
const createdAtRaw = new Date(JSON.parse(json)?.['createdAt'])
const createdAt = !isNaN(createdAtRaw.getTime())
Expand Down Expand Up @@ -88,10 +88,10 @@ export const getPostRecords = (db: Database) => {
const byKey = keyBy(details, 'uri')
const meta = req.uris.map((uri) => {
return new PostRecordMeta({
violatesThreadGate: !!byKey[uri]?.violatesThreadGate,
violatesEmbeddingRules: !!byKey[uri]?.violatesEmbeddingRules,
hasThreadGate: !!byKey[uri]?.hasThreadGate,
hasPostGate: !!byKey[uri]?.hasPostGate,
violatesThreadGate: !!byKey.get(uri)?.violatesThreadGate,
violatesEmbeddingRules: !!byKey.get(uri)?.violatesEmbeddingRules,
hasThreadGate: !!byKey.get(uri)?.hasThreadGate,
hasPostGate: !!byKey.get(uri)?.hasPostGate,
})
})
return { records, meta }
Expand Down
18 changes: 9 additions & 9 deletions packages/bsky/src/data-plane/server/routes/relationships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
.execute()
const byDid = keyBy(res, 'did')
const relationships = targetDids.map((did) => {
const row = byDid[did] ?? {}
const row = byDid.get(did)
return {
muted: row.muted ?? false,
mutedByList: row.mutedByList ?? '',
blockedBy: row.blockedBy ?? '',
blocking: row.blocking ?? '',
blockedByList: row.blockedByList ?? '',
blockingByList: row.blockingByList ?? '',
following: row.following ?? '',
followedBy: row.followedBy ?? '',
muted: row?.muted ?? false,
mutedByList: row?.mutedByList ?? '',
blockedBy: row?.blockedBy ?? '',
blocking: row?.blocking ?? '',
blockedByList: row?.blockedByList ?? '',
blockingByList: row?.blockingByList ?? '',
following: row?.following ?? '',
followedBy: row?.followedBy ?? '',
}
})
return { relationships }
Expand Down
3 changes: 1 addition & 2 deletions packages/bsky/src/data-plane/server/routes/reposts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
.selectAll()
.execute()
const bySubject = keyBy(res, 'subject')
// @TODO handling undefineds properly, or do we need to turn them into empty strings?
const uris = refs.map(({ uri }) => bySubject[uri]?.uri)
const uris = refs.map(({ uri }) => bySubject.get(uri)?.uri ?? '')
return { uris }
},

Expand Down
16 changes: 8 additions & 8 deletions packages/common-web/src/arrays.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export const keyBy = <T>(arr: T[], key: string): Record<string, T> => {
return arr.reduce(
(acc, cur) => {
acc[cur[key]] = cur
return acc
},
{} as Record<string, T>,
)
export function keyBy<T, K extends keyof T>(
arr: readonly T[],
key: K,
): Map<T[K], T> {
return arr.reduce((acc, cur) => {
acc.set(cur[key], cur)
return acc
}, new Map<T[K], T>())
}

export const mapDefined = <T, S>(
Expand Down

0 comments on commit 8a30e0e

Please sign in to comment.