Files: fd2459df8b46e1e0fe589fc20f3d16020399d671 / book / obs / book.js
2523 bytesRaw
1 | const nest = require('depnest') |
2 | const ref = require('ssb-ref') |
3 | const { Value, Struct, Dict } = require('mutant') |
4 | const deepEq = require('deep-equal') |
5 | |
6 | exports.needs = nest({ |
7 | 'book.async.get': 'first', |
8 | 'book.obs.struct': 'first', |
9 | 'keys.sync.id': 'first', |
10 | 'sbot.async.publish': 'first' |
11 | }) |
12 | |
13 | exports.gives = nest('book.obs.book') |
14 | |
15 | exports.create = function (api) { |
16 | return nest('book.obs.book', function (id) { |
17 | if (!ref.isLink(id)) throw new Error('a valid id must be specified') |
18 | |
19 | let book = api.book.obs.struct({ key: id }) |
20 | |
21 | api.book.async.get(id, dbBook => { |
22 | book.title.set(dbBook.common.title) |
23 | book.authors.set(dbBook.common.authors) |
24 | book.description.set(dbBook.common.description) |
25 | book.series.set(dbBook.common.series) |
26 | book.seriesNo.set(dbBook.common.seriesNo) |
27 | |
28 | const { image } = dbBook.common |
29 | |
30 | // workaround for https://github.com/mmckegg/mutant/issues/20 |
31 | if (image && !book.images().some(i => deepEq(i, image))) |
32 | book.images.add(dbBook.common.image) |
33 | |
34 | Object.keys(dbBook.subjective).forEach((user) => { |
35 | if (book.subjective.has(user)) |
36 | { |
37 | Object.keys(dbBook.subjective[user]).forEach((v) => { |
38 | book.subjective.get(user)[v].set(dbBook.subjective[user][v]) |
39 | }) |
40 | } |
41 | else |
42 | { |
43 | let values = {} |
44 | Object.keys(dbBook.subjective[user]).forEach((v) => { |
45 | values[v] = Value(dbBook.subjective[user][v]) |
46 | }) |
47 | book.subjective.put(user, Struct(values)) |
48 | } |
49 | }) |
50 | }) |
51 | |
52 | book.amend = function(cb) |
53 | { |
54 | let msg = { type: 'about', about: id } |
55 | |
56 | let b = book() |
57 | |
58 | msg.title = b.title |
59 | msg.authors = b.authors |
60 | msg.series = b.series |
61 | msg.seriesNo = b.seriesNo |
62 | msg.description = b.description |
63 | |
64 | if (b.images.length > 0) |
65 | msg.image = b.images[0] |
66 | |
67 | api.sbot.async.publish(msg, cb) |
68 | } |
69 | |
70 | book.updateSubjective = function(cb) |
71 | { |
72 | let b = book() |
73 | |
74 | let msg = { type: 'about', about: id } |
75 | msg = Object.assign(msg, b.subjective[api.keys.sync.id()]) |
76 | |
77 | api.sbot.async.publish(msg, cb) |
78 | } |
79 | |
80 | book.addCommentToSubjective = function(subjectiveId, comment, cb) |
81 | { |
82 | /* |
83 | "content": { |
84 | "type": "post", |
85 | "root": "%THGnuD1d3cnVaa9+d6mIxE9dBktlKc3UwrjlKWmVE+M=.sha256", <- about |
86 | "branch": "%THGnuD1d3cnVaa9+d6mIxE9dBktlKc3UwrjlKWmVE+M=.sha256", <- prev |
87 | "text": "testing comment" |
88 | } |
89 | */ |
90 | } |
91 | |
92 | return book |
93 | }) |
94 | } |
95 |
Built with git-ssb-web