Commit 3369e03e90e8b441a5c23b79c41b65724a98a39e
load names from mentions or from about messages
Dominic Tarr committed on 8/1/2016, 4:42:59 AMParent: c4b58ba4156a3f65f0661588e6d29049c8aff68c
Files changed
modules/names.js | changed |
modules/names.js | ||
---|---|---|
@@ -1,12 +1,15 @@ | ||
1 | 1 | var pull = require('pull-stream') |
2 | +var many = require('pull-many') | |
3 | +var mfr = require('map-filter-reduce') | |
2 | 4 | |
3 | 5 | function all(stream, cb) { |
4 | 6 | pull(stream, pull.collect(cb)) |
5 | 7 | } |
6 | 8 | |
7 | 9 | var plugs = require('../plugs') |
8 | 10 | var sbot_links2 = plugs.first(exports.sbot_links2 = []) |
11 | +var sbot_query = plugs.first(exports.sbot_query = []) | |
9 | 12 | |
10 | 13 | /* |
11 | 14 | filter(rel: ['mentions', prefix('@')]) | reduce(name: rel[1], value: count()) |
12 | 15 | */ |
@@ -17,20 +20,44 @@ | ||
17 | 20 | } |
18 | 21 | } |
19 | 22 | var map = { |
20 | 23 | $map: { |
21 | - id: 'dest', name: ['rel', 1], ts: 'ts', | |
24 | + name: ['rel', 1], | |
25 | + id: 'dest', | |
26 | + ts: 'ts', | |
22 | 27 | } |
23 | 28 | } |
24 | 29 | |
25 | 30 | var reduce = { |
26 | 31 | $reduce: { |
27 | - id: "dest", | |
28 | - name: ["rel", 1], | |
32 | + name: 'name', | |
33 | + id: 'id', | |
29 | 34 | rank: {$count: true} |
30 | 35 | } |
31 | 36 | } |
32 | 37 | |
38 | +var filter2 = { | |
39 | + $filter: { | |
40 | + value: { | |
41 | + content: { | |
42 | + type: "about", | |
43 | + name: {"$prefix": ""}, | |
44 | + about: {"$prefix": "@"} //better: match regexp. | |
45 | + } | |
46 | + } | |
47 | + } | |
48 | +} | |
49 | + | |
50 | +var map2 = { | |
51 | + $map: { | |
52 | + name: ["value", "content", "name"], | |
53 | + id: ['value', 'content', 'about'], | |
54 | + ts: "timestamp" | |
55 | + } | |
56 | +} | |
57 | + | |
58 | +//union with this query... | |
59 | + | |
33 | 60 | var names = [] |
34 | 61 | function update(name) { |
35 | 62 | var n = names.find(function (e) { |
36 | 63 | return e.id == name.id && e.name == e.name |
@@ -45,23 +72,47 @@ | ||
45 | 72 | } |
46 | 73 | |
47 | 74 | var ready = false, waiting = [] |
48 | 75 | |
76 | +var merge = { | |
77 | + $reduce: { | |
78 | + name: 'name', | |
79 | + id: 'id', | |
80 | + rank: {$sum: 'rank'}, | |
81 | + ts: {$max: 'ts'} | |
82 | + } | |
83 | +} | |
84 | + | |
85 | +function add_at(stream) { | |
86 | + return pull(stream, pull.map(function (e) { | |
87 | + if(!/^@/.test(e.name)) e.name = '@'+e.name | |
88 | + return e | |
89 | + }) | |
90 | + ) | |
91 | +} | |
92 | + | |
49 | 93 | exports.connection_status = function (err) { |
50 | 94 | if(!err) { |
51 | 95 | pull( |
52 | - sbot_links2({query: [filter, reduce]}), | |
96 | + many([ | |
97 | + sbot_links2({query: [filter, map, reduce]}), | |
98 | + add_at(sbot_query({query: [filter2, map2, reduce]})) | |
99 | + ]), | |
100 | + mfr.reduce(merge), | |
53 | 101 | pull.collect(function (err, ary) { |
54 | - console.log(err, ary) | |
55 | 102 | if(!err) { |
56 | 103 | names = ary |
57 | 104 | ready = true |
58 | 105 | while(waiting.length) waiting.shift()() |
59 | 106 | } |
60 | 107 | }) |
61 | 108 | ) |
62 | 109 | |
63 | - pull(sbot_links2({query: [filter, map], old: false}), pull.drain(update)) | |
110 | + pull(many([ | |
111 | + sbot_links2({query: [filter, map], old: false}), | |
112 | + add_at(sbot_query({query: [filter2, map2], old: false})) | |
113 | + ]), | |
114 | + pull.drain(update)) | |
64 | 115 | } |
65 | 116 | } |
66 | 117 | |
67 | 118 | function async(fn) { |
@@ -84,4 +135,8 @@ | ||
84 | 135 | var rx = new RegExp('^'+name) |
85 | 136 | return rank(names.filter(function (e) { return rx.test(e.name) })) |
86 | 137 | }) |
87 | 138 | |
139 | + | |
140 | + | |
141 | + | |
142 | + |
Built with git-ssb-web