git ssb

16+

Dominic / patchbay



Tree: 8fdad9fc0eaf9e15606a90dddd9356c3af5bf9ad

Files: 8fdad9fc0eaf9e15606a90dddd9356c3af5bf9ad / modules / names.js

1629 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 = [])
12var sbot_whoami = plugs.first(exports.sbot_whoami = [])
13
14exports.avatar_name =
15function name (id) {
16 var n = h('span', id.substring(0, 10))
17
18 //choose the most popular name for this person.
19 //for anything like this you'll see I have used sbot.links2
20 //which is the ssb-links plugin. as you'll see the query interface
21 //is pretty powerful!
22 //TODO: "most popular" name is easily gameable.
23 //must come up with something better than this.
24
25 /*
26 filter(rel: ['mentions', prefix('@')])
27 .reduce(name: rel[1], value: count())
28 */
29
30 all(
31 sbot_links2({query: [
32 {$filter: {rel: ['mentions', {$prefix: '@'}], dest: id}},
33 {$reduce: { name: ['rel', 1], count: {$count: true}
34 }}
35 ]}),
36 function (err, names) {
37 if(err) throw err
38 //if they have not been mentioned, fallback
39 //to patchwork style naming (i.e. self id)
40 if(!names.length)
41 return sbot_whoami(function (err, me) {
42 getAvatar({links: sbot_links}, me.id, id,
43 function (err, avatar) {
44 console.log(avatar)
45 n.textContent = avatar.name
46 })
47 })
48
49 n.textContent = names.reduce(function (max, item) {
50 return max.count > item.count ? max : item
51 }, {name: id.substring(0, 10), count: 0}).name
52 })
53
54 return n
55
56}
57
58
59

Built with git-ssb-web