Files: adca29179dee2177af6fefa725c91581ffef5395 / blog / html / blog.js
1852 bytesRaw
1 | const pull = require('pull-stream') |
2 | const nest = require('depnest') |
3 | const { h, onceTrue } = require('mutant') |
4 | const get = require('lodash/get') |
5 | const { isBlob } = require('ssb-ref') |
6 | |
7 | exports.gives = nest({ |
8 | 'blog.html.title': true, |
9 | 'blog.html.summary': true, |
10 | 'blog.html.thumbnail': true, |
11 | 'blog.html.content': true |
12 | }) |
13 | |
14 | exports.needs = nest({ |
15 | 'message.html.markdown': 'first', |
16 | 'sbot.pull.stream': 'first', |
17 | 'sbot.obs.connection': 'first' |
18 | }) |
19 | |
20 | exports.create = function (api) { |
21 | function loadBlob (data) { |
22 | const { blog } = data.value.content |
23 | if (!isBlob(blog)) { |
24 | console.log(`malformed Blog blog: ${blog}`, data) |
25 | return |
26 | } |
27 | |
28 | onceTrue( |
29 | api.sbot.obs.connection(), |
30 | sbot => sbot.blobs.want(blog, (err, success) => { |
31 | if (err) throw err |
32 | |
33 | // console.log(`want blog ${blog}, callback: ${success}`) |
34 | }) |
35 | ) |
36 | } |
37 | |
38 | return nest({ |
39 | 'blog.html.title': function (data) { |
40 | if (!isBlog(data)) return |
41 | |
42 | return data.value.content.title |
43 | }, |
44 | 'blog.html.summary': function (data) { |
45 | if (!isBlog(data)) return |
46 | |
47 | loadBlob(data) |
48 | return data.value.content.summary |
49 | }, |
50 | 'blog.html.thumbnail': function (data) { |
51 | if (!isBlog(data)) return |
52 | return data.value.content.thumbnail |
53 | }, |
54 | 'blog.html.content': function (data) { |
55 | if (!isBlog(data)) return |
56 | |
57 | loadBlob(data) |
58 | var div = h('Markdown') |
59 | pull( |
60 | api.sbot.pull.stream(function (sbot) { |
61 | return sbot.blobs.get(data.value.content.blog) |
62 | }), |
63 | pull.collect(function (err, ary) { |
64 | if (err) return |
65 | var md = api.message.html.markdown({text: Buffer.concat(ary).toString()}) |
66 | div.innerHTML = md.innerHTML |
67 | }) |
68 | ) |
69 | return div |
70 | } |
71 | }) |
72 | } |
73 | |
74 | function isBlog (msg) { |
75 | return get(msg, 'value.content.type') === 'blog' |
76 | } |
77 |
Built with git-ssb-web