git ssb

0+

ev / microbay



forked from Dominic / patchbay

Tree: 1d2a8b5a23e6c1810b78b10c60f7c1a05c9d76ae

Files: 1d2a8b5a23e6c1810b78b10c60f7c1a05c9d76ae / modules / names.js

1648 bytesRaw
1var h = require('hyperscript')
2var pull = require('pull-stream')
3
4function all(stream, cb) {
5 pull(stream, pull.collect(cb))
6}
7
8var plugs = require('../plugs')
9var getAvatar = require('ssb-avatar')
10var sbot_links2 = plugs.first(exports.sbot_links2 = [])
11var sbot_links = plugs.first(exports.sbot_links = [])
12
13exports.avatar_name =
14function name (id) {
15 var n = h('span', id.substring(0, 10))
16
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.
23
24 /*
25 filter(rel: ['mentions', prefix('@')])
26 .reduce(name: rel[1], value: count())
27 */
28
29 var self_id = require('../keys').id
30
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 })
47
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
53 return n
54
55}
56
57
58
59
60
61

Built with git-ssb-web