-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
566 lines (490 loc) · 17.5 KB
/
index.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// SPDX-FileCopyrightText: 2022 Andre 'Staltz' Medeiros <[email protected]>
//
// SPDX-License-Identifier: LGPL-3.0-only
const { promisify } = require('util')
const pull = require('pull-stream')
const paraMap = require('pull-paramap')
const pullMany = require('pull-many')
const pullFlatMerge = require('pull-flat-merge')
const pullAbortable = require('pull-abortable')
const pullDefer = require('pull-defer')
const clarify = require('clarify-error')
const {
where,
and,
isDecrypted,
type,
live: dbLive,
toPullStream,
} = require('ssb-db2/operators')
const {
validator: {
group: {
addMember: isAddMember,
content: isContent,
excludeMember: isExcludeMember,
},
},
keySchemes,
} = require('private-group-spec')
const { fromMessageSigil, isBendyButtV1FeedSSBURI } = require('ssb-uri2')
const startListeners = require('./listeners')
const buildGroupId = require('./lib/build-group-id')
const addTangles = require('./lib/tangles/add-tangles')
const publishAndPrune = require('./lib/prune-publish')
const MetaFeedHelpers = require('./lib/meta-feed-helpers')
const Epochs = require('./lib/epochs')
const { groupRecp } = require('./lib/operators')
const { createNewEpoch } = require('./lib/exclude')
module.exports = {
name: 'tribes2',
manifest: {
create: 'async',
get: 'async',
list: 'source',
addMembers: 'async',
excludeMembers: 'async',
publish: 'async',
listMembers: 'source',
listInvites: 'source',
acceptInvite: 'async',
start: 'async',
},
// eslint-disable-next-line no-unused-vars
init(ssb, config) {
const {
secretKeyFromString,
findOrCreateAdditionsFeed,
findOrCreateGroupFeed,
findOrCreateGroupWithoutMembers,
getRootFeedIdFromMsgId,
} = MetaFeedHelpers(ssb)
const {
getTipEpochs,
getPredecessorEpochs,
getPreferredEpoch,
getMembers,
} = Epochs(ssb)
function create(opts = {}, cb) {
if (cb === undefined) return promisify(create)(opts)
findOrCreateGroupWithoutMembers((err, group) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to create group init message when creating a group'))
const { groupInitMsg, groupFeed, myRoot } = group
const secret = secretKeyFromString(groupFeed.purpose)
const data = {
id: buildGroupId({
groupInitMsg,
secret: secret.toBuffer(),
}),
writeKey: {
key: secret.toBuffer(),
scheme: keySchemes.private_group,
},
readKeys: [
{ key: secret.toBuffer(), scheme: keySchemes.private_group },
],
root: fromMessageSigil(groupInitMsg.key),
subfeed: groupFeed.keys,
}
ssb.box2.addGroupInfo(data.id, {
key: data.writeKey.key,
root: data.root,
})
// Adding myself for recovery reasons
addMembers(data.id, [myRoot.id], {}, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to add myself to the group when creating a group'))
return cb(null, data)
})
})
}
function get(id, cb) {
if (cb === undefined) return promisify(get)(id)
ssb.box2.getGroupInfo(id, (err, info) => {
if (err) return cb(clarify(err, 'Failed to get group details'))
if (!info) return cb(new Error(`Couldn't find group with id ${id}`))
cb(null, {
...info,
id,
})
})
}
function list(opts = {}) {
return pull(
ssb.box2.listGroupIds({
live: !!opts.live,
excluded: !!opts.excluded,
}),
paraMap(get, 4)
)
}
function addMembers(groupId, feedIds, opts = {}, cb) {
if (cb === undefined) return promisify(addMembers)(groupId, feedIds, opts)
opts.oldSecrets ??= true
if (!feedIds || feedIds.length === 0) {
return cb(new Error('No feedIds provided to addMembers'))
}
if (feedIds.length > 15) {
// prettier-ignore
return cb(new Error(`Tried to add ${feedIds.length} members, the max is 15`))
}
if (feedIds.some((id) => !isBendyButtV1FeedSSBURI(id))) {
return cb(new Error('addMembers only supports bendybutt-v1 feed IDs'))
}
get(groupId, (err, { root }) => {
// prettier-ignore
if (err) return cb(clarify(err, `Failed to get group details when adding members`))
getRootFeedIdFromMsgId(root, (err, rootAuthorId) => {
// prettier-ignore
if (err) return cb(clarify(err, "couldn't get root id of author of root msg"))
getTipEpochs(groupId, (err, tipEpochs) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't get tip epochs when adding members"))
const getFeed = opts?._feedKeys
? (cb) => cb(null, { keys: opts._feedKeys })
: findOrCreateAdditionsFeed
getFeed((err, additionsFeed) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to find or create additions feed when adding members'))
const options = {
isValid: isAddMember,
tangles: ['members'],
feedKeys: additionsFeed.keys,
}
pull(
pull.values(tipEpochs),
pull.asyncMap((tipEpoch, cb) => {
getPredecessorEpochs(
groupId,
tipEpoch.id,
(err, predecessors) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't get predecessor epochs when adding members"))
const oldSecrets = predecessors.map((pred) =>
pred.secret.toString('base64')
)
const content = {
type: 'group/add-member',
version: 'v2',
secret: tipEpoch.secret.toString('base64'),
oldSecrets: opts.oldSecrets ? oldSecrets : undefined,
root,
creator: rootAuthorId,
text: opts?.text,
recps: [groupId, ...feedIds],
}
return cb(null, content)
}
)
}),
pull.asyncMap((content, cb) => publish(content, options, cb)),
pull.collect((err, msgs) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to publish add-member message(s)'))
return cb(null, msgs)
})
)
})
})
})
})
}
function excludeMembers(groupId, feedIds, opts = {}, cb) {
if (cb === undefined)
return promisify(excludeMembers)(groupId, feedIds, opts)
const excludeContent = {
type: 'group/exclude-member',
excludes: feedIds,
recps: [groupId],
}
const excludeOpts = {
tangles: ['members'],
isValid: isExcludeMember,
}
publish(excludeContent, excludeOpts, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to publish exclude msg'))
// prettier-ignore
if (opts?._newEpochCrash) return cb(new Error('Intentional crash before creating new epoch'))
createNewEpoch(
ssb,
groupId,
{
_reAddCrash: opts?._reAddCrash,
_reAddSkipMember: opts?._reAddSkipMember,
},
(err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't create new epoch and/or re-add members when excluding members"))
cb()
}
)
})
}
function publish(content, opts, cb) {
if (cb === undefined) return promisify(publish)(content, opts)
if (!content) return cb(new Error('Missing content'))
const isValid = opts?.isValid ?? isContent
const tangles = ['group', ...(opts?.tangles ?? [])]
const recps = content.recps
if (!recps || !Array.isArray(recps) || recps.length < 1) {
return cb(new Error('Missing recps'))
}
const groupId = recps[0]
get(groupId, (err, { writeKey, excluded }) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to get group details when publishing to a group'))
if (excluded)
return cb(
new Error("Cannot publish to a group we've been excluded from")
)
addTangles(ssb, content, tangles, (err, content) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to add group tangle when publishing to a group'))
if (!isValid(content))
return cb(
new Error(isValid.errorsString ?? 'content failed validation')
)
const getFeed = opts?.feedKeys
? (_, cb) => cb(null, { keys: opts.feedKeys })
: findOrCreateGroupFeed
getFeed(writeKey.key, (err, groupFeed) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to find or create group feed when publishing to a group'))
publishAndPrune(ssb, content, groupFeed.keys, (err, msg) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to publishAndPrune when publishing a group message'))
return cb(null, msg)
})
})
})
})
}
function listMembers(groupId, opts = {}) {
const { live, allAdded } = opts
const deferredSource = pullDefer.source()
const listAllAdded = () => {
const allAddedMembers = new Set()
const source = pull(
ssb.db.query(
where(
and(
isDecrypted('box2'),
type('group/add-member'),
groupRecp(groupId)
)
),
live ? dbLive({ old: true }) : null,
toPullStream()
),
pull.filter(isAddMember),
pull.map((msg) => msg.value.content),
pull.map((content) =>
// for situations where we haven't replicated much of the group yet, we make sure to at least include the group creator here so we're sure to make progress in replication
[content.creator, ...content.recps.slice(1)]
),
pull.flatten(),
pull.unique(),
pull.through((member) => allAddedMembers.add(member)),
// return the whole list every time there's an update, to have a consistent listMembers api
pull.map(() => ({ added: [...allAddedMembers], toExclude: [] }))
)
deferredSource.resolve(source)
}
const listUnlive = () => {
getPreferredEpoch(groupId, (err, epoch) => {
// prettier-ignore
if (err) return deferredSource.abort(clarify(err, 'failed to load preferred epoch'))
getMembers(epoch.id, (err, res) => {
// prettier-ignore
if (err) return deferredSource.abort(clarify(err, 'error getting members'))
const source = pull.once(res)
deferredSource.resolve(source)
})
})
}
const listLive = () => {
let abortable = pullAbortable()
const source = pull(
getPreferredEpoch.stream(groupId, { live }),
pull.map((epoch) => {
abortable.abort()
abortable = pullAbortable()
return pull(getMembers.stream(epoch.id, { live }), abortable)
}),
pullFlatMerge()
)
deferredSource.resolve(source)
}
get(groupId, (err, group) => {
// prettier-ignore
if (err) return deferredSource.abort(clarify(err, 'Failed to get group info when listing members'))
// prettier-ignore
if (group.excluded) return deferredSource.abort( new Error("We're excluded from this group, can't list members"))
if (allAdded) {
listAllAdded()
return
}
if (!live) {
listUnlive()
return
}
listLive()
})
return deferredSource
}
function listInvites() {
const deferredSource = pullDefer.source()
getMyReadKeys((err, myReadKeys) => {
// prettier-ignore
if (err) return deferredSource.abort(clarify(err, 'Failed to list group readKeys when listing invites'))
const source = pull(
// get all the additions we've heard of
ssb.db.query(
where(and(isDecrypted('box2'), type('group/add-member'))),
toPullStream()
),
pull.filter((msg) => isAddMember(msg)),
pull.map((msg) => msg.value.content),
// drop those we're grabbed secrets from already (in case we've been in the group before)
pull.filter((content) => !myReadKeys.has(content.secret)),
// groupId
pull.map((content) => content.recps[0]),
pull.unique(),
// gather all the data required for each group-invite
pull.asyncMap(getGroupInviteData)
)
deferredSource.resolve(source)
})
return deferredSource
// listInvites helpers
function getMyReadKeys(cb) {
const myReadKeys = new Set()
pull(
// TODO replace with pull.values (unless want "round-robbin" sampling)
pullMany([
ssb.box2.listGroupIds(),
ssb.box2.listGroupIds({ excluded: true }),
]),
pull.flatten(),
pull.asyncMap((groupId, cb) => ssb.box2.getGroupInfo(groupId, cb)),
pull.drain(
(groupInfo) =>
groupInfo.readKeys
.map((readKey) => readKey.key.toString('base64'))
.forEach((readKeyString) => myReadKeys.add(readKeyString)),
(err) => {
if (err) return cb(err)
return cb(null, myReadKeys)
}
)
)
}
function getGroupInviteData(groupId, cb) {
let root
const secrets = new Set()
let writeSecret = null
pull(
ssb.db.query(
where(
and(
isDecrypted('box2'),
type('group/add-member'),
groupRecp(groupId)
)
),
toPullStream()
),
pull.filter((msg) => isAddMember(msg)),
pull.drain(
(msg) => {
root ||= msg.value.content.root
const oldSecrets = msg.value.content.oldSecrets
if (oldSecrets) {
oldSecrets.forEach((oldSecret) => secrets.add(oldSecret))
}
const latestSecret = msg.value.content.secret
secrets.add(latestSecret)
writeSecret = latestSecret
},
(err) => {
if (err) return cb(err)
const readKeys = [...secrets].map((secret) => ({
key: Buffer.from(secret, 'base64'),
scheme: keySchemes.private_group,
}))
const invite = {
id: groupId,
root,
readKeys,
writeKey: {
key: Buffer.from(writeSecret, 'base64'),
scheme: keySchemes.private_group,
},
}
return cb(null, invite)
}
)
)
}
}
function acceptInvite(groupId, cb) {
if (cb === undefined) return promisify(acceptInvite)(groupId)
pull(
listInvites(),
pull.filter((inviteInfo) => inviteInfo.id === groupId),
pull.take(1),
pull.collect((err, inviteInfos) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to list invites when accepting an invite'))
// prettier-ignore
if (!inviteInfos.length) return cb(new Error("Didn't find invite for that group id"))
// TODO: which writeKey should be picked??
// this will essentially pick a random write key from the current epoch tips
const { id, root, readKeys, writeKey } = inviteInfos[0]
pull(
pull.values(readKeys),
pull.asyncMap((readKey, cb) => {
ssb.box2.addGroupInfo(id, { key: readKey.key, root }, cb)
}),
pull.collect((err) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to add group info when accepting an invite'))
ssb.box2.pickGroupWriteKey(id, writeKey, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to pick a write key when accepting invite to a group'))
ssb.db.reindexEncrypted((err) => {
// prettier-ignore
if (err) cb(clarify(err, 'Failed to reindex encrypted messages when accepting an invite'))
else cb(null, inviteInfos[0])
})
})
})
)
})
)
}
function start(cb) {
if (cb === undefined) return promisify(start)()
findOrCreateAdditionsFeed((err) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Error finding or creating additions feed when starting ssb-tribes2'))
cb(null)
startListeners(ssb, config, console.error)
})
}
return {
create,
get,
list,
addMembers,
excludeMembers,
publish,
listMembers,
listInvites,
acceptInvite,
start,
}
},
}