git ssb

30+

cel / git-ssb-web



Tree: dd4e83623a7471b95f8faa0bd8c16453c6b9f6f5

Files: dd4e83623a7471b95f8faa0bd8c16453c6b9f6f5 / lib / about.js

2565 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 info = {}
40 var target = dest.target || dest
41 var owner = dest.owner || dest
42
43 pull(
44 cat([
45 // First get About info that we gave them.
46 sbot.links({
47 source: source,
48 dest: target,
49 rel: 'about',
50 values: true,
51 reverse: true
52 }),
53 // If that isn't enough, then get About info that they gave themselves.
54 sbot.links({
55 source: owner,
56 dest: target,
57 rel: 'about',
58 values: true,
59 reverse: true
60 }),
61 ]),
62 pull.filter(function (msg) {
63 return msg && msg.value.content
64 }),
65 pull.drain(function (msg) {
66 if (info.name && info.image)
67 return gotIt()
68 var c = msg.value.content
69 if (!info.name && c.name)
70 info.name = c.name
71 if (!info.image && c.image)
72 info.image = c.image.link
73 }, gotIt)
74 )
75
76 function gotIt(err) {
77 if (!cb) {
78 if (err && err !== true)
79 console.error(err)
80 return
81 }
82 var _cb = cb
83 cb = null
84 if (err && err !== true) return _cb(err)
85 if (!info.name) info.name = truncate(target, 20)
86 _cb(null, info)
87 }
88
89 // Keep updated as changes are made
90 pull(
91 sbot.links({
92 dest: target,
93 rel: 'about',
94 live: true,
95 values: true,
96 gte: Date.now()
97 }),
98 pull.drain(function (msg) {
99 var c = msg.value.content
100 if (msg.value.author == source || msg.value.author == owner) {
101 // TODO: give about from source (self) priority over about from owner
102 if (c.name)
103 info.name = c.name
104 if (c.image)
105 info.image = c.image
106 }
107 }, function (err) {
108 if (err) console.error(err)
109 })
110 )
111}
112

Built with git-ssb-web