This repository has been archived by the owner on May 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeeds-lookup.js
336 lines (300 loc) · 8.96 KB
/
feeds-lookup.js
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// SPDX-FileCopyrightText: 2021 Anders Rune Jensen
//
// SPDX-License-Identifier: LGPL-3.0-only
const { seekKey } = require('bipf')
const pull = require('pull-stream')
const cat = require('pull-cat')
const Notify = require('pull-notify')
const SSBURI = require('ssb-uri2')
const DeferredPromise = require('p-defer')
const {
where,
and,
isPublic,
live,
equal,
authorIsBendyButtV1,
toPullStream,
toCallback,
} = require('ssb-db2/operators')
const validate = require('./validate')
const SUBFEED_PREFIX_OFFSET = Math.max(
'@'.length,
'ssb:feed/bendybutt-v1/'.length,
'ssb:feed/gabbygrove-v1/'.length
)
const B_VALUE = Buffer.from('value')
const B_CONTENT = Buffer.from('content')
const B_SUBFEED = Buffer.from('subfeed')
function seekSubfeed(buffer) {
let p = 0 // note you pass in p!
p = seekKey(buffer, p, B_VALUE)
if (p < 0) return
p = seekKey(buffer, p, B_CONTENT)
if (p < 0) return
return seekKey(buffer, p, B_SUBFEED)
}
function subfeed(feedId) {
return equal(seekSubfeed, feedId, {
prefix: 32,
prefixOffset: SUBFEED_PREFIX_OFFSET,
indexType: 'value_content_subfeed',
})
}
exports.init = function (sbot, config) {
let stateLoaded = false
const stateLoadedP = DeferredPromise()
let loadStateRequested = false
let liveDrainer = null
let notifyNewBranch = null
const detailsLookup = new Map() // feedId => details
const childrenLookup = new Map() // feedId => Set<FeedID>
const roots = new Set()
const ensureQueue = {
_map: new Map(), // feedId => Array<Callback>
add(feedId, cb) {
if (this._map.has(feedId)) this._map.get(feedId).push(cb)
else this._map.set(feedId, [cb])
},
flush(feedId) {
const queue = this._map.get(feedId)
this._map.delete(feedId)
while (queue && queue.length > 0) {
const cb = queue.shift()
stateLoadedP.promise.then(cb)
}
},
}
function assertFeedId(feedId) {
if (!feedId) {
throw new Error('feedId should be provided')
}
if (typeof feedId !== 'string') {
throw new Error('feedId should be a string, but got ' + feedId)
}
}
function detectFeedFormat(feedId) {
if (feedId.startsWith('@')) {
return 'classic'
} else if (SSBURI.isBendyButtV1FeedSSBURI(feedId)) {
return 'bendybutt-v1'
} else if (SSBURI.isGabbyGroveV1FeedSSBURI(feedId)) {
return 'gabbygrove-v1'
} else {
console.warn('Unknown feed format: ' + feedId)
return null
}
}
function msgToDetails(prevDetails, msg) {
const content = msg.value.content
const details = { ...prevDetails }
details.feedformat = detectFeedFormat(content.subfeed)
details.feedpurpose = content.feedpurpose || details.feedpurpose
details.metafeed = content.metafeed || details.metafeed
details.metadata = {} || details.metafeed
const NOT_METADATA = [
'metafeed',
'feedpurpose',
'type',
'tangles',
'reason',
'subfeed',
'nonce',
]
const keys = Object.keys(content).filter((k) => !NOT_METADATA.includes(k))
for (const key of keys) {
details.metadata[key] = content[key]
}
if (content.type === 'metafeed/tombstone') {
details.tombstoned = true
details.reason = content.reason
}
return details
}
function updateLookup(msg) {
const { type, subfeed, metafeed } = msg.value.content
// Update roots
if (!detailsLookup.has(metafeed)) {
detailsLookup.set(metafeed, null)
roots.add(metafeed)
}
// Update children
if (type.startsWith('metafeed/add/')) {
if (childrenLookup.has(metafeed)) {
const subfeeds = childrenLookup.get(metafeed)
subfeeds.add(subfeed)
} else {
const subfeeds = new Set()
subfeeds.add(subfeed)
childrenLookup.set(metafeed, subfeeds)
}
}
// Update details
const details = msgToDetails(detailsLookup.get(subfeed), msg)
detailsLookup.set(subfeed, details)
roots.delete(subfeed)
ensureQueue.flush(subfeed)
if (notifyNewBranch) notifyNewBranch(makeBranch(subfeed))
}
function loadState() {
loadStateRequested = true
notifyNewBranch = Notify()
pull(
sbot.db.query(
where(and(authorIsBendyButtV1(), isPublic())),
toPullStream()
),
pull.filter((msg) => validate.isValid(msg)),
pull.drain(updateLookup, (err) => {
if (err) return console.error(err)
stateLoaded = true
stateLoadedP.resolve()
sbot.close.hook(function (fn, args) {
if (liveDrainer) liveDrainer.abort(true)
if (notifyNewBranch) notifyNewBranch.abort(true)
fn.apply(this, args)
})
pull(
sbot.db.query(
where(and(authorIsBendyButtV1(), isPublic())),
live(),
toPullStream()
),
pull.filter((msg) => validate.isValid(msg)),
(liveDrainer = pull.drain(updateLookup))
)
})
)
}
function makeBranch(subfeed) {
const details = detailsLookup.get(subfeed)
const branch = [[subfeed, details]]
while (branch[0][1]) {
const metafeedId = branch[0][1].metafeed
const details = detailsLookup.get(metafeedId) || null
branch.unshift([metafeedId, details])
}
return branch
}
function traverseBranchesUnder(feedId, previousBranch, visit) {
const details = detailsLookup.get(feedId) || null
const branch = [...previousBranch, [feedId, details]]
visit(branch)
if (childrenLookup.has(feedId)) {
for (const childFeedId of childrenLookup.get(feedId)) {
traverseBranchesUnder(childFeedId, branch, visit)
}
}
}
function branchStreamOld(rootMetafeedId) {
const branches = []
if (rootMetafeedId) {
traverseBranchesUnder(rootMetafeedId, [], (branch) => {
branches.push(branch)
})
} else {
for (const rootMetafeedId of roots) {
traverseBranchesUnder(rootMetafeedId, [], (branch) => {
branches.push(branch)
})
}
}
return pull.values(branches)
}
function branchStreamLive(rootMetafeedId) {
if (rootMetafeedId) {
return pull(
notifyNewBranch.listen(),
pull.map(function cutBranch(branch) {
const idx = branch.findIndex(([feedId]) => feedId === rootMetafeedId)
if (idx < 0) return []
else if (idx === 0) return branch
else return branch.slice(idx)
}),
pull.filter(function hasRoot(branch) {
return branch.length > 0 && branch[0][0] === rootMetafeedId
})
)
} else {
return notifyNewBranch.listen()
}
}
return {
loadState(cb) {
if (!loadStateRequested) loadState()
if (cb) stateLoadedP.promise.then(cb)
},
ensureLoaded(feedId, cb) {
if (!loadStateRequested) loadState()
if (detailsLookup.has(feedId)) cb()
else ensureQueue.add(feedId, cb)
},
findByIdSync(feedId) {
if (!stateLoaded) {
throw new Error('Please call loadState() before using findByIdSync()')
}
assertFeedId(feedId)
return detailsLookup.get(feedId)
},
findById(feedId, cb) {
try {
assertFeedId(feedId)
detectFeedFormat(feedId)
} catch (err) {
return cb(err)
}
sbot.db.query(
where(subfeed(feedId)),
toCallback((err, msgs) => {
if (err) return cb(err)
msgs = msgs.filter((msg) => validate.isValid(msg))
if (msgs.find((m) => m.value.content.type === 'metafeed/tombstone')) {
return cb(null, null)
}
msgs = msgs.filter((m) =>
m.value.content.type.startsWith('metafeed/add/')
)
if (msgs.length === 0) {
return cb(null, null)
}
const details = msgToDetails(undefined, msgs[0])
cb(null, details)
})
)
},
branchStream(opts) {
if (!loadStateRequested) loadState()
const {
live = true,
old = false,
root = null,
tombstoned = null,
} = opts || {}
const filterTombstoneOrNot = (branch) => {
const [, leafDetails] = branch[branch.length - 1]
if (tombstoned === null) {
// Anything goes
return true
} else if (tombstoned === false) {
// All nodes in the branch must be non-tombstoned
return branch.every(([, details]) => !details || !details.tombstoned)
} else if (tombstoned === true) {
// The leaf must be tombstoned for this branch to be interesting to us
return leafDetails && !!leafDetails.tombstoned
}
}
if (old && live) {
return pull(
cat([branchStreamOld(root), branchStreamLive(root)]),
pull.filter(filterTombstoneOrNot)
)
} else if (old) {
return pull(branchStreamOld(root), pull.filter(filterTombstoneOrNot))
} else if (live) {
return pull(branchStreamLive(root), pull.filter(filterTombstoneOrNot))
} else {
return pull.empty()
}
},
}
}