Files: 24301103fc79102ebc4d71e1454571b15c58592e / book / pull / db.js
1550 bytesRaw
1 | const pull = require('pull-stream') |
2 | const nest = require('depnest') |
3 | |
4 | exports.gives = nest({ |
5 | 'book.pull': ['get', 'getAll', 'create', 'amend'] |
6 | }) |
7 | |
8 | exports.needs = nest({ |
9 | 'sbot.pull.messagesByType': 'first', |
10 | 'sbot.pull.links': 'first', |
11 | 'sbot.async.get': 'first' |
12 | }) |
13 | |
14 | exports.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 | api.sbot.async.get(key), |
24 | pull.asyncMap(hydrate), |
25 | pull.drain(cb) |
26 | ) |
27 | } |
28 | |
29 | function getAll() { |
30 | return pull( |
31 | api.sbot.pull.messagesByType({ type: 'bookclub', fillCache: true, keys: false }), |
32 | pull.asyncMap(hydrate) |
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 | book.common = Object.assign(book.common, msg.content.common) |
50 | book.subjective[msg.author] = Object.assign(book.subjective[msg.author], |
51 | msg.content.subjective) |
52 | }) |
53 | |
54 | cb(book) |
55 | }) |
56 | ) |
57 | } |
58 | |
59 | function hydrate(msg, cb) |
60 | { |
61 | var book = { |
62 | key: msg.key, |
63 | common: msg.content.common, |
64 | subjective: {} |
65 | } |
66 | book.subjective[msg.author] = msg.content.subjective |
67 | |
68 | applyAmends(book, cb) |
69 | } |
70 | } |
71 |
Built with git-ssb-web