git ssb

0+

ev / microbay



forked from Dominic / patchbay

Tree: 1102f8d1089ec8200edbda914e81221149bb8bfb

Files: 1102f8d1089ec8200edbda914e81221149bb8bfb / modules / names.js

1767 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) console.error(err), names = []
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 if (err) return console.error(err)
43 getAvatar({links: sbot_links}, me.id, id,
44 function (err, avatar) {
45 if (err) return console.error(err)
46 n.textContent = (avatar.name[0] == '@' ? '' : '@') + avatar.name
47 })
48 })
49
50 n.textContent = names.reduce(function (max, item) {
51 return max.count > item.count || item.name == '@' ? max : item
52 }, {name: id.substring(0, 10), count: 0}).name
53 })
54
55 return n
56
57}
58
59
60

Built with git-ssb-web