Files: c8ebe98ed356a8b95e516ca561f755e05ed43156 / book / pull / db.js
1614 bytesRaw
1 | const pull = require('pull-stream') |
2 | const nest = require('depnest') |
3 | |
4 | exports.gives = nest({ |
5 | 'book.pull': ['get', 'getAll'] |
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 | 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.drain(msg => { |
46 | if (msg.content.type !== "bookclub-update") return |
47 | |
48 | book.common = Object.assign(book.common, msg.content.common) |
49 | book.subjective[msg.author] = Object.assign(book.subjective[msg.author] || {}, |
50 | msg.content.subjective) |
51 | |
52 | cb(book) |
53 | }) |
54 | ) |
55 | } |
56 | |
57 | function hydrate(msg, key, cb) |
58 | { |
59 | var book = { |
60 | key: key, |
61 | common: msg.content.common, |
62 | subjective: {} |
63 | } |
64 | book.subjective[msg.author] = msg.content.subjective || |
65 | { rating: '', ratingType: '', review: '', shelve: '', genre: '' } |
66 | |
67 | applyAmends(book, cb) |
68 | } |
69 | } |
70 |
Built with git-ssb-web