Skip to content

Commit

Permalink
hydrate list items and likes
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Dec 7, 2023
1 parent eee2864 commit 4cd3c27
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
9 changes: 9 additions & 0 deletions packages/bsky/src/hydration/feed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DataPlaneClient } from '../data-plane/client'
import { Record as PostRecord } from '../lexicon/types/app/bsky/feed/post'
import { Record as LikeRecord } from '../lexicon/types/app/bsky/feed/like'
import { Record as FeedGenRecord } from '../lexicon/types/app/bsky/feed/generator'
import { Record as ThreadgateRecord } from '../lexicon/types/app/bsky/feed/threadgate'
import { HydrationMap } from './util'
Expand All @@ -21,6 +22,9 @@ export type PostAgg = {

export type PostAggs = HydrationMap<PostAgg>

export type Like = LikeRecord
export type Likes = HydrationMap<Like>

export type FeedGenAgg = {
likes: number
}
Expand Down Expand Up @@ -75,4 +79,9 @@ export class FeedHydrator {
async getThreadgatesForPosts(postUris: string[]): Promise<Threadgates> {
throw new Error('unimplemented')
}

// @TODO may not be supported yet by data plane
async getLikes(uris: string[]): Promise<Likes> {
throw new Error('unimplemented')
}
}
9 changes: 9 additions & 0 deletions packages/bsky/src/hydration/graph.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Record as ListRecord } from '../lexicon/types/app/bsky/graph/list'
import { Record as ListItemRecord } from '../lexicon/types/app/bsky/graph/listitem'
import { DataPlaneClient } from '../data-plane/client'
import { HydrationMap, RecordInfo, parseRecord } from './util'

export type List = RecordInfo<ListRecord>
export type Lists = HydrationMap<List>

export type ListItem = RecordInfo<ListItemRecord>
export type ListItems = HydrationMap<ListItem>

export type ListViewerState = {
viewerMuted?: string
viewerListBlockUri?: string
Expand Down Expand Up @@ -38,6 +42,11 @@ export class GraphHydrator {
}, new HydrationMap<List>())
}

// @TODO may not be supported yet by data plane
async getListItems(uris: string[]): Promise<ListItems> {
throw new Error('unimplemented')
}

async getListViewerStates(
uris: string[],
viewer: string,
Expand Down
58 changes: 53 additions & 5 deletions packages/bsky/src/hydration/hydrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import {
ProfileViewerStates,
ProfileViewerState,
} from './actor'
import { GraphHydrator, ListViewerStates, Lists } from './graph'
import { GraphHydrator, ListItems, ListViewerStates, Lists } from './graph'
import { LabelHydrator, Labels } from './label'
import { HydrationMap } from './util'
import {
FeedGenAggs,
FeedGens,
FeedGenViewerStates,
FeedHydrator,
Likes,
} from './feed'

export type HydrationState = {
Expand All @@ -24,6 +25,8 @@ export type HydrationState = {
profileAggs?: ProfileAggs
lists?: Lists
listViewers?: ListViewerStates
listItems?: ListItems
likes?: Likes
labels?: Labels
feedgens?: FeedGens
feedgenViewers?: FeedGenViewerStates
Expand Down Expand Up @@ -123,26 +126,57 @@ export class Hydrator {
return { lists, listViewers }
}

// app.bsky.graph.defs#listItemView
// - list item
// - profile
// - list basic
async hydrateListItems(
uris: string[],
viewer: string | null,
): Promise<HydrationState> {
const listItems = await this.graph.getListItems(uris)
const dids: string[] = []
listItems.forEach((item) => {
if (item) {
dids.push(item.record.subject)
}
})
const profileState = await this.hydrateProfiles(dids, viewer)
return mergeStates(profileState, { listItems })
}

// app.bsky.feed.defs#postView
async hydratePosts(uris: string[]): Promise<HydrationState> {
async hydratePosts(
uris: string[],
viewer: string | null,
): Promise<HydrationState> {
throw new Error('not implemented')
}

// app.bsky.feed.defs#feedViewPost
async hydrateFeedPosts(uris: string[]): Promise<HydrationState> {
async hydrateFeedPosts(
uris: string[],
viewer: string | null,
): Promise<HydrationState> {
throw new Error('not implemented')
}

// app.bsky.feed.defs#threadViewPost
async hydrateThreadPosts(uris: string[]): Promise<HydrationState> {
async hydrateThreadPosts(
uris: string[],
viewer: string | null,
): Promise<HydrationState> {
throw new Error('not implemented')
}

// app.bsky.feed.defs#generatorView
// - feedgen
// - profile
// - list basic
async hydrateFeedGens(uris: string[], viewer): Promise<HydrationState> {
async hydrateFeedGens(
uris: string[],
viewer: string | null,
): Promise<HydrationState> {
const [feedgens, feedgenAggs, feedgenViewers, profileState] =
await Promise.all([
this.feed.getFeedGens(uris),
Expand All @@ -156,6 +190,18 @@ export class Hydrator {
feedgenViewers,
})
}

// app.bsky.feed.getLikes#like
// - like
// - profile
// - list basic
async hydrateLikes(uris: string[], viewer: string | null) {
const [likes, profileState] = await Promise.all([
this.feed.getLikes(uris),
this.hydrateProfiles(uris.map(didFromUri), viewer),
])
return mergeStates(profileState, { likes })
}
}

const listUrisFromProfileViewer = (item: ProfileViewerState | null) => {
Expand Down Expand Up @@ -190,6 +236,8 @@ const mergeStates = (
profileViewers: mergeMaps(stateA.profileViewers, stateB.profileViewers),
lists: mergeMaps(stateA.lists, stateB.lists),
listViewers: mergeMaps(stateA.listViewers, stateB.listViewers),
listItems: mergeMaps(stateA.listItems, stateB.listItems),
likes: mergeMaps(stateA.likes, stateB.likes),
labels: mergeMaps(stateA.labels, stateB.labels),
feedgens: mergeMaps(stateA.feedgens, stateB.feedgens),
feedgenAggs: mergeMaps(stateA.feedgenAggs, stateB.feedgenAggs),
Expand Down

0 comments on commit 4cd3c27

Please sign in to comment.