git ssb

3+

arj / patchbook



Tree: ec7448dccb86df89b1a13d4732f13d1e5ecac1f6

Files: ec7448dccb86df89b1a13d4732f13d1e5ecac1f6 / book / obs / book.js

1805 bytesRaw
1const nest = require('depnest')
2const ref = require('ssb-ref')
3const { Value, Struct, Dict } = require('mutant')
4
5exports.needs = nest({
6 'book.pull.get': 'first',
7 'book.obs.struct': 'first',
8 'keys.sync.id': 'first',
9 'sbot.async.publish': 'first'
10})
11
12exports.gives = nest('book.obs.book')
13
14exports.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((k) => {
28 if (book.subjective.has(k))
29 {
30 Object.keys(dbBook.subjective[k]).forEach((v) => {
31 book.subjective.get(k)[v].set(dbBook.subjective[k][v])
32 })
33 }
34 else
35 {
36 let d = {}
37 Object.keys(dbBook.subjective[k]).forEach((v) => {
38 d[v] = Value(dbBook.subjective[k][v])
39 })
40 book.subjective.put(k, Struct(d))
41 }
42 })
43 })
44
45 book.amend = function(cb)
46 {
47 let msg = { type: 'about', about: id }
48
49 let s = book()
50
51 msg.title = s.title
52 msg.authors = s.authors
53 msg.description = s.description
54
55 if (s.images.length > 0)
56 msg.image = s.images[0]
57
58 api.sbot.async.publish(msg, cb)
59 }
60
61 book.updateSubjective = function(cb)
62 {
63 let s = book()
64
65 let msg = { type: 'about', about: id }
66 msg = Object.assign(msg, s.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