git ssb

30+

cel / git-ssb-web



Tree: fc00f1e2dd977f0c5a6d4f86c8e89ce1a35ba02c

Files: fc00f1e2dd977f0c5a6d4f86c8e89ce1a35ba02c / lib / about.js

3060 bytesRaw
1var pull = require('pull-stream')
2var cat = require('pull-cat')
3var u = require('./util')
4var ref = require('ssb-ref')
5var asyncMemo = require('asyncmemo')
6
7function mixin(a, b) {
8 if (b) for (var k in b) a[k] = b[k]
9}
10
11module.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 reverse: true,
48 query: [{$filter: {
49 dest: id,
50 value: {
51 content: {
52 type: 'about'
53 }
54 }
55 }}]
56 }) : sbot.links({
57 rel: 'about',
58 values: true,
59 dest: id
60 }),
61 u.decryptMessages(sbot),
62 u.readableMessages(),
63 pull.drain(processMsg, function (err) {
64 _err = err
65 if (!--w) next()
66 })
67 )
68 function next() {
69 cb(_err, abt)
70 }
71 })
72
73 function processMsg(msg) {
74 var c = msg.value.content
75 if (!c) return
76
77 // handle receiving a message after receiving a link to it
78 if (msg.key in abouts) msgs[msg.key] = c
79
80 var target = c.about
81 if (!target) return
82 var abt = abouts[target]
83 if (!abt) return
84 var ab = abt[msg.value.author] || (abt[msg.value.author] = {})
85 for (var key in c) {
86 if (key === 'about' || key === 'type') continue
87 var val = c[key]
88 if (!val) delete ab[key]
89 else {
90 if (key === 'image' && typeof val === 'object' && val.link) {
91 val = val.link
92 }
93 ab[key] = val
94 }
95 }
96 }
97
98 function getAbout(dest, cb) {
99 if (!dest) return cb(null, {})
100 if (dest[0] === '#') return cb(null, {name: dest})
101 var target = dest.target || dest
102 getAboutInfo(target, function (err, info) {
103 if (err) return cb(err)
104 // order of preference: source, owner, any, msg
105 var ab = {}
106 mixin(ab, msgs[target])
107 for (var feed in info) mixin(ab, info[feed])
108 mixin(ab, info[dest.owner || dest])
109 mixin(ab, info[source])
110 if (!ab.name) ab.name = u.truncate(target, 20)
111 cb(null, ab)
112 })
113 }
114
115 getAbout.getName = function (id, cb) {
116 getAbout(id, function (err, about) {
117 cb(err, about && about.name)
118 })
119 }
120
121 return getAbout
122}
123

Built with git-ssb-web