Files: 59b2150aa65f622113a8aae8016eb849a7345690 / book / obs / book.js
1844 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 | book.title.set(dbBook.common.title) |
21 | book.authors.set(dbBook.common.authors) |
22 | book.description.set(dbBook.common.description) |
23 | |
24 | if (dbBook.common.image) |
25 | book.images.add(dbBook.common.image) |
26 | |
27 | Object.keys(dbBook.subjective).forEach((user) => { |
28 | if (book.subjective.has(user)) |
29 | { |
30 | Object.keys(dbBook.subjective[user]).forEach((v) => { |
31 | book.subjective.get(user)[v].set(dbBook.subjective[user][v]) |
32 | }) |
33 | } |
34 | else |
35 | { |
36 | let values = {} |
37 | Object.keys(dbBook.subjective[user]).forEach((v) => { |
38 | values[v] = Value(dbBook.subjective[user][v]) |
39 | }) |
40 | book.subjective.put(user, Struct(values)) |
41 | } |
42 | }) |
43 | }) |
44 | |
45 | book.amend = function(cb) |
46 | { |
47 | let msg = { type: 'about', about: id } |
48 | |
49 | let b = book() |
50 | |
51 | msg.title = b.title |
52 | msg.authors = b.authors |
53 | msg.description = b.description |
54 | |
55 | if (b.images.length > 0) |
56 | msg.image = b.images[0] |
57 | |
58 | api.sbot.async.publish(msg, cb) |
59 | } |
60 | |
61 | book.updateSubjective = function(cb) |
62 | { |
63 | let b = book() |
64 | |
65 | let msg = { type: 'about', about: id } |
66 | msg = Object.assign(msg, b.subjective[api.keys.sync.id()]) |
67 | |
68 | api.sbot.async.publish(msg, cb) |
69 | } |
70 | |
71 | return book |
72 | }) |
73 | } |
74 |
Built with git-ssb-web