git ssb

1+

kieran / horcrux-schemas-and-thoughts



Commit ca0323c87d976d5762cc5e8fdcdcc10f37fe2ab9

Scaffold flume view to include all secret types - needs work!

Kieran committed on 4/10/2018, 8:11:23 PM
Parent: 707951cffedc5e5c1f850924ae7abcef1847c4e4

Files changed

index.jsadded
index.jsView
@@ -1,0 +1,88 @@
1 +const flumeView = require('flumeview-reduce')
2 +const pull = require('pull-stream')
3 +const get = require('lodash/get')
4 +const extend = require('xtend')
5 +const pry = require('pryjs')
6 +
7 +const FLUME_VIEW_VERSION = 1.01111111111
8 +const FLUME_VIEW_NAME = "secrets"
9 +
10 +const MODULE_NAME = "secrets"
11 +const MSG_TYPES = ["root", "invite", "response", "contract", "secret", "announcement"]
12 +
13 +module.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 +

Built with git-ssb-web