git ssb

16+

Dominic / patchbay



Tree: ef284a7d4ac7cdcbae3a5c6e10265c539ae34bff

Files: ef284a7d4ac7cdcbae3a5c6e10265c539ae34bff / modules_basic / names.js

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

Built with git-ssb-web