Files: 79c10b336a5d261d0c19aedbef677d2a5d332ff3 / modules / names.js
1629 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | |
4 | function all(stream, cb) { |
5 | pull(stream, pull.collect(cb)) |
6 | } |
7 | |
8 | var plugs = require('../plugs') |
9 | var getAvatar = require('ssb-avatar') |
10 | var sbot_links2 = plugs.first(exports.sbot_links2 = []) |
11 | var sbot_links = plugs.first(exports.sbot_links = []) |
12 | var sbot_whoami = plugs.first(exports.sbot_whoami = []) |
13 | |
14 | exports.avatar_name = |
15 | function 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