-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlisten.js
89 lines (85 loc) · 2.58 KB
/
listen.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
// const flumeView = require('flumeview-reduce')
const pull = require('pull-stream')
const pullMany = require('pull-many')
const Crut = require('ssb-crut')
const { where, and, type, isDecrypted, live: dbLive, toPullStream } = require('ssb-db2/operators')
const { isValid: isAddMember } = require('./spec/group/add-member')
const { isValid: isExcludeMember } = require('./spec/group/exclude-member')
const poBoxSpec = require('./spec/group/po-box')
module.exports = {
addMember (ssb) {
return pull(
pullMany([
ssb.db.query(
where(
and(
isDecrypted('box2'),
type('group/add-member')
)
),
dbLive({ old: true }),
toPullStream()
),
ssb.db.reindexed()
]),
// NOTE this will run through all messages on each startup, which will help guarentee
// all messages have been emitted AND processed
// (same not true if we used a dummy flume-view)
pull.filter(m => m.sync !== true),
pull.filter(isAddMember),
pull.unique('key')
// NOTE we DO NOT filter our own messages out
// this is important for rebuilding indexes and keyring state if we have to restore our feed
)
},
excludeMember (ssb) {
return pull(
pullMany([
ssb.db.query(
where(
and(
isDecrypted('box2'),
type('group/exclude-member')
)
),
dbLive({ old: true }),
toPullStream()
),
ssb.db.reindexed()
]),
pull.filter(m => m.sync !== true),
pull.filter(isExcludeMember),
pull.unique('key')
)
},
poBox (ssb, emit) {
const { isUpdate: isPOBox } = new Crut(ssb, poBoxSpec).spec
pull(
pullMany([
ssb.db.query(
where(
and(
isDecrypted('box2'),
type('group/po-box')
)
),
dbLive({ old: true }),
toPullStream()
),
pull(
ssb.db.reindexed(),
pull.filter(msg => msg.value.content.type === 'group/po-box')
)
]),
// NOTE this will run through all messages on each startup, which will help guarentee
// all messages have been emitted AND processed
// (same not true if we used a dummy flume-view)
pull.filter(m => m.sync !== true),
pull.filter(isPOBox),
pull.unique('key'),
// NOTE we DO NOT filter our own messages out
// this is important for rebuilding indexes and keyring state if we have to restore our feed
pull.drain(emit)
)
}
}