Files: f3fe7055afd7792fb682ade4e2edd1a11fa6b52b / book / obs / book.js
1320 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 | 'sbot.async.publish': 'first' |
9 | }) |
10 | |
11 | exports.gives = nest('book.obs.book') |
12 | |
13 | exports.create = function (api) { |
14 | return nest('book.obs.book', function (id) { |
15 | if (!ref.isLink(id)) throw new Error('a valid id must be specified') |
16 | |
17 | let book = api.book.obs.struct({ key: id }) |
18 | api.book.pull.get(id, dbBook => { |
19 | Object.keys(dbBook.common).forEach((k) => { |
20 | if (dbBook.common[k]) { |
21 | book[k].set(dbBook.common[k]) |
22 | } |
23 | }) |
24 | |
25 | Object.keys(dbBook.subjective).forEach((k) => { |
26 | var d = {} |
27 | Object.keys(dbBook.subjective[k]).forEach((v) => { |
28 | d[v] = Value(dbBook.subjective[k][v]) |
29 | }) |
30 | book.subjective.put(k, Struct(d)) |
31 | }) |
32 | }) |
33 | |
34 | book.amend = function(cb) |
35 | { |
36 | let msg = { type: 'bookclub-update', root: id } |
37 | |
38 | let s = book() |
39 | |
40 | msg.common = { |
41 | title: s.title, |
42 | authors: s.authors, |
43 | description: s.description, |
44 | images: s.images |
45 | } |
46 | |
47 | /* FIXME |
48 | if (book.subjective) |
49 | msg.subjective = subjectiveObj |
50 | */ |
51 | |
52 | api.sbot.async.publish(msg, cb) |
53 | } |
54 | |
55 | return book |
56 | }) |
57 | } |
58 |
Built with git-ssb-web