Files: 14217c23e38f473d897977ae266e423d4f98ae19 / lib / about.js
2750 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 | var u = require('./util') |
14 | |
15 | module.exports = function (sbot, id) { |
16 | var getAbout = asyncMemo(getAboutFull, sbot, id) |
17 | |
18 | getAbout.getName = function (id, cb) { |
19 | getAbout(id, function (err, about) { |
20 | cb(err, about && about.name) |
21 | }) |
22 | } |
23 | |
24 | getAbout.getImage = function (id, cb) { |
25 | getAbout(id, function (err, about) { |
26 | cb(err, about && about.image) |
27 | }) |
28 | } |
29 | |
30 | return getAbout |
31 | } |
32 | |
33 | // Get About info (name and icon) for a feed. |
34 | function getAboutFull(sbot, source, dest, cb) { |
35 | var info = {} |
36 | var target = dest.target || dest |
37 | var owner = dest.owner || dest |
38 | |
39 | pull( |
40 | cat([ |
41 | // First get About info that we gave them. |
42 | sbot.links({ |
43 | source: source, |
44 | dest: target, |
45 | rel: 'about', |
46 | values: true, |
47 | reverse: true |
48 | }), |
49 | // If that isn't enough, then get About info that they gave themselves. |
50 | sbot.links({ |
51 | source: owner, |
52 | dest: target, |
53 | rel: 'about', |
54 | values: true, |
55 | reverse: true |
56 | }), |
57 | // If that isn't enough, then get About info from other feeds |
58 | sbot.links({ |
59 | dest: target, |
60 | rel: 'about', |
61 | values: true, |
62 | reverse: true |
63 | }), |
64 | ]), |
65 | pull.filter(function (msg) { |
66 | return msg && msg.value && msg.value.content |
67 | }), |
68 | pull.drain(function (msg) { |
69 | if (info.name && info.image) return false |
70 | var c = msg.value.content |
71 | if (!info.name && c.name) |
72 | info.name = c.name |
73 | if (!info.image && c.image) |
74 | info.image = c.image.link |
75 | }, function (err) { |
76 | if (err && err !== true) return cb(err) |
77 | if (!info.name) info.name = u.truncate(target, 20) |
78 | cb(null, info) |
79 | }) |
80 | ) |
81 | |
82 | // Keep updated as changes are made |
83 | pull( |
84 | sbot.links({ |
85 | dest: target, |
86 | rel: 'about', |
87 | live: true, |
88 | old: false, |
89 | values: true, |
90 | }), |
91 | pull.drain(function (msg) { |
92 | if (!msg.value) return |
93 | var c = msg.value.content |
94 | if (!c) return |
95 | if (msg.value.author == source || msg.value.author == owner) { |
96 | // TODO: give about from source (self) priority over about from owner |
97 | if (c.name) |
98 | info.name = c.name |
99 | if (c.image) |
100 | info.image = c.image.link |
101 | } else { |
102 | if (c.name && !info.name) |
103 | info.name = c.name |
104 | if (c.image && !info.image) |
105 | info.image = c.image.link |
106 | } |
107 | }, function (err) { |
108 | if (err) console.error('about', err) |
109 | }) |
110 | ) |
111 | } |
112 |
Built with git-ssb-web