Files: b70759f872a7c4a5c73cc3413f10d221633d860c / modules / page / html / render / message.js
2191 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 (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(value.content.root || 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 | h('div.messages', [ |
60 | when(thread.branchId, h('a.full', {href: thread.rootId}, ['View full thread'])), |
61 | map(thread.messages, (msg) => { |
62 | return computed([msg, thread.previousKey(msg)], (msg, previousId) => { |
63 | return api.message.html.render(msg, {pageId: id, previousId, includeReferences: true}) |
64 | }) |
65 | }) |
66 | ]), |
67 | compose |
68 | ]) |
69 | result.set(when(thread.sync, container, loader)) |
70 | }) |
71 | |
72 | return h('div', {className: 'SplitView'}, [ |
73 | h('div.main', [ |
74 | result |
75 | ]) |
76 | ]) |
77 | }) |
78 | } |
79 |
Built with git-ssb-web