git ssb

16+

Dominic / patchbay



Tree: c4b58ba4156a3f65f0661588e6d29049c8aff68c

Files: c4b58ba4156a3f65f0661588e6d29049c8aff68c / modules / names.js

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

Built with git-ssb-web