Files: 50a8f45832d95d56a26433d9cd66e7453416a55e / blog / html / blog.js
1848 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 | |
21 | exports.create = function (api) { |
22 | function loadBlob (data) { |
23 | const { blog } = data.value.content |
24 | if (!isBlob(blog)) { |
25 | console.log(`malformed Blog blog: ${blog}`, data) |
26 | return |
27 | } |
28 | |
29 | onceTrue( |
30 | api.sbot.obs.connection(), |
31 | sbot => sbot.blobs.want(blog, (err, success) => { |
32 | if (err) throw err |
33 | |
34 | console.log(`want blog ${blog}, callback: ${success}`) |
35 | }) |
36 | ) |
37 | } |
38 | |
39 | return nest({ |
40 | 'blog.html.title': function (data) { |
41 | if(!isBlog(data)) return |
42 | |
43 | return data.value.content.title |
44 | }, |
45 | 'blog.html.summary': function (data) { |
46 | if(!isBlog(data)) return |
47 | |
48 | loadBlob(data) |
49 | return data.value.content.summary |
50 | }, |
51 | 'blog.html.thumbnail': function (data) { |
52 | if(!isBlog(data)) return |
53 | return data.value.content.thumbnail |
54 | }, |
55 | 'blog.html.content': function (data) { |
56 | if(!isBlog(data)) return |
57 | |
58 | loadBlob(data) |
59 | var div = h('Markdown') |
60 | pull( |
61 | api.sbot.pull.stream(function (sbot) { |
62 | return sbot.blobs.get(data.value.content.blog) |
63 | }), |
64 | pull.collect(function (err, ary) { |
65 | if(err) return |
66 | var md = api.message.html.markdown({text: Buffer.concat(ary).toString()}) |
67 | div.innerHTML = md.innerHTML |
68 | }) |
69 | ) |
70 | return div |
71 | } |
72 | }) |
73 | } |
74 | |
75 | function isBlog (msg) { |
76 | return get(msg, 'value.content.type') === 'blog' |
77 | } |
78 | |
79 |
Built with git-ssb-web