Files: b9268a67c8b095ed9b76e5d1192a6a88bddb1573 / app / page / home.js
2911 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 | const when = require('mutant/when') |
6 | |
7 | exports.gives = nest('app.page.home') |
8 | |
9 | exports.needs = nest({ |
10 | 'feed.pull.public': 'first', |
11 | 'app.html.nav': 'first', |
12 | 'history.sync.push': 'first', |
13 | 'message.sync.unbox': 'first', |
14 | 'about.html.image': 'first', |
15 | }) |
16 | |
17 | function firstLine (text) { |
18 | if(text.length < 80 && !~text.indexOf('\n')) return text |
19 | |
20 | return text.split('\n')[0].substring(0, 80) |
21 | } |
22 | |
23 | function isObject (o) { |
24 | return o && 'object' === typeof o |
25 | } |
26 | |
27 | function isString (s) { |
28 | return 'string' == typeof s |
29 | } |
30 | |
31 | exports.create = (api) => { |
32 | return nest('app.page.home', home) |
33 | |
34 | function home (location) { |
35 | // location here can expected to be: { page: 'home' } |
36 | |
37 | var container = h('div.container', []) |
38 | |
39 | function subject (msg) { |
40 | return firstLine(msg.content.subject || msg.content.text) |
41 | } |
42 | |
43 | function link(location) { |
44 | return {'ev-click': () => api.history.sync.push(location)} |
45 | } |
46 | |
47 | function item (name, thread) { |
48 | var reply = thread.replies && thread.replies[thread.replies.length-1] |
49 | if(!thread.value) { |
50 | |
51 | } |
52 | if(!thread.value) return |
53 | return h('div.threadLink', link(thread), [ |
54 | name, |
55 | h('div.subject', [subject(thread.value)]), |
56 | reply ? h('div.reply', [subject(reply.value)]) : null |
57 | ] |
58 | ) |
59 | } |
60 | |
61 | function threadGroup (threads, obj, toName) { |
62 | var div = h('div.group') |
63 | for(var k in obj) { |
64 | var id = obj[k] |
65 | var thread = threads.roots[id] |
66 | if(threads.roots[id] && threads.roots[id].value) { |
67 | //throw new Error('missing thread:'+id+' for channel:'+k) |
68 | var el = item(toName(k, thread), thread) |
69 | if(el) div.appendChild(el) |
70 | } |
71 | } |
72 | return div |
73 | } |
74 | |
75 | pull( |
76 | api.feed.pull.public({reverse: true, limit: 1000}), |
77 | pull.through(console.log), |
78 | pull.collect(function (err, messages) { |
79 | |
80 | var threads = messages.map(function (data) { |
81 | if(isObject(data.value.content)) return data |
82 | return api.message.sync.unbox(data) |
83 | }).filter(Boolean).reduce(threadReduce, null) |
84 | |
85 | container.appendChild(threadGroup( |
86 | threads, |
87 | threads.private, |
88 | function (_, msg) { |
89 | console.log(msg) |
90 | if(!msg.value) debugger |
91 | return h('div.recps', |
92 | msg.value.content.recps.map(function (link) { |
93 | return api.about.html.image(isString(link) ? link : link.link) |
94 | }) |
95 | ) |
96 | } |
97 | )) |
98 | |
99 | container.appendChild(threadGroup( |
100 | threads, |
101 | threads.channels, |
102 | ch => h('h2.title', '#'+ch) |
103 | )) |
104 | }) |
105 | ) |
106 | |
107 | return h('Page -home', [ |
108 | h('h1', 'Home'), |
109 | api.app.html.nav(), |
110 | container |
111 | ]) |
112 | } |
113 | } |
114 | |
115 | |
116 | |
117 |
Built with git-ssb-web