Files: 77e1c2621bdc21de0cd7c728832feda7f3db93ab / modules / names.js
1767 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) 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