Skip to content

Commit

Permalink
hydrate profile labels, detail lists
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Dec 6, 2023
1 parent b4e6e87 commit 90e7930
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
75 changes: 61 additions & 14 deletions packages/bsky/src/hydration/hydrator.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import { AtUri } from '@atproto/syntax'
import { DataPlaneClient } from '../data-plane/client'
import { ids } from '../lexicon/lexicons'
import {
ActorHydrator,
ProfileAggs,
ProfileInfos,
Profiles,
ProfileViewerStates,
} from './actor'
import { GraphHydrator, ListInfos, ListViewerStates } from './graph'
import { GraphHydrator, ListViewerStates, Lists } from './graph'
import { LabelHydrator, Labels } from './label'
import { HydrationMap } from './util'

export type HydrationState = {
profiles?: ProfileInfos
profiles?: Profiles
profileViewers?: ProfileViewerStates
profileAggs?: ProfileAggs
lists?: ListInfos
lists?: Lists
listViewers?: ListViewerStates
labels?: Labels
}

export class Hydrator {
actor: ActorHydrator
graph: GraphHydrator
label: LabelHydrator

constructor(public dataplane: DataPlaneClient) {
this.actor = new ActorHydrator(dataplane)
this.graph = new GraphHydrator(dataplane)
this.label = new LabelHydrator(dataplane)
}

// app.bsky.actor.defs#profileView
Expand All @@ -33,22 +40,24 @@ export class Hydrator {
viewer: string | null,
): Promise<HydrationState> {
const state: HydrationState = {}
const [profiles, profileViewers] = await Promise.all([
const [profiles, labels, profileViewers] = await Promise.all([
this.actor.getProfiles(dids),
this.label.getLabelsForSubjects(labelSubjectsForDid(dids)),
viewer ? this.actor.getProfileViewerStates(dids, viewer) : null,
])
state.profiles = profiles
state.labels = labels
if (profileViewers) {
state.profileViewers = profileViewers
const listUris = Object.values(profileViewers).reduce((acc, cur) => {
if (cur?.mutedByList) {
acc.push(cur.mutedByList)
const listUris: string[] = []
profileViewers.forEach((item) => {
if (item?.mutedByList) {
listUris.push(item.mutedByList)
}
if (cur?.blockingByList) {
acc.push(cur.blockingByList)
if (item?.blockingByList) {
listUris.push(item.blockingByList)
}
return acc
}, [] as string[])
})
const listState = await this.hydrateListsBasic(listUris, viewer)
state.lists = listState.lists
state.listViewers = listState.listViewers
Expand Down Expand Up @@ -91,8 +100,15 @@ export class Hydrator {
}

// app.bsky.graph.defs#listView
async hydrateLists(uris: string[]): Promise<HydrationState> {
throw new Error('not implemented')
async hydrateLists(
uris: string[],
viewer: string | null,
): Promise<HydrationState> {
const [listsState, profilesState] = await Promise.all([
await this.hydrateListsBasic(uris, viewer),
await this.hydrateProfilesBasic(uris.map(didFromUri), viewer),
])
return mergeHydrationStates(listsState, profilesState)
}

// app.bsky.graph.defs#listViewBasic
Expand Down Expand Up @@ -137,3 +153,34 @@ export class Hydrator {
throw new Error('not implemented')
}
}

const labelSubjectsForDid = (dids: string[]) => {
return [
...dids,
...dids.map((did) => `at://${did}/${ids.AppBskyActorProfile}/self`),
]
}

const didFromUri = (uri: string) => {
return new AtUri(uri).hostname
}

const mergeHydrationStates = (
stateA: HydrationState,
stateB: HydrationState,
): HydrationState => {
return {
labels: mergeMaps(stateA.labels, stateB.labels),
listViewers: mergeMaps(stateA.listViewers, stateB.listViewers),
lists: mergeMaps(stateA.lists, stateB.lists),
profileAggs: mergeMaps(stateA.profileAggs, stateB.profileAggs),
profileViewers: mergeMaps(stateA.profileViewers, stateB.profileViewers),
profiles: mergeMaps(stateA.profiles, stateB.profiles),
}
}

const mergeMaps = <T>(mapA?: HydrationMap<T>, mapB?: HydrationMap<T>) => {
if (!mapA) return mapB
if (!mapB) return mapA
return mapA.merge(mapB)
}
2 changes: 1 addition & 1 deletion packages/bsky/src/hydration/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type { Label } from '../lexicon/types/com/atproto/label/defs'

export type Labels = HydrationMap<Label>

export class ActorHydrator {
export class LabelHydrator {
constructor(public dataplane: DataPlaneClient) {}

async getLabelsForSubjects(subjects: string[]): Promise<Labels> {
Expand Down

0 comments on commit 90e7930

Please sign in to comment.