Files: 4b7fe8493fa89039404fac7dbeb5ff3a5326686b / lib / about.js
2477 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 | ]), |
62 | pull.filter(function (msg) { |
63 | return msg && msg.value && 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 | if (!msg.value) return |
90 | var c = msg.value.content |
91 | if (msg.value.author == source || msg.value.author == owner) { |
92 | // TODO: give about from source (self) priority over about from owner |
93 | if (c.name) |
94 | info.name = c.name |
95 | if (c.image) |
96 | info.image = c.image.link |
97 | } |
98 | }, function (err) { |
99 | if (err) console.error(err) |
100 | }) |
101 | ) |
102 | } |
103 |
Built with git-ssb-web