git ssb

30+

cel / git-ssb-web



Tree: 37ce8bccebec917257a650d3328c37f3dbd6cff7

Files: 37ce8bccebec917257a650d3328c37f3dbd6cff7 / about.js

2165 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')
12
13module.exports = function (sbot, myId) {
14 var cache = {/* id: about */}
15 var callbacks = {/* id: [callback] */}
16
17 function getAbout(id, cb) {
18 if (id in cache)
19 return cb(null, cache[id])
20 if (id in callbacks)
21 return callbacks[id].push(cb)
22 var cbs = callbacks[id] = [cb]
23 getAboutFull(sbot, myId, id, function (err, about) {
24 var about = !err && (cache[id] = about)
25 while (cbs.length)
26 cbs.pop()(err, about)
27 })
28 }
29
30 getAbout.getName = function (id, cb) {
31 getAbout(id, function (err, about) {
32 cb(err, about && about.name)
33 })
34 }
35
36 getAbout.getImage = function (id, cb) {
37 getAbout(id, function (err, about) {
38 cb(err, about && about.image)
39 })
40 }
41
42 return getAbout
43}
44
45function truncate(str, len) {
46 str = String(str)
47 return str.length < len ? str : str.substr(0, len-1) + '…'
48}
49
50// Get About info (name and icon) for a feed.
51function getAboutFull(sbot, source, dest, cb) {
52 var name, image
53 pull(
54 cat([
55 // First get About info that we gave them.
56 sbot.links({
57 source: source,
58 dest: dest,
59 rel: 'about',
60 values: true,
61 reverse: true
62 }),
63 // If that isn't enough, then get About info that they gave themselves.
64 sbot.links({
65 source: dest,
66 dest: dest,
67 rel: 'about',
68 values: true,
69 reverse: true
70 }),
71 ]),
72 pull.filter(function (msg) {
73 return msg && msg.value.content && (!name || !image)
74 }),
75 pull.drain(function (msg) {
76 var c = msg.value.content
77 if (!name) {
78 name = c.name
79 }
80 if (!image) {
81 image = c.image ? c.image.link : c.image
82 // var imgLink = mlib.link(c.image, 'blob')
83 // image = imgLink && imgLink.link
84 }
85 }, function (err) {
86 if (err) return cb(err)
87 cb(null, {name: name || truncate(id, 8), image: image})
88 })
89 )
90}
91

Built with git-ssb-web