Files: e5b730f23ccfc66e564bdd503f597a01ed9ed0d1 / app / page / home.js
1660 bytesRaw
1 | const nest = require('depnest') |
2 | const { h } = require('mutant') |
3 | const {threadReduce} = require('ssb-reduce-stream') |
4 | const pull = require('pull-stream') |
5 | |
6 | exports.gives = nest('app.page.home') |
7 | |
8 | exports.needs = nest({ |
9 | 'feed.pull.public': 'first', |
10 | 'app.html.nav': 'first' |
11 | }) |
12 | |
13 | function firstLine (text) { |
14 | if(text.length < 80 && !~text.indexOf('\n')) return text |
15 | |
16 | return text.split('\n')[0].substring(0, 80) |
17 | } |
18 | |
19 | exports.create = (api) => { |
20 | return nest('app.page.home', home) |
21 | |
22 | function home (location) { |
23 | // location here can expected to be: { page: 'home' } |
24 | |
25 | var div = h('div', []) |
26 | |
27 | function subject (msg) { |
28 | return firstLine(msg.content.subject || msg.content.text) |
29 | } |
30 | |
31 | function item (name, thread) { |
32 | var reply = thread.replies && thread.replies[thread.replies.length-1] |
33 | if(!thread.value) { |
34 | |
35 | } |
36 | if(!thread.value) return |
37 | return h('div', [ |
38 | h('h2', name), |
39 | h('div.subject', [subject(thread.value)]), |
40 | reply ? h('div.reply', [subject(reply.value)]) : null |
41 | ] |
42 | ) |
43 | } |
44 | |
45 | pull( |
46 | api.feed.pull.public({reverse: true, limit: 1000}), |
47 | pull.through(console.log), |
48 | pull.collect(function (err, messages) { |
49 | var threads = messages.reduce(threadReduce, null) |
50 | for(var k in threads.channels) { |
51 | var id = threads.channels[k] |
52 | if(!threads.roots[id]) throw new Error('missing thread:'+id+' for channel:'+k) |
53 | var el = item(k, threads.roots[id]) |
54 | if(el) |
55 | div.appendChild(el) |
56 | } |
57 | }) |
58 | ) |
59 | |
60 | return h('Page -home', [ |
61 | h('h1', 'Home'), |
62 | api.app.html.nav(), |
63 | div |
64 | ]) |
65 | } |
66 | } |
67 | |
68 |
Built with git-ssb-web