git ssb

1+

kieran / horcrux-schemas-and-thoughts



Tree: ca0323c87d976d5762cc5e8fdcdcc10f37fe2ab9

Files: ca0323c87d976d5762cc5e8fdcdcc10f37fe2ab9 / index.js

2484 bytesRaw
1const flumeView = require('flumeview-reduce')
2const pull = require('pull-stream')
3const get = require('lodash/get')
4const extend = require('xtend')
5const pry = require('pryjs')
6
7const FLUME_VIEW_VERSION = 1.01111111111
8const FLUME_VIEW_NAME = "secrets"
9
10const MODULE_NAME = "secrets"
11const MSG_TYPES = ["root", "invite", "response", "contract", "secret", "announcement"]
12
13module.exports = {
14 name: FLUME_VIEW_NAME,
15 version: '1.0.0',
16 manifest: {
17 get: 'async',
18 stream: 'source'
19 },
20 init: function (server, config) {
21 const db = server._flumeUse(
22 FLUME_VIEW_NAME,
23 flumeView(FLUME_VIEW_VERSION, reduce, map, null, {})
24 )
25
26 return {
27 get: db.get,
28 stream: db.stream
29 }
30
31 function reduce (accumulator, item) {
32 var accumulator = accumulator || {}
33 const { root, author, invite, response, contract } = item
34 var rootNode = get(accumulator, [root], {})
35 if (invite.recp) {
36 hasInvite = rootNode.invites.filter(invite => invite.id === invite.recp.id).length > 0
37 if (!hasInvite) rootNode.invites.push(invite)
38 accumulator[root].invites = rootNode.invites
39 } else if (response.recp) {
40 hasResponse = rootNode.responses.filter(resp => resp.id === response.recp.id).length > 0
41 if (!hasResponse) rootNode.responses.push(response)
42 accumulator[root].responses = rootNode.responses
43 } else if (contract.cosigners) {
44 accumulator[root].cosigners = rootNode.contract.cosigners
45 } else (
46 accumulator[root] = {}
47 )
48 return accumulator
49 }
50
51 function map (msg) {
52 const { author, content } = msg.value
53 var data = server.private.unbox(content, config.keys.private)
54 if (!isSecrets()) return null
55 recps = data.recps
56 ? data.recps.find(recp => {
57 return typeof recp === "string"
58 ? recp !== data.author
59 : recp.id !== data.author
60 })
61 : null
62 return {
63 root: data.root || msg.key,
64 author: author,
65 invite: {
66 recp: recps
67 },
68 response: {
69 recp: recps,
70 accept: data.accept,
71 },
72 contract: {
73 cosigners: data.recps,
74 quorum: data.quorum,
75 shared: data.shared
76 }
77 }
78
79 function isSecrets () {
80 return data &&
81 data.module &&
82 data.module === MODULE_NAME &&
83 MSG_TYPES.indexOf(data.type) !== -1
84 }
85 }
86 }
87}
88
89

Built with git-ssb-web