Files: 1d955c3387cdf42644ed019ae648fe45559ac290 / modules / names.js
1862 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | |
4 | function all(stream, cb) { |
5 | pull(stream, pull.collect(cb)) |
6 | } |
7 | |
8 | var plugs = require('../plugs') |
9 | var getAvatar = require('ssb-avatar') |
10 | var sbot_links2 = plugs.first(exports.sbot_links2 = []) |
11 | var sbot_links = plugs.first(exports.sbot_links = []) |
12 | |
13 | /* |
14 | filter(rel: ['mentions', prefix('@')]) | reduce(name: rel[1], value: count()) |
15 | */ |
16 | |
17 | var filter = { |
18 | $filter: { |
19 | rel: ["mentions", {$prefix: "@"}] |
20 | } |
21 | } |
22 | var map = { |
23 | $map: { |
24 | id: 'dest', name: ['rel', 1], ts: 'ts', |
25 | } |
26 | } |
27 | |
28 | var reduce = { |
29 | $reduce: { |
30 | id: "dest", |
31 | name: ["rel", 1], |
32 | rank: {$count: true} |
33 | } |
34 | } |
35 | |
36 | var names = [] |
37 | function update(name) { |
38 | var n = names.find(function (e) { |
39 | return e.id == name.id && e.name == e.name |
40 | }) |
41 | if(!n) { |
42 | name.rank = 1 |
43 | //this should be inserted at the right place... |
44 | names.push(name) |
45 | } |
46 | else |
47 | n.rank = n.rank += (name.rank || 1) |
48 | } |
49 | |
50 | var ready = false, waiting = [] |
51 | |
52 | exports.connection_status = function (err) { |
53 | if(!err) { |
54 | pull( |
55 | sbot_links2({query: [filter, reduce]}), |
56 | pull.collect(function (err, ary) { |
57 | console.log(err, ary) |
58 | if(!err) { |
59 | names = ary |
60 | ready = true |
61 | while(waiting.length) waiting.shift()() |
62 | } |
63 | }) |
64 | ) |
65 | |
66 | pull(sbot_links2({query: [filter, map], old: false}), pull.drain(update)) |
67 | } |
68 | } |
69 | |
70 | function async(fn) { |
71 | return function (value, cb) { |
72 | function go () { cb(null, fn(value)) } |
73 | if(ready) go() |
74 | else waiting.push(go) |
75 | } |
76 | } |
77 | |
78 | function rank(ary) { |
79 | return ary.sort(function (a, b) { return b.rank - a.rank }) |
80 | } |
81 | |
82 | exports.signifiers = async(function (id) { |
83 | return rank(names.filter(function (e) { return e.id == id})) |
84 | }) |
85 | |
86 | exports.signified = async(function (name) { |
87 | var rx = new RegExp('^'+name) |
88 | return rank(names.filter(function (e) { return rx.test(e.name) })) |
89 | }) |
90 | |
91 | |
92 |
Built with git-ssb-web