-
Notifications
You must be signed in to change notification settings - Fork 643
/
Copy pathgraph.ts
251 lines (219 loc) · 7.81 KB
/
graph.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { Record as FollowRecord } from '../lexicon/types/app/bsky/graph/follow'
import { Record as BlockRecord } from '../lexicon/types/app/bsky/graph/block'
import { Record as StarterPackRecord } from '../lexicon/types/app/bsky/graph/starterpack'
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, ItemRef, RecordInfo, parseRecord } from './util'
import { FollowInfo } from '../proto/bsky_pb'
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
viewerInList?: string
}
export type ListViewerStates = HydrationMap<ListViewerState>
export type Follow = RecordInfo<FollowRecord>
export type Follows = HydrationMap<Follow>
export type Block = RecordInfo<BlockRecord>
export type StarterPack = RecordInfo<StarterPackRecord>
export type StarterPacks = HydrationMap<StarterPack>
export type StarterPackAgg = {
joinedWeek: number
joinedAllTime: number
listItemSampleUris?: string[] // gets set during starter pack hydration (not for basic view)
}
export type StarterPackAggs = HydrationMap<StarterPackAgg>
export type ListAgg = {
listItems: number
}
export type ListAggs = HydrationMap<ListAgg>
export type RelationshipPair = [didA: string, didB: string]
const dedupePairs = (pairs: RelationshipPair[]): RelationshipPair[] => {
const mapped = pairs.reduce(
(acc, cur) => {
const sorted = ([...cur] as RelationshipPair).sort()
acc[sorted.join('-')] = sorted
return acc
},
{} as Record<string, RelationshipPair>,
)
return Object.values(mapped)
}
export class Blocks {
_blocks: Map<string, boolean> = new Map()
constructor() {}
static key(didA: string, didB: string): string {
return [didA, didB].sort().join(',')
}
set(didA: string, didB: string, exists: boolean): Blocks {
const key = Blocks.key(didA, didB)
this._blocks.set(key, exists)
return this
}
has(didA: string, didB: string): boolean {
const key = Blocks.key(didA, didB)
return this._blocks.has(key)
}
isBlocked(didA: string, didB: string): boolean {
if (didA === didB) return false // ignore self-blocks
const key = Blocks.key(didA, didB)
return this._blocks.get(key) ?? false
}
merge(blocks: Blocks): Blocks {
blocks._blocks.forEach((exists, key) => {
this._blocks.set(key, exists)
})
return this
}
}
export class GraphHydrator {
constructor(public dataplane: DataPlaneClient) {}
async getLists(uris: string[], includeTakedowns = false): Promise<Lists> {
if (!uris.length) return new HydrationMap<List>()
const res = await this.dataplane.getListRecords({ uris })
return uris.reduce((acc, uri, i) => {
const record = parseRecord<ListRecord>(res.records[i], includeTakedowns)
return acc.set(uri, record ?? null)
}, new HydrationMap<List>())
}
async getListItems(
uris: string[],
includeTakedowns = false,
): Promise<ListItems> {
if (!uris.length) return new HydrationMap<ListItem>()
const res = await this.dataplane.getListItemRecords({ uris })
return uris.reduce((acc, uri, i) => {
const record = parseRecord<ListItemRecord>(
res.records[i],
includeTakedowns,
)
return acc.set(uri, record ?? null)
}, new HydrationMap<ListItem>())
}
async getListViewerStates(
uris: string[],
viewer: string,
): Promise<ListViewerStates> {
if (!uris.length) return new HydrationMap<ListViewerState>()
const mutesAndBlocks = await Promise.all(
uris.map((uri) => this.getMutesAndBlocks(uri, viewer)),
)
const listMemberships = await this.dataplane.getListMembership({
actorDid: viewer,
listUris: uris,
})
return uris.reduce((acc, uri, i) => {
return acc.set(uri, {
viewerMuted: mutesAndBlocks[i].muted ? uri : undefined,
viewerListBlockUri: mutesAndBlocks[i].listBlockUri || undefined,
viewerInList: listMemberships.listitemUris[i],
})
}, new HydrationMap<ListViewerState>())
}
private async getMutesAndBlocks(uri: string, viewer: string) {
const [muted, listBlockUri] = await Promise.all([
this.dataplane.getMutelistSubscription({
actorDid: viewer,
listUri: uri,
}),
this.dataplane.getBlocklistSubscription({
actorDid: viewer,
listUri: uri,
}),
])
return {
muted: muted.subscribed,
listBlockUri: listBlockUri.listblockUri,
}
}
async getBidirectionalBlocks(pairs: RelationshipPair[]): Promise<Blocks> {
if (!pairs.length) return new Blocks()
const deduped = dedupePairs(pairs).map(([a, b]) => ({ a, b }))
const res = await this.dataplane.getBlockExistence({ pairs: deduped })
const blocks = new Blocks()
for (let i = 0; i < deduped.length; i++) {
const pair = deduped[i]
blocks.set(pair.a, pair.b, res.exists[i] ?? false)
}
return blocks
}
async getFollows(uris: string[], includeTakedowns = false): Promise<Follows> {
if (!uris.length) return new HydrationMap<Follow>()
const res = await this.dataplane.getFollowRecords({ uris })
return uris.reduce((acc, uri, i) => {
const record = parseRecord<FollowRecord>(res.records[i], includeTakedowns)
return acc.set(uri, record ?? null)
}, new HydrationMap<Follow>())
}
async getBlocks(uris: string[], includeTakedowns = false): Promise<Follows> {
if (!uris.length) return new HydrationMap<Block>()
const res = await this.dataplane.getBlockRecords({ uris })
return uris.reduce((acc, uri, i) => {
const record = parseRecord<BlockRecord>(res.records[i], includeTakedowns)
return acc.set(uri, record ?? null)
}, new HydrationMap<Block>())
}
async getActorFollows(input: {
did: string
cursor?: string
limit?: number
}): Promise<{ follows: FollowInfo[]; cursor: string }> {
const { did, cursor, limit } = input
const res = await this.dataplane.getFollows({
actorDid: did,
cursor,
limit,
})
return { follows: res.follows, cursor: res.cursor }
}
async getActorFollowers(input: {
did: string
cursor?: string
limit?: number
}): Promise<{ followers: FollowInfo[]; cursor: string }> {
const { did, cursor, limit } = input
const res = await this.dataplane.getFollowers({
actorDid: did,
cursor,
limit,
})
return { followers: res.followers, cursor: res.cursor }
}
async getStarterPacks(
uris: string[],
includeTakedowns = false,
): Promise<StarterPacks> {
if (!uris.length) return new HydrationMap<StarterPack>()
const res = await this.dataplane.getStarterPackRecords({ uris })
return uris.reduce((acc, uri, i) => {
const record = parseRecord<StarterPackRecord>(
res.records[i],
includeTakedowns,
)
return acc.set(uri, record ?? null)
}, new HydrationMap<StarterPack>())
}
async getStarterPackAggregates(refs: ItemRef[]) {
if (!refs.length) return new HydrationMap<StarterPackAgg>()
const counts = await this.dataplane.getStarterPackCounts({ refs })
return refs.reduce((acc, { uri }, i) => {
return acc.set(uri, {
joinedWeek: counts.joinedWeek[i] ?? 0,
joinedAllTime: counts.joinedAllTime[i] ?? 0,
})
}, new HydrationMap<StarterPackAgg>())
}
async getListAggregates(refs: ItemRef[]) {
if (!refs.length) return new HydrationMap<ListAgg>()
const counts = await this.dataplane.getListCounts({ refs })
return refs.reduce((acc, { uri }, i) => {
return acc.set(uri, {
listItems: counts.listItems[i] ?? 0,
})
}, new HydrationMap<ListAgg>())
}
}