-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
591 lines (516 loc) · 18.6 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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// 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 pullDefer = require('pull-defer')
const chunk = require('lodash.chunk')
const clarify = require('clarify-error')
const {
where,
and,
isDecrypted,
type,
live,
toPullStream,
} = require('ssb-db2/operators')
const {
validator: {
group: {
addMember: isAddMember,
content: isContent,
exclude: isExclude,
initEpoch: isInitEpoch,
},
},
keySchemes,
} = require('private-group-spec')
const { SecretKey } = require('ssb-private-group-keys')
const { fromMessageSigil, isBendyButtV1FeedSSBURI } = require('ssb-uri2')
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 { groupRecp } = require('./lib/operators')
// const Epochs = require('./lib/epochs')
module.exports = {
name: 'tribes2',
manifest: {
create: 'async',
get: 'async',
list: 'source',
addMembers: 'async',
excludeMembers: 'async',
publish: 'async',
listMemebers: '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 { getEpochs } = 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,
groupKey: 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)
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, { writeKey, 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"))
const content = {
type: 'group/add-member',
version: 'v2',
groupKey: writeKey.key.toString('base64'),
root,
creator: rootAuthorId,
recps: [groupId, ...feedIds],
}
if (opts.text) content.text = opts.text
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,
}
publish(content, options, (err, msg) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to publish add-member message'))
return cb(null, msg)
})
})
})
})
}
function excludeMembers(groupId, feedIds, opts = {}, cb) {
if (cb === undefined)
return promisify(excludeMembers)(groupId, feedIds, opts)
ssb.metafeeds.findOrCreate(function gotRoot(err, myRoot) {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't get own root when excluding members"))
const excludeContent = {
type: 'group/exclude',
excludes: feedIds,
recps: [groupId],
}
const excludeOpts = {
tangles: ['members'],
isValid: isExclude,
}
publish(excludeContent, excludeOpts, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to publish exclude msg'))
pull(
listMembers(groupId),
pull.collect((err, beforeMembers) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't get old member list when excluding members"))
const remainingMembers = beforeMembers.filter(
(member) => !feedIds.includes(member)
)
const newGroupKey = new SecretKey()
const addInfo = { key: newGroupKey.toBuffer() }
ssb.box2.addGroupInfo(groupId, addInfo, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't store new key when excluding members"))
const newKey = {
key: newGroupKey.toBuffer(),
scheme: keySchemes.private_group,
}
ssb.box2.pickGroupWriteKey(groupId, newKey, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't switch to new key for writing when excluding members"))
const newEpochContent = {
type: 'group/init',
version: 'v2',
groupKey: newGroupKey.toString('base64'),
tangles: {
members: { root: null, previous: null },
},
recps: [groupId, myRoot.id],
}
const newTangleOpts = {
tangles: ['epoch'],
isValid: isInitEpoch,
}
publish(newEpochContent, newTangleOpts, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't post init msg on new epoch when excluding members"))
pull(
pull.values(chunk(remainingMembers, 15)),
pull.asyncMap((membersToAdd, cb) =>
addMembers(groupId, membersToAdd, {}, cb)
),
pull.collect((err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't re-add remaining members when excluding members"))
return 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 deferedSource = pullDefer.source()
get(groupId, (err, group) => {
// prettier-ignore
if (err) return deferedSource.abort(clarify(err, 'Failed to get group info when listing members'))
// prettier-ignore
if (group.excluded) return deferedSource.abort( new Error("We're excluded from this group, can't list members"))
const source = pull(
ssb.db.query(
where(
and(
isDecrypted('box2'),
type('group/add-member'),
groupRecp(groupId)
)
),
opts.live ? live({ old: true }) : null,
toPullStream()
),
pull.map((msg) => msg.value.content.recps.slice(1)),
pull.flatten(),
pull.unique()
)
deferedSource.resolve(source)
})
return deferedSource
}
function listInvites() {
const deferedSource = pullDefer.source()
getMyGroups((err, myGroups) => {
// prettier-ignore
if (err) return deferedSource.abort(clarify(err, 'Failed to list group IDs when listing invites'))
const source = pull(
// get all the groupIds we've heard of from invites
ssb.db.query(
where(and(isDecrypted('box2'), type('group/add-member'))),
toPullStream()
),
pull.filter((msg) => isAddMember(msg)),
pull.map((msg) => msg.value.content.recps[0]),
pull.unique(),
// drop those we're a part of already
pull.filter((groupId) => !myGroups.has(groupId)),
// gather all the data required for each group-invite
pull.asyncMap(getGroupInviteData)
)
deferedSource.resolve(source)
})
return deferedSource
// listInvites helpers
function getMyGroups(cb) {
const myGroups = new Set()
pull(
pullMany([
ssb.box2.listGroupIds(),
ssb.box2.listGroupIds({ excluded: true }),
]),
pull.flatten(),
pull.drain(
(groupId) => myGroups.add(groupId),
(err) => {
if (err) return cb(err)
return cb(null, myGroups)
}
)
)
}
function getGroupInviteData(groupId, cb) {
let root
const secrets = new Set()
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
secrets.add(msg.value.content.groupKey)
},
(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,
}
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
const { id, root, readKeys } = 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.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'))
return cb()
})
ssb.metafeeds.findOrCreate((err, myRoot) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Error getting own root in start()'))
// check if we've been excluded
pull(
ssb.db.query(
where(and(isDecrypted('box2'), type('group/exclude'))),
live({ old: true }),
toPullStream()
),
pull.filter(isExclude),
pull.filter((msg) =>
// it's an exclusion of us
msg.value.content.excludes.includes(myRoot.id)
),
pull.drain(
(msg) => {
const groupId = msg.value.content.recps[0]
ssb.box2.excludeGroupInfo(groupId, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Error on excluding group info after finding exclusion of ourselves'))
})
},
(err) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Error on looking for exclude messages excluding us'))
}
)
)
// look for new epochs that we're added to
pull(
ssb.db.query(
where(and(isDecrypted('box2'), type('group/add-member'))),
live({ old: true }),
toPullStream()
),
pull.filter(isAddMember),
// groups/epochs we're added to
pull.filter((msg) => {
return msg.value.content.recps.includes(myRoot.id)
}),
// to find new epochs we only check groups we've accepted the invite to
paraMap((msg, cb) => {
pull(
ssb.box2.listGroupIds(),
pull.filter((groupId) => groupId === msg.value.content.recps[0]),
pull.take(1),
pull.collect((err, groupIds) => {
// prettier-ignore
if (err) return cb(clarify(err, "Error getting groups we're already in when looking for new epochs"))
cb(null, groupIds.length ? msg : null)
})
)
}, 4),
pull.filter(Boolean),
pull.drain(
(msg) => {
const groupId = msg.value.content.recps[0]
const newKey = Buffer.from(msg.value.content.groupKey, 'base64')
ssb.box2.addGroupInfo(groupId, { key: newKey }, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Error adding new epoch key that we found'))
const newKeyPick = {
key: newKey,
scheme: keySchemes.private_group,
}
// TODO: naively guessing that this is the latest key for now
ssb.box2.pickGroupWriteKey(groupId, newKeyPick, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Error switching to new epoch key that we found'))
ssb.db.reindexEncrypted((err) => {
// prettier-ignore
if (err) cb(clarify(err, 'Error reindexing after finding new epoch'))
})
})
})
},
(err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Error finding new epochs we've been added to"))
}
)
)
})
}
return {
create,
get,
list,
addMembers,
excludeMembers,
publish,
listMembers,
listInvites,
acceptInvite,
start,
}
},
}