git ssb

3+

arj / patchbook



Tree: c6808ebda95764b1cdd2b287584c051d8901fb42

Files: c6808ebda95764b1cdd2b287584c051d8901fb42 / book / pull / db.js

1654 bytesRaw
1const pull = require('pull-stream')
2const nest = require('depnest')
3
4exports.gives = nest({
5 'book.pull': ['get', 'getAll']
6})
7
8exports.needs = nest({
9 'sbot.pull.messagesByType': 'first',
10 'sbot.pull.links': 'first',
11 'sbot.async.get': 'first'
12})
13
14exports.create = function (api) {
15
16 return nest({
17 'book.pull.get': get,
18 'book.pull.getAll': getAll
19 })
20
21 function get(key, cb) {
22 pull(
23 pull.values([key]),
24 pull.asyncMap((key, cb) => api.sbot.async.get(key, cb)),
25 pull.asyncMap((msg) => hydrate(msg, key, cb)),
26 pull.drain(cb)
27 )
28 }
29
30 function getAll() {
31 return pull(
32 api.sbot.pull.messagesByType({ type: 'bookclub', fillCache: true, keys: true })
33 )
34 }
35
36 // internal
37
38 function applyAmends(book, cb) {
39 pull(
40 api.sbot.pull.links({ dest: book.key }), // live: true
41 pull.filter(data => data.key),
42 pull.asyncMap((data, cb) => {
43 api.sbot.async.get(data.key, cb)
44 }),
45 pull.collect((err, msgs) => { // for live use drain
46 if (err) throw err
47
48 msgs.forEach(msg => {
49 if (msg.type !== "bookclub-update") return
50
51 book.common = Object.assign(book.common, msg.content.common)
52 book.subjective[msg.author] = Object.assign(book.subjective[msg.author],
53 msg.content.subjective)
54 })
55
56 if (cb)
57 cb(book)
58 })
59 )
60 }
61
62 function hydrate(msg, key, cb)
63 {
64 var book = {
65 key: key,
66 common: msg.content.common,
67 subjective: {}
68 }
69 book.subjective[msg.author] = msg.content.subjective
70
71 applyAmends(book, cb)
72 }
73}
74

Built with git-ssb-web