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 pathapi.js
267 lines (237 loc) · 7.7 KB
/
api.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
// SPDX-FileCopyrightText: 2021 Anders Rune Jensen
//
// SPDX-License-Identifier: LGPL-3.0-only
const run = require('promisify-tuple')
const debug = require('debug')('ssb:meta-feeds')
const validate = require('./validate')
const alwaysTrue = () => true
exports.init = function (sbot, config) {
function filter(metafeed, visit, maybeCB) {
if (!metafeed) {
cb(new Error('expected metafeed argument'))
} else if (typeof metafeed === 'function') {
cb(new Error('expected metafeed argument and visit argument'))
} else {
const cb = maybeCB
sbot.metafeeds.query.hydrate(
metafeed.keys.id,
metafeed.seed,
(err, hydrated) => {
if (err) return cb(err)
if (visit === alwaysTrue) return cb(null, hydrated.feeds)
const filtered = hydrated.feeds.filter((feed) => visit(feed))
cb(null, filtered)
}
)
}
}
function find(metafeed, visit, maybeCB) {
if (!metafeed) {
cb(new Error('expected metafeed argument'))
} else if (typeof metafeed === 'function') {
cb(new Error('expected metafeed argument and visit argument'))
} else {
const cb = maybeCB
filter(metafeed, visit, (err, feeds) => {
if (err) return cb(err)
if (feeds.length === 0) return cb(null, null)
const found = feeds[0]
cb(null, found)
})
}
}
function findById(feedId, cb) {
sbot.metafeeds.lookup.findById(feedId, cb)
}
function loadState(cb) {
sbot.metafeeds.lookup.loadState(cb)
}
function ensureLoaded(feedId, cb) {
sbot.metafeeds.lookup.ensureLoaded(feedId, cb)
}
function findByIdSync(feedId) {
return sbot.metafeeds.lookup.findByIdSync(feedId)
}
function branchStream(opts) {
return sbot.metafeeds.lookup.branchStream(opts)
}
function create(metafeed, details, maybeCB) {
if (!metafeed) {
const cb = maybeCB
getOrCreateRootMetafeed(cb)
} else if (typeof metafeed === 'function') {
const cb = metafeed
getOrCreateRootMetafeed(cb)
} else {
const cb = maybeCB
if (!details.feedpurpose) return cb(new Error('Missing feedpurpose'))
if (!details.feedformat) return cb(new Error('Missing feedformat'))
sbot.metafeeds.query.getLatest(metafeed.keys.id, (err, latest) => {
if (err) return cb(err)
const msgVal = sbot.metafeeds.messages.getMsgValAddDerived(
metafeed.keys,
latest,
details.feedpurpose,
metafeed.seed,
details.feedformat,
details.metadata
)
const encrypted = typeof msgVal.content === 'string'
if (!encrypted) {
const contentSection = [msgVal.content, msgVal.contentSignature]
const validationResult = validate.validateSingle(contentSection)
if (validationResult instanceof Error) return cb(validationResult)
}
sbot.db.add(msgVal, (err, addedMsg) => {
if (err) return cb(err)
if (encrypted)
sbot.db.get(addedMsg.key, (err, msgVal) => {
const msg = {
key: addedMsg.key,
value: msgVal,
}
cb(null, sbot.metafeeds.query.hydrateFromMsg(msg, metafeed.seed))
})
else {
cb(
null,
sbot.metafeeds.query.hydrateFromMsg(addedMsg, metafeed.seed)
)
}
})
})
}
}
function findOrCreate(metafeed, maybeVisit, details, maybeCB) {
if (!metafeed) {
const cb = maybeCB
getOrCreateRootMetafeed(cb)
} else if (typeof metafeed === 'function') {
const cb = metafeed
getOrCreateRootMetafeed(cb)
} else {
const cb = maybeCB
const visit = maybeVisit || alwaysTrue
find(metafeed, visit, (err, found) => {
if (err) return cb(err)
if (found) return cb(null, found)
create(metafeed, details, cb)
})
}
}
function findAndTombstone(metafeed, visit, reason, cb) {
const { getLatest } = sbot.metafeeds.query
const { getMsgValTombstone } = sbot.metafeeds.messages
find(metafeed, visit, (err, found) => {
if (err) return cb(err)
if (!found) return cb(new Error('Cannot find subfeed to tombstone'))
getLatest(metafeed.keys.id, (err, latest) => {
if (err) return cb(err)
getMsgValTombstone(
metafeed.keys,
latest,
found.keys,
reason,
(err, msgVal) => {
if (err) return cb(err)
sbot.db.add(msgVal, (err, msg) => {
if (err) return cb(err)
cb(null, true)
})
}
)
})
})
}
// lock to solve concurrent getOrCreateRootMetafeed
const rootMetaFeedLock = {
_cbs: [],
_cachedMF: null,
acquire(cb) {
if (this._cachedMF) {
cb(null, this._cachedMF)
return false
}
this._cbs.push(cb)
return this._cbs.length === 1
},
release(err, mf) {
this._cachedMF = mf
const cbs = this._cbs
this._cbs = []
for (const cb of cbs) cb(err, mf)
},
}
function getRoot(cb) {
sbot.metafeeds.query.getSeed((err, seed) => {
if (err) return cb(err)
if (!seed) return cb(null, null)
const metafeed = {
seed,
keys: sbot.metafeeds.keys.deriveRootMetaFeedKeyFromSeed(seed),
}
cb(null, metafeed)
})
}
async function getOrCreateRootMetafeed(cb) {
if (!rootMetaFeedLock.acquire(cb)) return
// Pluck relevant internal APIs
const { deriveRootMetaFeedKeyFromSeed } = sbot.metafeeds.keys
const { getSeed, getAnnounces, getLatest } = sbot.metafeeds.query
const { getContentSeed, getContentAnnounce, getMsgValAddExisting } =
sbot.metafeeds.messages
// Ensure seed exists
let mf
const [err1, loadedSeed] = await run(getSeed)()
if (err1 || !loadedSeed) {
if (err1) debug('generating a seed because %o', err1)
else debug('generating a seed')
const seed = sbot.metafeeds.keys.generateSeed()
const mfKeys = deriveRootMetaFeedKeyFromSeed(seed)
const content = getContentSeed(mfKeys.id, sbot.id, seed)
const [err2] = await run(sbot.db.publish)(content)
if (err2) return cb(err2)
mf = { seed, keys: mfKeys }
} else {
debug('loaded seed')
const mfKeys = deriveRootMetaFeedKeyFromSeed(loadedSeed)
mf = { seed: loadedSeed, keys: mfKeys }
}
// Ensure root meta feed announcement exists on the main feed
const [err2, announcements] = await run(getAnnounces)()
if (err2 || !announcements || announcements.length === 0) {
if (err2) debug('announcing meta feed on main feed because %o', err2)
else debug('announcing meta feed on main feed')
const [err3, content] = await run(getContentAnnounce)(mf.keys)
if (err3) return cb(err3)
const [err4] = await run(sbot.db.publish)(content)
if (err4) return cb(err4)
} else {
debug('announce post exists on main feed')
}
// Ensure the main feed was "added" on the root meta feed
const [err3, added] = await run(find)(mf, (f) => f.feedpurpose === 'main')
if (err3) return cb(err3)
if (!added) {
const [err4, latest] = await run(getLatest)(mf.keys.id)
if (err4) return cb(err4)
debug('adding main feed to root meta feed')
const msgVal = getMsgValAddExisting(mf.keys, latest, 'main', config.keys)
const [err5] = await run(sbot.db.add)(msgVal)
if (err5) return cb(err5)
} else {
debug('main feed already added to root meta feed')
}
rootMetaFeedLock.release(null, mf)
}
return {
getRoot,
findOrCreate,
findAndTombstone,
findById,
findByIdSync,
loadState,
ensureLoaded,
branchStream,
}
}