modules/names.jsView |
---|
9 | 9 | var getAvatar = require('ssb-avatar') |
10 | 10 | var sbot_links2 = plugs.first(exports.sbot_links2 = []) |
11 | 11 | var sbot_links = plugs.first(exports.sbot_links = []) |
12 | 12 | |
13 | | -exports.avatar_name = |
14 | | -function name (id) { |
15 | | - var n = h('span', id.substring(0, 10)) |
| 13 | + |
| 14 | + filter(rel: ['mentions', prefix('@')]) | reduce(name: rel[1], value: count()) |
| 15 | +*/ |
16 | 16 | |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
| 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 | +} |
23 | 27 | |
24 | | - |
25 | | - filter(rel: ['mentions', prefix('@')]) |
26 | | - .reduce(name: rel[1], value: count()) |
27 | | - */ |
| 28 | +var reduce = { |
| 29 | + $reduce: { |
| 30 | + id: "dest", |
| 31 | + name: ["rel", 1], |
| 32 | + rank: {$count: true} |
| 33 | + } |
| 34 | +} |
28 | 35 | |
29 | | - var self_id = require('../keys').id |
| 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 | + |
| 44 | + names.push(name) |
| 45 | + } |
| 46 | + else |
| 47 | + n.rank = n.rank += (name.rank || 1) |
| 48 | +} |
30 | 49 | |
31 | | - all( |
32 | | - sbot_links2({query: [ |
33 | | - {$filter: {rel: ['mentions', {$prefix: '@'}], dest: id}}, |
34 | | - {$reduce: { name: ['rel', 1], count: {$count: true} |
35 | | - }} |
36 | | - ]}), |
37 | | - function (err, names) { |
38 | | - if(err) console.error(err), names = [] |
39 | | - |
40 | | - |
41 | | - if(!names.length) |
42 | | - return getAvatar({links: sbot_links}, self_id, id, |
43 | | - function (err, avatar) { |
44 | | - if (err) return console.error(err) |
45 | | - n.textContent = (avatar.name[0] == '@' ? '' : '@') + avatar.name |
46 | | - }) |
| 50 | +var ready = false, waiting = [] |
47 | 51 | |
48 | | - n.textContent = names.reduce(function (max, item) { |
49 | | - return max.count > item.count || item.name == '@' ? max : item |
50 | | - }, {name: id.substring(0, 10), count: 0}).name |
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 | + ) |
52 | 65 | |
53 | | - return n |
| 66 | + pull(sbot_links2({query: [filter, map], old: false}), pull.drain(update)) |
| 67 | + } |
| 68 | +} |
54 | 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 | + } |
55 | 76 | } |
56 | 77 | |
| 78 | +function rank(ary) { |
| 79 | + return ary.sort(function (a, b) { return b.rank - a.rank }) |
| 80 | +} |
57 | 81 | |
| 82 | +exports.signifiers = async(function (id) { |
| 83 | + return rank(names.filter(function (e) { return e.id == id})) |
| 84 | +}) |
58 | 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 | +}) |
59 | 90 | |
60 | 91 | |