git ssb

3+

arj / patchbook



Tree: 74ba75f717d9bffcc0ea0b128ef9e0b0f02593c3

Files: 74ba75f717d9bffcc0ea0b128ef9e0b0f02593c3 / db.js

1971 bytesRaw
1var pull = require('pull-stream')
2var through = require('pull-through')
3var multicb = require('multicb')
4
5module.exports = {
6
7 sbot: null,
8 myId: "",
9
10 bookDB: function(sbot, cb)
11 {
12 if (!sbot) throw "Missing sbot"
13
14 var self = Object.create(this)
15
16 self.sbot = sbot
17 self.myId = sbot.id
18
19 return self
20 },
21
22 create: function(commonObj, subjectiveObj, cb)
23 {
24 this.sbot.publish({ type: 'bookclub',
25 common: commonObj,
26 subjective: subjectiveObj }, cb)
27 },
28
29 amend: function(id, commonObj, subjectiveObj, cb)
30 {
31 var msg = { type: 'bookclub-update', root: id }
32 if (commonObj)
33 msg.common = commonObj
34 if (subjectiveObj)
35 msg.subjective = subjectiveObj
36
37 this.sbot.publish(msg, cb)
38 },
39
40 getAll: function(cb) {
41 var books = []
42
43 pull(
44 this.getStreamByType('bookclub'),
45 pull.asyncMap((msg, cb) => {
46 var book = {
47 key: msg.key,
48 common: msg.content.common,
49 subjective: {}
50 }
51 book.subjective[msg.author] = msg.content.subjective
52 books.push(book)
53
54 this.applyAmends(book, cb)
55 }),
56 pull.collect((err, msgs) => {
57 cb(books)
58 })
59 )
60 },
61
62 // internal
63
64 applyAmends: function(book, cb) {
65 var sbot = this.sbot
66 pull(
67 sbot.links({ dest: book.key }), // live: true
68 pull.filter(data => data.key),
69 pull.asyncMap((data, cb) => {
70 sbot.get(data.key, cb)
71 }),
72 pull.collect((err, msgs) => { // for live use drain
73 if (err) throw err
74
75 msgs.forEach(msg => {
76 book.common = Object.assign(book.common, msg.content.common)
77 book.subjective[msg.author] = Object.assign(book.subjective[msg.author], msg.content.subjective)
78 })
79
80 cb(book)
81 })
82 )
83 },
84
85 getStreamByType: function(type)
86 {
87 return this.sbot.messagesByType({ type: type, fillCache: true, keys: false })
88 }
89}
90

Built with git-ssb-web