git ssb

30+

cel / git-ssb-web



Tree: 171bb8a2c96096c302ba35950277404010f2ddf5

Files: 171bb8a2c96096c302ba35950277404010f2ddf5 / about.js

1828 bytesRaw
1/* ssb-about
2 * factored out of ssb-notifier
3 *
4 * TODO:
5 * - publish as own module
6 * - handle live updates and reconnecting
7 * - deprecate when ssb-names is used in scuttlebot
8 */
9
10var pull = require('pull-stream')
11var cat = require('pull-cat')
12var asyncMemo = require('asyncmemo')
13
14module.exports = function (sbot, id) {
15 var getAbout = asyncMemo(getAboutFull, sbot, id)
16
17 getAbout.getName = function (id, cb) {
18 getAbout(id, function (err, about) {
19 cb(err, about && about.name)
20 })
21 }
22
23 getAbout.getImage = function (id, cb) {
24 getAbout(id, function (err, about) {
25 cb(err, about && about.image)
26 })
27 }
28
29 return getAbout
30}
31
32function truncate(str, len) {
33 str = String(str)
34 return str.length < len ? str : str.substr(0, len-1) + '…'
35}
36
37// Get About info (name and icon) for a feed.
38function getAboutFull(sbot, source, dest, cb) {
39 var name, image
40 pull(
41 cat([
42 // First get About info that we gave them.
43 sbot.links({
44 source: source,
45 dest: dest,
46 rel: 'about',
47 values: true,
48 reverse: true
49 }),
50 // If that isn't enough, then get About info that they gave themselves.
51 sbot.links({
52 source: dest,
53 dest: dest,
54 rel: 'about',
55 values: true,
56 reverse: true
57 }),
58 ]),
59 pull.filter(function (msg) {
60 return msg && msg.value.content && (!name || !image)
61 }),
62 pull.drain(function (msg) {
63 var c = msg.value.content
64 if (!name) {
65 name = c.name
66 }
67 if (!image) {
68 image = c.image ? c.image.link : c.image
69 // var imgLink = mlib.link(c.image, 'blob')
70 // image = imgLink && imgLink.link
71 }
72 }, function (err) {
73 if (err) return cb(err)
74 cb(null, {name: name || truncate(id, 8), image: image})
75 })
76 )
77}
78

Built with git-ssb-web