git ssb

30+

cel / git-ssb-web



Tree: 0badbae079cf31c7da22f205cb2264254553d8c1

Files: 0badbae079cf31c7da22f205cb2264254553d8c1 / about.js

2430 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) return false
67 var c = msg.value.content
68 if (!info.name && c.name)
69 info.name = c.name
70 if (!info.image && c.image)
71 info.image = c.image.link
72 }, function (err) {
73 if (err && err !== true) return cb(err)
74 if (!info.name) info.name = truncate(target, 20)
75 cb(null, info)
76 })
77 )
78
79 // Keep updated as changes are made
80 pull(
81 sbot.links({
82 dest: target,
83 rel: 'about',
84 live: true,
85 values: true,
86 gte: Date.now()
87 }),
88 pull.drain(function (msg) {
89 var c = msg.value.content
90 if (msg.value.author == source || msg.value.author == owner) {
91 // TODO: give about from source (self) priority over about from owner
92 if (c.name)
93 info.name = c.name
94 if (c.image)
95 info.image = c.image
96 }
97 }, function (err) {
98 if (err) console.error(err)
99 })
100 )
101}
102

Built with git-ssb-web