Files: 82418e8c0eab5c0b94532a1f8612097db1fc1bb4 / modules / names.js
3750 bytesRaw
1 | var pull = require('pull-stream') |
2 | var many = require('pull-many') |
3 | var mfr = require('map-filter-reduce') |
4 | |
5 | function all(stream, cb) { |
6 | pull(stream, pull.collect(cb)) |
7 | } |
8 | |
9 | exports.needs = { |
10 | sbot_links2: 'first', |
11 | sbot_query: 'first' |
12 | } |
13 | |
14 | exports.gives = { |
15 | connection_status: true, |
16 | signifier: true, |
17 | signified: true, |
18 | } |
19 | |
20 | /* |
21 | filter(rel: ['mentions', prefix('@')]) | reduce(name: rel[1], value: count()) |
22 | */ |
23 | |
24 | var filter = { |
25 | $filter: { |
26 | rel: ["mentions", {$prefix: "@"}] |
27 | } |
28 | } |
29 | var map = { |
30 | $map: { |
31 | name: ['rel', 1], |
32 | id: 'dest', |
33 | ts: 'ts', |
34 | } |
35 | } |
36 | |
37 | var reduce = { |
38 | $reduce: { |
39 | name: 'name', |
40 | id: 'id', |
41 | rank: {$count: true}, |
42 | ts: {$max: 'ts'} |
43 | } |
44 | } |
45 | |
46 | var filter2 = { |
47 | $filter: { |
48 | value: { |
49 | content: { |
50 | type: "about", |
51 | name: {"$prefix": ""}, |
52 | about: {"$prefix": ""} |
53 | } |
54 | } |
55 | } |
56 | } |
57 | |
58 | var map2 = { |
59 | $map: { |
60 | name: ["value", "content", "name"], |
61 | id: ['value', 'content', 'about'], |
62 | ts: "timestamp" |
63 | } |
64 | } |
65 | |
66 | //union with this query... |
67 | |
68 | var names = [] |
69 | var NAMES = names |
70 | function update(name) { |
71 | var n = names.find(function (e) { |
72 | return e.id == name.id && e.name == e.name |
73 | }) |
74 | if(!n) { |
75 | name.rank = 1 |
76 | //this should be inserted at the right place... |
77 | names.push(name) |
78 | } |
79 | else |
80 | n.rank = n.rank += (name.rank || 1) |
81 | } |
82 | |
83 | var ready = false, waiting = [] |
84 | |
85 | var merge = { |
86 | $reduce: { |
87 | name: 'name', |
88 | id: 'id', |
89 | rank: {$sum: 'rank'}, |
90 | ts: {$max: 'ts'} |
91 | } |
92 | } |
93 | |
94 | function add_sigil(stream) { |
95 | return pull(stream, pull.map(function (e) { |
96 | if (e && e.id && e.name && e.id[0] !== e.name[0]) |
97 | e.name = e.id[0] + e.name |
98 | return e |
99 | }) |
100 | ) |
101 | } |
102 | |
103 | var queryNamedGitRepos = [ |
104 | {$filter: { |
105 | value: { |
106 | content: { |
107 | type: "git-repo", |
108 | name: {"$prefix": ""} |
109 | } |
110 | } |
111 | }}, |
112 | {$map: { |
113 | name: ["value", "content", "name"], |
114 | id: ['key'], |
115 | ts: "timestamp" |
116 | }}, |
117 | reduce |
118 | ] |
119 | exports.create = function (api) { |
120 | |
121 | var exports = {} |
122 | exports.connection_status = function (err) { |
123 | if(!err) { |
124 | pull( |
125 | many([ |
126 | api.sbot_links2({query: [filter, map, reduce]}), |
127 | add_sigil(api.sbot_query({query: [filter2, map2, reduce]})), |
128 | add_sigil(api.sbot_query({query: queryNamedGitRepos})) |
129 | ]), |
130 | //reducing also ensures order by the lookup properties |
131 | //in this case: [name, id] |
132 | mfr.reduce(merge), |
133 | pull.collect(function (err, ary) { |
134 | if(!err) { |
135 | NAMES = names = ary |
136 | ready = true |
137 | while(waiting.length) waiting.shift()() |
138 | } |
139 | }) |
140 | ) |
141 | |
142 | pull(many([ |
143 | api.sbot_links2({query: [filter, map], old: false}), |
144 | add_sigil(api.sbot_query({query: [filter2, map2], old: false})), |
145 | add_sigil(api.sbot_query({query: queryNamedGitRepos, old: false})) |
146 | ]), |
147 | pull.drain(update)) |
148 | } |
149 | } |
150 | |
151 | function async(fn) { |
152 | return function (value, cb) { |
153 | function go () { cb(null, fn(value)) } |
154 | if(ready) go() |
155 | else waiting.push(go) |
156 | } |
157 | } |
158 | |
159 | function rank(ary) { |
160 | //sort by most used, or most recently used |
161 | return ary.sort(function (a, b) { return b.rank - a.rank || b.ts - a.ts }) |
162 | } |
163 | |
164 | //we are just iterating over the entire array. |
165 | //if this becomes a problem, maintain two arrays |
166 | //one of each sort order, but do not duplicate the objects. |
167 | //that should mean the space required is just 2x object references, |
168 | //not 2x objects, and we can use binary search to find matches. |
169 | |
170 | exports.signifier = async(function (id) { |
171 | return rank(names.filter(function (e) { return e.id == id})) |
172 | }) |
173 | |
174 | exports.signified = async(function (name) { |
175 | var rx = new RegExp('^'+name) |
176 | return rank(names.filter(function (e) { return rx.test(e.name) })) |
177 | }) |
178 | |
179 | return exports |
180 | } |
181 |
Built with git-ssb-web