Files: 09a90c8c8dd420350b85fbb6d781b88a9786512f / lib / about.js
2838 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 | |
10 | var pull = require('pull-stream') |
11 | var cat = require('pull-cat') |
12 | var asyncMemo = require('asyncmemo') |
13 | |
14 | module.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 | |
32 | function 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. |
38 | function 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 | // If that isn't enough, then get About info from other feeds |
62 | sbot.links({ |
63 | dest: target, |
64 | rel: 'about', |
65 | values: true, |
66 | reverse: true |
67 | }), |
68 | ]), |
69 | pull.filter(function (msg) { |
70 | return msg && msg.value && msg.value.content |
71 | }), |
72 | pull.drain(function (msg) { |
73 | if (info.name && info.image) return false |
74 | var c = msg.value.content |
75 | if (!info.name && c.name) |
76 | info.name = c.name |
77 | if (!info.image && c.image) |
78 | info.image = c.image.link |
79 | }, function (err) { |
80 | if (err && err !== true) return cb(err) |
81 | if (!info.name) info.name = truncate(target, 20) |
82 | cb(null, info) |
83 | }) |
84 | ) |
85 | |
86 | // Keep updated as changes are made |
87 | pull( |
88 | sbot.links({ |
89 | dest: target, |
90 | rel: 'about', |
91 | live: true, |
92 | old: false, |
93 | values: true, |
94 | }), |
95 | pull.drain(function (msg) { |
96 | if (!msg.value) return |
97 | var c = msg.value.content |
98 | if (!c) return |
99 | if (msg.value.author == source || msg.value.author == owner) { |
100 | // TODO: give about from source (self) priority over about from owner |
101 | if (c.name) |
102 | info.name = c.name |
103 | if (c.image) |
104 | info.image = c.image.link |
105 | } else { |
106 | if (c.name && !info.name) |
107 | info.name = c.name |
108 | if (c.image && !info.image) |
109 | info.image = c.image.link |
110 | } |
111 | }, function (err) { |
112 | if (err) console.error('about', err) |
113 | }) |
114 | ) |
115 | } |
116 |
Built with git-ssb-web