Commit c1997c8a400cf294934a5e97a93d35b3705dec55
get flume style friends plugin working right, still needs block though
Dominic Tarr committed on 5/24/2017, 11:24:54 AMParent: e56dd0fc65220e4f40d8a41f95306b8d8bb09369
Files changed
plugins/friends.js | changed |
plugins/friends.js | ||
---|---|---|
@@ -29,17 +29,12 @@ | ||
29 | 29 | exports.version = '1.0.0' |
30 | 30 | exports.manifest = mdm.manifest(apidoc) |
31 | 31 | |
32 | 32 | exports.init = function (sbot, config) { |
33 | - var g = null | |
34 | - | |
35 | - var index = sbot._flumeUse('friends', Reduce(1, function (g, rel) { | |
36 | - if(!g) g = {} | |
33 | + var g = {} | |
34 | + var index = sbot._flumeUse('friends', Reduce(1, function (_, rel) { | |
35 | + //if(!g) g = {} | |
37 | 36 | if(!ref.isFeed(rel.from)) throw new Error('FROM is not id') |
38 | - if(!ref.isFeed(rel.to)) { | |
39 | - console.log('???', rel) | |
40 | - throw new Error('TO is not id') | |
41 | - } | |
42 | 37 | G.addEdge(g, rel.from, rel.to, rel.value) |
43 | 38 | return g |
44 | 39 | }, function (data) { |
45 | 40 | if(data.value.content.type === 'contact' && ref.isFeed(data.value.content.contact)) { |
@@ -62,25 +57,32 @@ | ||
62 | 57 | var live = opts.live === true |
63 | 58 | var meta = opts.meta === true |
64 | 59 | var start = opts.start || sbot.id |
65 | 60 | var first = true |
66 | - var reachable = {} | |
67 | - g = g || {} | |
61 | + var reachable | |
62 | + if(!g) throw new Error('not initialized') | |
63 | + //g = g || {} | |
68 | 64 | return pull( |
69 | 65 | index.stream(opts), |
70 | 66 | FlatMap(function (v) { |
67 | + if(!v) return [] | |
68 | + | |
71 | 69 | //this code handles real time streaming of the hops map. |
72 | 70 | function push (to, hops) { |
73 | 71 | out.push(meta ? {id: to, hops: hops} : to) |
74 | 72 | } |
75 | 73 | var out = [] |
76 | - if(!v) return [] | |
77 | 74 | if(v.from && v.to) { |
78 | - //add edge from->to (value) | |
79 | - G.addEdge(g, v.from, v.to, v.value) | |
75 | + if(!reachable) { | |
76 | + //this is is hack... | |
77 | + reachable = {} | |
78 | + reachable[sbot.id] = 0 | |
79 | + push(sbot.id, 0) | |
80 | + } | |
80 | 81 | //recalculate the portion of the graph, reachable in opts.hops |
81 | - var _reachable = G.hops(g, start, 0, opts.hops || 3, reachable) | |
82 | - //for each node currently reachable | |
82 | + //(but only the portion not already reachable) | |
83 | + var _reachable = G.hops(g, v.from, reachable[v.from], opts.hops || 3, reachable) | |
84 | + | |
83 | 85 | for(var k in _reachable) { |
84 | 86 | //check if it has _become_ reachable just now. |
85 | 87 | //if so add to the set |
86 | 88 | if(reachable[k] == null) |
@@ -91,10 +93,10 @@ | ||
91 | 93 | //else, we where already able to reach this node. |
92 | 94 | } |
93 | 95 | } |
94 | 96 | else { |
95 | - g = v | |
96 | - reachable = G.hops(g, start, 0, opts.hops || 3) | |
97 | + var _g = v | |
98 | + reachable = G.hops(_g, start, 0, opts.hops || 3) | |
97 | 99 | for(var k in reachable) |
98 | 100 | push(k, reachable[k]) |
99 | 101 | } |
100 | 102 | if(first) { |
@@ -102,15 +104,18 @@ | ||
102 | 104 | if(live) { |
103 | 105 | out.push({sync: true}) |
104 | 106 | } |
105 | 107 | } |
108 | + | |
106 | 109 | return out |
107 | 110 | }) |
108 | 111 | |
109 | 112 | ) |
110 | 113 | }, 'createFriendStreamOpts?'), |
111 | 114 | |
112 | 115 | hops: function (opts, cb) { |
116 | + if(isFunction(opts)) | |
117 | + cb = opts, opts = {} | |
113 | 118 | opts = opts || {} |
114 | 119 | if(isString(opts)) |
115 | 120 | opts = {start: opts} |
116 | 121 | index.get(null, function (err, g) { |
@@ -121,5 +126,4 @@ | ||
121 | 126 | } |
122 | 127 | } |
123 | 128 | |
124 | 129 | |
125 | - |
Built with git-ssb-web