UNPKG

1.01 kBJavaScriptView Raw
1const Reduce = require('flumeview-reduce')
2const isFeed = require('ssb-ref').isFeed
3// track contact messages, follow, unfollow, block
4
5module.exports = function (sbot, createLayer, config) {
6 const layer = createLayer('contacts')
7 let initial = false
8
9 const INDEX_VERSION = 10
10 const index = sbot._flumeUse('contacts2', Reduce(INDEX_VERSION, (g, data) => {
11 if (!g) g = {}
12
13 const from = data.value.author
14 const to = data.value.content.contact
15 const value = (data.value.content.blocking || data.value.content.flagged)
16 ? -1
17 : (data.value.content.following === true) ? 1 : -2
18
19 if (isFeed(from) && isFeed(to)) {
20 if (initial) {
21 layer(from, to, value)
22 }
23 g[from] = g[from] || {}
24 g[from][to] = value
25 }
26 return g
27 }))
28
29 // trigger flume machinery to wait until index is ready,
30 // otherwise there is a race condition when rebuilding the graph.
31 index.get((err, g) => {
32 if (err) throw err
33 initial = true
34 layer(g || {})
35 })
36}