Files: cbcc8655311b7fc4049ed4bd9b5af4364791a2e4 / modules / page / html / render / message.js
2106 bytesRaw
1 | var { h, when, map, Proxy, Struct, Value, computed } = require('mutant') |
2 | var nest = require('depnest') |
3 | var ref = require('ssb-ref') |
4 | |
5 | exports.needs = nest({ |
6 | 'keys.sync.id': 'first', |
7 | 'feed.obs.thread': 'first', |
8 | 'message.sync.unbox': 'first', |
9 | 'message.html': { |
10 | render: 'first', |
11 | compose: 'first' |
12 | }, |
13 | 'sbot.async.get': 'first' |
14 | }) |
15 | |
16 | exports.gives = nest('page.html.render') |
17 | |
18 | exports.create = function (api) { |
19 | return nest('page.html.render', function channel (id) { |
20 | if (!ref.isMsg(id)) return |
21 | var loader = h('div', {className: 'Loading -large'}) |
22 | |
23 | var result = Proxy(loader) |
24 | |
25 | var meta = Struct({ |
26 | type: 'post', |
27 | root: Proxy(id), |
28 | branch: Proxy(id), |
29 | channel: Value(undefined), |
30 | recps: Value(undefined) |
31 | }) |
32 | |
33 | var compose = api.message.html.compose({ |
34 | meta, |
35 | shrink: false, |
36 | placeholder: when(meta.recps, 'Write a private reply', 'Write a public reply') |
37 | }) |
38 | |
39 | api.sbot.async.get(id, (err, value) => { |
40 | if (err) return result.set(h('div', {className: 'Error'}, ['Cannot load thead'])) |
41 | |
42 | if (typeof value.content === 'string') { |
43 | value = api.message.sync.unbox(value) |
44 | } |
45 | |
46 | // what happens in private stays in private! |
47 | meta.recps.set(value.content.recps) |
48 | |
49 | var isReply = !!value.content.root |
50 | var thread = api.feed.obs.thread(id, {branch: isReply}) |
51 | |
52 | meta.channel.set(value.content.channel) |
53 | meta.root.set(thread.rootId) |
54 | |
55 | // if root thread, reply to last post |
56 | meta.branch.set(isReply ? thread.branchId : thread.lastId) |
57 | |
58 | var container = h('Thread', [ |
59 | when(thread.branchId, h('a.full', {href: thread.rootId}, ['View full thread'])), |
60 | map(thread.messages, (msg) => { |
61 | return computed([msg, thread.previousKey(msg)], (msg, previousId) => { |
62 | return api.message.html.render(msg, {previousId, backlinks: true}) |
63 | }) |
64 | }), |
65 | compose |
66 | ]) |
67 | result.set(when(thread.sync, container, loader)) |
68 | }) |
69 | |
70 | return h('div', {className: 'SplitView'}, [ |
71 | h('div.main', [ |
72 | result |
73 | ]) |
74 | ]) |
75 | }) |
76 | } |
77 |
Built with git-ssb-web