forked from ssbc/ssb-tribes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
229 lines (197 loc) · 6.78 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
const { join } = require('path')
const pull = require('pull-stream')
const set = require('lodash.set')
const { isFeed, isCloakedMsg: isGroup } = require('ssb-ref')
const Obz = require('obz')
const KeyStore = require('./key-store')
const Envelope = require('./envelope')
const listen = require('./listen')
const { FeedId } = require('./lib/cipherlinks')
const GroupId = require('./lib/group-id')
const GetGroupTangle = require('./lib/get-group-tangle')
const Method = require('./method')
module.exports = {
name: 'tribes',
version: require('./package.json').version,
manifest: {
register: 'async',
registerAuthors: 'async',
create: 'async',
invite: 'async',
get: 'async',
list: 'async',
listAuthors: 'async',
link: {
create: 'async'
},
findByFeedId: 'async',
application: {
create: 'async ',
get: 'async',
comment: 'async',
accept: 'async',
reject: 'async',
update: 'async',
list: 'async,'
}
},
init
}
function init (ssb, config) {
const state = {
keys: ssb.keys,
feedId: new FeedId(ssb.id).toTFK(),
loading: {
keystore: Obz()
},
newAuthorListeners: [],
closed: false
}
/* secret keys store / helper */
const keystore = KeyStore(join(config.path, 'tribes/keystore'), ssb.keys, () => {
state.loading.keystore.set(false)
})
ssb.close.hook(function (fn, args) {
state.closed = true
keystore.close(() => fn.apply(this, args))
})
/* register the boxer / unboxer */
const { boxer, unboxer } = Envelope(keystore, state)
ssb.addBoxer({ init: onKeystoreReady, value: boxer })
ssb.addUnboxer({ init: onKeystoreReady, ...unboxer })
function onKeystoreReady (done) {
if (state.closed === true) return
if (state.loading.keystore.value === false) return done()
state.loading.keystore.once(done)
}
/* start listeners */
listen.addMember(ssb, m => {
const { root, groupKey } = m.value.content
ssb.get({ id: root, meta: true }, (err, groupInitMsg) => {
if (err) throw err
const groupId = GroupId({ groupInitMsg, groupKey })
const authors = [
m.value.author,
...m.value.content.recps.filter(isFeed)
]
keystore.processAddMember({ groupId, groupKey, root, authors }, (err, newAuthors) => {
if (err) throw err
if (newAuthors.length) {
state.newAuthorListeners.forEach(fn => fn({ groupId, newAuthors }))
console.log('rebuild!!! (ノ´ヮ´)ノ*:・゚✧')
ssb.rebuild(() => console.log('rebuild finished'))
}
})
})
})
setImmediate(() => {
if (ssb.replicate) {
state.newAuthorListeners.push(({ newAuthors }) => {
newAuthors
.filter(id => id !== ssb.id)
.forEach(id => ssb.replicate.request({ id, replicate: true }))
})
state.loading.keystore.once((s) => {
const peers = new Set()
keystore.group.list()
.map(groupId => keystore.group.listAuthors(groupId))
.forEach(authors => authors.forEach(author => peers.add(author)))
peers.delete(ssb.id)
Array.from(peers)
.forEach(id => ssb.replicate.request({ id, replicate: true }))
})
}
})
/* We care about group/add-member messages others have posted which:
* 1. add us to a new group
* 2. add other people to a group we're already in
*
* In (2) we may be able to skip re-indexing if they haven't published
* any brand new private messages since they were added.
* This would require knowing their feed seq at time they were entrusted with key
* (because they can't post messages to the group before then)
*/
/* auto-add group tangle info to all private-group messages */
const getGroupTangle = GetGroupTangle(ssb, keystore)
ssb.publish.hook(function (fn, args) {
const [content, cb] = args
if (!content.recps) return fn.apply(this, args)
if (!isGroup(content.recps[0])) return fn.apply(this, args)
getGroupTangle(content.recps[0], (err, tangle) => {
if (err) {
// NOTE there are two ways an err can occur in getGroupTangle
// 1. recps is not a groupId
// 2. unknown groupId,
//
// Rather than cb(err) here we we pass it on to boxers to see if an err is needed
return fn.apply(this, args)
}
fn.apply(this, [set(content, 'tangles.group', tangle), cb])
})
})
/* API */
const scuttle = Method(ssb, keystore, state) // ssb db methods
return {
register: keystore.group.register,
registerAuthors (groupId, authorIds, cb) {
pull(
pull.values(authorIds),
pull.asyncMap((authorId, cb) => keystore.group.registerAuthor(groupId, authorId, cb)),
pull.collect((err) => {
if (err) cb(err)
else cb(null, true)
})
)
},
create (opts, cb) {
scuttle.group.init((err, data) => {
if (err) return cb(err)
const { groupId, groupKey, groupInitMsg: root } = data
keystore.group.register(groupId, { key: groupKey, root: root.key }, (err) => {
if (err) return cb(err)
keystore.group.registerAuthor(groupId, ssb.id, (err) => {
if (err) return cb(err)
const readKey = unboxer.key(root.value.content, root.value)
if (!readKey) return cb(new Error('tribes.group.init failed, please try again while not publishing other messages'))
// NOTE this checks out group/init message was encrypted with the right `previous`.
// There is a potential race condition where the init method calls `ssb.getFeedState` to
// access `previous` but while encrypting the `group/init` message content another
// message is pushed into the queue, making our enveloping invalid.
state.newAuthorListeners.forEach(fn => fn({ groupId, newAuthors: [ssb.id] }))
cb(null, data)
})
})
})
},
invite (groupId, authorIds, opts = {}, cb) {
scuttle.group.addMember(groupId, authorIds, opts, (err, data) => {
if (err) return cb(err)
keystore.group.registerAuthors(groupId, authorIds, (err) => {
if (err) return cb(err)
cb(null, data)
})
})
},
list (cb) {
onKeystoreReady(() => cb(null, keystore.group.list()))
},
get (id, cb) {
onKeystoreReady(() => {
const data = keystore.group.get(id)
if (!data) return cb(new Error(`unknown groupId ${id})`))
cb(null, data)
})
},
listAuthors (groupId, cb) {
onKeystoreReady(() => cb(null, keystore.group.listAuthors(groupId)))
},
link: {
create: scuttle.link.create
},
findByFeedId: scuttle.link.findGroupByFeedId,
addNewAuthorListener (fn) {
state.newAuthorListeners.push(fn)
},
application: scuttle.application
}
}