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