git ssb

30+

cel / git-ssb-web



Tree: b42d7298e1e20b9c578308ea765f1364160aca75

Files: b42d7298e1e20b9c578308ea765f1364160aca75 / lib / about.js

2367 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 pull.drain(processMsg, function (err) {
18 if (err) console.error('about', err)
19 })
20 )
21
22 var getAboutInfo = asyncMemo(function (id, cb) {
23 var abt = abouts[id] = {}
24 var _err
25 var w = 1
26 if (ref.isMsg(id)) {
27 w++
28 sbot.get(id, function (err, value) {
29 if (err) console.error('about: missing message', id, err)
30 msgs[id] = value && value.content
31 if (!--w) next()
32 })
33 }
34 pull(
35 sbot.links({
36 rel: 'about',
37 values: true,
38 dest: id
39 }),
40 pull.drain(processMsg, function (err) {
41 _err = err
42 if (!--w) next()
43 })
44 )
45 function next() {
46 cb(_err, abt)
47 }
48 })
49
50 function processMsg(msg) {
51 var c = msg.value.content
52 if (!c) return
53
54 // handle receiving a message after receiving a link to it
55 if (msg.key in abouts) msgs[msg.key] = c
56
57 var target = c.about
58 if (!target) return
59 var abt = abouts[target]
60 if (!abt) return
61 var ab = abt[msg.value.author] || (abt[msg.value.author] = {})
62 for (var key in c) {
63 if (key === 'about' || key === 'type') continue
64 var val = c[key]
65 if (!val) delete ab[key]
66 else {
67 if (key === 'image' && typeof val === 'object' && val.link) {
68 val = val.link
69 }
70 ab[key] = val
71 }
72 }
73 }
74
75 function getAbout(dest, cb) {
76 if (!dest) return cb(null, {})
77 var target = dest.target || dest
78 getAboutInfo(target, function (err, info) {
79 if (err) return cb(err)
80 // order of preference: source, owner, any, msg
81 var ab = {}
82 mixin(ab, msgs[target])
83 for (var feed in info) mixin(ab, info[feed])
84 mixin(ab, info[dest.owner || dest])
85 mixin(ab, info[source])
86 if (!ab.name) ab.name = u.truncate(target, 20)
87 cb(null, ab)
88 })
89 }
90
91 getAbout.getName = function (id, cb) {
92 getAbout(id, function (err, about) {
93 cb(err, about && about.name)
94 })
95 }
96
97 return getAbout
98}
99

Built with git-ssb-web