Files: 9c6467e267ec5e13ac657da60215077fc7b42510 / book / pull / db.js
1930 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 | 'keys.sync.id': 'first' |
13 | }) |
14 | |
15 | exports.create = function (api) { |
16 | |
17 | return nest({ |
18 | 'book.pull.get': get, |
19 | 'book.pull.getAll': getAll |
20 | }) |
21 | |
22 | function get(key, cb) { |
23 | pull( |
24 | pull.values([key]), |
25 | pull.asyncMap((key, cb) => api.sbot.async.get(key, cb)), |
26 | pull.asyncMap((msg) => hydrate(msg, key, cb)), |
27 | pull.drain(cb) |
28 | ) |
29 | } |
30 | |
31 | function getAll() { |
32 | return pull( |
33 | api.sbot.pull.messagesByType({ type: 'bookclub', fillCache: true, |
34 | keys: true, reverse: true, live: true }) |
35 | ) |
36 | } |
37 | |
38 | // internal |
39 | |
40 | function applyAmends(book, cb) { |
41 | pull( |
42 | api.sbot.pull.links({ dest: book.key, live: true }), |
43 | pull.filter(data => data.key), |
44 | pull.asyncMap((data, cb) => { |
45 | api.sbot.async.get(data.key, cb) |
46 | }), |
47 | pull.drain(msg => { |
48 | if (msg.content.type !== "about") return |
49 | |
50 | if (msg.content.rating || msg.content.ratingType || msg.content.shelve || |
51 | msg.content.genre || msg.content.review) { |
52 | book.subjective[msg.author] = { |
53 | rating: msg.content.rating, |
54 | ratingType: msg.content.ratingType, |
55 | shelve: msg.content.shelve, |
56 | genre: msg.content.genre, |
57 | review: msg.content.review |
58 | } |
59 | } else |
60 | book.common = Object.assign(book.common, msg.content) |
61 | |
62 | cb(book) |
63 | }) |
64 | ) |
65 | } |
66 | |
67 | function hydrate(msg, key, cb) |
68 | { |
69 | var book = { |
70 | key: key, |
71 | common: msg.content, |
72 | subjective: {} |
73 | } |
74 | |
75 | book.subjective[api.keys.sync.id()] = { |
76 | rating: '', ratingType: '', review: '', shelve: '', genre: '' |
77 | } |
78 | |
79 | cb(book) |
80 | |
81 | applyAmends(book, cb) |
82 | } |
83 | } |
84 |
Built with git-ssb-web