Files: 8fbdc0d4ddc18ce405cf50944702c26c42885bce / lib / about.js
3037 bytesRaw
1 | var pull = require('pull-stream') |
2 | var cat = require('pull-cat') |
3 | var u = require('./util') |
4 | var ref = require('ssb-ref') |
5 | var asyncMemo = require('asyncmemo') |
6 | |
7 | function mixin(a, b) { |
8 | if (b) for (var k in b) a[k] = b[k] |
9 | } |
10 | |
11 | module.exports = function (sbot, source) { |
12 | var abouts = {/* link: {feed: name} */} |
13 | var msgs = {/* key: content */} |
14 | |
15 | pull( |
16 | sbot.createLogStream({old: false}), |
17 | u.decryptMessages(sbot), |
18 | u.readableMessages(), |
19 | pull.drain(processMsg, function (err) { |
20 | if (err) console.error('about', err) |
21 | }) |
22 | ) |
23 | |
24 | var getAboutInfo = asyncMemo(function (id, cb) { |
25 | var abt = abouts[id] = {} |
26 | var _err |
27 | var w = 1 |
28 | if (ref.isMsg(id)) { |
29 | w++ |
30 | sbot.get(id, function (err, value) { |
31 | if (err) { |
32 | console.error('about: missing message', id, err) |
33 | if (!--w) next() |
34 | return |
35 | } |
36 | u.decryptMessage(sbot, {key: id, value: value}, function (err, msg) { |
37 | if (err) console.error('decrypt failed', id, err) |
38 | else if (u.isMessageReadable(msg)) { |
39 | msgs[id] = msg && msg.value && msg.value.content |
40 | } |
41 | if (!--w) next() |
42 | }) |
43 | }) |
44 | } |
45 | pull( |
46 | sbot.backlinks ? sbot.backlinks.read({ |
47 | query: [{$filter: { |
48 | dest: id, |
49 | value: { |
50 | content: { |
51 | type: 'about' |
52 | } |
53 | } |
54 | }}] |
55 | }) : sbot.links({ |
56 | rel: 'about', |
57 | values: true, |
58 | dest: id |
59 | }), |
60 | u.decryptMessages(sbot), |
61 | u.readableMessages(), |
62 | pull.drain(processMsg, function (err) { |
63 | _err = err |
64 | if (!--w) next() |
65 | }) |
66 | ) |
67 | function next() { |
68 | cb(_err, abt) |
69 | } |
70 | }) |
71 | |
72 | function processMsg(msg) { |
73 | var c = msg.value.content |
74 | if (!c) return |
75 | |
76 | // handle receiving a message after receiving a link to it |
77 | if (msg.key in abouts) msgs[msg.key] = c |
78 | |
79 | var target = c.about |
80 | if (!target) return |
81 | var abt = abouts[target] |
82 | if (!abt) return |
83 | var ab = abt[msg.value.author] || (abt[msg.value.author] = {}) |
84 | for (var key in c) { |
85 | if (key === 'about' || key === 'type') continue |
86 | var val = c[key] |
87 | if (!val) delete ab[key] |
88 | else { |
89 | if (key === 'image' && typeof val === 'object' && val.link) { |
90 | val = val.link |
91 | } |
92 | ab[key] = val |
93 | } |
94 | } |
95 | } |
96 | |
97 | function getAbout(dest, cb) { |
98 | if (!dest) return cb(null, {}) |
99 | if (dest[0] === '#') return cb(null, {name: dest}) |
100 | var target = dest.target || dest |
101 | getAboutInfo(target, function (err, info) { |
102 | if (err) return cb(err) |
103 | // order of preference: source, owner, any, msg |
104 | var ab = {} |
105 | mixin(ab, msgs[target]) |
106 | for (var feed in info) mixin(ab, info[feed]) |
107 | mixin(ab, info[dest.owner || dest]) |
108 | mixin(ab, info[source]) |
109 | if (!ab.name) ab.name = u.truncate(target, 20) |
110 | cb(null, ab) |
111 | }) |
112 | } |
113 | |
114 | getAbout.getName = function (id, cb) { |
115 | getAbout(id, function (err, about) { |
116 | cb(err, about && about.name) |
117 | }) |
118 | } |
119 | |
120 | return getAbout |
121 | } |
122 |
Built with git-ssb-web