Files: 2c5df571b198509b12f1ea83f3f4e16d782acb81 / book / pull / db.js
2214 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, reverse: 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 == "about") { |
47 | // FIXME: refactor this |
48 | if (msg.content.rating || msg.content.ratingType || msg.content.shelve || |
49 | msg.content.genre || msg.content.review) { |
50 | book.subjective[msg.author]= { |
51 | rating: msg.content.rating, |
52 | ratingType: msg.content.ratingType, |
53 | shelve: msg.content.shelve, |
54 | genre: msg.content.genre, |
55 | review: msg.content.review |
56 | } |
57 | } else |
58 | book.common = Object.assign(book.common, msg.content) |
59 | } else if (msg.content.type == "bookclub-subjective") { |
60 | book.subjective[msg.author]= { |
61 | rating: msg.content.rating, |
62 | ratingType: msg.content.ratingType, |
63 | shelve: msg.content.shelve, |
64 | genre: msg.content.genre, |
65 | review: msg.content.review |
66 | } |
67 | } |
68 | cb(book) |
69 | }) |
70 | ) |
71 | } |
72 | |
73 | function hydrate(msg, key, cb) |
74 | { |
75 | var book = { |
76 | key: key, |
77 | common: msg.content, |
78 | subjective: {} |
79 | } |
80 | book.subjective[msg.author] = { rating: '', ratingType: '', review: '', shelve: '', genre: '' } |
81 | |
82 | cb(book) |
83 | |
84 | applyAmends(book, cb) |
85 | } |
86 | } |
87 |
Built with git-ssb-web