Files: c8ebe98ed356a8b95e516ca561f755e05ed43156 / book / obs / book.js
1581 bytesRaw
1 | const nest = require('depnest') |
2 | const ref = require('ssb-ref') |
3 | const { Value, Struct, Dict } = require('mutant') |
4 | |
5 | exports.needs = nest({ |
6 | 'book.pull.get': 'first', |
7 | 'book.obs.struct': 'first', |
8 | 'keys.sync.id': 'first', |
9 | 'sbot.async.publish': 'first' |
10 | }) |
11 | |
12 | exports.gives = nest('book.obs.book') |
13 | |
14 | exports.create = function (api) { |
15 | return nest('book.obs.book', function (id) { |
16 | if (!ref.isLink(id)) throw new Error('a valid id must be specified') |
17 | |
18 | let book = api.book.obs.struct({ key: id }) |
19 | api.book.pull.get(id, dbBook => { |
20 | Object.keys(dbBook.common).forEach((k) => { |
21 | if (dbBook.common[k]) { |
22 | book[k].set(dbBook.common[k]) |
23 | } |
24 | }) |
25 | |
26 | Object.keys(dbBook.subjective).forEach((k) => { |
27 | if (book.subjective.has(k)) |
28 | { |
29 | Object.keys(dbBook.subjective[k]).forEach((v) => { |
30 | book.subjective.get(k)[v].set(dbBook.subjective[k][v]) |
31 | }) |
32 | } |
33 | else |
34 | { |
35 | let d = {} |
36 | Object.keys(dbBook.subjective[k]).forEach((v) => { |
37 | d[v] = Value(dbBook.subjective[k][v]) |
38 | }) |
39 | book.subjective.put(k, Struct(d)) |
40 | } |
41 | }) |
42 | }) |
43 | |
44 | book.amend = function(cb) |
45 | { |
46 | let msg = { type: 'bookclub-update', root: id } |
47 | |
48 | let s = book() |
49 | |
50 | msg.common = { |
51 | title: s.title, |
52 | authors: s.authors, |
53 | description: s.description, |
54 | images: s.images |
55 | } |
56 | |
57 | if (book.subjective) |
58 | msg.subjective = s.subjective[api.keys.sync.id()] |
59 | |
60 | api.sbot.async.publish(msg, cb) |
61 | } |
62 | |
63 | return book |
64 | }) |
65 | } |
66 |
Built with git-ssb-web