git ssb

0+

ev / microbay



forked from Dominic / patchbay

Commit 0ea8e47dd13c972419f7ec529f51d81623acaea3

implement signified and signifier apis to ask what is named, or to retrieve a name

Dominic Tarr committed on 7/31/2016, 10:49:45 AM
Parent: 980a3832ab3d08f6d1e49c6d551bca33493eaf59

Files changed

modules/names.jschanged
modules/avatar-name.jsadded
modules/names.jsView
@@ -9,52 +9,83 @@
99 var getAvatar = require('ssb-avatar')
1010 var sbot_links2 = plugs.first(exports.sbot_links2 = [])
1111 var sbot_links = plugs.first(exports.sbot_links = [])
1212
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+*/
1616
17- //choose the most popular name for this person.
18- //for anything like this you'll see I have used sbot.links2
19- //which is the ssb-links plugin. as you'll see the query interface
20- //is pretty powerful!
21- //TODO: "most popular" name is easily gameable.
22- //must come up with something better than this.
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+}
2327
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+}
2835
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+ //this should be inserted at the right place...
44+ names.push(name)
45+ }
46+ else
47+ n.rank = n.rank += (name.rank || 1)
48+}
3049
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- //if they have not been mentioned, fallback
40- //to patchwork style naming (i.e. self id)
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 = []
4751
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+ )
5265
53- return n
66+ pull(sbot_links2({query: [filter, map], old: false}), pull.drain(update))
67+ }
68+}
5469
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+ }
5576 }
5677
78+function rank(ary) {
79+ return ary.sort(function (a, b) { return b.rank - a.rank })
80+}
5781
82+exports.signifiers = async(function (id) {
83+ return rank(names.filter(function (e) { return e.id == id}))
84+})
5885
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+})
5990
6091
modules/avatar-name.jsView
@@ -1,0 +1,21 @@
1+
2+var signifers = require('../plugs').first(exports.signifers = [])
3+
4+exports.avatar_name =
5+function name (id) {
6+ var n = h('span', id.substring(0, 10))
7+
8+ //choose the most popular name for this person.
9+ //for anything like this you'll see I have used sbot.links2
10+ //which is the ssb-links plugin. as you'll see the query interface
11+ //is pretty powerful!
12+ //TODO: "most popular" name is easily gameable.
13+ //must come up with something better than this.
14+
15+ exports.signifiers(id, function (_, names) {
16+ if(names.length) n.textContent = names[0].name
17+ })
18+
19+ return n
20+}
21+

Built with git-ssb-web